Skip to content

fix(3): bounded findings-pattern scan — no suffix rescans (CodeQL #3) - #467

Closed
Alajmah wants to merge 3 commits into
masterfrom
security/findings-pattern-bounded
Closed

Alajmah wants to merge 3 commits into
masterfrom
security/findings-pattern-bounded

Conversation

@Alajmah

@Alajmah Alajmah commented Nov 20, 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

… YAML and shell layers

CodeQL js/incomplete-sanitization #5 remediation (flakyTestService.js:220):
testFilter escaped only double quotes. A test name containing a backslash
(specifically \") defeats that escaping in the generated Actions command —
the doubled backslash collapses and the quote closes the shell argument
early, executing the remainder as shell syntax in the runner. Dollar signs
and backticks also expand inside double quotes.

New boundary sanitizer sanitizeTestIdentifier(), applied uniformly at both
interpolation sites (filter and comments):
- YAML layer: all C0 controls + DEL become spaces — the only characters
  that can escape a command:| block scalar or a trailing comment line and
  inject workflow structure
- shell layer: backslash FIRST, then double quote, dollar sign, backtick

buildQuarantineWorkflow now exported for testing.

Regressions (flaky-quarantine-escaping.test.js, 13 tests): real js-yaml
parse of the generated document with structure assertions (one job, four
steps, injected runs-on/on: keys survive only as data); POSIX double-quote
decoder proving the closing quote is the intended one (rest === 2>&1) for
crafted \", $(), backtick, ${IFS}, newline and CR payloads; benign names
byte-identical (equivalent quarantine behavior).

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
…nce regexes

CodeQL js/polynomial-redos #1/#2 remediation (reviewSchema.js:273/284). The
fence regex pair's greedy \s* (matches newlines) interacting with the
following \n? made the whitespace bridge ambiguous: O(n^2) per fence
occurrence on unclosed-fence + long-whitespace inputs, multiplied by
matchAll per fence. Input is the review LLM completion (max_tokens 32768) —
reachable via prompt-injection echo — and extraction runs on the shared
worker event loop, so pathological output blocked all 14 workers plus /health.

Replacement: scanFencedBlocks() — monotonic indexOf pairing of opening and
closing fences, linear worst case regardless of whitespace shape. Semantics
preserved from the regex pair: sequential pairing (matchAll continuation),
immediate-json label stripping (the (?:json)? branch; JSON never starts
with the letters json), unclosed trailing fence yields no block, caller-side
trim unchanged, strategy identifiers fenced/fenced_multi unchanged.

Regressions (review-schema-fence-scanner.test.js, 22 tests):
- EQUIVALENCE: old regexes reproduced verbatim in-tree; 13-member corpus
  (labels, label-not-immediate, multi-fence, adjacency, unclosed,
  mid-line) asserts identical strategy+json outcomes
- BOUNDEDNESS (maintainer-specified shapes, all under 500ms):
  unclosed fence + 200k spaces (the maintainer's reproduced 2.06s case,
  scaled), + 200k newlines (CodeQL's stated shape), 30k repeated opening
  fences, 5k-fence valid multi-fence response, 150k-space run inside a
  valid fence
- Pre-existing reviewSchema suite (60 tests) unchanged and green; full
  rules suite 328/328.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
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-20T23:46:19.851756Z
We couldn’t complete this review. Comment @codex review to try again.
843e4e3 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.

@Alajmah

Alajmah commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #470: stacked branch (three units in the diff); #470 is the same unit as a single commit on master, cherry-pick conflict resolved to exclude the fence unit. Its review errored on provider output (ai_reviews 1444).

@Alajmah Alajmah closed this Nov 21, 2026
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