You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ROSETTA] git-branch-delete pattern scales O(n^2) on separator-free commands #313
The git-branch-delete pattern added in #299 (src/hooks/src/hooks/dangerous-actions/patterns.ts) uses two independent lookaheads, each of which rescans forward from the match position:
At 8000 repetitions: 552.9 ms unseparated, versus 0.1-0.3 ms once any ;, &&, or newline appears.
Why it is worth fixing anyway
This is a PreToolUse hook, so it runs on every Bash tool call and its latency is in the agent's critical path. Reaching the bad case needs thousands of git branch tokens in one command with no separator at all, which is not a realistic hand-written command — but it is reachable by a generated or concatenated command, and the cost is paid before the tool runs.
It is polynomial, not exponential: 200 KB of letters, 20k flag tokens, and 2000 x 100-char flags all stayed under 0.8 ms, so there is no catastrophic-backtracking hang.
Suggested direction
Bound the lookahead window instead of letting it run to the next separator — for example cap it at a plausible command-segment length, or split the command on separators once and evaluate each segment, so the two lookaheads operate on short strings.
Not a regression
#299 is a genuine security improvement: it closed 19 real force-delete bypasses (-d -f, -fd, -df, --delete --force, clustered and reordered forms, flags after the branch name), verified against real git. This issue is only about the cost profile of the implementation.
Classification: bug (already applied) — algorithmic-complexity defect in a security hook's regex, not a detection-correctness regression Priority assessment: Low. Verified polynomial (not exponential) cost, confirmed via analysis of the pattern and the reporter's own numbers (halving/doubling input length tracks O(n²): 4x length → ~15.6x time, 2x length → ~4.0x time — both consistent with quadratic, not catastrophic, backtracking). Triggering the slow path needs thousands of unseparated git branch tokens in one command, which the reporter correctly notes is implausible hand-typed input but reachable via a generated/concatenated command. Worst measured cost (~550ms at 8000 reps) adds latency to a PreToolUse hook's critical path without blocking or crashing it; typical commands are unaffected (0.01ms).
Findings:
Verified at src/hooks/src/hooks/dangerous-actions/patterns.ts:103-105: GIT_BRANCH_DELETE_LA and GIT_BRANCH_FORCE_LA are independent lookaheads, each scanning forward via [^;&|\r\n]* from the current match position. When neither flag is found within an unseparated span, the engine advances the match anchor and rescans forward again at each subsequent git branch occurrence — O(n) attempts × O(n) scan = O(n²) for one long unseparated command.
git-branch-delete.test.ts covers correctness (variant matching, marker bypass, separator boundaries) but has no performance/characterization test, so a future fix has nothing pinning the current cost profile or preventing silent reintroduction.
Suggestions:
Reporter's direction (bound the lookahead window, or split on separators once and evaluate each segment independently) is sound and matches the file's existing separator-stop intent — currently that "stop" is a character class, not a length bound, so it still rescans the full remaining unseparated segment.
Add a characterization test asserting the guard evaluates within a fixed time budget on a large adversarial (unseparated, repeated-token) input, so the fix and future changes are pinned against regression.
Ready to implement per the reporter's suggested direction; no further clarification needed.
Activity
github-actions commentedon Aug 19, 2026
Rosetta Triage
Classification:
bug(already applied) — algorithmic-complexity defect in a security hook's regex, not a detection-correctness regressionPriority assessment: Low. Verified polynomial (not exponential) cost, confirmed via analysis of the pattern and the reporter's own numbers (halving/doubling input length tracks O(n²): 4x length → ~15.6x time, 2x length → ~4.0x time — both consistent with quadratic, not catastrophic, backtracking). Triggering the slow path needs thousands of unseparated
git branchtokens in one command, which the reporter correctly notes is implausible hand-typed input but reachable via a generated/concatenated command. Worst measured cost (~550ms at 8000 reps) adds latency to a PreToolUse hook's critical path without blocking or crashing it; typical commands are unaffected (0.01ms).Findings:
src/hooks/src/hooks/dangerous-actions/patterns.ts:103-105:GIT_BRANCH_DELETE_LAandGIT_BRANCH_FORCE_LAare independent lookaheads, each scanning forward via[^;&|\r\n]*from the current match position. When neither flag is found within an unseparated span, the engine advances the match anchor and rescans forward again at each subsequentgit branchoccurrence — O(n) attempts × O(n) scan = O(n²) for one long unseparated command.git-branch-delete.test.tscovers correctness (variant matching, marker bypass, separator boundaries) but has no performance/characterization test, so a future fix has nothing pinning the current cost profile or preventing silent reintroduction.Suggestions:
Ready to implement per the reporter's suggested direction; no further clarification needed.
Automated triage by Rosetta agent