Skip to content

feat(rules): add leaked-secret source metadata - #1277

Closed
WilliamK112 wants to merge 2 commits into
squirrelscan:mainfrom
WilliamK112:codex/leaked-secrets-location-1230
Closed

WilliamK112 wants to merge 2 commits into
squirrelscan:mainfrom
WilliamK112:codex/leaked-secrets-location-1230

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

  • add value length and zero-based source offset to every leaked-secret item
  • identify inline findings with their position in the page's full script list
  • keep existing masked IDs unchanged and point decoded base64 findings at the encoded source blob
  • document the metadata and add focused inline, external, generic-assignment, and base64 coverage

Closes #1230.

Verification

  • Bun 1.4.4: 2,254 existing leaked-secret corpus tests and 4 new location tests passed (25 existing todo cases, 0 failures)
  • focused decoder and critical-escalation suites: 43 existing tests passed
  • bun run typecheck
  • bun run lint
  • bun run format:check
  • git diff --check

Checklist

  • I added or updated tests for behavioral changes.
  • I did not include secrets, customer data, or incompatible third-party material.
  • Every commit includes a DCO Signed-off-by line (git commit -s).

@nc9 nc9 added enhancement New feature or request rules packages/rules — audit rule definitions security Security hardening labels Oct 29, 2026

nc9 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed head: 4c49327

Thanks @WilliamK112, this is a clean, well-scoped take on #1230. Verdict: changes needed before merge (CI is red); the design itself looks right.

Blocking

  1. CLI tests fail. security/leaked-secrets: the content prefilter never loses a finding > every finding made unpadded is still made, unchanged, inside 70 KB of bundle fails with a toEqual mismatch. That test compares findings found in a bare script with the same findings inside a padded bundle. offset now differs between the two by construction, so the comparison needs to ignore offset (or compare it against the padding length). Please check that this is the cause and fix the test, not the offsets.
  2. Secret scan fails. I could not see the detector's output from the job log. The likeliest source is the new fixtures in leaked-secrets-location.test.ts (inlineSecret, externalSecret, and the base64 case, which has no pragma: allowlist secret on the line that builds decoded/encoded). Please run the scan locally and annotate or restructure whatever it flags.

Checked, no issues

  • Correctness against the security/leaked-secrets: items hide value length and location; add meta.length and an offset #1230 criteria: meta.length and meta.offset are on every item; inline items carry scriptIndex counted over all <script> elements; the masked id is unchanged and the tests assert the raw value never appears in the item.
  • Offsets: offsetBase: -openTag.length correctly rebases inline offsets to the script body, and generic assignments use value.lastIndexOf(body) to point at the value, not the key. Base64 findings point at the encoded blob start, which the docs state.
  • Input safety: no new regexes or unbounded loops. Array.from(querySelectorAll("script")) is one extra array per page, which is fine.
  • Conventions: DCO sign-off present, no new dependency, CHANGELOG entry under [Unreleased], rule doc updated.

Low
3. lastIndexOf(body) assumes the value's last occurrence is the matched one. For a generic match whose key text also contains the value (rare) the offset could be off; a test for that is optional.
4. The base64 comment says an outer blob "overwrites" the offset, and the wrapper in scanContent applies offsetBase on top. Worth a test with a base64 blob inside an inline script to confirm the offset stays a body offset.
5. The acceptance criterion asks for a test that the offset "points at the first character of the match" for the fetched body. The tests do assert content.indexOf(secret), so this is met; just noting it is covered.

@nc9

nc9 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Reviewed head: 4c49327

Thanks @WilliamK112. The metadata design is sound: every item gets length, the masked id is unchanged so finding identity stays stable, scriptIndex comes from the same function in both engine paths, and the tests assert the raw value never appears in the output. #1230's criteria for length, the inline script identity and masking are met. The external-script offset is correct.

Must fix (both CI failures):

  1. CLI tests, the content prefilter never loses a finding. The test compares whole findings between the bare fixture and the padded one. offset now legitimately shifts by the padding, so every finding differs. The offset is right; the test needs to change: compare findings with offset removed, and assert padded.offset === bare.offset + \${PAD}\n;\n`.length`. That keeps "padding loses nothing" and also checks that the offset tracks the padding.
  2. Secret scan, exit 123. .secrets.baseline wasn't updated after lines moved in leaked-secrets.ts, and the hook rewrites line numbers and fails. Please run detect-secrets scan --baseline .secrets.baseline and commit the result. A scan of the PR found no new candidates, so only line numbers change.

Should fix (docs, or the html offset itself):

  1. The html offset isn't into the fetched body. scanPageForSecrets scans doc.toString(), the re-serialized DOM, so quoting, entities, whitespace and implied tags can shift it against the raw response. Either offset into the raw body, or document exactly what it indexes.
  2. The inline offset is relative to that script element's text, not the page. That works together with scriptIndex, but the docs say "in the scanned HTML or script body". Please say it explicitly.
  3. Unit: offsets and length are UTF-16 code units (JS string indices), not bytes or characters. Please say so in the docs, and add a test with a non-ASCII prefix to pin it.

Low:

  • Math.max(0, …) clamps a negative offset to 0, for a match inside the synthetic open tag or in the base64 path. Omitting the offset there would be more honest than reporting 0.
  • Dedupe keeps one record per value, so one offset/scriptIndex survives and other occurrences are dropped. Worth one line in the docs.
  • meta is now always present, and length/offset override same-named extra keys (none exist today).
  • Missing tests: multibyte, nested or inline base64, and an html-location case.

The CHANGELOG entry, DCO sign-off and lack of new dependencies are all fine.

Verdict: needs changes (1 and 2 required; 3 to 5 strongly suggested).

@WilliamK112

Copy link
Copy Markdown
Contributor Author

Addressed both CI failures and the location-semantics review at 8371601. The prefilter test now compares findings without offset and separately proves that the padded offset shifts by the exact prefix length. I refreshed .secrets.baseline with detect-secrets 1.5.0 and verified the hook passes. The docs now define offsets as UTF-16 indices, distinguish serialized HTML, fetched external scripts, and per-element inline text, and explain deduplication. Added multibyte-prefix, serialized-HTML, and inline-base64 coverage; synthetic-tag findings with negative rebased positions are now omitted instead of being reported at zero. Verification: 2,270 passed, 25 existing todos, 0 failed across the corpus, critical, and location suites; typecheck, lint, format check, diff check, and secret scan all pass.

nc9 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed head: 8371601

Thanks @WilliamK112. Verdict: pending maintainer review. Every item from the earlier review is addressed and I found nothing blocking. CI is green on this head (CLI tests, secret scan, changelog guard, DCO, docs build, CodeQL).

Earlier review (4c49327), checked against this head

  1. Prefilter test: fixed. It now compares findings without offset and asserts the padded offset equals the bare offset plus the prefix length (leaked-secrets.test.ts, the "content prefilter never loses a finding" test).
  2. Secret scan: fixed. .secrets.baseline moves the one entry to line 2495 and the scan job passes.
  3. HTML offset: addressed by documenting that it indexes the DOM serialization (docs/rules/security/leaked-secrets.mdx), with a test that pins the serialized offset and asserts it differs from the raw source.
  4. Inline offset: the docs now say it indexes that script element's text, with scriptIndex counting all <script> elements.
  5. Unit: docs state UTF-16 code units, and the external-script test has a non-ASCII prefix. One nit: the assertion [...slice].length + 1 only holds for a single astral character, which is fine for the fixture.
  • Negative offsets: findings inside the synthetic open tag are now dropped from the inline pass instead of reported at 0 (leaked-secrets.ts, the adjustedOffset < 0 checks). The HTML pass reports those, so nothing is lost.
  • Dedupe note and the missing multibyte, inline base64 and html-location tests: all added.

Checked, no issues

  • Correctness: inline offsets are rebased by -openTag.length; base64 findings take the outer blob start at each level (decode.ts:490), so the outermost blob wins, and the wrapper then applies offsetBase, which the inline base64 test confirms.
  • Input safety: no new regexes or unbounded loops.
  • Conventions: DCO sign-off present, no new dependency, CHANGELOG entry under [Unreleased], rule doc updated.

Low, optional

  • meta is now always present and length/offset override same-named extra keys (none exist today).
  • lastIndexOf(body) could point at an earlier-in-key occurrence only if the value text also ends the key; very rare.

@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Landed, thank you @WilliamK112. Your two commits are on main under your name: d1b8b95 and 6c8eba6, shipped in v0.0.142 via the stacked release PR #1331 (rebase-merged, so this PR shows as closed rather than merged). Leaked-secret findings now carry the matched value's length and source offset, which makes them much easier to act on. (Nik)

@nc9 nc9 closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rules packages/rules — audit rule definitions security Security hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security/leaked-secrets: items hide value length and location; add meta.length and an offset

2 participants