Skip to content

TD-5450: Scrubber: real tokens after a secret name in any case go - #311

Merged
captcrock merged 2 commits into
mainfrom
td/5450-secret-names-real-tokens
Dec 4, 2026
Merged

captcrock merged 2 commits into
mainfrom
td/5450-secret-names-real-tokens

Conversation

@captcrock

@captcrock captcrock commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Secret names in any case (TD-5447) still let real tokens through, because the code check took them for names: npm_token=npm_…, shopify_access_token=shpat_…, sendgrid_api_key=SG.….…, jwt_secret=MySuperSecretKey2024, smtp_password=Abc123Def456, db_password=Summer2024!. A quoted JSON or dict key ({"client_secret": "…"}) never matched, and neither did the credential word alone (password=AbC123/xyz+9Q=). Now they all go, and code that names a secret stays as it is. Nothing main masks shows: main's rule still runs beside the new one (second commit, after the Manager's review).

What changes

Second commit (238729cb), after the Manager's review found passwords main masked showing:

  • Main's any-case rule runs, unchanged, beside the new one. A value either rule finds is masked, so nothing main masks shows.
  • One masking pass. Main's rules run exactly as on main, with main's own regexes, in main's order (the upper-case rule, its any-case rule on what that left, URL user info on what both left), and their masks are taken back to the text. The new rule and URL user info are found on that same text, and every span is masked once. Before, one rule's mask could hide the name or the scheme:// the next rule needed (the re-review's …\nDB_PASSWORD=…\napi_token = … showed the second value). secret_spans uses the same spans. The upper-case rule's new form, right after an escaped line break ("a\nDB_PASSWORD=hunter22"), is a separate regex found on the text beside main's rules, so it can't change what main's rules see (the second re-review).
  • The new rule reads the review's shapes as values too: $ only starts a name (Pa$$w0rd); a lone upper-case word after : is a type only with up to 3 digits (UINT32, not ORACLE2024); a quoted value holds no code's assignments, conditionals or module paths ("Pa55=word", "ya29.…"); only a 1-2 character name counts as minified code before =; a name doesn't end in .; a backslash ends a value only as an escape that ends it (\n, \", \\), so a JSON-escaped \/ stays inside.
  • Found, missed on main too: an index as the name (os.environ["DB_PASSWORD"] = "…"), a flag and its value after a space (--password X, --token X), b'…', r'…' and '''…''' strings, and YAML's db_password: Summer2024!.
  • Left alone (the review's over-masks): @param password: Password/secret …, token: docs/auth/v2.md, a secret's name inside an ARN, a version with a pre-release (v1.2.3-rc.1+build.5), a ~/ path, a sha256: digest; a quoted value stops before a following ;key=.
  • Speed: names in a long path or expression are checked once each, and the ARN check is one pass.

First commit (ced1e839), core/tstd/logging.py, the any-case rule, plus one fix to the upper-case rule:

  • Names in code are checked part by part. A value counts as code names only if each part (between _, . and ::) reads as lower, camel, Pascal or UPPER case, with digits only at the end of a word. Outside a type annotation, a part may also hold at most 3 digits, unless it is a lower-case word and its digits (x25519, sha256). Before, any identifier, dotted name or PascalCase word with digits counted.
    • Now masked: npm_aBc…789, shpat_0123…, SG.….…, Summer2024, Abc123Def456, Passw0rd.
    • Still code: token_v2, cfg.token, process.env.API_TOKEN, Http2Client, Uint8Array, vocab_size-1.
    • The same check applies to the names in arithmetic, so Summer2024! is no longer read as a name followed by an operator.
  • More ways to name a secret:
    • the credential word alone (password=, PASSWORD=, Token:); before, a name character had to come before it;
    • a quoted key, plain or escaped ({"client_secret": "…"}, {\"client_secret\":\"…\"}), and a backtick-quoted value;
    • PHP's and Ruby's =>, Go's :=, Make's ?=, and an X-API-Key header (api-key joins the credential words).
  • Values:
    • A key's == padding is not a comparison. db_secret=dGhp…== was skipped on main for every lower-case name.
    • A quoted value runs to its closing quote: 'django-insecure-h7#q!x2v@k9$z&3m' showed &3m on main. It doesn't run past a space or bracket, where the quote may close a string instead.
  • The upper-case rule finds a name right after an escaped line break: "a\nDB_PASSWORD=hunter22" showed the password on main.
  • A type after an unquoted : (_is_type) has capitalised words, at most one acronym, at most two digit groups, and at most one one-letter word ("Ed"), not right after the acronym. The second review found that a looser shape let about 1 random key in 20 through after client_secret: in YAML or a header.
  • So that real code stays (each found by the scans of real code below, or by the reviews):
    • :: is never a separator, so a Rust path stays (use winnow::token::take_till1;);
    • a type after : with no quote, with acronyms, digits, generics and paths: private_key: Ed25519PrivateKey, secret: Zeroizing<PayloadU8>, pub Token: super::super::Win32::Foundation::HANDLE, D3DKMT_PRESENTHISTORYTOKEN_0;
    • a name on a lower-case module path (rust_openssl.hpke.MLKEM768X25519PrivateKey);
    • a colour (Token: "#ddd0c0"), a version range or npm alias ("^1.0.0-beta.2", "npm:jsonwebtoken@9.0.0");
    • a numeronym in a name (k8s_secret, i18n_token, e2e_token), an address (127.0.0.1:8080), a lower-case path (/run/secrets/db.pem), and a number with an exponent in arithmetic (1e3*r);
    • a regular expression literal (/--ease-out:\s*…/), a conditional ({token:e?1:0}), and an assignment inside minified code (case"Token":i=e.value).
    • Where main's rule masks code (frag_token: crate::__u32, api_token = Config::TOKEN_V2, InheritPrivateKey: X509RequestInheritOptions), it still does, since main's rule still runs (see the question below).
  • Docs: the TD-5450 backlog entry, with what is left as it is.

Before / after

e2e/td5450_cases.py, 13 shapes (the ticket's 11, plus == padding twice): main 64d25272 shows 13; this branch masks 13.

Nothing main masks shows (e2e/td5450_regression_fuzz.py, synthetic passwords and keys in 10 assignment forms, plus pairs of assignments joined by escapes, ;, & and spaces, with URL values). A value counts if any character main masks shows:

single values, 12,000 single values, 30,000 (wider symbols) pairs, 20,000 (2 seeds)
first commit ced1e839 560 1,339 1,589 (801 + 788)
draft: main's rule kept, before the single pass (not pushed) 0 0 65 (35 + 30)
this branch 0 0 1 (1 + 0; below)

The 1 is in secret_spans only: in "…aws_secret_access_key = 1fCKuL/9\"}" main's span took in the \ of the escaped closing quote, and this branch's stops before it, since masking it would break the JSON escape. Main's redact_secrets shows that value; this branch's masks it.

The review's cases (e2e/td5450_review_cases.py): all 10 of 1a-1e masked (the first commit showed all 10); the 4 prose and ARN cases left alone; 8 of the 9 shapes main misses now masked, all but a key and its value on separate lines.

Real code and docs (e2e/td5450_corpus_diff.py): secret_spans with main's logging.py (4ff45906) and this branch's, over this repository, the core's installed Python packages, the UI's node packages and the Rust crates in the Cargo registry: 43,442 files, about 513 million characters.

  • 0 masks lost. (The first commit lost 20, all main masking code; main's rule now runs, so they are masked again.)
  • 94 new masks, all secret-shaped: this PR's own test strings and backlog entry; fake secrets in other tests (TOKEN = "td5203-literal-…", password: "s3cret-smtp-pw"); URL-parser test vectors ("password": "%3A%40c"); example passwords in library docs (Playwright's password: 's3cr3t', Tauri's); rustls test secrets in b"…" strings; and codecov badge tokens in README URLs.

Random keys (e2e/td5450_rate.py, 2,000 keys per row): the share that still shows.

key after client_secret: (main) (this branch) after token:, password: or X-Auth-Token: (main) (this branch)
32 base62 characters 0.3% 0.3% 100% 0.3%
20 base62 characters 3.6% 3.6% 100% 3.8%
40 characters, first upper-case 0.1% 0.1% 100% 0.1%
32 hex 0% 0% 100% 0%

Over 10,000 keys of 20 and 32 characters after client_secret: and token:, 0 show on this branch and not on main; 9,780 show on main and not here. What still shows is mostly main's own rule that a word with up to 3 digits after it is code (secret1, gpt2).

Time (e2e/td5450_timing.py, one core, 4 MiB; every shape grows linearly from 1 MiB):

shape main first commit this branch
prose 0.43 s 0.50 s 0.39 s
x_token=a+a+… (the review's) 1.21 s 4.48 s (7.4 s on the review's machine) 1.50 s
a long dotted path after secret: 0.82 s 3.14 s 1.03 s
x_token= + 40 different names joined by + 0.83 s 2.15 s 2.37 s (the slowest)
"token": keys in a row 0.65 s 1.27 s 1.62 s

Independent review

A fresh reviewer agent with read-only access, and no access to my conclusions, reviewed three rounds. It wrote its own scripts and ran them on one core.

First round, FIX FIRST, on the first commit:

  • Real Rust code masked (high): in its scan of the Cargo registry, about 30 spans. The bare-name rule took the second : of :: as a separator (use winnow::token::take_till1; became token:[REDACTED]). The digit limit also rejected real names (x25519_generate_private_key, Zeroizing<PayloadU8>, Curve25519SeedBin). My own scan had covered only the Python and node packages.
  • Still shown: escaped JSON keys, PHP =>, Go :=, X-API-Key, and a quoted value cut at &.
  • Over-masks: npm aliases, prerelease ranges, {token:e?1:0}.
  • Untested: 2 of 14 mutants changed no test result.

All fixed in the second round. The Cargo registry joined my scan.

Second round, FIX FIRST:

  • A new leak (medium-high): after an unquoted name:, about 1 random key in 20 read as a type name (client_secret: RVdD53X83RZJ…). Main shows 0.3% of 32-character keys; the second commit showed 4.7%.
  • New over-masks: names with a digit inside a word (k8s_secret, i18n_token, e2e_token).
  • Untested: 3 of 28 mutants changed no test result.

All fixed in the third round, as above, with a test for each.

Third round, MERGE. Its measurements:

  • 32-character keys show at 0.3% (main 0.3%), 40-character keys at 0.1% (0.1%), and 20-character keys at 3.8% (3.6%).
  • The round-2 over-masks stay unchanged, as on main.
  • Its scans: Python packages, 4,429 files, no new spans; node packages, 13,192 files, 9 new (badges and example passwords); Cargo, 23,715 files, 16 new (test data and badges), and 16 code spans that main masks are left alone.
  • No exceptions in 200,000 fuzzed strings, and all 42 adversarial shapes are linear: 4 MiB takes 3.0–4.4× as long as 1 MiB, at most 6.7 s.

Its low items:

  • Short random keys after an unquoted name show a little more often than on main: 12 characters 18.2% vs 15.5%, 16 characters 8.3% vs 7.8%.
  • Some values are still masked that main leaves alone, listed under "Left as they are".
  • Shown on main too: the weak passwords below.
  • 4 of 43 mutants changed no test result. I added tests for the _ALGORITHM exception (private_key = x25519_private_key) and the backtick-quoted value, and one random key for each of the two type rules. The reviewer's 2,000-key rate didn't move without those two rules. On my 10,000 keys, though, 7 more keys show without the digit-group limit and 3 more without the acronym rule, so they stay.

Fourth round (the Manager), FIX FIRST, on the first commit, against the bar "nothing main masked may show":

  • 416 of 12,000 synthetic passwords that main masked showed: $ inside a word, name: UPPER+digits, = in a quoted value, a trailing . or $, a JSON-escaped \/.
  • Over-masks: @param password: Password/secret …, token: docs/auth/v2.md, a secret name inside an ARN.
  • Shapes main misses too: os.environ["…"] = "…", --password X, "ya29.…", b'…' / r'…' / '''…''', YAML Summer2024!.
  • Tests checked only where a mask started; x_token=a+a+… took 7.4 s at 4 MiB against 2.2 s on main.

All addressed in the second commit, as described above.

Fifth round (a fresh reviewer), FIX FIRST, on the first draft of the second commit:

  • High: after an escaped line break, the upper-case rule's mask ran over the next name, so a second secret showed (…\nDB_PASSWORD=…\napi_token = Zry9…); the first commit had the same bug.
  • Medium: a mask ending at a backslash cut scheme:// off a URL, so part of its password showed.
  • Low: a quoted value ran into the next ;key=; versions, ~/ paths and sha256: digests were masked.
  • Fixed with the single masking pass and the small rules above, with a test for each.

Sixth round, FIX FIRST: findings 1 and 2 fixed and the span mapping exact (50,000 cases against its own reference), but main's chain still used the changed upper-case regex, so after \nDB_TOKEN=…\n a few characters main masked showed (AKIA… and ghp_… tails). Fixed by running main's chain with main's own regex and finding the escaped-line-break form separately. It also judged the escaped-quote backslash in the fuzz table not a regression: main's own output shows that value.

Seventh round, MERGE, on this commit:

  • No regressions: 125,000 fuzz cases (5 seeds; one seed puts literal [REDACTED] text in the inputs): 0 where main's redact_secrets masks a character this branch shows (the drafts had 25, then 2). In secret_spans, 2 differences, both characters main's own output printed while its spans claimed them.
  • Main's rules reproduced exactly: against its own reference, which runs main's rules by real substitution and tracks where each character came from, 0 mismatches on all 125,000 cases.
  • Real code: 6,134 files, 55 million characters: 0 masks lost; 60 new, all secret literals in tests or docs. Line by line through redact_secrets, 46,550 lines, 0 lost.
  • Speed: linear; at most about 4× main on its adversarial inputs (worst 3.0 s at 4 MiB, against 1.29 s on main; peak memory 142 MB). On x_token=a+a+… it measured 1.12 s against main's 1.21 s.
  • Note: the escaped-line-break rule uses main's value characters, so "FOO\nDB_PASSWORD=hunter22\nLOG_LEVEL=debug" masks through LOG_LEVEL=debug, as main does for the same line without the escape.
  • Its question: the AWS, GitHub, Google, npm, Shopify and SendGrid test values are built at run time; other made-up values such as x9K2mQ7vB4nL8pR3tY6wZ1aS5dF0gH and the Django key are literal. They match no provider's format, and push protection passed them on the first commit.

Tests

GitHub's push protection blocked the first push: it took the test's fake Shopify token for a real one. The test now builds the npm, Shopify and SendGrid values from pieces at run time, so the repository holds no token-shaped text. The npm and Shopify values are the same as before; the SendGrid value's second part is a different made-up run. The code under test is unchanged.

New tests (tests/test_pem_redaction.py). Each secret case checks the whole output and secret_spans, so a value masked only in part fails (the Manager's review: before, only where the mask started was checked):

  • 54 secret cases: the ticket's shapes, == padding, the credential word alone, quoted, escaped and backtick-quoted values, PHP, Go, Make, the header, the Django key, random keys after an unquoted name, an upper-case name after an escaped line break; and, from the second commit, the review's 1a-1e (Pa$$w0rd, ORACLE2024, "Pa55=word", summer2024., monkey2010$, wJalr…\/K7MDENG\/…, …) and the shapes main misses (os.environ[…], --password X, "ya29.…", b'…', r'…', '''…''', YAML Summer2024!);
  • 8 cases where one mask must not hide the next secret: the re-review's escaped line break and tab, two URLs with a backslash in the password, a quoted value before ;user=, and the second re-review's three upper-case names after an escaped line break followed by an AWS key, a GitHub token or a JSON-escaped quote; secret_spans must agree with the output;
  • 60 code and prose shapes that stay, including the review's @param password: Password/secret …, token: docs/auth/v2.md, ARNs, a version with a pre-release, a ~/ path and a sha256: digest;
  • 3 shapes of code that main masks and that stay masked (crate::__u32, Config::TOKEN_V2, X509RequestInheritOptions);
  • an assignment chain and 64,000 quoted keys in a row, each read in one pass.

The whole file: 562 pass on this branch. With this branch's tests, the first commit fails 40 and main (4ff45906) fails 42. Two unpushed drafts of the second commit failed 8 (before the single pass: the 5 cases above and the version, ~/ path and digest shapes) and 3 (before main's own upper-case regex: the second re-review's 3 cases).

Run, one core, on this branch: the 48 test files that mention redaction or hold the fixtures above: 2,146 passed. The full suite runs in CI; the machine is on a CPU hold for other work.

Left as they are

  • Weak passwords that read as a name: Welcome1!, hunter2, Summer_2024. Each is shaped like a code name or arithmetic (secret1, value!, cache_2024); main shows them too.
  • A Pascal-case password as an unquoted YAML value: smtp_password: Abc123Def456 reads exactly like a type annotation (private_key: Ed25519PrivateKey). Quoted, or with =, it goes.
  • A key and its value on separate lines, Authorization: Token … and <password>…</password>: each needs a different rule; all show on main too.
  • Masked, where main leaves them alone: a quoted Python forward reference (private_key: "Ed25519PrivateKey"), a quoted algorithm name ("secret": "ED25519"), api_key: IPv4Address, Token: 1h30m, secret: oauth2/v3/certs, and the user name in a URL whose password holds a backslash (secret: https://admin:Abc\\def123@db.local becomes secret: [REDACTED]@db.local). None of the first five turned up in the 43,442 files scanned.
  • The \ of an escaped closing quote after a value is outside secret_spans here and inside on main (see the fuzz table); the value itself is masked.

Question

Keeping main's rule keeps main's masks on code too: frag_token: crate::__u32, api_token = Config::TOKEN_V2 and InheritPrivateKey: X509RequestInheritOptions are masked again (the first commit had stopped masking 20 such spans). Leaving out of main's rule the values the new rule reads as Rust paths or annotation types would bring those back, but nothing main masks showing would then hold only as far as that carve-out is right. I recommend keeping it as it is. Your call.

Checks

  • ruff, ruff format and mypy: clean.
  • release_scan --git: 0 hits.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

R&D12 and others added 2 commits December 3, 2026 23:41
A value that only looked like a code name was let through: npm_…,
shpat_…, SG.….…, MySuperSecretKey2024, Abc123Def456, Summer2024!. Each
part of a name now reads as lower, camel, Pascal or UPPER case with
digits only at a word's end, and outside a type annotation holds at most
3 digits unless it is a lower-case word and its digits (x25519). The
credential word alone, quoted and escaped-JSON keys, "=>", ":=" and
X-API-Key count as a name; a quoted value runs to its closing quote; a
key's "==" padding is not a comparison; an upper-case name after an
escaped line break is found again.

So that code stays, "::" is not a separator, and a type annotation (with
generics and paths), a name on a lower-case module path, a colour, a
version range or npm alias, a regex literal, a conditional and an
assignment inside minified code read as code. Checked on 513 MB of real
code: every new mask is a secret-shaped value, and every lost one was
main masking code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
Review (FIX FIRST): synthetic passwords main masked showed ("$" in a
word, UPPER+digits after ":", "=" in quoted values, a trailing "." or
"$", JSON-escaped "/"). Main's any-case rule now runs unchanged beside
the new one. Main's rules run in main's order and their masks are taken
back to the text; the new rule and URL user info are found on that same
text; all are masked once, so one mask never hides the name or the
"scheme://" another rule needs (re-review). secret_spans uses the same
spans.

The new rule reads those shapes as values too, and finds
os.environ["X"] = "…", --password X, quoted "ya29.…" tokens,
b''/r''/''' strings and YAML Summer2024!. Prose (Password/secret),
relative file paths, versions, ~/ paths, digests and names inside an
ARN stay; a quoted value stops before a following ";key=".

Speed: names in a long path or expression are checked once each; the
ARN check is one pass.

Tests assert the whole output (where each mask ends) and add each
review case; provider-shaped values are built at run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
@captcrock
captcrock merged commit cb20b68 into main Dec 4, 2026
10 checks passed
@captcrock
captcrock deleted the td/5450-secret-names-real-tokens branch December 4, 2026 01:43
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.

1 participant