Repository navigation
fix(permissions): block allows shadowed by brace-range restrictions - #3840
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
A critical active finding shows nested brace ranges can bypass detection, allowing commands to remain auto-approved despite broader downstream shell expansion.
1 open issue
What changed in this PR
This pull request closes a permission-withholding gap by treating brace ranges such as {1..3} as potentially overlapping patterns during shadowing checks, while leaving emitted target permission patterns unchanged.
Changes:
- Extends overlap-only widening to recognize brace ranges alongside comma alternatives.
- Adds regression coverage for matching and unrelated allows, confirming fail-closed behavior.
| File | Description |
|---|---|
| src/features/permissions/shell-command-categories.ts | Extends overlap detection to widen brace-range patterns to * for overlap checks only. |
| src/features/permissions/shell-command-categories.test.ts | Adds regression coverage for numeric brace-range restrictions, including matching and unrelated allows. |
Review effort: Lite
dyoshikawa
left a comment
There was a problem hiding this comment.
Thanks for picking up the brace-range gap — widening {1..3} to * is the right direction, and the first new test is a good regression test for it.
The catch is that switching [^}] to [^{}] drops some patterns the old regex caught, and that cuts against the "only ever report more overlaps" promise in the comment above widenForOverlap. Details are inline. I'd like that fixed before merging, since it means a deny can stop withholding an allow it covers.
| // covers. | ||
| function widenForOverlap(pattern: string): string { | ||
| if (/\{[^}]*,[^}]*\}/.test(pattern)) { | ||
| if (/\{[^{}]*(?:,|\.\.)[^{}]*\}/.test(pattern)) { |
There was a problem hiding this comment.
Because [^{}] won't cross an inner {, an alternation that has a non-expanding {...} inside one of its alternatives no longer matches. The old regex matched every one of these, and the new one matches none of them:
rm -rf {build,${OUT}}git {push,{fetch}}x{a,{b}}
The first is a pretty realistic deny. A shell expands it to rm -rf build plus rm -rf $OUT, but now it isn't widened. The glob checker compares the braces literally, decides it doesn't overlap something like rm -rf build, and the allow is written out. The same thing happens the other way round, when an allow has that shape.
The simplest fix is to keep the old character class and just add the range case: /\{[^}]*(?:,|\.\.)[^}]*\}/. Even simpler and stricter: widen any pattern containing { to *. That also covers nested shapes like x{{a},b}, which neither regex catches today. Either way, please add the examples above as regression tests.
There was a problem hiding this comment.
You're right—the [^{}] change inadvertently narrowed the original overlap detection. Fixed in 4725139 by widening any brace-containing pattern to * for overlap checks only, as you suggested. This preserves all previously detected overlaps and covers arbitrary nesting. I added regression cases for rm -rf {build,${OUT}}, git {push,{fetch}}, x{a,{b}}, and x{{a},b}, plus the allow-side case. All 2,848 permissions tests, typecheck, formatting, and lint pass. Full suite: 17,009 pass; one unrelated test requires Bun, which is not installed here.
| expect(shadowed("unrelated.sh")).toEqual(["backup-{1..3}.sh"]); | ||
| }); | ||
|
|
||
| it("also withholds allows covered by a nested brace range", () => { |
There was a problem hiding this comment.
Small thing: backup-{x,{1..3}}.sh is already widened by the old regex on main ({x,{1..3} matches \{[^}]*,[^}]*\}), so this test passes without the change and doesn't really exercise the inner range. You could swap it for one of the nested non-expanding cases from the other comment, which would actually guard the regex.
There was a problem hiding this comment.
Agreed—the earlier nested-range test did not exercise the regression because the main-branch regex already caught it. I replaced it with the three nested patterns from your other comment (and a deeper nesting case), confirmed those fail against the PR's previous regex, and added allow-side coverage in 4725139.
|
@jdotc1 Thank you! |
…hecks
Document that a pattern containing `{` (alternation, ranges, nested
braces or a `${VAR}` reference) overlaps everything in the withheld-allow
comparison, and add a regression test for a comma-less `${OUT}` restriction
that the pre-dyoshikawa#3840 regex let through.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XcU6mYjWis3a6YpqCW2xgB

Addresses item 10 of #3822 (remaining permission-withholding gaps).
A canonical
deny/askpattern containing brace expansion, such asbackup-{1..3}.shor nestedrm -rf {build,${OUT}}, can match commands that the downstream overlap checker otherwise treats as literal brace text. An adapter that cannot express the restriction could then emit an auto-allow that the canonical policy forbids.*only for overlap checks, including ranges and arbitrarily nested alternatives. This also preserves all overlaps detected before this PR.Verification:
pnpm exec vp test run src/features/permissions/shell-command-categories.test.ts --silent=true— 50 passed.pnpm exec vp test run src/features/permissions --silent=true— 2,848 passed across 58 files.pnpm run typecheck, Oxfmt check, Oxlint,node --checkon both edited TypeScript files, andgit diff --check— passed.pnpm test— 17,009 passed across 615 files; 1 unrelated AmpHooks test cannot run in this workspace because thebunexecutable is absent (spawn bun ENOENT).