You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Security: heredoc opener can bypass command auto-approval via operator chaining #640
A command like cat <<EOF && rm -rf /tmp\nhello\nEOF with an allowlist of ["cat"] is auto-approved, allowing arbitrary operators chained on the heredoc opener line to execute without user approval.
Root cause
In src/shared/parse-command.ts:519-525, when a top-level quote placeholder is restored and the resulting string contains embedded newlines (i.e. a heredoc span), the entire restored string is pushed as a single atomic command:
This means cat <<EOF && rm -rf /tmp\nhello\nEOF is treated as one command. In src/core/auto-approval/commands.ts:145-148, that single command is validated by prefix match:
Since the string starts with cat, it matches the "cat" allowlist entry and is auto-approved — including the && rm -rf /tmp operator chain that follows the heredoc opener.
Fix
Split and validate operators on the heredoc opener line before treating the body as opaque. Only the heredoc body content (lines between the opener and the terminator) should be atomic. The opener line itself (cat <<EOF && rm -rf /tmp) should be split on &&, ||, ;, and | operators and each sub-command validated independently.
Add a regression test: cat <<EOF && rm -rf /tmp\nhello\nEOF with allowlist ["cat"] should not return auto_approve.
Discovered
Identified during code review of PR #250 (chore: re-enable prefer-const ESLint rule). The vulnerability is pre-existing and unrelated to that PR.
This bug still occurs on main (18861b2) and after #2397.
Command
Allowlist
Denylist
parseCommand result
Decision
cat <<EOF && rm -rf x + body + EOF
cat
rm
1 command (the full text)
auto_approve
cat <<EOF; rm -rf x + body + EOF
cat
rm
1 command (the full text)
malformed_command
cat <<EOF + body + EOF + rm -rf x on the next line
cat
rm
cat <<EOF…EOF, rm -rf x
auto_deny
The root cause in this issue is now in a different location. #2397 changes the multi-line branch of parseCommand: it splits the line with parseCommandLine before it restores the quotes. But the heredoc scanner in scanTopLevelQuotes masks the text from <<EOF to the terminator, which includes && rm -rf x on the opener line. So that text is inside one placeholder, and no split occurs.
A possible fix: when the scanner finds a heredoc opener, mask only the body and the terminator line, not the rest of the opener line. Then parseCommandLine splits the opener line on &&, ||, ; and |.
I can work on this after #2397 if a maintainer assigns it to me.
1 command, auto_approve (with and without a denylist)
cat <<EOF…EOF, rm -rf /tmp → auto_deny with denylist ["rm"], ask_user with the allowlist alone
cat <<E | rm x + body + E
1 command, auto_approve
split → auto_deny
cat <<EOF; rm -rf x + body + EOF
malformed_command
split → auto_deny
Why it holds now: skipHeredoc masks only <<WORD … WORD as one host word. The text after the delimiter word on the opener line is emitted as live host text (emitRemainder → remainderHost), so parseLine splits it on &&, ||, ;, |, & and |& like any other line. Substitutions on the opener line ($(…), backticks, ${x:-$(…)}, >(…)) are extracted as their own segments.
Variants replayed on a2922268a — each operator after the delimiter, <<-EOF, a quoted delimiter, > out.txt before the operator, chained heredocs (cat <<EOF <<EOF2 && …), a delimiter glued to the operator (<<EOF&&), an operator after a tab, a newline after EOF, and the opener inside $(…): all split, none is auto-approved on cat alone. Continuation forms (backslash-newline on the opener line, CRLF, a quote carried across the newline) fail closed as malformed_command. An operator that ends the opener line (cat <<EOF && + body + EOF + operand) puts the operand on the line after the terminator in bash; the parser splits that line too.
In-tree regression tests from that commit:
src/core/auto-approval/__tests__/commands.spec.ts — "does not auto-approve a denied command chained after a heredoc delimiter", "asks user for a substitution on the heredoc opener line after the delimiter"
src/shared/__tests__/parse-command.spec.ts — "splits a chain operator after the delimiter out of the heredoc segment", "keeps an operator-free opener remainder in one segment (no over-split)"
Note: the #2068 description still lists "Splitting the remainder of a heredoc opener line (cat <<EOF && evil) is unchanged from main" under follow-ups. The merged commit does split it, so that note is stale. Suggest closing this issue.
Activity
kanchinadd98 commentedon Nov 10, 2026
This bug still occurs on
main(18861b2) and after #2397.parseCommandresultcat <<EOF && rm -rf x+ body +EOFcatrmauto_approvecat <<EOF; rm -rf x+ body +EOFcatrmmalformed_commandcat <<EOF+ body +EOF+rm -rf xon the next linecatrmcat <<EOF…EOF,rm -rf xauto_denyThe root cause in this issue is now in a different location. #2397 changes the multi-line branch of
parseCommand: it splits the line withparseCommandLinebefore it restores the quotes. But the heredoc scanner inscanTopLevelQuotesmasks the text from<<EOFto the terminator, which includes&& rm -rf xon the opener line. So that text is inside one placeholder, and no split occurs.A possible fix: when the scanner finds a heredoc opener, mask only the body and the terminator line, not the rest of the opener line. Then
parseCommandLinesplits the opener line on&&,||,;and|.I can work on this after #2397 if a maintainer assigns it to me.
gh311 commentedon Dec 3, 2026
Already fixed on
main— by #2068 (merge commitebfe57454, merged 2026-11-24). The issue stayed open because that PR closes #1947 only.Before / after replay — real
parseCommand+getCommandDecisionDetailed, allowlist["cat"]:mainbefore #2068 (1d78f7499)mainnow (a2922268a)cat <<EOF && rm -rf /tmp+ body +EOFauto_approve(with and without a denylist)cat <<EOF…EOF,rm -rf /tmp→auto_denywith denylist["rm"],ask_userwith the allowlist alonecat <<E | rm x+ body +Eauto_approveauto_denycat <<EOF; rm -rf x+ body +EOFmalformed_commandauto_denyWhy it holds now:
skipHeredocmasks only<<WORD … WORDas one host word. The text after the delimiter word on the opener line is emitted as live host text (emitRemainder→remainderHost), soparseLinesplits it on&&,||,;,|,&and|&like any other line. Substitutions on the opener line ($(…), backticks,${x:-$(…)},>(…)) are extracted as their own segments.Variants replayed on
a2922268a— each operator after the delimiter,<<-EOF, a quoted delimiter,> out.txtbefore the operator, chained heredocs (cat <<EOF <<EOF2 && …), a delimiter glued to the operator (<<EOF&&), an operator after a tab, a newline afterEOF, and the opener inside$(…): all split, none is auto-approved oncatalone. Continuation forms (backslash-newline on the opener line, CRLF, a quote carried across the newline) fail closed asmalformed_command. An operator that ends the opener line (cat <<EOF &&+ body +EOF+ operand) puts the operand on the line after the terminator in bash; the parser splits that line too.In-tree regression tests from that commit:
src/core/auto-approval/__tests__/commands.spec.ts— "does not auto-approve a denied command chained after a heredoc delimiter", "asks user for a substitution on the heredoc opener line after the delimiter"src/shared/__tests__/parse-command.spec.ts— "splits a chain operator after the delimiter out of the heredoc segment", "keeps an operator-free opener remainder in one segment (no over-split)"Note: the #2068 description still lists "Splitting the remainder of a heredoc opener line (
cat <<EOF && evil) is unchanged frommain" under follow-ups. The merged commit does split it, so that note is stale. Suggest closing this issue.