Skip to content

fix(permission-manager): fail safe when analysis fails (jq depth, parse errors, write-check overflow) - #257

Merged
st0nefish-ci[bot] merged 2 commits into
masterfrom
bug-permission-manager-jq-depth-fail-open
Nov 26, 2026
Merged

st0nefish-ci[bot] merged 2 commits into
masterfrom
bug-permission-manager-jq-depth-fail-open

Conversation

@St0nefish

Copy link
Copy Markdown
Owner

Summary

The permission-manager gate failed open whenever its analysis could not run: the command passed through with no decision, so a danger rule's ask was lost.

jq parse-depth limit (the root cause of two tests failing locally but passing in CI). shfmt nests the AST about two levels per pipe stage. jq builds with a 256 parse-depth limit (jq 1.6, Debian's jq 1.7) reject a pipeline of 62 or more stages with Exceeds depth limit. classify treated that as a passthrough. Reproduced on jq-1.7: git push -f asks, but git push -f; ls|ls|… with 130 pipes passed through. CI's Ubuntu jq accepts deeper input, so CI never hit it.

  • New MAX_AST_DEPTH=120 bound, checked before the engine runs. It measures the tab indentation of shfmt's JSON, which tracks the nesting exactly. It also covers $( ), { }, if and arithmetic, which an operator count misses.
  • New engine_failed: an engine timeout (124) asks as oversize, and a depth error or any other engine failure asks.

Two pre-existing fail-open paths, closed after a security review.

  • shfmt parse failure now asks ([parse-error]), both at top level and in the bash -c/eval payload re-parse. bash runs a valid prefix before it reaches the syntax error, so git push -f + newline + ( used to run the push unchecked. shfmt is also stricter than bash (it rejects ! ! ls). The undecodable-shfmt and operator-probe cases keep their documented passthrough.
  • Write-target check overflow now asks ([write-check]): more than 128 targets, over 32 KB of paths, or a realpath failure. Before, a ~/.bashrc symlink write could hide behind 130 extra targets. A missing or non-GNU realpath still passes through as a missing dependency. The allow path only drops the allow, as before.

Tests

  • tests/permission-manager/test-gate.sh:
    • The 150-stage allow case is now 50 stages.
    • New regressions:
      • danger command + 130 pipes
      • a 57-stage pipeline
      • nested $( )
      • prefix-then-syntax-error
      • ! !
      • nested-payload syntax error
      • 130-write-target symlink
    • New fake-jq cases: engine crash, depth error, timeout 124, check.jq failure, and bypassPermissions staying silent.
  • tests/permission-manager/cases/structural.tsv: 8 syntax-error cases change from PASS to ASK. They encoded the old parse-failure passthrough.
  • Gate suite 1711/1711, test.sh 0 failures, lint-shell.sh clean. Latency is unchanged within noise.

Caveats

  • Any command shfmt rejects now prompts. That includes zsh-only syntax such as glob qualifiers (ls *(.)), which matters when the Bash tool runs zsh.
  • Pipelines of more than about 56 stages, or about 16 nested $( ), now prompt on every jq build.
  • Suggestion: pin a jq with the 256 depth limit in CI to exercise the weakest build. Workflow edits can't be pushed from this environment.

🤖 Generated with Claude Code

https://claude.ai/code/session_018KC1pVVZ3E6ui65fNGWfdT

St0nefish and others added 2 commits November 26, 2026 16:50
…ngine fails

classify() discarded jq's stderr and treated any engine failure as a silent
passthrough. shfmt nests its AST two levels per pipe stage, and jq builds with
a 256-entry parse stack (jq 1.6, Debian jq 1.7) reject trees past about 61
stages, so `git push -f; ls|ls|...` lost its danger ask. Engine failures now
ask, and a direct AST depth bound makes the outcome the same on every jq build.

- gate.sh: add MAX_AST_DEPTH=120, checked against shfmt's tab indentation
  before jq runs (oversize ask); a direct depth check covers $( ), { }, if and
  $(( )) nesting, which a pipe/operator count would miss
- gate.sh: classify captures jq's stderr and exit status; new engine_failed
  routes timeouts (124) and depth-limit errors to oversize, other failures to
  an engine-error ask; an unreadable operator probe or non-JSON shfmt output
  still passes through like a missing dependency
- gate.sh: new ask_reason (used by oversize) falls back to a fixed hook JSON
  when jq cannot render the ask
- gate.sh: resolve_check returns 2 on a check-engine failure; a failed
  write-target check after a passthrough verdict now asks
- tests: 150-stage allow case becomes 50 stages; add depth regressions
  (git push -f + 130 stages, 57-stage pipeline, 20 nested $( )) and fake-jq
  tests for engine crash, depth error, check failure and bypassPermissions
- README: document the depth bound and that engine failure asks
- bump permission-manager to 3.2.1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KC1pVVZ3E6ui65fNGWfdT
…te targets

Two older paths still fell open. A command shfmt cannot parse passed through,
but bash runs the complete statements before a syntax error (`git push -f`
then a stray `(`), and shfmt rejects some syntax bash accepts (`! ! ls`). And
more than MAX_PATHS write targets (or a realpath failure) skipped the symlink
check silently, so padding hid `echo x > link-to-bashrc`.

- gate.sh classify: a non-zero shfmt exit (other than timeout and its depth
  limit, already oversize) is now a parse-error ask, at top level and for
  bash -c/eval payloads; shfmt exiting 0 without JSON and an unreadable
  operator probe still pass through as an unsupported shfmt
- gate.sh resolve_check: returns 3 for a missing or non-GNU realpath (probed
  only after a failed call); the write-target block asks (write-check) on
  rc 1 (overflow, realpath failure) as on rc 2; the allow path still only
  drops the allow
- tests: cases/structural.tsv flips 8 syntax-error cases from PASS to ASK
  (unterminated quote, unterminated heredoc, `ls '`, `if then`, `;;`, `)`,
  `echo $((`, `git push -f "`), which encoded the old parse-failure
  passthrough; add parse-error tests (danger prefix then `(`, `! !`, bash -c
  payloads with danger and harmless prefixes, single `!` still allowed),
  a 130-target write-check test, a plain-writes passthrough check, and a
  fake-jq exit-124 timeout test
- gate.sh header comment and README outcome table, parse-failure and
  write-check sections updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KC1pVVZ3E6ui65fNGWfdT
@st0nefish-ci
st0nefish-ci Bot enabled auto-merge November 26, 2026 17:17
@st0nefish-ci
st0nefish-ci Bot merged commit 9abb63e into master Nov 26, 2026
8 checks passed
@st0nefish-ci
st0nefish-ci Bot deleted the bug-permission-manager-jq-depth-fail-open branch November 26, 2026 17:23
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.

1 participant