Skip to content

fix(tools): make bash whitelist shell-aware to fix benign-command rejections (#581) - #605

Merged
edenreich merged 2 commits into
mainfrom
fix/bash-whitelist-redirects-compound-581
Jun 5, 2026
Merged

edenreich merged 2 commits into
mainfrom
fix/bash-whitelist-redirects-compound-581

Conversation

@edenreich

@edenreich edenreich commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #581. An infer session reported a 36% tool-call success rate because benign, read-only GitHub/git commands were rejected by the bash whitelist — mostly because the model appended 2>&1 / 2>/dev/null / || echo ..., which broke the (often end-anchored) whitelist patterns. The same naive prefix/regex matching also let a whitelisted head command carry an arbitrary tail, so the boundary was both too strict for benign commands and too loose to be a real boundary.

This makes the matcher shell-aware and unifies the previously-duplicated logic (bash.go::isCommandAllowed and config.go::IsBashCommandWhitelisted) into a single source of truth — config.IsBashCommandWhitelisted — now consulted by the Bash tool, the approval policy, and agent auto-approval, so all three agree on exactly what runs without prompting.

What changed

Reliability (the reported failures):

  • Strip benign trailing redirections (2>&1, 2>/dev/null, &>/dev/null, fd-dups) per segment before matching, so they no longer defeat end-anchored patterns. → fixes gh api … 2>&1, git push … 2>&1, etc.
  • gh api --jq/-q now accept quoted, space-containing expressions — e.g. gh api repos/o/r --jq '.[] | .id', the canonical read idiom (previously rejected because --jq [^ ]+ forbade spaces).
  • Add read-only gh search (issues|code|prs|repos|commits).
  • git status flags allowed (git status --porcelain) — aligns it with the already-unanchored git log/git diff/git show read patterns.
  • System-prompt guidance: the Bash tool already captures stdout/stderr/exit code, so don't append 2>&1, 2>/dev/null, or || echo ...; prefer gh api over fetching api.github.com directly. Lives in the dynamic context, so it reaches existing users regardless of their prompts.yaml override.

Security coherence (the "no real boundary" critique):

  • Compound commands (&&, ||, |, |&, ;, &, newlines) are split quote-aware, and every segment must be independently whitelisted. Enables benign chains (gh issue view 5 && gh issue comment 5 …) while closing the prefix-match hole (gh issue list && rm -rf / is now rejected).
  • Reject command substitution: $(...), backticks, and process substitution <(...)/>(...) (e.g. echo $(rm -rf /)).
  • Reject dangerous find primaries that execute or delete: -exec, -execdir, -ok, -okdir, -delete, -fprintf, … (read-only find -name/-type is unaffected).

Verification

  • task test green; golangci-lint reports 0 issues; markdownlint clean.
  • New exhaustive matcher tests cover redirect stripping, compound chains, command substitution, quoted operators, unbalanced quotes, gh search, gh api --jq, find actions, and git status flags; plus tokenizer unit tests and a Bash-tool integration test.
  • Ran a 6-lens adversarial security review (multi-agent) and executed every candidate command through the real compiled matcher for ground truth. It reduced confirmed issues from 27 → 17; the remaining 17 are all pre-existing and unrelated to the reliability fix.

Deliberately out of scope (pre-existing; documented, not bundled)

These were surfaced by the adversarial review but not changed here because fixing them carries real false-reject cost (the opposite of this issue's goal) or belongs in a separate hardening decision:

  • Bare $VAR expansion (echo $GITHUB_TOKEN) — blocking $ would reject echo $HOME, git log --since=$DATE, etc.
  • Redirects to real files (echo … > /tmp/x) — blocking would reject legitimate gh pr diff > out.patch. The redirect just changes where already-permitted output goes.
  • git branch flag loosening — risky because -D/-m mutate.

Happy to file a focused follow-up issue for $VAR/file-redirect hardening if desired.

Note: arithmetic expansion (echo $((1+1))) is conservatively treated like command substitution and falls through to approval. Negligible in practice.

…ections (#581)

The bash whitelist matched commands with naive prefix/regex checks, so models
that append `2>&1` or `|| echo ...` had benign GitHub/git reads rejected
(#581), while that same loose matching let a whitelisted head command carry an
arbitrary tail. The logic was also duplicated between the Bash tool and config.

Unify and harden into config.IsBashCommandWhitelisted, now the single source of
truth for the Bash tool, the approval policy, and agent auto-approval:

- strip benign trailing redirections (2>&1, 2>/dev/null, &>/dev/null, fd-dups)
  before matching, so they no longer defeat end-anchored patterns
- split compound commands (&&, || , |, |&, ;, &, newlines) quote-aware and
  require every segment to be independently whitelisted
- reject command substitution: $(...), backticks, <(...)/>(...)
- reject find primaries that execute or delete (-exec, -delete, ...)
- allow `gh api --jq`/`-q` with quoted, space-containing expressions
- add read-only `gh search (issues|code|prs|repos|commits)`
- allow `git status` flags (align with the unanchored git log/diff/show reads)
- system prompt: the Bash tool already captures stdout/stderr/exit code; do not
  append 2>&1, 2>/dev/null, or `|| echo ...`; prefer `gh api` over api.github.com
@edenreich
edenreich requested a review from a team as a code owner June 5, 2026 12:52
Comment thread config/bash_whitelist_test.go Resolved
Comment thread config/bash_whitelist_test.go Resolved
@edenreich
edenreich merged commit 6119f2a into main Jun 5, 2026
@edenreich
edenreich deleted the fix/bash-whitelist-redirects-compound-581 branch June 5, 2026 17:12
inference-gateway-releaser Bot pushed a commit that referenced this pull request Jun 5, 2026
## [0.118.0](v0.117.1...v0.118.0) (2026-06-05)

### 🚀 Features

* add file explorer panel with tree view, preview, and fuzzy finder ([#589](#589)) ([2b73fcc](2b73fcc)), closes [#588](#588)
* **cli:** adopt fang for styled help, errors, version and completions ([#592](#592)) ([b7f649e](b7f649e))
* **tui:** animate loading states and modernise progress/cursor styling ([#594](#594)) ([06057d7](06057d7))
* **tui:** drive diff colours from the active theme ([#598](#598)) ([f279c1c](f279c1c))
* modernize diff display and migrate Charm UI stack to v2 ([#576](#576)) ([6bf8ac6](6bf8ac6))
* **tui:** rebuild GitHub Action setup wizard with huh forms ([#597](#597)) ([c875e84](c875e84))
* theme collapsed tool result previews ([#603](#603)) ([ffc2ee9](ffc2ee9))

### 🐛 Bug Fixes

* **pricing:** flag Pro-subscription Ollama Cloud models in model picker ([#604](#604)) ([5ec0298](5ec0298))
* **tools:** make bash whitelist shell-aware to fix benign-command rejections ([#581](#581)) ([#605](#605)) ([6119f2a](6119f2a))

### ♻️ Code Refactoring

* **tui:** migrate theme selector to bubbles/v2/list ([#596](#596)) ([a46079c](a46079c))
* move git branch indicator to input view border ([#602](#602)) ([7132237](7132237))
* remove redundant comment ([8e3210d](8e3210d))
* **tui:** render help bar with bubbles/v2/help ([#595](#595)) ([5aed140](5aed140))
* **cli:** render list commands with native lipgloss tables ([#593](#593)) ([28a0e55](28a0e55))
* **config:** replace bespoke config commands with generic get/set ([#601](#601)) ([c80321a](c80321a)), closes [#600](#600)
* separate git branch into own status bar row ([#591](#591)) ([9afbeb4](9afbeb4))

### 💄 Styles

* standardize log message capitalization ([e8ae466](e8ae466))

### 🔧 Build System

* **deps:** bump modernc.org/sqlite from 1.50.1 to 1.51.0 in the gomod group ([#584](#584)) ([908cfc4](908cfc4))
* **deps:** bump the github-actions group with 3 updates ([#585](#585)) ([a14de3e](a14de3e))
* **deps:** upgrade fang to charm.land/fang/v2 v2.0.1 ([d1b861f](d1b861f))

### 👷 CI/CD

* centralize claude.yml via reusable workflow ([#583](#583)) ([9a6839d](9a6839d))
* replace Nix build check with adl-cli recompute pattern ([#586](#586)) ([9c5f7cc](9c5f7cc)), closes [#585](#585) [#585](#585) [#585](#585)

### 🧹 Maintenance

* **deps:** bump claude-code 2.1.148 -> 2.1.158 ([#582](#582)) ([5e8d0b5](5e8d0b5))
* **deps:** bump infer CLI v0.117.0 -> v0.117.1, infer-action v0.9.1 -> v0.11.1 ([#580](#580)) ([90ddcf3](90ddcf3))
* **deps:** bump infer-action v0.11.1 -> v0.11.2 ([#587](#587)) ([56f2df8](56f2df8))
* regenerate keybindings from earlier features ([ff09192](ff09192))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.118.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jun 5, 2026
edenreich added a commit that referenced this pull request Jun 6, 2026
… pattern (#613)

## Summary

Closes #560.

A whitelisted Bash command could write to an **arbitrary file**, because
the matcher prefix-matched the whole command string including a trailing
`> file`. With `echo` (or any prefix-matched command) or a non-anchored
pattern (`^git log`) whitelisted, these were silently auto-approved:

- `echo secret > /etc/passwd`
- `git log > /etc/passwd`

Pipes (`|`) and command substitution were already handled by the
shell-aware matcher added in #581 / #605 — the issue's headline `ls |
xargs rm` is already denied (`xargs rm` isn't whitelisted). The
remaining, genuine gap was **file-write redirects**, which this PR
closes.

## What changed

A segment that still carries a non-benign file-write redirection (`>`,
`>>`, `&>file`, `>&file`) now:

- **bypasses the plain command list**, and
- is allowed **only by a whitelist pattern that matches the entire
command** (`\A(?:…)\z`), so a prefix pattern like `^git log` cannot
unlock a redirected variant, while an anchored `^…$` pattern can opt one
in.

Benign redirects (`2>&1`, `>/dev/null`) are still stripped and allowed;
command substitution is still rejected. Denied commands now return
**actionable feedback** naming the operator and pointing at
`tools.bash.whitelist.patterns`.

Per discussion, pipe behavior from #581 is intentionally unchanged (each
segment must be independently whitelisted), and restricted-operator
commands still flow through the normal approval prompt rather than being
hard-denied.

## Acceptance criteria (#560)

- [x] `|`, `>`, `>>` handled — `>`/`>>` restricted (new); `|`
segment-split (existing, now covered by tests)
- [x] whitelist patterns take precedence (anchored full-command match)
- [x] documented (`docs/tools-reference.md`,
`docs/configuration-reference.md`)
- [x] tested

## Tests

- `config/bash_whitelist_test.go`: redirect restriction,
pattern-unlocks-redirect (incl. prefix-pattern must-**not**-unlock),
pipe policy (`ls | xargs rm` denied, `ls | head` allowed),
`containsFileRedirect`, `matchesEntirePattern`, rejection-hint.
- `internal/agent/tools/bash_test.go`: redirect `Validate` case +
error-message assertion.
- `task test` (full suite), `task lint` (golangci-lint 0 issues +
markdownlint), `task build` all pass.

## Docs / cross-repo

In-repo `docs/` updated. A matching `[DOCS]` ticket for
`inference-gateway/docs` may be warranted if the docs site mirrors these
pages - happy to draft it.
inference-gateway-releaser Bot pushed a commit that referenced this pull request Jun 6, 2026
## [0.120.0](v0.119.0...v0.120.0) (2026-06-06)

### 🚀 Features

* add gh api graphql and gh project subcommands to default bash whitelist ([#612](#612)) ([b0f8bf3](b0f8bf3)), closes [#606](#606)
* **tools:** restrict bash file-write redirects unless whitelisted by pattern ([#613](#613)) ([13b058a](13b058a)), closes [#581](#581) [#605](#605) [#581](#581) [#560](#560)

### ♻️ Code Refactoring

* **ci:** remove NPM_TOKEN - use trusted publisher ([843457c](843457c))

### 🧹 Maintenance

* **deps:** bump infer CLI v0.117.1 -> v0.119.0, infer-action v0.11.2 -> v0.11.4 ([#611](#611)) ([30a536b](30a536b))
* regenerate config ([93e3de0](93e3de0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] infer agent: ~36% tool-call success rate - sandbox whitelist rejects benign gh/WebFetch calls

1 participant