Skip to content

Commit 45ff87a

Browse files
committed
fix: placeholder-safe single-pass multi-literal redaction
Addresses review feedback on #2013: sequential single-literal replacement (register somethinglong -> ${ABC}, then ABC -> ${OTHER}) could rewrite a placeholder produced by an earlier pass, corrupting it to ${${OTHER}}. Replaces the per-pair sequential loop with one placeholder-safe left-to-right multi-literal pass (parameterizeAgainstLiteralMap): it never re-scans text it has already emitted, so no literal can be matched inside another pair's placeholder token in either direction. A registered literal is matched before checking for an existing placeholder token, so a value that itself happens to look like ${SOMETHING} is still redacted correctly. The scan uses a sticky regex instead of slicing per character, and literal pairs are sorted once per payload/evidence walk instead of once per string leaf. parameterizeRecordedFillPayload/parameterizeBackendOutput are generalized to take injected leaf-transform/carries callbacks so the single-pair fill-boundary path (with its existing whitespace-collapse behavior) and the new multi-pair session-wide path share one structural traversal. Adds regression coverage for both result payloads and action-mode target evidence, plus the placeholder-shaped-literal edge case. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RarRVX34ZW25TJejBZJ2Ui
1 parent 7506e5d commit 45ff87a

4 files changed

Lines changed: 311 additions & 98 deletions

File tree

‎docs/adr/0017-parameterized-recorded-inputs.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,15 @@ redaction lookup, not a secret registry with a naming or comparison API.
122122

123123
- **Result/event/backend-output fields** (display data, never compared at replay): content-aware
124124
substring redaction, reusing the same recursive backend-output scrub the fill boundary already applies
125-
to its own entry, generalized over every literal registered so far in the session. Pairs are applied
126-
longest-literal-first so one registered value that is a substring of another (e.g. a username that is a
127-
prefix of a password) is never partially consumed by the shorter pair first.
125+
to its own entry, generalized over every literal registered so far in the session. The substitution
126+
itself is one placeholder-safe left-to-right pass over each string, not N sequential full-string passes
127+
— a naive sequential pass over the same value can corrupt an EARLIER pair's just-inserted placeholder
128+
when a LATER pair's literal happens to be a substring of it (register `somethinglong -> ${ABC}`, then
129+
`ABC -> ${OTHER}`, and a second pass over a value already rewritten to `${ABC}` matches "ABC" *inside*
130+
that token, producing `${${OTHER}}`). The single pass tries every registered literal longest-first at
131+
each position — so one registered value that is a substring of another (a username that is a prefix of
132+
a password) is never partially consumed by the shorter pair — and never re-scans text it has already
133+
emitted, so no literal can ever be matched inside another pair's placeholder token in either direction.
128134
- **`target-v1`/`targets-v1` identity evidence** (`label`, `ancestry[].label`, `scrollRegion.label` — the
129135
fields replay's own classification compares): never silently text-substituted while still claiming a
130136
trustworthy identity. Replay compares recorded identity against the *live* tree, which legitimately

‎src/daemon/__tests__/session-action-recorder.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,109 @@ test('#1398: two distinct --record-as names sharing the same literal value deter
500500
expect(JSON.stringify(session.actions)).not.toContain('hunter2');
501501
});
502502

503+
// #1398 review fix: a naive sequential per-pair pass can corrupt an EARLIER
504+
// pair's just-inserted placeholder when a LATER pair's literal happens to be
505+
// a substring of it. Register `somethinglong -> ${ABC}` then `ABC ->
506+
// ${OTHER}` (a --record-as name that collides with an unrelated variable's
507+
// own literal value): a later echo of "somethinglong" must become exactly
508+
// `${ABC}`, never the corrupted `${${OTHER}}` a naive second full-string pass
509+
// over the already-rewritten text would produce.
510+
test('#1398: redacting one literal never corrupts a placeholder another literal already produced (result payload)', () => {
511+
const session = makeIosSession('default');
512+
513+
recordActionEntry(session, {
514+
command: 'fill',
515+
positionals: ['id="field-one"', 'somethinglong'],
516+
flags: { recordAs: 'ABC' },
517+
result: { text: 'somethinglong' },
518+
});
519+
recordActionEntry(session, {
520+
command: 'fill',
521+
positionals: ['id="field-two"', 'ABC'],
522+
flags: { recordAs: 'OTHER' },
523+
result: { text: 'ABC' },
524+
});
525+
recordActionEntry(session, {
526+
command: 'wait',
527+
positionals: ['id="confirmation-banner"'],
528+
flags: {},
529+
result: { waitedMs: 1, text: 'Echo: somethinglong' },
530+
});
531+
532+
expect(session.actions[2]?.result).toEqual({ waitedMs: 1, text: 'Echo: ${ABC}' });
533+
const serialized = JSON.stringify(session.actions);
534+
expect(serialized).not.toContain('somethinglong');
535+
expect(serialized).not.toContain('${${OTHER}}');
536+
});
537+
538+
test('#1398: redacting one literal never corrupts a placeholder another literal already produced (action-mode target evidence)', () => {
539+
const session = makeIosSession('default');
540+
541+
recordActionEntry(session, {
542+
command: 'fill',
543+
positionals: ['id="field-one"', 'somethinglong'],
544+
flags: { recordAs: 'ABC' },
545+
result: { text: 'somethinglong' },
546+
});
547+
recordActionEntry(session, {
548+
command: 'fill',
549+
positionals: ['id="field-two"', 'ABC'],
550+
flags: { recordAs: 'OTHER' },
551+
result: { text: 'ABC' },
552+
});
553+
recordActionEntry(session, {
554+
command: 'is',
555+
positionals: ['visible', 'id="confirmation-banner"'],
556+
flags: {},
557+
result: {},
558+
targetEvidence: {
559+
role: 'text',
560+
label: 'Echo: somethinglong',
561+
ancestry: [],
562+
sibling: 0,
563+
viewportOrder: 0,
564+
verification: 'verified',
565+
},
566+
});
567+
568+
expect(session.actions[2]?.targetEvidence).toEqual({
569+
role: 'text',
570+
label: 'Echo: ${ABC}',
571+
ancestry: [],
572+
sibling: 0,
573+
viewportOrder: 0,
574+
verification: 'unverifiable',
575+
});
576+
const serialized = JSON.stringify(session.actions);
577+
expect(serialized).not.toContain('somethinglong');
578+
expect(serialized).not.toContain('${${OTHER}}');
579+
});
580+
581+
// #1398 review fix: a registered literal is matched BEFORE checking whether
582+
// an existing placeholder token starts at the current position, so a typed
583+
// value that itself happens to look like `${SOMETHING}` is still redacted
584+
// rather than being skipped as if it were already-parameterized text.
585+
test('#1398: a literal whose own text is shaped like an existing placeholder is still redacted', () => {
586+
const session = makeIosSession('default');
587+
const literal = '${OTHERVAR}';
588+
589+
recordActionEntry(session, {
590+
command: 'fill',
591+
positionals: ['id="field"', literal],
592+
flags: { recordAs: 'TOKEN' },
593+
result: { text: literal },
594+
});
595+
recordActionEntry(session, {
596+
command: 'wait',
597+
positionals: ['id="confirmation-banner"'],
598+
flags: {},
599+
result: { waitedMs: 1, text: `Echo: ${literal}` },
600+
});
601+
602+
expect(session.actions[1]?.result).toEqual({ waitedMs: 1, text: 'Echo: ${TOKEN}' });
603+
expect(JSON.stringify(session.actions)).not.toContain('OTHERVAR');
604+
});
605+
503606
test('#1398: dual-endpoint (targetEvidences) evidence is protected independently per endpoint', () => {
504607
const session = makeIosSession('default');
505608
const literal = 'hunter2-secret';

0 commit comments

Comments
 (0)