fix(secrets): redact base64-encoded secrets in traces

Merged
OK
2mo ago
+106-4
  • With web({ basicAuth: { username, password: secrets.get('BASIC_PW') } }), trace redaction rewrote the context-options httpCredentials entry, but every request after the 401 challenge carried Authorization: Basic <base64(user:pass)> into trace.network untouched. report.json still labelled the trace redaction: "complete". The MCP session trace goes through the same redactTraceArchives, so it leaked too.
  • redactFragments (packages/e2e/src/internal/redact.ts, used only by trace redaction) now decodes each base64 or base64url run ([A-Za-z0-9+/_-]{8,}={0,2}). A run whose decoded text holds a value, or an 8-char fragment of one, is replaced whole by that value's <secret:name> marker.
  • A run can open with text that is not part of the encoding, like https://app.test/reset/<b64>, cookie=v2_<b64>, or tokenValue<b64>. To catch those, each run is decoded from all 4 offsets a base64 group can start at. A <secret:name> marker that the decoded page text already contains counts as page text, not as a find. The same rule covers cookies and token segments that encode a secret.
  • 5 new unit tests (secret-ledger.test.ts, trace-redaction.test.ts) fail on main. docs/security.mdx updated, changeset added.
// before
{ "name": "Authorization", "value": "Basic YmJ1c2VyOlMzY3JldFBhc3N3MHJkLVhZWg==" }
// after
{ "name": "Authorization", "value": "Basic <secret:BASIC_PW>" }

For the reviewer

  • Cost. Each base64-alphabet run is decoded 4 times, and each decode goes through the same fragment scan the plain text does. That scan is slow on the non-ASCII garbage a misaligned decode yields (caseKey's per-character path). The extra cost only applies to traces of attempts or sessions that hold a secret. Measured on redactFragments (median of 5, range across runs, on a machine shared with other jobs):

    inputmainthis branch
    6.9 MB snapshot text with a 3 MB base64 data URI0.24s1.4 to 1.8s
    16.8 MB snapshot JSON, no data URI0.49s1.8 to 2.3s

    Decoding only at offset 0 cost about 3x and missed prefixed tokens. A prefilter could skip garbage decodes, but it would need a second copy of the spelling rules in valuePattern, so I left it out.

  • Over-masking. A run is replaced whole, so a data URI or token whose decoded bytes happen to share 8 characters with a secret is masked in the trace. This matches the existing plain-text fragment rule.

Verified

Ran it locally: yes

  • testbed, scratch config (trace: 'on', basicAuth with secrets.get('BASIC_PW'), local app on 4281 that sends a 401 challenge, one page load, a fetch, and a link navigation), e2e run --config e2e.bugbash-cfg.config.ts, then counted Authorization headers in the trace zip:
    unzip -p .../attempt-0/trace/trace.zip | grep -ao 'Authorization","value":"Basic [^"]*' | sort | uniq -c
  • MCP: e2e mcp --headless with the same config, open_session, start_recording, navigate /two, stop_recording, close_session. Same count over the session's trace.zip and trace-part1.zip.
  • Main was built at 65ae4a75. The main commits since then do not touch redaction. The branch was rebased onto bb51ec1f and the CLI run repeated.
surfacemainthis branch
e2e run trace.zip5 Authorization","value":"Basic YmJ1c2VyOlMzY3JldFBhc3N3MHJkLVhZWg==5 Authorization","value":"Basic <secret:BASIC_PW>
e2e mcp trace.zip3 ... Basic YmJ1c2VyOlMzY3JldFBhc3N3MHJkLVhZWg==3 ... Basic <secret:BASIC_PW>
e2e mcp trace-part1.zip2 ... Basic YmJ1c2VyOlMzY3JldFBhc3N3MHJkLVhZWg==2 ... Basic <secret:BASIC_PW>
report.json redaction"complete" (wrong)"complete"

On the branch, grepping the unzipped traces finds 0 hits for the plaintext password, 0 for its base64, and 0 for a base64 fragment of it.