Repository navigation
fix(gh-cli): do not match heredoc bodies as commands - #349
sushant-me wants to merge 3 commits into
Conversation
|
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. |
|
Both cases covered in You were right that the strip was too broad, and it was broad in a way that mattered:
Two implementation details you may want to weigh in on:
Verified: all 53 cases in One thing I did not do, since it is beyond what you asked: |
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)
1dc3aea to
71ce71d
Compare
Fixes #333.
The bug
The hook ran both of its regexes over the whole
tool_input.commandstring. So a command that writes a file through a heredoc was denied whenever the file's content happened to mentioncurland a GitHub URL — acat > .github/workflows/scan.yml <<'E-O-F'whose body carries acurl ... 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/mainbefore 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
awkwas invoked with. The program split mid-regex,awkerrored to stderr,cmd_matchedcame 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 toallow.The fix is that the awk program now contains no quote characters at all; it builds them with
sprintf("%c", 39)andsprintf("%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 againstorigin/main, which is where the four failures below come from.Verification
Ran the hook directly on seven inputs:
curlto github.comcurlto api.github.comwgetafter the body endsThe 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 againstorigin/main:46/46pass in the file and119/119across the gh-cli hooks, so the 39 pre-existing cases are unaffected.bash -nclean.