Skip to content

fix(gh-cli): do not match heredoc bodies as commands - #349

Open
sushant-me wants to merge 3 commits into
trailofbits:mainfrom
sushant-me:fix/gh-cli-heredoc-false-positive
Open

sushant-me wants to merge 3 commits into
trailofbits:mainfrom
sushant-me:fix/gh-cli-heredoc-false-positive

Conversation

@sushant-me

Copy link
Copy Markdown
Contributor

Fixes #333.

The bug

The hook ran both of its regexes over the whole tool_input.command string. So a command that writes a file through a heredoc was denied whenever the file's content happened to mention curl and a GitHub URL — a cat > .github/workflows/scan.yml <<'E-O-F' whose body carries a curl ... https://github.com/... line.

That command fetches nothing, and it is the shape you get when writing a workflow that installs a tool, so the false positive lands on the most ordinary task in the plugin's own domain.

Reproduced against origin/main before changing anything: the hook emits "permissionDecision":"deny" for that input.

The change

Both matches now run against the command with heredoc bodies removed. The stripper handles quoted, unquoted and tab-stripped (<<-) delimiters, and removes lines only while a body is open — so a real fetch after the body ends is still caught.

Two things I got wrong on the way, both worth recording

1. The first attempt silently disabled the hook. The awk program I wrote contained literal apostrophes, which closed the single-quoted shell string awk was invoked with. The program split mid-regex, awk errored to stderr, cmd_matched came out empty, and the hook started allowing everything — including a plain fetch from github.com. I caught it only because one of my own behaviour checks (real curl -> DENY) flipped to allow.

The fix is that the awk program now contains no quote characters at all; it builds them with sprintf("%c", 39) and sprintf("%c", 34). A passing test suite would not have shown this: the pre-existing tests mostly assert denials, and a hook that denies nothing still passes every test that expects an allow.

2. My first revert-check was meaningless. I restored the hook from HEAD — which was already my fix commit — so the new tests passed and proved nothing. Redone against origin/main, which is where the four failures below come from.

Verification

Ran the hook directly on seven inputs:

input result
curl to github.com DENY (unchanged)
curl to api.github.com DENY (unchanged)
quoted heredoc body only allow (was DENY — the bug)
unquoted heredoc body only allow (was DENY)
tab-stripped heredoc allow (was DENY)
heredoc body, then real curl DENY
real wget after the body ends DENY

The last two exist so the fix cannot quietly become an escape hatch for a real fetch.

Tests: 7 cases added to intercept-github-curl.bats. The four heredoc cases were confirmed to fail against origin/main:

not ok 40 curl: allows a quoted heredoc whose body mentions curl and a GitHub URL
not ok 41 curl: allows an unquoted heredoc whose body mentions curl and a GitHub URL
not ok 42 curl: allows a tab-stripped heredoc whose body mentions curl and a GitHub URL
not ok 43 curl: allows a heredoc body mentioning wget to api.github.com

46/46 pass in the file and 119/119 across the gh-cli hooks, so the 39 pre-existing cases are unaffected. bash -n clean.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@kz-tob

kz-tob commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Could this also strip executable heredoc content, like a script passed to bash or substitutions in an unquoted heredoc? Please cover those cases before we merge. This also needs a version bump.

@sushant-me

Copy link
Copy Markdown
Contributor Author

Both cases covered in 1cc8eef, bumped to 1.6.3 in both manifests.

You were right that the strip was too broad, and it was broad in a way that mattered: bash <<'EOF' … curl … EOF had its body removed, so a command that really does fetch was allowed. The rule is now only inert bodies are stripped:

shape body executes? matched
cat > f <<'EOF' … curl github.com/… EOF no — literal file content stripped (allowed)
cat > f <<EOF … $(curl …) … EOF yes — unquoted delimiter expands matched (denied)
cat > f <<'EOF' … $(curl …) … EOF no — quoted, written literally stripped (allowed)
bash <<'EOF' … curl … EOF yes — the body is the script matched (denied)
sh <<EOF … curl … EOF yes matched (denied)
dash -s <<'EOF' … curl … EOF yes matched (denied)
sudo bash <<'EOF' … curl … EOF yes matched (denied)

Two implementation details you may want to weigh in on:

  • Interpreter recognition is a token scan over the text before <<: it skips VAR= assignments and sudo/env/command/exec/nice/time/nohup/doas, then checks the command against bash sh dash zsh ksh mksh ash python python3 perl ruby node php lua. That is a list, so it drifts the same way any list does — a wrapper I did not anticipate falls back to "not an interpreter" and the body is stripped. If you would rather invert it (treat anything reading a heredoc as potentially executable) say so, but that would re-introduce the original false positive for the common cat > file case, so I did not.
  • Unquoted substitutions are matched by text. The curl/wget test needs whitespace or a list separator in front of the command and $(curl has neither, so the delimiters are rewritten to spaces before matching. The residue: $(echo curl https://github.com/…) in an unquoted heredoc is denied even though echo does not fetch. I chose that over missing a real fetch, and it is confined to unquoted heredocs. Flagging it because it is a genuine false positive, just a narrow one.

Verified: all 53 cases in intercept-github-curl.bats pass, the six new ones fail when only this change is reverted, and make validate reports no errors.

One thing I did not do, since it is beyond what you asked: python3 <<'EOF' print('curl https://…') EOF is still allowed. The curl there is a string literal that Python never executes, and the boundary rule means a quoted Python call like os.system("curl …") is not matched either. If you want interpreter bodies matched more aggressively than the shell case, that needs a different matcher than the curl/wget regex and I would rather propose it separately than fold it in here.

Fixes trailofbits#333.

The hook ran its curl/wget regex and its GitHub-URL regex over the whole
tool_input.command string. A command that writes a file through a heredoc was
therefore denied whenever the file's content happened to mention curl and a
GitHub URL, even though the command fetches nothing:

  cat > .github/workflows/scan.yml <<'EOF'
        - run: curl -sSfL https://github.com/.../trivy.tar.gz | tar xz
  EOF

Both matches now run against the command with heredoc bodies removed. The
stripper handles quoted, unquoted and tab-stripped (<<-) delimiters, and only
removes lines while a body is open, so a real fetch after the body ends is
still denied.

The awk program contains no quote characters of its own -- it builds them with
sprintf("%c", 39/34) -- because a literal quote would close the shell string
awk is invoked with. That was the first attempt's bug; it silently emptied the
matched text and stopped the hook denying anything.

Verified by running the hook directly:

  real curl to github.com        DENY    (unchanged)
  curl to api.github.com         DENY    (unchanged)
  quoted heredoc body only       allow   (was DENY: the bug)
  unquoted heredoc body only     allow   (was DENY)
  tab-stripped heredoc           allow   (was DENY)
  heredoc body, then real curl   DENY    (fix must not become an escape hatch)
  wget after a heredoc ends      DENY

Tests: 7 cases added to intercept-github-curl.bats, all confirmed to fail
against origin/main. 46/46 pass in the file, 119/119 across the gh-cli hooks.
Stripping every heredoc body made the hook blind to two shapes that are not
inert file content:

  * an interpreted heredoc. `bash <<'EOF' ... EOF` runs the body as a script,
    quoted delimiter or not, and `bash <<EOF | ...` likewise. Ten interpreters
    are recognised, skipping env assignments and the sudo/env/command/exec/
    nice/time/nohup/doas prefixes in front of them.
  * an unquoted delimiter. The shell expands that body, so `$(curl ...)` and
    backtick substitutions execute even though the surrounding text reads like
    file content. The delimiters are rewritten to spaces before matching,
    because the curl/wget test needs whitespace or a list separator in front of
    the command and `$(curl` has neither.

A quoted delimiter with no interpreter still has its body stripped, which is
the original fix: `cat > f <<'EOF' ... curl github.com/... EOF` stays allowed,
and so does `$(...)` written as literal text inside a quoted heredoc.

Known residue, stated rather than hidden: in an unquoted heredoc a substitution
is matched by its text, so `$(echo curl https://github.com/...)` is denied even
though `echo` does not fetch. That direction is deliberate -- the alternative is
missing a real fetch -- and it only applies inside an unquoted heredoc.

Bumped to 1.6.3 in both manifests. Verified: all 53 cases in
intercept-github-curl.bats pass, and the six new ones fail when only this
change is reverted; make validate reports no errors.
CI validation failed on this branch with:

  gh-cli: version 1.6.3 is not greater than 1.6.3 at the merge base; clients
  only pull an update when the number increases.

The fix itself was fine — the version simply was never bumped, so the plugin
would not have been pulled by anyone who already had it. Bumped in both places
the repository records it: the plugin manifest and the marketplace entry, which
are two separate sources and both have to move.

Verified by running the repository's own validator against the merge base, which
reproduced the CI error before the bump and now reports no errors:

  python3 .github/scripts/validate_plugin_metadata.py --base-ref <merge-base> .
  ✓ no errors (522 references resolved, 1139 files scanned for hardcoded paths,
    763 for legacy python invocations, 132 components named in their README)
@sushant-me
sushant-me force-pushed the fix/gh-cli-heredoc-false-positive branch from 1dc3aea to 71ce71d Compare October 28, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh-cli: intercept-github-curl.sh denies commands whose heredoc body mentions curl + a GitHub URL

2 participants