Skip to content

fix(3): bounded findings-pattern scan — no suffix rescans (CodeQL #3) [v2 sibling] - #470

Open
Alajmah wants to merge 1 commit into
masterfrom
security/findings-pattern-bounded-v2
Open

Alajmah wants to merge 1 commit into
masterfrom
security/findings-pattern-bounded-v2

Conversation

@Alajmah

@Alajmah Alajmah commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Unit 3 of the authorized #425 remediation sequence (#5 → #1+#2 → #3 → #4). Remediation of CodeQL js/polynomial-redos #3 (reviewSchema.js:324–325) under the corrected acceptance criterion: a monotonic scan that does not repeatedly rescan suffixes — not an anchored regex. Merge authority reserved; returned at exact head for clearance.

The defect

Strategy 5's regex /\{"findings"\s*:\s*\[[\s\S]*?\]\s*,\s*"overall_correctness"/ retried its literal prefix at every index, and per candidate the lazy body rescanned the remaining suffix hunting for a completing ] — quadratic on inputs that repeat {"findings":[ without ever completing the pattern (each of the ~n/14 occurrences starts a fresh suffix expansion). Same shared-event-loop blast radius as #1/#2.

The correction (per the corrected criterion)

New findFindingsPatternStart() — two monotonic indexOf passes, no suffix ever rescanned:

  1. Pass 1 (closes): scan the tail literal "overall_correctness" once; for each occurrence walk left over the two whitespace runs to validate the ] , " shape, collecting valid ] positions. Each character is visited a constant number of times.
  2. Pass 2 (heads): scan the head literal {"findings" in order; per candidate, the deterministic \s*:\s*\[ shape check (whitespace runs have exactly one viable split — the first non-whitespace character either is the required token or the candidate fails, no backtracking alternatives), then an existence test against the sorted close list.

Total work linear in the input. First-match-start semantics preserved exactly, including the empty-array case ([] — the lazy body may be zero-length, so a valid close exactly at the body start completes; caught and fixed during this unit's development as a strict-> vs >= off-by-one my first scanner version had). The old code's extra trimmed.indexOf(matchText) was provably redundant and is noted in the scanner's documentation: an earlier occurrence of the full matched text would itself have been an earlier pattern match, contradicting first-match selection.

Strategy 5's downstream flow — slice from the match start, findMatchingBrace, JSON.parse, pattern_match identifier — is byte-unchanged.

Regressions (packages/rules/tests/review-schema-findings-pattern.test.js, 11 tests)

Equivalence — the old regex and the old strategy-5 flow (match → slice → brace balance → parse, with findMatchingBrace replicated verbatim) are reproduced in-tree; a 7-member corpus asserts fire/no-fire agreement: firing shapes (defeating strategies 1–4 by construction), first-candidate-completes-via-later-text (lazy-body reach), shape-completes-but-JSON-invalid, :-then-no-[ candidates, later-partial candidates. Plus tab-spread whitespace tolerance and first-candidate precedence.

Boundedness (all < 500 ms):

  • 30,000 repeated {"findings":[ prefixes, no completion — linear, falls through.
  • 30,000 prefixes + one completion — the shape completes (old regex matches it too — asserted) but brace balance cannot close 30k opens; both old and new flows have the identical terminal fall-through, bounded here.
  • 1,000-member single-candidate scale-up resolving pattern_match.

Verification


v2 note: replaces #467 — the original branch was accidentally stacked on the #465 and #466 commits. This branch is a single commit on master (89d5e51) carrying only the findings-pattern unit; the cherry-pick conflict (the shared insertion anchor with the fence unit) was resolved by keeping this branch free of fence-scanner changes. Rules suite 320/320 on this branch.

CodeQL js/polynomial-redos #3 remediation (reviewSchema.js:324-325). The
strategy-5 regex retried its literal prefix at every index and rescanned
the remaining suffix per candidate via the lazy body — quadratic on inputs
repeating {"findings":[ without completion, on the shared worker event loop.

Replacement: findFindingsPatternStart() — two monotonic indexOf passes with
O(1) shape checks: (1) every literal "overall_correctness" occurrence is
walked LEFT to collect valid ]-closes (ws* , ws* ], no suffix rescans);
(2) every {"findings" occurrence gets the deterministic \s*:\s*\[ shape
check and an existence test against the sorted close list. Linear total
work. First-match-start semantics preserved (incl. empty-array [] closes —
the lazy body may be zero-length; pinned by regression); the old
indexOf(matchText) was provably redundant (an earlier occurrence of the
full match text would itself be an earlier match, contradicting
first-match selection) — noted in the scanner doc.

Strategy 5's downstream flow (slice from match start, findMatchingBrace,
JSON.parse, pattern_match identifier) unchanged.

Regressions (review-schema-findings-pattern.test.js, 11 tests):
- EQUIVALENCE: old regex + old strategy-5 flow reproduced verbatim
  (findMatchingBrace replicated); 7-member corpus (firing shapes, first
  candidate completing via later text, invalid-JSON tails, no-array
  candidates, later-partial candidates) asserts fire/no-fire agreement;
  tab-spread whitespace tolerance; first-candidate precedence
- BOUNDEDNESS (<500ms): 30k repeated prefixes no completion (linear, falls
  through); 30k prefixes + one completion (shape completes, brace balance
  cannot close 30k opens — old flow identical terminal result); 1k-member
  single-candidate scale-up resolving pattern_match

Full rules suite 342/342 (fence-scanner + pre-existing suites unchanged).

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-21T00:00:25.607173Z
We couldn’t complete this review. Comment @codex review to try again.
9c29b0d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@gitwire-hq gitwire-hq Bot 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.

🤖 AI Code Review — ✅ Approved

Confidence: high · Findings: 1
AI judgment: APPROVE · Evidence: COMPLETE · Authority: advisory
Evidence complete · 2/2 changed files accounted for

Other findings (1)

  • [low] Misleading invariant comment: validCloses are collected left-to-right, not right-to-left

🔮 Devil's Advocate (2 turns): 1 upheld


GitWire AI Review Gate (bundle-driven v2) · Structured schema · Scope-validated · Devil's Advocate · AI recommendation — maintainers and repository policy retain authority

validCloses.sort((a, b) => a - b); // collected right-to-left

// Pass 2 — candidate heads in order; O(1) shape check plus an
// existence test against the sorted close list.

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.

[LOW] Misleading invariant comment: validCloses are collected left-to-right, not right-to-left

In findFindingsPatternStart(), the comment // collected right-to-left on the sort line is factually wrong: the Pass 1 loop enumerates TAIL occurrences via indexOf in strictly ascending order, and each pushed close index i satisfies i < t with all closes strictly increasing (a later close cannot precede an earlier one, since the chars between a close and its tail must be whitespace/comma, which excludes an earlier close's ]). So validCloses is already sorted ascending and the sort is a defensive no-op — harmless, but the comment misstates the invariant in a security-sensitive bounded-scan algorithm and could mislead a future maintainer into reasoning incorrectly about ordering (e.g., 'fixing' the code to match the comment, or removing the sort while assuming the wrong order). Suggested fix: change the comment to something like // already ascending (indexOf enumerates tails left-to-right); sort is defensive or delete the sort with an accurate note. No functional impact.

@Alajmah

Alajmah commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Acknowledged and recorded as minor cleanup (per the interim review): validCloses is collected in ascending tail order (each later tail's leftward walk cannot pass a previous tail's closing quote), so the sort is redundant and the "collected right-to-left" comment is inaccurate. The worst case is therefore O(n log n) from the sort, not the claimed strict O(n). Head 9c29b0d is preserved per the execution boundary (non-substantive finding); the sort removal + comment fix land as a follow-up cleanup commit alongside or after merge, with authorization.

This branch has not been deployed

No deployments
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