Skip to content

[ROSETTA] git-branch-delete pattern scales O(n^2) on separator-free commands #313

Closed

Description

@isolomatov-gd

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:

\bgit\s+branch\b  +  (?=[^;&|\r\n]*<delete-flag>)  +  (?=[^;&|\r\n]*<force-flag>)

Because the negated class [^;&|\r\n]* spans everything up to the next separator, cost is quadratic in the length of a single unseparated command.

Measured

Through the real evaluateDangerous, main vs the PR head:

input main after #299
14 KB (1000 x git branch -a ) 0.1 ms 8.7 ms
56 KB (4000 x) 0.2 ms 136.1 ms
112 KB (8000 x) 0.4 ms 546.3 ms
typical 74-char command 0.00 ms 0.01 ms
32 KB one-liner, one git branch 0.09 ms 0.14 ms

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.

Activity

  1. added
    bug
    on Aug 19, 2026
  2. github-actions commented on Aug 19, 2026

    @github-actions
    Contributor

    Rosetta Triage

    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.
    • Confirmed this is scoped correctly as cost-only: fix(hooks): detect git branch force-delete flag variants #299 (merged) is a genuine fix for 19 real force-delete bypasses; this issue doesn't dispute detection correctness.
    • 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.

    Automated triage by Rosetta agent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions