Skip to content

Commit 5f96199

Browse files
thymikeeclaude
andcommitted
fix(fill): redact read-back before literal scrubbing and in settle output
Redact a sensitive fill's read-back fields before the literal is scrubbed, so a short literal cannot rewrite the verification discriminator and leak the altered value. Keep the unconfirmed evidence keys structural. Replace the diff line text and tail labels of a --settle observation with the placeholder. Run the error read-back redaction for an empty sensitive value too. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 97e4390 commit 5f96199

8 files changed

Lines changed: 200 additions & 30 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+TextEntryConfirmationTests.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ extension RunnerTests {
302302
XCTAssertFalse(didRecordXCTestFailure(since: failures))
303303
XCTAssertFalse(response.ok)
304304
XCTAssertEqual(response.error?.code, "TEXT_ENTRY_MISMATCH")
305-
XCTAssertFalse(response.error?.message.contains(text) ?? true)
305+
XCTAssertEqual(response.error?.message, "text entry read-back did not match the requested text")
306306
XCTAssertEqual(field.value as? String, "a")
307307
let counters = app.staticTexts["agent-device-text-entry-write-backs"].label.split(separator: " ")
308308
XCTAssertTrue(counters.contains("total-edits=11"))

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+TextTypingTests.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ extension RunnerTests {
460460
XCTAssertFalse(response.ok)
461461
XCTAssertEqual(response.error?.code, "TEXT_ENTRY_MISMATCH")
462462
// A sensitive fill's value must not come back, and the observed text is an altered form of it.
463-
XCTAssertFalse(response.error?.message.contains("23456") ?? true)
463+
XCTAssertEqual(response.error?.message, "text entry read-back did not match the requested text")
464464
XCTAssertEqual(app.textFields.count, 1)
465465
XCTAssertEqual(editableTextValue(for: app.textFields.element(boundBy: 0), treatingPlaceholderAsEmpty: true), "23456")
466466
}

‎packages/daemon/src/core/fill-read-back-redaction.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,50 @@ const UNCONFIRMED_READ_BACK_FIELDS = ['requested', 'before', 'after'] as const;
1313
const FAILURE_READ_BACK_FIELDS = ['expected', 'actual'] as const;
1414
const FAILURE_INPUT_NODE_FIELDS = ['targetInput', 'actualInput'] as const;
1515

16-
export function redactUnconfirmedFillReadBack<TPayload extends ReadBackRecord | undefined>(
16+
/**
17+
* Replaces the text a sensitive fill's response read back from the field: the unconfirmed
18+
* evidence, and the diff lines and tail labels of a `--settle` observation. Refs, counts and
19+
* summaries stay. This runs before literal parameterization, so a short literal cannot rewrite
20+
* the `verification` discriminator this checks.
21+
*/
22+
export function redactFillReadBack<TPayload extends ReadBackRecord | undefined>(
1723
payload: TPayload,
1824
placeholder: string,
1925
): TPayload {
20-
if (!payload || payload.verification !== 'unconfirmed') return payload;
21-
return { ...payload, ...placeholderFields(payload, UNCONFIRMED_READ_BACK_FIELDS, placeholder) };
26+
if (!payload) return payload;
27+
const unconfirmed =
28+
payload.verification === 'unconfirmed'
29+
? placeholderFields(payload, UNCONFIRMED_READ_BACK_FIELDS, placeholder)
30+
: {};
31+
return {
32+
...payload,
33+
...unconfirmed,
34+
...(isRecord(payload.settle)
35+
? { settle: redactSettleReadBack(payload.settle, placeholder) }
36+
: {}),
37+
};
38+
}
39+
40+
function redactSettleReadBack(settle: ReadBackRecord, placeholder: string): ReadBackRecord {
41+
const { diff, tail } = settle;
42+
return {
43+
...settle,
44+
...(isRecord(diff) && Array.isArray(diff.lines)
45+
? {
46+
diff: {
47+
...diff,
48+
lines: diff.lines.map((line) => redactTextField(line, 'text', placeholder)),
49+
},
50+
}
51+
: {}),
52+
...(Array.isArray(tail)
53+
? { tail: tail.map((entry) => redactTextField(entry, 'label', placeholder)) }
54+
: {}),
55+
};
56+
}
57+
58+
function redactTextField(entry: unknown, field: string, placeholder: string): unknown {
59+
return isRecord(entry) ? { ...entry, ...placeholderFields(entry, [field], placeholder) } : entry;
2260
}
2361

2462
/** A backend fill failure is typed by `failureReason`; only that shape carries read-back text. */

‎packages/daemon/src/execution/interaction/internal/interaction-common.test.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test('parameterized fill scrubs backend and nested settle echoes at the response
110110
},
111111
settle: {
112112
diff: {
113-
lines: [{ kind: 'added', text: `TextField value=prefix${placeholder}suffix` }],
113+
lines: [{ kind: 'added', text: placeholder }],
114114
},
115115
hint: `Observed ${placeholder}4`,
116116
[`echo${placeholder}key`]: true,
@@ -127,6 +127,62 @@ test('parameterized fill scrubs backend and nested settle echoes at the response
127127
expect(JSON.stringify(recordedResult)).not.toContain(secret);
128128
});
129129

130+
test('a sensitive fill replaces the text its --settle observation read back and keeps refs and counts', () => {
131+
const sessionStore = makeSessionStore();
132+
const session = makeAuthoringSession('sensitive-settle');
133+
sessionStore.publish(session.name, session);
134+
const payload = {
135+
text: '000629177',
136+
settle: {
137+
settled: true,
138+
waitedMs: 1,
139+
captures: 2,
140+
quietMs: 1,
141+
timeoutMs: 100,
142+
diff: {
143+
summary: { additions: 1, removals: 1, unchanged: 3 },
144+
lines: [
145+
{ kind: 'removed', text: 'TextField value=000 62' },
146+
{ kind: 'added', text: 'TextField value=00 062 91 77', ref: 'e4' },
147+
],
148+
},
149+
tail: [{ ref: 'e5', role: 'button', label: 'Code 00 062 91 77' }],
150+
},
151+
};
152+
153+
const response = finalizeTouchInteraction({
154+
ref: sessionStore.lookup(session.name)!,
155+
sessionStore,
156+
command: 'fill',
157+
positionals: ['id="code"', '000629177'],
158+
flags: { recordAs: 'CODE' },
159+
result: payload,
160+
responseData: payload,
161+
actionStartedAt: 1,
162+
actionFinishedAt: 2,
163+
});
164+
165+
expect(response.ok).toBe(true);
166+
if (!response.ok) return;
167+
const settled = {
168+
settle: {
169+
settled: true,
170+
captures: 2,
171+
diff: {
172+
summary: { additions: 1, removals: 1, unchanged: 3 },
173+
lines: [
174+
{ kind: 'removed', text: '${CODE}' },
175+
{ kind: 'added', text: '${CODE}', ref: 'e4' },
176+
],
177+
},
178+
tail: [{ ref: 'e5', role: 'button', label: '${CODE}' }],
179+
},
180+
};
181+
expect(response.data).toMatchObject(settled);
182+
expect(session.actions[0]?.result).toMatchObject(settled);
183+
expect(JSON.stringify([response.data, session.actions])).not.toMatch(/062 91|000 62/);
184+
});
185+
130186
test('parameterized fill scrubs concatenated backend values and object keys through the handler route', async () => {
131187
const secret = 'TOKEN123';
132188
const placeholder = '${PASSWORD}';

‎packages/daemon/src/execution/interaction/internal/interaction-common.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
sensitiveFillPlaceholder,
1515
} from '@agent-device/ad-script';
1616
import { parameterizeRecordedFillPayload } from '@agent-device/selectors/parameterized-recorded-fill';
17-
import { redactUnconfirmedFillReadBack } from '../../../core/fill-read-back-redaction.ts';
17+
import { redactFillReadBack } from '../../../core/fill-read-back-redaction.ts';
1818
import type { InteractionFinalizationOperations } from '../../../core/interaction/internal/types.ts';
1919

2020
export function finalizeTouchInteraction(params: {
@@ -126,9 +126,6 @@ function parameterizeFillPayloads(params: {
126126
});
127127
const placeholder = sensitiveFillPlaceholder(params.flags);
128128
const redact = (payload: Record<string, unknown>) =>
129-
redactUnconfirmedFillReadBack(
130-
parameterizeRecordedFillPayload(payload, literal, placeholder),
131-
placeholder,
132-
);
129+
parameterizeRecordedFillPayload(redactFillReadBack(payload, placeholder), literal, placeholder);
133130
return [redact(params.result), redact(params.responseData)];
134131
}

‎packages/daemon/src/server/__tests__/request-router-fill-text-stdin.test.ts‎

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ test('a whitespace-only --text-stdin value collapses the error text it appears i
321321
const FILL_TARGET = {
322322
resourceId: 'otp',
323323
className: 'android.widget.EditText',
324-
packageName: 'com.example',
324+
packageName: 'com.test',
325325
rect: { x: 0, y: 0, width: 100, height: 40 },
326326
};
327327

@@ -379,30 +379,100 @@ test('an unrecorded fill that settles on a formatted value still returns its rea
379379
if (result.ok) expect(result.data).toMatchObject({ after: '00 062 91 77' });
380380
});
381381

382-
test('a --text-stdin fill failure that reports an altered read-back returns no part of it', async () => {
383-
const { response } = fillWithStdinText(
384-
makeSession('web', { device: WEB_DESKTOP_DEVICE }),
385-
{},
386-
(text) =>
382+
const ALTERED_READ_BACK_FAILURE = (text: string) =>
383+
new AppError('COMMAND_FAILED', 'Android fill verification failed', {
384+
failureReason: 'text_mismatch',
385+
expected: text,
386+
actual: '00 062 91 77',
387+
targetInput: { text: '000 62', className: 'android.widget.EditText' },
388+
actualInput: { text: '00 062 91 77', className: 'android.widget.EditText' },
389+
});
390+
391+
test.each([
392+
['an unrecorded', {}, '[REDACTED]', makeSession],
393+
['a --record-as', { recordAs: 'CODE' }, '${CODE}', makeAuthoringSession],
394+
])(
395+
'%s --text-stdin fill failure that reports an altered read-back returns no part of it',
396+
async (_name, flags, placeholder, sessionFor) => {
397+
const { response } = fillWithStdinText(
398+
sessionFor('web', { device: WEB_DESKTOP_DEVICE }),
399+
flags,
400+
ALTERED_READ_BACK_FAILURE,
401+
'000629177',
402+
);
403+
404+
const result = await response;
405+
expect(result.ok).toBe(false);
406+
if (result.ok) return;
407+
expect(result.error.details).toMatchObject({
408+
failureReason: 'text_mismatch',
409+
expected: placeholder,
410+
actual: placeholder,
411+
targetInput: { text: placeholder, className: 'android.widget.EditText' },
412+
actualInput: { text: placeholder, className: 'android.widget.EditText' },
413+
});
414+
expect(JSON.stringify(result)).not.toContain('062 91');
415+
},
416+
);
417+
418+
test('an empty --record-as fill that fails to clear returns no part of the residual field text', async () => {
419+
const { response } = sendToWebSession(
420+
makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }),
421+
{ command: 'fill', positionals: ['10', '20', ''], flags: { recordAs: 'CODE' } },
422+
() =>
387423
new AppError('COMMAND_FAILED', 'Android fill verification failed', {
388424
failureReason: 'text_mismatch',
389-
expected: text,
390-
actual: '00 062 91 77',
391-
targetInput: { text: '000 62', className: 'android.widget.EditText' },
392-
actualInput: { text: '00 062 91 77', className: 'android.widget.EditText' },
425+
expected: '',
426+
actual: 'residual-4471',
427+
targetInput: { text: 'residual-4471', className: 'android.widget.EditText' },
428+
actualInput: { text: 'residual-4471', className: 'android.widget.EditText' },
393429
}),
394-
'000629177',
395430
);
396431

397432
const result = await response;
398433
expect(result.ok).toBe(false);
399434
if (result.ok) return;
400435
expect(result.error.details).toMatchObject({
401-
failureReason: 'text_mismatch',
402-
expected: '[REDACTED]',
403-
actual: '[REDACTED]',
404-
targetInput: { text: '[REDACTED]', className: 'android.widget.EditText' },
405-
actualInput: { text: '[REDACTED]', className: 'android.widget.EditText' },
436+
actual: '${CODE}',
437+
targetInput: { text: '${CODE}' },
438+
actualInput: { text: '${CODE}' },
406439
});
407-
expect(JSON.stringify(result)).not.toContain('062 91');
440+
expect(JSON.stringify(result)).not.toContain('residual-4471');
408441
});
442+
443+
test.each([
444+
['a one-letter value', 'a'],
445+
['a value that is the verification discriminator', 'unconfirmed'],
446+
['a value that is a response key', 'target'],
447+
])(
448+
'a --record-as fill of %s keeps the unconfirmed structure and redacts its read-back',
449+
async (_name, text) => {
450+
const { response, sessionStore } = fillWithStdinText(
451+
makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }),
452+
{ recordAs: 'CODE' },
453+
undefined,
454+
text,
455+
{
456+
verification: 'unconfirmed',
457+
requested: text,
458+
before: 'zq7',
459+
after: 'Q7z',
460+
target: FILL_TARGET,
461+
},
462+
);
463+
464+
const result = await response;
465+
expect(result.ok).toBe(true);
466+
if (!result.ok) return;
467+
const unconfirmed = {
468+
verification: 'unconfirmed',
469+
requested: '${CODE}',
470+
before: '${CODE}',
471+
after: '${CODE}',
472+
target: FILL_TARGET,
473+
};
474+
expect(result.data).toMatchObject(unconfirmed);
475+
expect(sessionStore.get('web')?.actions[0]?.result).toMatchObject(unconfirmed);
476+
expect(JSON.stringify(sessionStore.get('web')?.actions)).not.toMatch(/zq7|Q7z/);
477+
},
478+
);

‎packages/daemon/src/server/request-router.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ const ERROR_TEXT_FIELDS: ReadonlySet<string> = new Set(['message', 'hint', 'caus
598598
*/
599599
function redactSensitiveFillTextInError(req: DaemonRequest, error: DaemonError): DaemonError {
600600
const text = sensitiveFillText(req);
601-
if (!text) return error;
601+
if (text === undefined) return error;
602602
const placeholder = sensitiveFillPlaceholder(req.flags);
603603
const replace = (value: string) => parameterizeSensitiveString(value, text, placeholder);
604604
return Object.fromEntries(

‎packages/selectors/src/parameterized-recorded-fill.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { selectorContainsValue } from './index.ts';
33

44
const STRUCTURAL_ROOT_OUTPUT_KEYS = new Set([
55
'action',
6+
'after',
67
'backend',
8+
'before',
79
'cost',
810
'delayMs',
911
'evidence',
@@ -19,13 +21,16 @@ const STRUCTURAL_ROOT_OUTPUT_KEYS = new Set([
1921
'referenceHeight',
2022
'referenceWidth',
2123
'refLabel',
24+
'requested',
2225
'resolution',
2326
'selector',
2427
'selectorChain',
2528
'settle',
29+
'target',
2630
'targetHittable',
2731
'targetKind',
2832
'text',
33+
'verification',
2934
'warning',
3035
'x',
3136
'y',
@@ -39,8 +44,9 @@ const STABLE_ROOT_OUTPUT_STRING_KEYS = new Set([
3944
'ref',
4045
'selector',
4146
'targetKind',
47+
'verification',
4248
]);
43-
const STRUCTURED_OUTPUT_KEYS = new Set(['cost', 'evidence', 'resolution', 'settle']);
49+
const STRUCTURED_OUTPUT_KEYS = new Set(['cost', 'evidence', 'resolution', 'settle', 'target']);
4450
const STRUCTURAL_KEYS_BY_PATH = new Map<string, ReadonlySet<string>>([
4551
['cost', new Set(['nodeCount', 'runnerRoundTrips', 'wallClockMs'])],
4652
[
@@ -82,6 +88,8 @@ const STRUCTURAL_KEYS_BY_PATH = new Map<string, ReadonlySet<string>>([
8288
['settle.diff.summary', new Set(['additions', 'removals', 'unchanged'])],
8389
['settle.refs', new Set(['ref'])],
8490
['settle.tail', new Set(['label', 'ref', 'role'])],
91+
['target', new Set(['className', 'packageName', 'rect', 'resourceId'])],
92+
['target.rect', new Set(['height', 'width', 'x', 'y'])],
8593
]);
8694
const STABLE_STRUCTURAL_STRING_PATHS = new Set([
8795
'evidence.digest',
@@ -99,6 +107,7 @@ const STABLE_STRUCTURAL_STRING_PATHS = new Set([
99107
'settle.refs.ref',
100108
'settle.tail.ref',
101109
'settle.tail.role',
110+
'target.className',
102111
]);
103112

104113
/**

0 commit comments

Comments
 (0)