Skip to content

fix(rules): cap the stored length of a leaked-secret value #1592 - #1597

Closed
anhnt256 wants to merge 4 commits into
squirrelscan:mainfrom
anhnt256:fix/leaked-secrets-value-cap
Closed

anhnt256 wants to merge 4 commits into
squirrelscan:mainfrom
anhnt256:fix/leaked-secrets-value-cap

Conversation

@anhnt256

@anhnt256 anhnt256 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Closes #1592

Problem

The caps on security/leaked-secrets count findings, not bytes. A generic
secret value has no length cap: scanPageForSecrets on an inline
const password="<1MB>"; keeps two records with 1,024,022-character values
(the page pass and the inline-script pass). A crawl of 100 such pages with
distinct values stays far below every finding and page ceiling while
retaining over 100 MB of value text.

Fix

  • A finding's stored value is cut to STORED_VALUE_MAX_LENGTH (2,048
    characters) at the single construction point (build in scanInto), so
    every path that produces findings is covered: page scans, external-script
    scans, and decoded-blob scans. The cut is surrogate-safe via the repo's
    own clampItemString.
  • The cap value, with a measured reason: 2,048 keeps the longest ordinary
    structured secret whole (a 2048-bit PEM private key is about 1,675
    characters; fixed-format tokens and connection strings are far shorter),
    while bounding a site at the 10,000-finding site cap to about 20M
    characters of value text.
  • Classification still sees the full value: only the stored record is cut,
    so a value over the cap is still reported, never skipped.
  • A cut value is marked: LeakedSecret.truncated is set, and the rule's
    check item carries truncated: true in meta (which the JSON, LLM and
    XML renderers carry through verbatim), while length keeps the full
    length so a reader can tell how much was cut.
  • Docs re-derived: docs/rules/security/leaked-secrets.mdx no longer
    calls value text unbounded, and states the new worst case. CHANGELOG
    [Unreleased] carries the bullet, calling out the new check-item
    truncated field.

Notes for the reviewer

  • The masked check id is produced by the unchanged maskSecret from the
    stored (cut) value, so for a cut value its last 4 characters are the
    tail of the cut, not of the original. The type, the recognisable head,
    the truncated flag and the full length still identify the match.
  • Dedupe by value now runs on cut values: two distinct over-cap values
    sharing a 2,048-character prefix merge into one record. Both are still
    reported in the sense of the issue (cut and marked, never skipped).
  • A 4096-bit PEM (about 3.2 KB) or a many-claim JWT over the cap is cut
    the same way; it stays reported with its type, head, flag and length.

How it was verified

  • Reproduced before the fix: the issue's shape yields 2 records of
    1,024,022 characters. After the fix: 2 records of 2,048 characters,
    truncated: true, length 1,024,011.
  • New packages/rules/tests/security/leaked-secrets-value-cap-1592.test.ts
    (4 tests, explicit 30 s timeouts, no process-wide patches): 1 MB value
    cut and marked but still reported; a value exactly at the cap kept whole
    and unmarked; an ordinary short value untouched; the rule check carries
    truncated: true and the full length in meta.
  • Bite check: with only the value cut disabled, the 1 MB test fails;
    restored, all 4 pass.
  • Full packages/rules/tests/security/ suite: 2,943 tests pass, 0 fail
    (includes the corpus snapshot test, unchanged).
  • bun run typecheck in packages/rules: clean. New test file:
    oxfmt --check and oxlint clean. bun run docs:check: 62 pass.
    bun run scripts/test-suites.ts --check: new file covered by the rules
    suite.
  • The existing leaked-secrets.ts is not formatted or linted by CI for
    this package, so it was left unformatted (reformatting it would churn
    ~960 lines); the edited regions introduce no new formatter diff.

@nc9

nc9 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thanks, this is a good fix. Code, tests and typecheck look right: classification and overlap suppression run on the full value, the cut happens when the record is built, and the new test plus all existing leaked-secrets tests pass.

One change before we merge, in docs/rules/security/leaked-secrets.mdx (the worst-case sentence): the site budget also admits copies of kept values, up to another cap of 10,000 records, so the bound is about 40M characters (about 82 MB as UTF-16), not 20M (41 MB). The same paragraph already says "at most as much again for copies of kept values", so the new figure contradicts it. Please state the 40M / 82 MB bound, or say the figure excludes copies.

Optional, non-blocking: a test where the cut lands inside a surrogate pair, and a line in the docs that two values sharing the same first 2,048 characters now collapse into one finding.

nik

nc9 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed head: 5bdc863

Verdict: pending maintainer review. The change requested in the earlier maintainer review is still open on this head (no commits since that review).

  1. Still open, docs/rules/security/leaked-secrets.mdx (worst-case sentence in the new paragraph): it still says 10,000 distinct findings hold "about 20M characters (about 41 MB as UTF-16)". The site budget also admits copies of kept values, so the bound is about 40M characters (about 82 MB), and the same paragraph says "at most as much again for copies of kept values". State 40M / 82 MB or say the figure excludes copies.
  2. Same figure in the new doc comment above STORED_VALUE_MAX_LENGTH in packages/rules/src/security/leaked-secrets.ts ("about 20M characters of value text"). Fix it together with the docs.
  3. Low: the commit history is four commits with an identical subject. Fine if squashed on merge.

Checked and fine: the cut happens in the record builder in scanInto while classification sees the full value; length keeps the full length; truncated is passed through to the check item meta; the cap uses clampItemString; tests cover over-cap, exactly-at-cap, short value and the check meta; CHANGELOG [Unreleased] entry present; DCO sign-off on all commits; no new dependency. No check runs are reported on this head, so I could not confirm CI state.

nik

@nc9

nc9 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thank you for this. It landed in #1603: your commits, rebased onto main with your authorship kept, plus one docs correction (the worst-case figure now counts the copies of kept values the site budget also admits, about 40M characters). It goes out in the next release, closing #1592. Closing this PR as superseded by the merge.

nik

@nc9 nc9 closed this Nov 5, 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.

leaked-secrets: cap the stored length of a finding's value (a 1 MB password value is kept whole)

2 participants