Skip to content

security/leaked-secrets: Discord Bot Token pattern is quadratic on long base64 runs (2.7 s per 200 KB data URI) #365

Closed

Description

@nc9

FAST_PATTERNS entry Discord Bot Token, /[MN][A-Za-z\d]{23,}\.[\w-]{6}\.[\w-]{27}/g, has an unbounded alphanumeric run before the first .. On a long run of base64 (an inline image data URI, an inlined font, a wasm blob) every M or N in the run consumes the rest of it before failing on the ., so the cost is quadratic in the run length.

Measured while building the #360 decoders (page = one 200 KB data:image/png;base64,… in an <img>): the whole scanContent of the serialized page takes 4.6 s, of which this one pattern is 2.7 s; every other pattern is under 5 ms. A base64 run of "M" repeated 8k/16k/32k characters scans in 83/314/1313 ms. Pages with a couple of inlined images pay this on every audit, twice (the html scan and the inline-script scan both see it).

Found on the #360/#361 PR, where the corpus probe for the "200 KB data URI" criterion had to be shrunk to 20 KB because of it. The new decoders skip data URIs entirely; the cost is the pre-existing pattern.

Discord token structure per gitleaks: first segment 24–28 chars (base64 of the numeric user id), so bounding it is safe: [MN][A-Za-z\d]{23,28}\.[\w-]{6}\.[\w-]{25,110} (gitleaks uses {23,25} / {25,110}).

Acceptance Criteria

  • scanContent of a page carrying a single 200 KB base64 image data URI completes in under 50 ms on the bench machine (currently 4.6 s), with no change to which secrets the corpus reports (bun test tests/security/leaked-secrets.test.ts stays green without a snapshot change)
  • A real-shaped Discord bot token (M/N + 24–28 chars, ., 6 chars, ., 27+ chars) still reports high; the corpus generator for it is unchanged or updated to the bounded shape
  • The probe probe:base64-image-data-uri-is-never-decoded in tests/security/leaked-secrets/cases.ts is restored to a 200 KB URI

Activity

  1. added
    bug
    P2
    size: S
    rules
    security
    on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2bugrulessecuritysize: S

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions