fix(gh-cli): fall back to well-known install paths for shim PATH walk (closes #145)#161
Open
voidborne-d wants to merge 2 commits intotrailofbits:mainfrom
Open
Conversation
…closes trailofbits#145) Homebrew's superenv strips child PATH to internal shims + system dirs during `brew install` with HOMEBREW_VERIFY_ATTESTATIONS=true. ensure_executable! resolves `gh` against ORIGINAL_PATHS, picks the shim, then forks under the stripped PATH — so the shim's PATH walk can't find Homebrew-installed gh and exits 127. Net effect: `brew install` / `brew bundle` fails for any formula needing attestation verification when run inside a Claude Code session with the gh-cli plugin installed. Add a fallback list after the PATH walk covering the well-known install locations (Apple Silicon Homebrew, Intel Homebrew, Linuxbrew, /usr/bin). The Cellar opt symlinks (.../opt/gh/bin/gh) are stable across upgrades. Tests can override the list via GH_SHIM_FALLBACKS (colon-separated). The anti-pattern checks (api contents deny, non-session clone deny) still run before the fallback, so coverage is preserved when gh is reachable only via the fallback list. Bumps gh-cli plugin version to 1.4.2.
|
|
Ninja3047
previously approved these changes
May 9, 2026
Naive PATH stripping skips every dir containing gh. On CI runners gh
is apt-installed into /usr/bin alongside bash, rm, and mktemp, so the
helper killed core utilities and four fallback tests failed with exit
127 ("bash: command not found"). Symlink the essentials we use into
a fresh dir and keep that on PATH; teardown cleans it up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #145.
The
gh-cliplugin's PATH shim (hooks/shims/gh) fails to find the realghbinary when Homebrew forks anattestation verifysubprocess under its build-isolation superenv. Homebrew'sensure_executable!resolvesghagainstORIGINAL_PATHS, picks our shim (first match), thensystem_command!forks under the parent's strippedPATH(/opt/homebrew/Library/Homebrew/shims/shared:/usr/bin:/bin:/usr/sbin:/sbin). The shim's PATH walk doesn't include/opt/homebrew/bin, finds nothing, and exits 127.User impact:
brew install/brew reinstall/brew bundlefails for any formula withHOMEBREW_VERIFY_ATTESTATIONS=trueset, the moment a Claude Code session is the parent context. Reporter pinned the cause withbrew rubyand 6 captured shim invocations.Fix
Add a fallback after the PATH walk covering the well-known install paths. The Homebrew Cellar opt symlinks (
.../opt/gh/bin/gh) are stable across upgrades, the prefixbinentries cover manual installs, and/usr/bin/ghcovers system package managers (apt/dnf/pacman):GH_SHIM_FALLBACKS(colon-separated) overrides the list — used by tests, also doubles as an escape hatch for non-standard installs.The anti-pattern checks (
api contentsdeny, non-session-scoped clone deny) still execute before the fallback, so the security/anti-pattern coverage is preserved whenghis reachable only via the fallback list — pinned by two new tests.The reporter offered two alternatives I deliberately did not take:
exec gh): re-introduces the recursion the shim already guards against; doesn't fix Homebrew's resolution, just shifts the error.Tests
plugins/gh-cli/hooks/shims/gh-shim.bats— 6 new cases under a new "Homebrew superenv fallback" section, plus an updatedexits 127 when real gh not foundto neutralize the new fallback list (CI runners often have/usr/bin/ghpre-installed):falls back to GH_SHIM_FALLBACKS when PATH walk finds nothing— the bug-fix pin.prefers PATH-found gh over fallback— regression guard for normal-PATH callers.tries fallbacks in order, skipping non-executable entries— pins ordering and-xcheck.exits 127 when neither PATH nor fallback have gh— preserves the original failure mode when nothing is reachable.anti-pattern checks fire before fallback (api contents)— pins ordering with deny rules.anti-pattern checks fire before fallback (clone temp path)— same, for the clone deny.Red-baseline verified: tests #48 (
falls back to GH_SHIM_FALLBACKS…) and #50 (tries fallbacks in order…) FAIL onmain(stashthe source change → run bats), then PASS once the source change is restored.Local gates
bats plugins/gh-cli/hooks/shims/gh-shim.bats→ 53/53 pass.find plugins/gh-cli -name '*.bats' -type f -print0 | xargs -0 bats→ 144/144 pass (all gh-cli bats suites).shellcheck -x plugins/gh-cli/hooks/shims/gh→ clean.shfmt -i 2 -ci -d plugins/gh-cli/hooks/shims/gh plugins/gh-cli/hooks/shims/gh-shim.bats→ clean.python3 .github/scripts/validate_codex_skills.py→ 73/74 OK.python3 .github/scripts/validate_plugin_metadata.py→ all in sync.python3 -c 'import json; json.load(...)'on bothplugin.jsonfiles → valid.grep -rPn '(?<![a-zA-Z])(/home/[a-z]|/Users/[A-Z])' plugins/ --include='*.md' --include='*.py' --include='*.json'→ no matches (excluded extensions cover the bash file).Version bump
gh-cli1.4.1 → 1.4.2 in bothplugins/gh-cli/.claude-plugin/plugin.jsonand the root.claude-plugin/marketplace.jsonper CONTRIBUTING.md (substantive change to a published plugin).Reproducer (from issue #145)
Out of scope
shim_dirin PATH walk) is unchanged. The fallback paths are absolute and do not include the shim dir, so that guard's invariant still holds.GH_SHIM_FALLBACKS=...is the documented override.🤖 Generated with Claude Code