Skip to content

TD-5447: Scrubber: a private key's body never shows, however it is written or cut - #306

Open
captcrock wants to merge 1 commit into
mainfrom
td/5447-pem-escaped-no-end
Open

captcrock wants to merge 1 commit into
mainfrom
td/5447-pem-escaped-no-end

Conversation

@captcrock

Copy link
Copy Markdown
Contributor

The scrubber missed a private key that was cut short (no END line) once its line breaks were written as escapes. That covers a key in a JSON or .env value, a Python repr, JSON inside JSON, and many tools' output. Its body then showed in chat text, tool output and on the wire. This PR closes that gap and matches secret names in any case (aws_secret_access_key = …).

What changes

A private key's body never shows, however it is written or cut (core/tstd/logging.py).

  • Line breaks: real (including Word's and Unicode's), escaped at any depth (\n, \\n, \u000a, \012, \N{LINE FEED}), %0A, &#10;/&amp;#10;/&NewLine;, <br>, a <p>/<div> per line, SQL's || chr(10) ||, and strings joined across lines in source code or a JSON array.
  • Decoration: spaces, commas, +, a shell's \, cat -A's $, ANSI colours and zero-width characters around the break.
  • Line prefixes:
    • comment, quote and diff marks;
    • a log line's time, source or level (syslog/journalctl, container logs, INFO);
    • line numbers (cat -n, less -N, N | , bat's │, grep's 2:/k.pem:2:/k.pem-2-).
  • Key characters escaped: \/, \u002b, %2F, &#x2F;, &sol;.
  • Bodies and blocks: base64url bodies, old-style Proc-Type/DEK-Info lines, lower-case and URL-encoded BEGIN lines, PGP private key blocks, SSH2 private keys.

Prose and code after a BEGIN line keep their words.

  • The first line must read as key text: a DER or OpenSSH start, or a digit, + or /. It must not read as an assignment, a dict key, a call, an attribute or words.
  • A later line that is one plain word ends the key.
  • A key cut short inside a string stops at the string's end, so the next line, dict key or list item stays.
  • Only the BEGIN line itself is masked.

Read once.

  • The search for an END line stops at the next BEGIN line. A 1 MiB text with 2,000 BEGIN lines and no END line took about two minutes and now takes 0.21 s.
  • Header lines, prefixes and long runs are each read once.

Secret names in any case. aws_secret_access_key = …, db_password=…, apiToken: '…'. Code that names a secret the same way is left alone. In lower or mixed case:

  • the name must end in a credential word, with a name character before it (auth_token_ttl, max_tokens, x_tokenizer stay);
  • ==, !=, >= and <= are comparisons;
  • the value must hold a digit, /, + or =, and must not read as code:
    • a number with or without a unit, a version, time or date;
    • a hex, octal or binary literal;
    • a word with up to 3 digits after it;
    • a snake_case, dotted or type name, or words joined by /;
    • arithmetic such as pos+1 or offset+limit;
    • a call or an index.

Each value is read once. The capitals pattern main already had is unchanged.

Docs: the TD-5447 backlog entry.

Before / after

For each of 80 key shapes: every cut point × 5 endings × 3 places (text, the wire, JSON-escaped on the wire). The 11 shapes from TD-5445's report are first; the rest were found by me and by four review rounds.

main 258ba80 this branch
Shapes showing part of a key 78 of 80 0 of 80
2,000 BEGIN lines, no END line, 1 MiB (median) 114 s 0.21 s
aws_secret_access_key = wJalr… shown masked
83 code, prose and settings texts that must stay — all unchanged

tests/test_pem_redaction.py is new. Against main's tstd: 290 failed, 133 passed (td5447-new-tests-on-main-by-test). The 133 that pass on main are:

  • the 55 code texts left alone (main has no lower-case rule to mask them);
  • the 62 cases where main already hides the key (42 cut-short cases, 20 chat/tool cases);
  • 16 others: 5 code-after-BEGIN and 2 prose texts, 3 long-run timings, 3 runs of secret words, the END-line case, one lower-case secret and the 8-39 character cut.

Main fails 5 of the 7 prose texts and 7 of the 12 code-after-BEGIN texts: it masks the words after a BEGIN line.

Cost

Ordinary large text, redact_secrets, median of 5 interleaved runs against main on a heavily loaded shared machine (load average 68):

Text size main this branch ratio
python source 4.2 MB 1.25 s 1.45 s 1.16×
markdown docs 3.0 MB 1.00 s 1.09 s 1.08×
TypeScript source 4.2 MB 1.23 s 1.48 s 1.20×
base64 blob, 76-char lines 4.2 MB 1.08 s 1.16 s 1.07×
JSON 4.2 MB 1.00 s 1.07 s 1.06×
log lines 4.2 MB 0.97 s 1.03 s 1.06×

The text is this repo's own source and docs, plus generated base64, JSON and log lines (e2e script td5447_perf_ordinary.py). The reply funnel runs this scrub on the event loop today; TD-5446 moves large replies off it.

Independent review

Four rounds. Each was a fresh reviewer agent with read-only access and no access to my conclusions, and each wrote its own scripts. I reproduced every finding before fixing it.

  • Round 1 (first version):

    • token counts and code were masked (max_tokens=4096, my_secret = secret1);
    • Word's and Unicode's line breaks and // comments showed the key; main hid those;
    • a long name full of secret words was quadratic.
  • Round 2: my fixes had made three new problems:

    • a quadratic header line;
    • a quadratic chain of names;
    • code after a BEGIN line eaten.

    It also found settings masked (auth_token_ttl=3600s, apiToken: Uint8Array) and 15 more key shapes.

  • Round 3: my fixes had again made problems:

    • a quadratic snake_case check;
    • a quote that let the mask run past a string's end ("\nother = 1, """\n\ndef, the next dict key);
    • no-space code masked (prevToken===nextToken, offset+limit).

    It also found grep -A and bat gutters and \u002b showing the key.

  • Round 4:

    • a closing quote after \n was read as a new string, which ate the next line (return KEY, "hello" in a list);
    • " + base64Body + " eaten;
    • URL-encoded BEGIN lines not matched at all;
    • log and tool line prefixes;
    • numbers with units and >=/<= masked.

Checking round 4's fixes: I reran all four reviewers' scripts on them. No key shows, except one synthetic shape described below. All 26 of round 4's adversarial inputs grow about 4× from 1M to 4M repetitions (td5447-review4-perf-rerun). There was no fifth fresh review.

Known and left as they are:

  • Lines that open a quote and never close it ("MIIE… then "NYv… with no closing quotes) still show. Crossing such a quote is what made the mask eat code after a string's closing quote. I don't know a tool that writes this.
  • Values under secret names stay masked when they are hex, UUIDs, URLs, paths or model names (commit_token=1c57…, model_token = gpt-4o-mini, ssh_private_key: /home/u/.ssh/id_rsa). See question (c).
  • Bare names are not masked in lower case: password=… without a prefix, and word-plus-3-digits values (hunter22). See question (a).
  • secret_spans and redact_secrets differ on glued fragments about as often as on main: 959 against 1,062 of 30,000 random texts.

Questions

  • (a) Should bare lower-case names (password=…, token=…) be covered too? Code uses them most.
  • (b) Main's capitals pattern is quadratic on "X_" + "TOKEN_" * N + "=" (6.9 s at 48 KB on main). Fix it here or separately?
  • (c) Should a path or URL under a secret-named key count as a secret?

Full suite

On 1b2dc5b0, at load average 20-21: 2 failed, 10,989 passed, 13 skipped, 4 deselected.

  • Both failures are in test_session_preset.py: a test daemon hadn't bound its port in time (assert 0 > 0 on ws_server.port).
  • On their own: that file passed 3 of 3 times with this branch's code and 3 of 3 times with main's (7 passed each time).

An earlier full run on the same commit, at load average about 68 while the machine's disk was running low, had 48 failed and 4 errors. Nearly all were test daemons that didn't bind or answer in time.

  • Rerun: I reran the 31 files involved three times on this branch and three times on main, interleaved. Each side ran from its own full copy of the repo.
  • Result: every run passed: 391 passed, 0 failed, 2 min 22 s to 2 min 29 s each.

CI on a clean runner is the reference.

Checks

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

…itten or cut

A private key cut short (no END line) lost only the part before its first
escaped newline, so a key in a JSON or .env value, a repr, or JSON inside JSON
showed its body in chat, tool output and on the wire. Its body now runs across
any line break, real or escaped, with the decoration, line prefixes and string
joins that tools and source code put around lines, and escaped key characters.
Base64url bodies, lower-case BEGIN lines, PGP private key blocks and SSH2
private keys are covered. The first line must read as key text, not as code
or words, and a key cut short inside a string stops at the string's end, so
prose and code after a BEGIN line keep their words. The END search stops at
the next BEGIN line (2,000 BEGIN lines in 1 MiB: ~2 minutes before, ~0.2 s
now); header lines and long runs are read once.

Secret names match in any case (aws_secret_access_key = ...), with code left
alone: the name must end in a credential word, == is a comparison, and the
value must not read as code (numbers, versions, dates, type names, snake_case
and dotted names, arithmetic such as pos+1 or offset+limit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
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