Skip to content

TD-5451: Scrubber: a private key streamed in chunks, and the last PEM shapes - #314

Open
captcrock wants to merge 1 commit into
mainfrom
td/5451-pem-gaps
Open

captcrock wants to merge 1 commit into
mainfrom
td/5451-pem-gaps

Conversation

@captcrock

@captcrock captcrock commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

A private key streamed in chunks showed its body. A reply, its thinking and a command's output reach the event log a chunk at a time, and each chunk was scrubbed alone, so a key whose BEGIN line came in one chunk left every body line in the chunks after it as plain text.

Now, once a key's BEGIN line has come in a stream, the stream holds what follows until the key's END line, the stream's end or 16 KB. It then scrubs that text in one piece, exactly as the whole text is scrubbed. These are also masked now, with no END line: body lines behind log prefixes (Python logging, Go, logback, Rust tracing, Procfile runners, glog, logfmt, slog, JSON lines; quoted or not, with fields after the quote; in base64 or base64url), PHP string joins, PowerShell's `n, <line> elements, a mixed-case header, spaces before the closing dashes, Compose v1's app_1 | , and a one-line key whose last run is all letters.

What changes

Streams (core/tstd/logging.py KeyStreams, core/tstd/session.py SessionEventLog):

  • Each stream is read in place: a model response's text, its thinking, and a command's stdout and stderr.

  • Before a BEGIN line, a chunk is scrubbed as it comes, as before. The one exception is a line end that may start a BEGIN line (-----BEG), which is read with the next chunk.

  • Once a BEGIN line with no END after it arrives, everything from that line on is held. It goes out when one of these comes:

    • The END line: the text up to the END line is scrubbed as a whole, and what follows is read on.
    • The stream's end: the turn's end; the command's result, for its output; or the reply's own tool call, for its text. A tool call a worker made and relayed onto this log doesn't count.
    • 16 KB: the text before the last BEGIN line in it goes out, and the stream holds on from that line.

    Nothing is guessed about what is key text and what is prose: the whole-text scrub decides.

  • The held text and the event that ends its stream are appended together, under one hold of the log's lock.

  • A chunk held back whole adds no event.

  • A withdrawn response loses what it held.

Shapes (the PEM rules in core/tstd/logging.py):

  • A header in any case, and with spaces before its closing dashes.
  • Line prefixes: file:42:, a logger's name right after a level (INFO app: ), app_1 | , PowerShell's `n and `r`n, <line> elements, and PHP's "…" . joins.
  • Any other log prefix, up to 256 characters, but only before a line that reads as a random key line: 48 characters or more of base64 or base64url, mixed case, three digits or more, and at least 12 changes between lower case and upper case or digits. A key's shorter last line counts right after such a line. The line may be quoted, with fields after the closing quote (msg="…" caller=…, {"msg": "…", "level": 30}), and escaped quotes are read too.
  • JSON object keys: a JSON object's key is never read as the next string of a key.
  • One-line keys: the last run of a one-line key, all letters and cut short.

Before / after

Streamed (e2e/td5451_stream.py): every shape in the test file, in 14 chunkings each (sizes 1–120 and random, with and without text after the key).

shapes that show body text streams that show body text prose and code that differ from a whole-text scrub
main (each chunk scrubbed alone) 96 of 96 1,152 of 1,344 6 of 21
this branch 0 of 104 0 of 1,456 0 of 21

Whole text (e2e/td5451_shapes.py): the ticket's 14 shapes show the key on main in 14 of 14 cases, and here in 0 of 14.

Log formats (results/td5451-quoted-logs-final.log): Python logging with quotes, logfmt with a closing quote and more fields, slog, JSON lines with either key first, Go %q and Rust tracing fields show 123 of 123 key fragments on main cb20b685 and 0 here, as text and escaped inside a JSON string.

Independent review

Five independent reviews. Each got the diff, the ticket and main's code, and wrote its own probes. All five said FIX FIRST, and every finding was fixed, with tests that fail on the code before the fix.

The first four rounds reviewed an earlier streaming design, which guessed chunk by chunk what was key text and what was prose. Each round found new edges in those guesses:

  • leaks behind log prefixes;
  • padding ending a key, or being held without bound (a million blank lines held 1 MB and took 104 s);
  • a context that grew with the text, making streaming quadratic;
  • words masked at chunk ends;
  • quoted log lines leaking (in whole text too, as on main);
  • the generic prefix masking ordinary log lines ending in a path, hash or long name (a regression against main);
  • a whole stdout masked while stderr interleaved;
  • unknown-shape keys over 4 KB;
  • an ordering race.

After round four, I replaced that design with the one above. It holds what follows a BEGIN line and lets the whole-text scrub decide.

Fifth review: FIX FIRST (two defects, both fixed):

  1. The 16 KB window released everything when it overflowed, so a whole key that a grep hit's BEGIN line had cut showed past the cut: 17 of 25 lines in one case, and head over 12 keys showed 23–24 lines. It now holds on from the last BEGIN line in the window; both show 0.
  2. The 12-switch count was super-linear (every subset of 11 switches: 1.1 s a line). Each step is now atomic: 6–9 µs a character.

It also found long CamelCase names after a BEGIN line masked; a key line now also needs three digits. Its 450-case fuzz through the event log (sibling streams, thinking, relayed worker tool calls, results and the turn's end) matched the whole-text scrub every time.

Re-review of the fifth round: FIX FIRST (all fixed, and the reviewer's own probes are clean on the final code):

  1. The window still let a key through when its overflowing chunk ended inside the key's BEGIN line: the next chunk began mid-line. It showed a whole key for 30 of 200 padding lengths in 64-byte chunks. Now a BEGIN line being written is held there too: 0 of 200.
  2. My three-digit rule let a key through when its first line had fewer than three digits (about 1.5 in 1,000 random 64-character lines), behind an unknown prefix. A line is now refused only when it has fewer than three digits and three CamelCase words in a row, about 1 in 37,000 for a random line.
  3. Python's padded level (INFO app: ) fell outside the logger-name prefix. The level, its padding and the logger name are now one prefix; no cut of such a key shows.
  4. Runs of = were slow (2.4 s for 200 KB against 35 ms on main), since = counted as key text. It now counts only as padding at a line's end: 34 ms.

The re-reviewer confirmed the switch count is linear and unchanged, and that holding on from the last BEGIN line loses and reorders nothing.

Every review's probes, rerun on the final code:

  • 0 leaks in 1,116 streamed shapes, 11,160 cut-short streams and 43,490 two-way splits (plain, wire-escaped and JSON-escaped).
  • 0 of 36 log prefixes leak.
  • The fourth and fifth reviews' repros all pass.
  • Prose fuzz: the stream equals the whole-text scrub in 2,500 of 2,500 documents (results/td5451-review-probes-r18.log).

Speed

Times per MiB (this branch: results/td5451-timing-r9.log; main: results/td5451-timing-r8.log, the same hour). On main, each chunk is scrubbed alone.

text whole text, main whole text, here 64-char chunks, main 64-char chunks, here 4096-char chunks, main 4096-char chunks, here
prose 0.15 s 0.14 s 0.30 s 0.47 s 0.15 s 0.18 s
BEGIN lines, no END 0.15 s 0.25 s 0.29 s 0.39 s 0.15 s 0.27 s
key body after one BEGIN 0.17 s 0.25 s 0.31 s 0.48 s 0.15 s 0.19 s
PowerShell `n (worst whole text) 0.14 s 0.27 s 0.31 s 0.37 s 0.14 s 0.28 s
  • Streaming costs 1.2–2.0× main's per-chunk scrub. It's linear: held text is scrubbed once, when it goes out, never on every chunk. The earlier design was up to 8.6×.
  • Whole text costs up to 1.9× main, on BEGIN-heavy text.
  • Texts built against the generic prefix take 0.14–0.27 s per MiB.
  • The streamed-shape probe takes 59 s, where the earlier design took 18 minutes.

Bounds and trade-offs (mine, not from the ticket)

  • Delay: text after a BEGIN line shows when the key's END line comes, the stream ends or 16 KB pass. A reply that quotes a BEGIN line in prose streams the rest of its text at the end. Thinking held this way goes out after the reply text that came later.

  • 16 KB: a single key over 16 KB shows past 16 KB when streamed. RSA-16384, the largest common key, is about 12.6 KB.

  • Lost on cancel: a stream whose turn is cancelled without a turn's end loses the text it held. That text is never shown.

  • Generic log prefix: up to 256 characters, and only before random-looking key lines (48+ characters, 12 switches or more, and not name-like: fewer than three digits together with three CamelCase words in a row). With no END line:

    • a key's first line shorter than 48 characters shows behind an unknown prefix;
    • a key behind a prefix holding a backslash, a CSV prefix, app; or a prefix over 256 characters shows, as on main.

    With an END line, all of these are masked.

  • Still masked after a BEGIN line, though main keeps them: a line ending in a random-looking token behind a prefix, such as an integrity: sha512-… hash, a mixed-case @sha256: digest, or a CamelCase name with digits between its words.

Left as they are

  • A base64url key line that holds sk- (…joEO_sk-ufby…, no END line) is cut there by the API-key rule, and the key lines after it show. Main does the same. It's a separate rule, so it's left for its own ticket.
  • Other rules split across chunks. Assignment rules (FILES_TOKEN: then its value in the next chunk) are still read chunk by chunk, as on main. This change carries only PEM keys across chunks.

Tests

  • tests/test_pem_redaction.py: 1,144 passed (435 on main). Each review round's tests fail on the code before it.
    • Every shape streamed in chunks of 5 and 64, and 12 shapes in chunks of 1.
    • Each review's findings: log prefixes and quoted log lines, padding, bounds, the 16 KB window, held text and its order, interleaved streams, relayed worker tool calls, withdrawals and the end of the turn.
    • Four tests from the earlier design asserted its internal size bounds. They now assert the held design's bound (16 KB and a chunk) and time.
  • Scrubber-related test files: 2,817 passed (results/td5451-related-tests-r5.log).
  • Full core suite on the final commit: 11,767 passed, 13 skipped, 0 failed (results/td5451-full-suite-r3.log).
  • Corpus (43,442 files: this repo, the venv, node_modules, the cargo registry): masked as on main, with 0 masks lost and 1 added, in my comment quoting a mixed-case BEGIN line (results/td5451-corpus-diff-r8.log).

Checks

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

… shapes

Once a key's BEGIN line opens in a stream (a reply, its thinking, a command's
stdout or stderr), what follows is held until its END line, the stream's end or
16 KB, and scrubbed in one piece as the whole text. New no-END shapes: log-line
prefixes (only before random-looking key lines), PHP joins, PowerShell, <line>
elements, mixed-case headers, spaces before closing dashes, a letters-only last
run cut short.

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