Skip to content

Commit 19d3ffd

Browse files
committed
fix(fill): never return --text-stdin text on any response surface
A fill whose text is sensitive (--record-as set or --text-stdin) now decides it through one predicate, isSensitiveFillText, for the response payloads, the recorder skip and diagnostics registration. Without --record-as, the response and result text read [REDACTED] and value-carrying selector candidates are dropped, so an unrecorded or --no-record stdin fill no longer echoes the value in --json output or the client result. An error leaving the router now passes through the request's registered sensitive values, so a backend message that echoes the value after admission is redacted as well.
1 parent 0964704 commit 19d3ffd

8 files changed

Lines changed: 166 additions & 9 deletions

File tree

‎packages/ad-script/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export {
6666

6767
export {
6868
inferFillText,
69+
isSensitiveFillText,
6970
readRecordedInputVariableName,
7071
recordedInputPlaceholder,
7172
validateRecordedInputVariableName,

‎packages/ad-script/src/internal/recorded-input.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CommandFlags } from '@agent-device/contracts/command';
12
import type { SessionAction } from '@agent-device/contracts/session';
23
import { AppError } from '@agent-device/kernel/errors';
34
import { REPLAY_VAR_KEY_RE } from './script.ts';
@@ -20,6 +21,17 @@ export function validateRecordedInputVariableName(raw: string): string {
2021
return raw;
2122
}
2223

24+
/**
25+
* A fill's text is sensitive when the caller marked it: `--record-as` names it for the script, or
26+
* `--text-stdin` kept it out of argv. Sensitive text never appears as a literal on any surface:
27+
* response data, result, recorded action or diagnostics.
28+
*/
29+
export function isSensitiveFillText<TFlags extends Pick<CommandFlags, 'recordAs' | 'textStdin'>>(
30+
flags: TFlags | undefined,
31+
): flags is TFlags {
32+
return typeof flags?.recordAs === 'string' || flags?.textStdin === true;
33+
}
34+
2335
export function recordedInputPlaceholder(variableName: string): string {
2436
return `\${${variableName}}`;
2537
}

‎packages/host-kit/src/diagnostics.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
emitDiagnostic,
66
flushDiagnosticsToSessionFile,
77
getDiagnosticsMeta,
8+
redactRegisteredSensitiveValues,
89
registerDiagnosticSensitiveValue,
910
type ResourceDiagnostic,
1011
updateDiagnosticsScope,

‎packages/host-kit/src/internal/diagnostics.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ export function registerDiagnosticSensitiveValue(value: string): void {
107107
scope.sortedSensitiveValues = undefined;
108108
}
109109

110+
/**
111+
* Replace this request's registered sensitive values in `input`, as its diagnostics already do. For
112+
* an error leaving the daemon: a backend message can echo a value the caller kept out of argv.
113+
*/
114+
export function redactRegisteredSensitiveValues<T>(input: T): T {
115+
const scope = diagnosticsStorage.getStore();
116+
if (!scope || scope.sensitiveValues.size === 0) return input;
117+
return replaceSensitiveValues(input, sortedScopeSensitiveValues(scope)) as T;
118+
}
119+
110120
/**
111121
* Sum the number of diagnostic events emitted in the current scope whose phase
112122
* is one of `phases`. Backed by the flush-surviving `phaseCounts` tally, so it
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { createTestDeviceInventoryGateways } from '../../__tests__/test-utils/device-inventory-gateways.ts';
2+
/**
3+
* #3260: `fill --text-stdin` keeps the value out of argv, so it must not come back on the
4+
* response either. Sent through the real request handler to a web session, the narrowest
5+
* runtime that still runs the full fill admission, dispatch and response projection.
6+
*/
7+
import { expect, test } from 'vitest';
8+
import path from 'node:path';
9+
import type { WebProvider } from '@agent-device/platform-web';
10+
import type { SessionState } from '../session-state.ts';
11+
import { createRequestHandler } from './test-device-runtime-gateway.ts';
12+
import { LeaseRegistry } from '../lease-registry.ts';
13+
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
14+
import { makeAuthoringSession, makeSession } from '../../__tests__/test-utils/session-factories.ts';
15+
import { WEB_DESKTOP_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts';
16+
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
17+
import {
18+
createPlatformRuntimeGateway,
19+
createRequestPlatformProviders,
20+
} from '../../platform-runtime.ts';
21+
22+
const SECRET = 'stdin-s3cret-value';
23+
24+
function fillWithStdinText(
25+
session: SessionState,
26+
flags: Record<string, unknown> = {},
27+
fillFails?: (text: string) => Error,
28+
) {
29+
const sessionStore = makeSessionStore('agent-device-router-fill-text-stdin-');
30+
sessionStore.publish(session.name, session);
31+
const typed: string[] = [];
32+
const webProvider: WebProvider = {
33+
open: async () => {},
34+
close: async () => {},
35+
snapshot: async () => ({ nodes: [] }),
36+
screenshot: async () => {},
37+
setViewport: async () => {},
38+
click: async () => {},
39+
fill: async (_x, _y, text) => {
40+
typed.push(text);
41+
if (fillFails) throw fillFails(text);
42+
},
43+
typeText: async () => {},
44+
scroll: async () => {},
45+
};
46+
const dir = mkdtempForTestSync('agent-device-router-fill-text-stdin-runtime-');
47+
const handler = createRequestHandler({
48+
logPath: path.join(dir, 'daemon.log'),
49+
token: 'test-token',
50+
sessionStore,
51+
leaseRegistry: new LeaseRegistry(),
52+
deviceInventoryGateways: createTestDeviceInventoryGateways(),
53+
requestPlatformProviders: createRequestPlatformProviders({
54+
providers: { webProvider: () => webProvider },
55+
}),
56+
deviceRuntimeGateway: createPlatformRuntimeGateway({
57+
sessionsDir: dir,
58+
resolveSessionArtifacts: (sessionId) => ({
59+
outputPath: path.join(dir, sessionId, 'app.log'),
60+
pidPath: path.join(dir, sessionId, 'app-log.pid'),
61+
}),
62+
}),
63+
trackDownloadableArtifact: () => 'artifact-id',
64+
});
65+
const response = handler({
66+
token: 'test-token',
67+
session: session.name,
68+
command: 'fill',
69+
positionals: ['10', '20', SECRET],
70+
flags: { textStdin: true, ...flags },
71+
meta: { requestId: 'req-fill-text-stdin' },
72+
});
73+
return { response, typed, sessionStore };
74+
}
75+
76+
test('an unrecorded --text-stdin fill types the value but returns no part of it', async () => {
77+
const { response, typed } = fillWithStdinText(makeSession('web', { device: WEB_DESKTOP_DEVICE }));
78+
79+
const result = await response;
80+
expect(result.ok).toBe(true);
81+
expect(typed).toEqual([SECRET]);
82+
expect(JSON.stringify(result)).not.toContain(SECRET);
83+
if (result.ok) expect(result.data?.text).toBe('[REDACTED]');
84+
});
85+
86+
test('an armed --text-stdin --no-record fill returns no part of the value and records nothing', async () => {
87+
const session = makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE });
88+
const { response, typed, sessionStore } = fillWithStdinText(session, { noRecord: true });
89+
90+
const result = await response;
91+
expect(result.ok).toBe(true);
92+
expect(typed).toEqual([SECRET]);
93+
expect(JSON.stringify(result)).not.toContain(SECRET);
94+
expect(sessionStore.get('web')?.actions).toEqual([]);
95+
});
96+
97+
test('an armed --text-stdin fill without --record-as or --no-record is refused before typing', async () => {
98+
const session = makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE });
99+
const { response, typed } = fillWithStdinText(session);
100+
101+
const result = await response;
102+
expect(result.ok).toBe(false);
103+
if (result.ok) return;
104+
expect(result.error.code).toBe('INVALID_ARGS');
105+
expect(result.error.details?.reason).toBe('fill_text_stdin_unparameterized_recording');
106+
expect(typed).toEqual([]);
107+
expect(JSON.stringify(result)).not.toContain(SECRET);
108+
});
109+
110+
test('a backend error that echoes the --text-stdin value does not return it', async () => {
111+
const { response, typed } = fillWithStdinText(
112+
makeSession('web', { device: WEB_DESKTOP_DEVICE }),
113+
{},
114+
(text) => new Error(`could not type "${text}" into the field`),
115+
);
116+
117+
const result = await response;
118+
expect(result.ok).toBe(false);
119+
expect(typed).toEqual([SECRET]);
120+
expect(JSON.stringify(result)).not.toContain(SECRET);
121+
if (!result.ok) expect(result.error.message).toBe('could not type "[REDACTED]" into the field');
122+
});

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
type RecordedTargetCapture,
88
} from '@agent-device/selectors/target-evidence';
99
import type { MultiTargetAnnotationV1 } from '@agent-device/contracts/replay';
10-
import { inferFillText, recordedInputPlaceholder } from '@agent-device/ad-script';
10+
import {
11+
inferFillText,
12+
isSensitiveFillText,
13+
recordedInputPlaceholder,
14+
} from '@agent-device/ad-script';
1115
import { parameterizeRecordedFillPayload } from '@agent-device/selectors/parameterized-recorded-fill';
1216
import type { InteractionFinalizationOperations } from './types.ts';
1317

@@ -90,14 +94,17 @@ function computeMultiTargetEvidence(recordedTargets: {
9094
return source && destination ? { source, destination } : undefined;
9195
}
9296

97+
/** What sensitive fill text reads as when no `--record-as` placeholder names it. */
98+
const REDACTED_FILL_TEXT = '[REDACTED]';
99+
93100
function parameterizeFillPayloads(params: {
94101
command: string;
95102
positionals: string[];
96103
flags: CommandFlags | undefined;
97104
result: Record<string, unknown>;
98105
responseData: Record<string, unknown>;
99106
}): readonly [result: Record<string, unknown>, responseData: Record<string, unknown>] {
100-
if (params.command !== 'fill' || typeof params.flags?.recordAs !== 'string') {
107+
if (params.command !== 'fill' || !isSensitiveFillText(params.flags)) {
101108
return [params.result, params.responseData];
102109
}
103110
const literal = inferFillText({
@@ -107,7 +114,10 @@ function parameterizeFillPayloads(params: {
107114
flags: params.flags,
108115
result: params.result,
109116
});
110-
const placeholder = recordedInputPlaceholder(params.flags.recordAs);
117+
const placeholder =
118+
typeof params.flags?.recordAs === 'string'
119+
? recordedInputPlaceholder(params.flags.recordAs)
120+
: REDACTED_FILL_TEXT;
111121
return [
112122
parameterizeRecordedFillPayload(params.result, literal, placeholder),
113123
parameterizeRecordedFillPayload(params.responseData, literal, placeholder),

‎src/daemon/request-router.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
emitDiagnostic,
3838
flushDiagnosticsToSessionFile,
3939
getDiagnosticsMeta,
40+
redactRegisteredSensitiveValues,
4041
registerDiagnosticSensitiveValue,
4142
withDiagnosticsScope,
4243
} from '@agent-device/host-kit/diagnostics';
@@ -55,7 +56,7 @@ import {
5556
import { unsupportedSaveScriptFlagResponse } from './request-save-script-policy.ts';
5657
import { canRunReplayScopedAction } from './daemon-command-registry.ts';
5758
import { isWebSession } from './web-session-names.ts';
58-
import { inferFillText } from '@agent-device/ad-script';
59+
import { inferFillText, isSensitiveFillText } from '@agent-device/ad-script';
5960
import { createPlatformRequestScope } from './platform-request-scope.ts';
6061
import { createOwnerScopedDeviceClaimReconciler } from './device/device-claim-owner-recovery.ts';
6162
import { isConfinedToAppLease, scopeRequestSession } from './request-admission.ts';
@@ -207,7 +208,7 @@ export function createRequestHandler(deps: RequestRouterDeps): DaemonInvokeFn {
207208
repairExpiredIfTombstoned(req, response.error, sessionStore),
208209
sessionStore,
209210
);
210-
return { ok: false, error: enrichDaemonError(error) };
211+
return { ok: false, error: redactRegisteredSensitiveValues(enrichDaemonError(error)) };
211212
}
212213
// Phase 4 (agent-cost) grafts on the success path. Runs inside the
213214
// diagnostics scope so cost can read this request's runner-round-trip tally.
@@ -498,8 +499,7 @@ function customActionFlagsResponse(req: DaemonRequest): DaemonResponse | undefin
498499
}
499500

500501
function registerParameterizedFillDiagnosticValue(req: DaemonRequest): void {
501-
if (req.command !== 'fill') return;
502-
if (typeof req.flags?.recordAs !== 'string' && req.flags?.textStdin !== true) return;
502+
if (req.command !== 'fill' || !isSensitiveFillText(req.flags)) return;
503503
registerDiagnosticSensitiveValue(
504504
inferFillText({
505505
ts: 0,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { repairSessionBoundary } from './session-replay-transaction.ts';
99
import type { MultiTargetAnnotationV1, TargetAnnotationV1 } from '@agent-device/contracts/replay';
1010
import {
1111
inferFillText,
12+
isSensitiveFillText,
1213
recordedInputPlaceholder,
1314
validateRecordedInputVariableName,
1415
} from '@agent-device/ad-script';
@@ -59,8 +60,8 @@ export function recordActionEntry(
5960
entry: RecordActionEntry,
6061
): SessionAction | undefined {
6162
if (entry.flags?.noRecord) return undefined;
62-
// Stdin text stays out of session actions unless `--record-as` parameterizes it.
63-
if (entry.flags?.textStdin === true && entry.flags.recordAs === undefined) return undefined;
63+
// Sensitive fill text stays out of session actions unless `--record-as` parameterizes it.
64+
if (isSensitiveFillText(entry.flags) && entry.flags.recordAs === undefined) return undefined;
6465
if (isExcludedRepairSegmentObservation(session, entry)) return undefined;
6566
if (entry.flags) applyRecordedSaveScriptFlags(session, entry.flags);
6667
const fillLiteral = readRecordedFillLiteral(entry);

0 commit comments

Comments
 (0)