Skip to content

fix(permissions): block allows shadowed by brace-range restrictions - #3840

Merged
dyoshikawa merged 5 commits into
dyoshikawa:mainfrom
jdotc1:fix/3822-range-brace-overlap
Nov 14, 2026
Merged

dyoshikawa merged 5 commits into
dyoshikawa:mainfrom
jdotc1:fix/3822-range-brace-overlap

Conversation

@jdotc1

@jdotc1 jdotc1 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Addresses item 10 of #3822 (remaining permission-withholding gaps).

A canonical deny/ask pattern containing brace expansion, such as backup-{1..3}.sh or nested rm -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.

  • Conservatively widen any brace-containing pattern to * only for overlap checks, including ranges and arbitrarily nested alternatives. This also preserves all overlaps detected before this PR.
  • This may withhold additional allows when braces are literal, but cannot grant permissions. Emitted target permission patterns remain unchanged.
  • Cover numeric ranges, the maintainer's three nested examples, deeper nesting, and brace-containing allows with focused regressions. Unrelated ordinary patterns retain their existing behavior.
  • No change to canonical permission formats, parser, or target adapters.

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 --check on both edited TypeScript files, and git diff --check — passed.
  • Full pnpm test — 17,009 passed across 615 files; 1 unrelated AmpHooks test cannot run in this workspace because the bun executable is absent (spawn bun ENOENT).

Copilot AI lite review requested due to automatic review settings November 13, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI lite review requested due to automatic review settings November 13, 2026 22:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change addresses the brace-range permission-withholding gap and adds regression coverage for direct, nested, and fail-closed shadowing cases, with no active review issues reported.

1 open issue

Review effort: Lite

Copilot AI lite review requested due to automatic review settings November 13, 2026 22:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the permission-overlap changes and their added regression coverage.

No open issues 🎉

1 resolved since last review

Review effort: Lite

Copilot AI lite review requested due to automatic review settings November 13, 2026 22:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded, and the change addresses the identified permission-withholding gap for brace-pattern shell commands.

No open issues 🎉

Review effort: Lite

@dyoshikawa dyoshikawa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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", () => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI lite review requested due to automatic review settings November 14, 2026 06:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified, and the change adds targeted regression coverage for brace-pattern overlap handling while preserving fail-closed behavior.

No open issues 🎉

Review effort: Lite

@dyoshikawa
dyoshikawa merged commit e9555e6 into dyoshikawa:main Nov 14, 2026
6 checks passed
@dyoshikawa

Copy link
Copy Markdown
Owner

@jdotc1 Thank you!

Upad1992 pushed a commit to Upad1992/rulesync that referenced this pull request Nov 23, 2026
…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
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.

3 participants