Skip to content

Commit 6cdfbc7

Browse files
committed
fix(fill): keep a stdin value that contains its placeholder out of responses
parameterizeSensitiveString split its input on the placeholder before looking for the value, so a value that contains the placeholder (`abc[REDACTED]xyz`, or `${VAR}` inside a `--record-as VAR` value) was never found and came back whole in a backend error. The helper now collapses the whole string to the placeholder whenever the value still occurs outside a placeholder. A value found only inside placeholders (`$` in `${DOLLAR}`) stays as before.
1 parent 14eafc4 commit 6cdfbc7

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,54 @@ export function parameterizeSensitiveString(
373373
if (!literal) return value;
374374

375375
const unparameterizedSegments = placeholder ? value.split(placeholder) : [value];
376-
if (unparameterizedSegments.every((segment) => !segment.includes(literal))) return value;
376+
if (unparameterizedSegments.every((segment) => !segment.includes(literal))) {
377+
return collapseIfLiteralRemains(value, literal, placeholder);
378+
}
377379

378380
// Replacing whitespace inline would make every ordinary separator look like
379381
// authored secret data. Collapse the whole untrusted string/key instead.
380382
if (!literal.trim()) return placeholder;
381383

382-
return unparameterizedSegments
383-
.map((segment) => segment.replaceAll(literal, placeholder))
384-
.join(placeholder);
384+
return collapseIfLiteralRemains(
385+
unparameterizedSegments
386+
.map((segment) => segment.replaceAll(literal, placeholder))
387+
.join(placeholder),
388+
literal,
389+
placeholder,
390+
);
391+
}
392+
393+
/**
394+
* The output never carries the literal outside a placeholder. A literal that contains the
395+
* placeholder (`abc[REDACTED]xyz`, or `${VAR}` inside a `--record-as VAR` value) or crosses one
396+
* spans the segments split on it, so the segment-wise replace above leaves it whole; the whole
397+
* string collapses to the placeholder instead. A literal found only inside placeholders (`$` in
398+
* `${DOLLAR}`) reveals nothing and stays.
399+
*/
400+
function collapseIfLiteralRemains(value: string, literal: string, placeholder: string): string {
401+
const placeholderStarts: number[] = [];
402+
if (placeholder) {
403+
for (
404+
let at = value.indexOf(placeholder);
405+
at !== -1;
406+
at = value.indexOf(placeholder, at + placeholder.length)
407+
) {
408+
placeholderStarts.push(at);
409+
}
410+
}
411+
// Placeholders do not overlap, so the first one ending at or after an occurrence of the
412+
// literal is the only one that can cover it, and that index only moves forward.
413+
let next = 0;
414+
for (let at = value.indexOf(literal); at !== -1; at = value.indexOf(literal, at + 1)) {
415+
while (
416+
next < placeholderStarts.length &&
417+
placeholderStarts[next]! + placeholder.length < at + literal.length
418+
) {
419+
next += 1;
420+
}
421+
if (next === placeholderStarts.length || placeholderStarts[next]! > at) return placeholder;
422+
}
423+
return value;
385424
}
386425

387426
/**

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,44 @@ test('a backend error that echoes a --record-as stdin value shows its placeholde
171171
if (!result.ok) expect(result.error.message).toBe('could not type "${PASSWORD}" into the field');
172172
});
173173

174+
test.each([
175+
['an unrecorded', 'abc[REDACTED]xyz', {}, '[REDACTED]', makeSession],
176+
[
177+
'a --record-as',
178+
'abc${PASSWORD}xyz',
179+
{ recordAs: 'PASSWORD' },
180+
'${PASSWORD}',
181+
makeAuthoringSession,
182+
],
183+
])(
184+
'%s --text-stdin value that contains its own placeholder is returned by neither a success nor an error',
185+
async (_name, secret, flags, placeholder, sessionFor) => {
186+
const filled = fillWithStdinText(
187+
sessionFor('web', { device: WEB_DESKTOP_DEVICE }),
188+
flags,
189+
undefined,
190+
secret,
191+
);
192+
const failed = fillWithStdinText(
193+
sessionFor('web', { device: WEB_DESKTOP_DEVICE }),
194+
flags,
195+
(text) => new Error(`could not type "${text}" into the field`),
196+
secret,
197+
);
198+
199+
const succeeded = await filled.response;
200+
const errored = await failed.response;
201+
expect(filled.typed).toEqual([secret]);
202+
expect(failed.typed).toEqual([secret]);
203+
expect(succeeded.ok).toBe(true);
204+
expect(JSON.stringify(succeeded)).not.toContain(secret);
205+
expect(JSON.stringify(filled.sessionStore.get('web')?.actions ?? [])).not.toContain(secret);
206+
expect(errored.ok).toBe(false);
207+
expect(JSON.stringify(errored)).not.toContain(secret);
208+
if (!errored.ok) expect(errored.error.message).toBe(placeholder);
209+
},
210+
);
211+
174212
test('a short --text-stdin value is redacted from the error text but not from its log path', async () => {
175213
// `stdin` also appears in this request's id, which names its diagnostics file.
176214
const { response } = fillWithStdinText(

0 commit comments

Comments
 (0)