Skip to content

Commit 14eafc4

Browse files
committed
fix(fill): redact a replayed fill's error and accept textStdin false in batch
- A same-session replay runs its fill through the replay-scoped invoker, which registers the sensitive value but returned the fill's error as is. It now redacts that error like handleRequest does, so the replay message carries the fill's placeholder. - The daemon batch validator refuses only textStdin: true, matching the CLI guard; false is the ordinary argv path. - The error path uses the success path's parameterizeSensitiveString for values and keys, so a whitespace-only value collapses the string. - Docs: limit the redaction guarantee to value-bearing fields.
1 parent a16d9ff commit 14eafc4

6 files changed

Lines changed: 84 additions & 9 deletions

File tree

‎packages/command-registry/src/batch.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,14 @@ export function validateAndNormalizeBatchSteps(
239239

240240
/**
241241
* A batch step has no stdin of its own, and a failed step reports its positionals. The CLI refuses
242-
* `textStdin` on a step before reading stdin; this refuses it for every other daemon client.
242+
* `textStdin: true` on a step before reading stdin; this refuses it for every other daemon client.
243+
* `false` is the ordinary argv path, and a value of another type is refused per step by the router.
243244
*/
244245
function assertNoTextStdinStep(
245246
flags: Record<string, unknown> | undefined,
246247
stepNumber: number,
247248
): void {
248-
if (flags?.textStdin === undefined) return;
249+
if (flags?.textStdin !== true) return;
249250
throw new AppError(
250251
'INVALID_ARGS',
251252
`Batch step ${stepNumber}: fill textStdin is not supported because batch steps have no stdin of their own.`,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ function filterSensitiveSelectorCandidates(
362362
return value.filter((candidate) => !carries(candidate));
363363
}
364364

365-
function parameterizeSensitiveString(value: string, literal: string, placeholder: string): string {
365+
export function parameterizeSensitiveString(
366+
value: string,
367+
literal: string,
368+
placeholder: string,
369+
): string {
366370
// The empty literal (`fill <target> "" --record-as VAR`, #2063) matches inside every string:
367371
// it reveals nothing if echoed, so it redacts nothing. Only the fill's own semantic `text`
368372
// field is force-parameterized, by `parameterizeRecordedFillPayload`, not here.

‎src/commands/batch/cli.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,5 +457,6 @@ test('batch passes an explicit fill textStdin false through to dispatch', async
457457
]);
458458

459459
assert.equal(result.calls.length, 1);
460+
assert.equal((result.calls[0]?.flags?.batchSteps ?? [])[0]?.flags?.textStdin, false);
460461
assert.doesNotMatch(result.stderr, /textStdin is not supported/);
461462
});

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* #3260: `fill --text-stdin` keeps the value out of argv, so it must not come back on the
3-
* response either. Sent through the real request handler to a web session, the narrowest
4-
* runtime that still runs the full fill admission, dispatch and response projection.
3+
* response either, and neither may a replayed `--record-as` value. Sent through the real request
4+
* handler to a web session, the narrowest runtime that still runs the full fill admission,
5+
* dispatch and response projection.
56
*/
67
import { expect, test } from 'vitest';
78
import fs from 'node:fs';
@@ -16,6 +17,7 @@ import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
1617
import { makeAuthoringSession, makeSession } from '../../__tests__/test-utils/session-factories.ts';
1718
import { WEB_DESKTOP_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts';
1819
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
20+
import { replayScriptSourceBundleFor } from '../../__tests__/test-utils/replay-script-source.ts';
1921
import {
2022
createPlatformRuntimeGateway,
2123
createRequestPlatformProviders,
@@ -206,3 +208,58 @@ test('a batch step carrying --text-stdin is refused before typing and returns no
206208
expect(typed).toEqual([]);
207209
expect(JSON.stringify(result)).not.toContain(SECRET);
208210
});
211+
212+
test('a replayed --record-as fill whose backend error echoes the value does not return it', async () => {
213+
const flowPath = path.join(
214+
mkdtempForTestSync('agent-device-router-replay-record-as-'),
215+
'flow.ad',
216+
);
217+
fs.writeFileSync(flowPath, 'fill 10 20 "${PASSWORD}"\n');
218+
const { response, typed } = sendToWebSession(
219+
makeSession('web', { device: WEB_DESKTOP_DEVICE }),
220+
{
221+
command: 'replay',
222+
positionals: [flowPath],
223+
flags: {
224+
replayEnv: [`PASSWORD=${SECRET}`],
225+
replayScriptSource: replayScriptSourceBundleFor(flowPath),
226+
},
227+
},
228+
(text) => new Error(`could not type "${text}" into the field`),
229+
);
230+
231+
const result = await response;
232+
expect(result.ok).toBe(false);
233+
expect(typed).toEqual([SECRET]);
234+
expect(JSON.stringify(result)).not.toContain(SECRET);
235+
if (!result.ok) expect(result.error.message).toContain('could not type "${PASSWORD}"');
236+
});
237+
238+
test('a batch fill step with textStdin false is dispatched like any fill', async () => {
239+
const { response, typed } = sendToWebSession(makeSession('web', { device: WEB_DESKTOP_DEVICE }), {
240+
command: 'batch',
241+
positionals: [],
242+
flags: {
243+
batchSteps: [
244+
{ command: 'fill', positionals: ['10', '20', 'plain'], flags: { textStdin: false } },
245+
],
246+
},
247+
});
248+
249+
const result = await response;
250+
expect(result.ok).toBe(true);
251+
expect(typed).toEqual(['plain']);
252+
});
253+
254+
test('a whitespace-only --text-stdin value collapses the error text it appears in', async () => {
255+
const { response } = fillWithStdinText(
256+
makeSession('web', { device: WEB_DESKTOP_DEVICE }),
257+
{},
258+
(text) => new Error(`could not type "${text}" into the field`),
259+
' ',
260+
);
261+
262+
const result = await response;
263+
expect(result.ok).toBe(false);
264+
if (!result.ok) expect(result.error.message).toBe('[REDACTED]');
265+
});

‎src/daemon/request-router.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
sensitiveFillPlaceholder,
6262
} from '@agent-device/ad-script';
6363
import { createPlatformRequestScope } from './platform-request-scope.ts';
64+
import { parameterizeSensitiveString } from '@agent-device/selectors/parameterized-recorded-fill';
6465
import { createOwnerScopedDeviceClaimReconciler } from './device/device-claim-owner-recovery.ts';
6566
import { isConfinedToAppLease, scopeRequestSession } from './request-admission.ts';
6667
import { redactMacOsAppLeaseResponse } from './macos-app-lease.ts';
@@ -426,7 +427,11 @@ export function createRequestHandler(deps: RequestRouterDeps): DaemonInvokeFn {
426427
} catch (error) {
427428
response = finalizeThrownRequestError(error);
428429
}
429-
return await finalizeRequestBindingCleanup(childScope, response);
430+
// This entry point registers a sensitive fill too, so it redacts that fill's error as well.
431+
return redactSensitiveFillResponse(
432+
req,
433+
await finalizeRequestBindingCleanup(childScope, response),
434+
);
430435
};
431436
}
432437

@@ -544,7 +549,8 @@ const ERROR_TEXT_FIELDS: ReadonlySet<string> = new Set(['message', 'hint', 'caus
544549
function redactSensitiveFillTextInError(req: DaemonRequest, error: DaemonError): DaemonError {
545550
const text = sensitiveFillText(req);
546551
if (!text) return error;
547-
const replace = (value: string) => value.replaceAll(text, sensitiveFillPlaceholder(req.flags));
552+
const placeholder = sensitiveFillPlaceholder(req.flags);
553+
const replace = (value: string) => parameterizeSensitiveString(value, text, placeholder);
548554
return Object.fromEntries(
549555
Object.entries(error).map(([field, value]) => [
550556
field,
@@ -558,10 +564,16 @@ function mapErrorStrings(value: unknown, replace: (value: string) => string): un
558564
if (Array.isArray(value)) return value.map((entry) => mapErrorStrings(entry, replace));
559565
if (!value || typeof value !== 'object') return value;
560566
return Object.fromEntries(
561-
Object.entries(value).map(([key, entry]) => [key, mapErrorStrings(entry, replace)]),
567+
Object.entries(value).map(([key, entry]) => [replace(key), mapErrorStrings(entry, replace)]),
562568
);
563569
}
564570

571+
function redactSensitiveFillResponse(req: DaemonRequest, response: DaemonResponse): DaemonResponse {
572+
return response.ok
573+
? response
574+
: { ...response, error: redactSensitiveFillTextInError(req, response.error) };
575+
}
576+
565577
async function dispatchGenericForLockedScope(params: {
566578
lockedScope: LockedRequestScope;
567579
logPath: string;

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ agent-device gesture transform 200 420 80 -40 2 35 700 # combined pan, zoom, and
465465
```
466466
467467
`fill` clears then types. `type` does not clear.
468-
`fill --text-stdin` reads the text from stdin instead of a text argument, so a secret never appears in the CLI's process arguments. Pipe the value with `printf %s`, which sends it verbatim. It reads at most 64 KiB, removes exactly one trailing newline (`\n` or `\r\n`), and refuses a terminal, empty input, invalid UTF-8, or an additional text argument. The value is registered as sensitive for diagnostics, and command output, error messages included, shows it as `${VAR}` with `--record-as` and as `[REDACTED]` otherwise. While script recording is armed, combine it with `--record-as <VAR>` to publish `${VAR}`, or `--no-record` to leave the step out; passing neither is an error. Without an armed recording the step is not recorded. Batch steps do not accept it.
468+
`fill --text-stdin` reads the text from stdin instead of a text argument, so a secret never appears in the CLI's process arguments. Pipe the value with `printf %s`, which sends it verbatim. It reads at most 64 KiB, removes exactly one trailing newline (`\n` or `\r\n`), and refuses a terminal, empty input, invalid UTF-8, or an additional text argument. The value is registered as sensitive for diagnostics, and command output shows it as `${VAR}` with `--record-as` and as `[REDACTED]` otherwise, in success data and in an error's message, hint, cause and details; daemon-owned fields such as `logPath` and `diagnosticId` are left unchanged. While script recording is armed, combine it with `--record-as <VAR>` to publish `${VAR}`, or `--no-record` to leave the step out; passing neither is an error. Without an armed recording the step is not recorded. Batch steps do not accept it.
469469
A successful `click` or `press` means the action was sent to the target where the command found it, whether that is a tap, a mouse click, or an accessibility action. It does not confirm that the action reached that element or changed the screen. When your next step depends on it, add `--verify` to learn whether the screen changed (`changedFromBefore`), add `--settle` to get the diff once the UI goes quiet, or follow with `wait <selector>` for the screen you expect.
470470
When an interaction fails, see [Retry after a failed command](#retry-after-a-failed-command) before you retry.
471471
`type` accepts text only. Do not pass `@ref` to `type`; use `fill @ref "text"` to target a field directly, or `press @ref` then `type "text"` to append in the focused field.

0 commit comments

Comments
 (0)