Skip to content

fix(code-scanner): remove ReDoS and TOCTOU risks CodeQL found in secrets - #52

Merged
ralyodio merged 1 commit into
masterfrom
fix/code-scanner-redos-and-toctou
Jul 28, 2026
Merged

fix(code-scanner): remove ReDoS and TOCTOU risks CodeQL found in secrets#52
ralyodio merged 1 commit into
masterfrom
fix/code-scanner-redos-and-toctou

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

CodeQL flagged three findings in modules/code-scanner/src/secrets/ after PR #50 merged. All three are real.

They matter more here than they would in most code: this module reads file content and operator config that the scanner does not control. An unbounded quantifier turns a crafted file into a denial of service against the security agent itself — which is a worse outcome than the missed placeholder that a bound might cost.

Alert Location Cause
js/polynomial-redos rules.ts <[^>]+> in the placeholder pattern
js/polynomial-redos index.ts /^\*+/ trimming an allowlist entry
js/file-system-race index.ts stat(path) then readFile(path)

Fixes

  • Bounded quantifiers in the placeholder pattern: <[^<>]{1,64}>, 0{8,12}.
  • Star-trimming is a plain loop, not a regex — in isAllowed and matchesIgnore. CodeQL only flagged the first; the second has the identical weakness, so it's fixed now rather than waiting to be reported.
  • One file handle for the size check and the read. stat(path) followed by readFile(path) is a time-of-check/time-of-use gap: the path can be replaced between the calls, so the bytes examined need not be the bytes measured. On a host where an attacker can write to a scanned directory, that's a way to feed the scanner something other than what it approved. Holding a descriptor and stat-ing that closes the gap.

Verification

Four regression tests feed 50,000-character pathological inputs and assert completion within a second. 90 tests total, build clean.

CodeQL flagged three findings in the secrets subsystem after it merged. All
three are real, and they matter more here than they would elsewhere: this code
reads file content and operator config that the scanner does not control, so a
crafted file becomes a denial of service against the security agent itself —
a worse outcome than any placeholder a bound might cause us to miss.

  js/polynomial-redos  rules.ts   `<[^>]+>` in the placeholder pattern
  js/polynomial-redos  index.ts   `/^\*+/` trimming an allowlist entry
  js/file-system-race  index.ts   stat(path) then readFile(path)

Fixes:

  - the placeholder pattern is bounded (`<[^<>]{1,64}>`, `0{8,12}`)
  - star-trimming is a plain loop, not a regex, in both isAllowed and
    matchesIgnore. CodeQL only flagged the first; the second has the identical
    weakness and is fixed too rather than waiting to be reported
  - size check and read now go through ONE file handle. stat-then-read is a
    time-of-check/time-of-use gap: on a host where an attacker can write to a
    scanned directory, the bytes examined need not be the bytes measured

Four regression tests assert 50,000-character pathological inputs complete
within a second.

90 tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

vu1nz Security Review

0 finding(s) in PR #?

No security issues found.

@ralyodio
ralyodio merged commit c37626d into master Jul 28, 2026
9 checks passed
@ralyodio
ralyodio deleted the fix/code-scanner-redos-and-toctou branch July 28, 2026 17:25
ralyodio added a commit that referenced this pull request Jul 28, 2026
The secrets subsystem's stat-then-read gap was fixed in #52; sast had the
identical pattern and CodeQL flagged it on the next scan. Same fix: one file
handle for the size check and the read, so the bytes analysed are the bytes
measured.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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