Skip to content

test(rules): leaked-secrets corpus, unit tests and benchmark baseline - #358

Merged
nc9 merged 3 commits into
mainfrom
test/leaked-secrets-harness
Sep 16, 2026
Merged

nc9 merged 3 commits into
mainfrom
test/leaked-secrets-harness

Conversation

@nc9

@nc9 nc9 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What

A unit-test and benchmark harness for security/leaked-secrets. No detector changes in this PR: the corpus comes first, fixes are separate PRs.

  • packages/rules/tests/security/leaked-secrets/generators.ts: one seeded generator per FAST/CONTEXT pattern (88). Every value is assembled at runtime from parts; no line is a token-shaped literal and nothing is a real credential.
  • contexts.ts: 12 embedding contexts (inline config object, window.__ENV, __NEXT_DATA__, __NUXT__, HTML comment, <meta content>, data-*, JSON-LD, external webpack bundle, fetch() Bearer header, .env dump in <pre>, sourcemap comment), each declaring the rule-level location.
  • negatives.ts: hand-written look-alikes at pattern and filter boundaries (GTM/GA ids, pk_live/pk_test, AIza browser key, Sentry DSN, PostHog phc_, Supabase anon JWT vs first-party HS256/RS256 JWTs, base64 image, checksums beside brand prose, UUID ids, Bearer templates, identifiers under credential keys, sk-prefixed identifiers one over and one under the floor, 7- vs 8-char passwords, placeholders, a ghp_ token suppressed by the FP word list, CSS soup, Google OAuth client id, Mapbox pk., PayPal ids).
  • cases.ts: every generator × every accepting context (realism-gated), the negatives, and interaction probes. Exact (pattern, check, location) per case.
  • expected.json: snapshot of raw scanPageForSecrets + external-script output per case (masked values only). UPDATE_SECRETS_SNAPSHOT=1 rewrites it.
  • tests/security/leaked-secrets.test.ts: 1015 pass / 45 todo / 0 fail. Asserts exact finding sets, negatives, masking, dedup across three locations, and the double scan of inline scripts.
  • scripts/bench-leaked-secrets.ts + bun run bench:secrets: 200-page synthetic corpus (66.7 MB, minified filler with segment/cloudflare/twilio sprinkled, three 2–5 MB bundles). Baseline on M2/16 GB: best 946 ms, 14.2 ms/MB, peak RSS 383 MB.

Known gaps (asserted as current behaviour, one test.todo each; the suite prints the list)

20 distinct. The six briefed (Clerk shadowed by Stripe, PayPal class omits B–Y, Sanity fires on sk… identifiers, inline scripts scanned twice, no PostHog pattern, no size cap) plus 14 found while building the corpus, among them: Twitter bearer tokens are always dropped by the a{16,} filter; the FP list is unanchored substring tests (a key containing xxx/fake/dummy is dropped); Sanity fires inside base64 images; a Bearer header around a dotted value (JWT) yields a duplicate medium finding, so a Supabase anon key in a Bearer header is warned on; PayPal runs before Azure and DO Spaces before New Relic and both downgrade the later high finding; looksLikeCodeIdentifier drops ~30% of random keyed values (camelCase, on/is prefixes, _ with lowercase); the Supabase pattern is the generic HS256 header, so any session JWT is "Supabase anon" and a service_role JWT is reported as public. Full list in the README.

Checks

  • bun test packages/rules/tests/security/leaked-secrets.test.ts green; the pre-existing tests/leaked-secrets.test.ts untouched and green.
  • tsgo --noEmit on the package and on the harness files (with noUnusedLocals) clean.
  • detect-secrets hook clean; .secrets.baseline gains one entry for the bench:secrets script name.
  • Focused codex review folded in: realistic JWT/Mapbox/Discord/Basic/Azure structures, per-case seeds (no order dependence), realism gating of Bearer/sourcemap contexts, harder boundary negatives.

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

A corpus-driven harness for security/leaked-secrets: one seeded generator
per FAST/CONTEXT pattern (values built from parts, never a token literal),
twelve embedding contexts, hand-written boundary negatives, interaction
probes, exact (pattern, check, location) expectations per case, and a
checked-in snapshot of the raw scanner output so any detector change is a
reviewable diff. 20 known gaps are asserted as CURRENT behaviour with a
todo each, so the suite is green today and every fix flips one expectation.

bench:secrets scans a 200-page synthetic corpus (66.7 MB) and records
ms/MB, findings and peak RSS; baseline in the README.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
… audited sites

Negatives synthesised from what the detector fired on in the cloud audit
history (no real values reproduced): ordinary-word CONTEXT keywords beside
hashes and nonces, prefix patterns with no left boundary (re_ inside
_Care_Dry…, a UUID tail as a Telegram id, fooghp_/xsk_live_/abcAKIA), Neon
and PayPal on URL slugs, i18n sentences as secrets, Shopify storefront
tokens as a scoping question, Bearer template vs literal, AIza in the public
tier, and one-finding-per-distinct-value dedup on a shared theme bundle.
All pinned as knownGap; 33 distinct gaps now. 1038 pass / 64 todo.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9

nc9 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Second commit (8dbb30b): real-world false-positive shapes

real-world.ts synthesises what the detector fired on across 776 audited sites (no real values reproduced), all pinned as knownGap: ordinary-word CONTEXT keywords beside hashes/nonces (linkedin, cloudflare, together, segment, heroku, mistral, datadog); prefix patterns with no left boundary (re_ inside _Care_Dry…, a UUID tail as a Telegram id, fooghp_/xsk_live_/abcAKIA); Neon and PayPal on URL slugs; i18n sentences under password keys; Shopify storefront tokens as a scoping question; Bearer template vs literal; AIza in the public tier; one finding per distinct value on a shared theme bundle. 33 distinct gaps, 1038 pass / 64 todo.

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

… secrets corpus

Every key and value interpolated into script text now goes through
JSON.stringify (js/bad-code-sanitization), and the mirrored `example.com`
false-positive test is a substring predicate rather than a regex CodeQL reads
as an unanchored URL check (js/regex/missing-regexp-anchor). Same output:
the snapshot is byte-identical.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 merged commit cd9f746 into main Sep 16, 2026
15 checks passed
@nc9
nc9 deleted the test/leaked-secrets-harness branch September 16, 2026 00:30
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.

2 participants