fix(e2e): keep secrets out of observed node fields and collapsed cuts

Merged
OK
2mo ago
+824-378

Registered secrets leaked into model input and .e2e/cache entries through two holes:

  • B46: formatNode printed testid raw, and projectTree passed framePath (an iframe name) through raw. Selectors and attribute keys were also never redacted.
  • B34/B44/B47: an engine collapses whitespace, then cuts name at 256 and text at 512. redactCut only compared the cut against the value as written, so a secret like letters\nletters left a plaintext prefix of its collapsed form in observation text, the executor tree, and cache endAnchors.

Fix

  • redactNode (packages/e2e/src/agent/observation.ts) runs once in prepareObservation and redacts every field of SemanticNode. It is driven by NODE_FIELDS, a mapped type with one entry per contract field, so a new field does not compile until it says how it is redacted. Keys that are not in the contract are dropped.
  • formatNode and projectTree now take a branded RedactedNode and no redact argument, so a raw node no longer type-checks there. MCP locate and failure evidence go through the same path.
  • Each string field is redacted as written and in its collapseText form. If only the collapsed form holds a secret, the field is stored collapsed and masked.
  • redactCut and redactFragments in packages/e2e/src/internal/redact.ts now compare on readingKey: case-folded, with each whitespace run read as one space, plus an index map back to the original text. This catches cut and fragment forms after LF, tab, CRLF, NBSP, or repeated spaces.
  • Descriptors, end anchors, and relocation (describeTarget, containerKey, describeNodes, relocateDescriptor, describePosition, describeAnchors, anchorsPresent) take only RedactedNode and lose their own weaker redact pass, which redacted before collapsing and never applied redactCut. ReplayHost.redact goes with it.
  • readingKey returns the text as-is when it has nothing to collapse. Otherwise it builds the key in typed arrays and keeps one offset pair per collapsed run, so a large trace text stays cheap. On a 22 MB newline-heavy text with one secret, main uses 98 MB, a naive per-unit map 954 MB, and this branch 262 MB.
  • Before anchors are derived, the start capture is redacted again with the current ledger (redactNodesAgain). Otherwise a provider secret resolved mid-step would make an unchanged node look new.
  • describeAction redacts each acted-on node again with the current ledger for the same reason, so a secret resolved inside the action (a provider-backed typeSecret) is masked in the recorded descriptor and summary, as main's describeTarget(node, redact) did.
  • The container key an action sat within is redacted again with the current ledger too, as main's containerKey(..., redact) did at placement time.
  • docs/security.mdx: the claim that cache entries are secret-free now holds. "Where redaction stops" no longer lists whitespace-collapsed cuts.

Verified

Ran it locally: yes

  • Real CLI with a deterministic StepExecutor in a scratch project (/tmp/oskar-fix-observation-secret-leaks/proj-*). A page with a secret data-testid, a secret iframe name, and six variants of a whitespace secret (letters plus LF, tab, CRLF, NBSP, or 3 spaces; hex plus LF), each padded so it is cut at 256 and 512.
  • Two runs (record, then replay). A scan of the executor's observe({ tree: true }) dumps, .e2e/cache/*.json, and the reports, looking for the raw value, its collapsed form, its first 24 chars, and a 30-char collapsed prefix.
surfacemainthis branch
observation textprobe raw; prefixes of all 6 whitespace variantsclean
executor treeprobe raw; prefixes of all 6 whitespace variantsclean
.e2e/cache entry (endAnchors)prefixes of 5 whitespace variantsclean
report / junit / summarycleanclean
replayreplayedreplayed (anchors still match)
  • New regression tests fail on main and pass here:
    • secret-ledger.test.ts: 16 of 86 fail on main. Covers LF, tab, CRLF, NBSP, repeated spaces, and hex, with the cut in the first part, on the collapsed space, or in the second part, plus fragments.
    • agent-observation.test.ts: every field redacted; cut at the 256/512 limits across text, tree, and anchors.
    • trace-recorder.test.ts: a secret registered after the node was captured is masked in the recorded target.
    • agent-policy.test.ts security invariant: a full record and replay run through the fake engine, asserting no value or fragment reaches observations, the cache file, or the report. It fails on main.
  • Cost:
measuremainthis branch
prepareObservation, 5000 nodes8.7 ms18.8 ms
redactFragments, 22 MB text, single spaces668 ms / +98 MB590 ms / +98 MB
redactFragments, 22 MB text, newlines + indent709 ms / +98 MB746 ms / +262 MB

A size and memory regression test sits beside the linear-scan test in secret-ledger.test.ts.