Skip to content

feat(rules): decode encoded content and token structure in leaked-secrets (#360, #361) - #364

Merged
nc9 merged 1 commit into
mainfrom
feat/360-361-secrets-decoders
Sep 16, 2026
Merged

nc9 merged 1 commit into
mainfrom
feat/360-361-secrets-decoders

Conversation

@nc9

@nc9 nc9 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Closes #360, closes #361.

What

Two new modules under packages/rules/src/security/secrets/, hooked into leaked-secrets.ts at two points (content entering scanContent, and the build of each finding), plus a base64 pass after the existing two passes. The FAST/CONTEXT tables are untouched except for one appended CONTEXT entry (Algolia secured key).

#360 content decoders (decode.ts)

  • HTML character references decoded before the html scan (the serializer writes attribute quotes back as ", so data-state="{"apiKey":…}" was invisible). Reports at html.
  • \uXXXX / \u{X} / \xXX decoded in script text; an escaped backslash is left alone. Reports at the script's location.
  • base64 runs of 64+ chars that decode to text are scanned as their own content, depth 2, reported with a (base64) location suffix. data: URI payloads and SRI hashes are never decoded; binary blobs are skipped; 256 blobs / 8 MB decoded per text.
  • The run finder samples every 32nd character (a char loop or a global {64,} regex both blew the budget). A 200 KB image data URI adds under 1 ms (pinned in a test).

#361 token decoders (confidence.ts), offline only

  • AWS access key id: base32 account-id decode (algorithm and references in the comment). Undecodable suffix, or a decode past 12 digits, downgrades to medium. extra.accountId.
  • GitHub ghp_/gho_/ghu_/ghr_: CRC32 of the 30-char body, base62, 6 chars; verified against a published expired token. Wrong checksum drops the finding.
  • JWTs: service_role (Supabase) high, anon public, expired exp → new leaked-secrets-expired info check, other issuers medium at most as JSON Web Token (never Supabase), admin scopes high. Claims in extra.
  • Algolia secured keys: restriction params decoded; restricted → public, unrestricted → medium, non-keys dropped. 32-hex near algolia stays medium with keyType.
  • Stripe kind/mode, Slack structure, Sentry org/project as extra.
  • extra becomes the check item's meta, which packages/report JSON/LLM/XML and the API's run-report item shape already carry through.

Tests

suite result
tests/security/leaked-secrets.test.ts (corpus) 1419 pass, 70 todo
tests/leaked-secrets.test.ts 62 pass
tests/security/secrets-decode.test.ts (new) 24 pass
tests/security/secrets-confidence.test.ts (new, independent CRC32/base32 oracles, fetch throws during the scan) 22 pass
packages/audit-engine/tests/streaming-collector-rules-golden.test.ts 1 pass

Snapshot regenerated; the only changed pre-existing rows gained extra fields, masked values are unchanged. Four new corpus contexts, 20 new probes, known gap 18 flipped to fixed.

Bench

Back to back on the same machine, 5 iterations: before cd9f746 931 ms / 13.97 ms/MB, after 990 ms / 14.85 ms/MB (+6.3%). Recorded in the README.

Found, not fixed here

The pre-existing Discord Bot Token pattern is quadratic on long base64 runs (2.7 s per scan of a 200 KB data URI page today). The decoders do not add to it, but the corpus probe for the data-URI criterion carries a 20 KB URI for that reason. Filed separately.

Codex focused review (throw/loop/memory) folded in: decoded findings appended by iteration instead of spread, decode budget charged before allocation, exp outside Date range read as absent.

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

@nc9
nc9 marked this pull request as draft September 16, 2026 01:06
@nc9
nc9 force-pushed the feat/360-361-secrets-decoders branch from cd9c585 to 33201a6 Compare September 16, 2026 02:40
@nc9
nc9 force-pushed the feat/360-361-secrets-decoders branch from 33201a6 to 1a1f5c0 Compare September 16, 2026 02:51
@nc9
nc9 force-pushed the feat/360-361-secrets-decoders branch from 1a1f5c0 to 3096c5a Compare September 16, 2026 03:03
@nc9
nc9 marked this pull request as ready for review September 16, 2026 03:09
…rets (#360, #361)

Content decoders (#360), packages/rules/src/security/secrets/decode.ts:
- HTML character references in the serialized page are decoded before the
  html scan, so a JSON object stored entity-encoded in an attribute is read
  as JSON and reports at `html`.
- `\uXXXX`, `\u{X}` and `\xXX` escapes in script text are decoded (an
  escaped backslash is left alone), so an escaped string literal reports
  at the script's location.
- Percent-encoded runs (3+ `%XX` escapes that decode to text) are decoded
  in place, so a URL-encoded settings blob reports at its plain location.
- Standalone base64 runs of 64+ chars that decode to text are scanned as
  content of their own, two levels deep, reported with a ` (base64)`
  location suffix. data: URI payloads and SRI hashes are never decoded.
  The run finder samples every 32nd character rather than scanning them
  all; a 200 KB image data URI adds under 1 ms.

Token decoders (#361), packages/rules/src/security/secrets/confidence.ts,
all offline, never a provider call:
- AWS access key id: base32 account id decode; a suffix that does not
  decode to a 12-digit id is medium, not high. `extra.accountId`.
- GitHub classic tokens: CRC32/base62 checksum of the 30-char body,
  verified against a published expired token; a wrong checksum drops the
  finding.
- JWTs: the Supabase HS256 header literal becomes a general `JSON Web
  Token` FAST pattern (any alg, three base64url segments); which token it
  is comes from the decoded claims. Supabase `service_role` high, `anon`
  public, expired `exp` and standalone first-party session tokens (a
  subject, an `exp` within 30 days, no admin scope, no third-party issuer,
  not inside an `Authorization: Bearer` literal or under a credential key)
  into a new `leaked-secrets-info` check, other issuers medium at most and
  never labelled Supabase, admin scopes high. Claims ride along as `extra`.
- Algolia secured keys (new CONTEXT pattern): restriction params decoded;
  restricted keys public, unrestricted medium, non-keys dropped.
- Stripe mode/kind, Slack structure, Sentry org/project as `extra`.

`extra` is attached to the finding and emitted as the check item's `meta`,
which the JSON, LLM and XML renderers and the API report already carry.

Corpus: six new contexts (entity attribute, escaped script string, base64
data attribute, base64 global, URL-encoded data attribute and inline
settings), 29 new probes, generators for GitHub and
AWS produce structurally valid values, known gap 18 fixed. Two new unit
suites with independent CRC32/base32 oracles and a fetch-throws guard.
Bench on the precision base f445a7d: 13.96 -> 14.62 ms/MB (+4.7%),
recorded in the README.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/360-361-secrets-decoders branch from 3096c5a to ae73289 Compare September 16, 2026 03:11
@nc9
nc9 merged commit f35371e into main Sep 16, 2026
14 checks passed
@nc9
nc9 deleted the feat/360-361-secrets-decoders branch September 16, 2026 03:19
@nc9 nc9 mentioned this pull request Sep 16, 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

2 participants