Skip to content

Commit 3536e00

Browse files
committed
fix(overlay): mask a secret after a compact chain of key names
In 'Error: context:password: "x"' the whole chain 'context:password:' was read as the value of 'Error', and only a single key was read again, so the password showed. A value that is a chain of key names is now read again from its last key, which skips the chain in one step and keeps the scan linear.
1 parent ced39c0 commit 3536e00

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎packages/devtools/src/__tests__/redaction-leak-fixes.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ describe('a value read again as the next key', () => {
6666
const out = redactText(`${'a: '.repeat(50)}password: "hunter2"`, [], 'text');
6767
expect(out).not.toContain('hunter2');
6868
});
69+
70+
it.each([
71+
'Error: context:password: "hunter2"',
72+
'Error: context:password: hunter2',
73+
'Error: a:b:c:token: hunter2',
74+
`Error: ${'a:'.repeat(5000)}password: hunter2`,
75+
])('finds a secret after a compact chain of key names: %s', (text) => {
76+
expect(redactText(text, [], 'text')).not.toContain('hunter2');
77+
});
6978
});

‎packages/devtools/src/text-redact.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ type PairReplacer = (
168168
*/
169169
/** A pair value that is one key name and its `:` or `=`, read again as the next pair. */
170170
const NEXT_KEY_VALUE = /^[\w$%.[\]-]+[:=]$/;
171+
/** A pair value that is a chain of key names, each with its `:` (`context:password:`). */
172+
const KEY_CHAIN_VALUE = /^(?:[\w$%.[\]-]+:)+$/;
171173

172174
function replacePairs(text: string, pair: PairReplacer): string {
173175
const re = new RegExp(TEXT_PAIR.source, TEXT_PAIR.flags);
@@ -191,10 +193,13 @@ function replacePairs(text: string, pair: PairReplacer): string {
191193
// A value that is itself the next key (`Error: password: "x"` reads `password:` as the
192194
// value of `Error`) is read again from its start, so that pair is found too. A `key=`
193195
// value counts only after `: ` with a space, so `xmlns:auth="urn:a"` stays one name.
194-
// Only a single key is read again, never a chain like `a:a:a:`, so the scan stays linear.
195-
const nextKey = NEXT_KEY_VALUE.test(value) && (value.endsWith(':') || /\s/.test(eq));
196+
// For a chain such as `context:password:` only its last key is read again, so a chain
197+
// like `a:a:a:` is skipped in one step and the scan stays linear.
198+
const chain = !NEXT_KEY_VALUE.test(value) && KEY_CHAIN_VALUE.test(value);
199+
const nextKey = chain || (NEXT_KEY_VALUE.test(value) && (value.endsWith(':') || /\s/.test(eq)));
196200
if (replaced === match && value && !/^["']/.test(value) && nextKey) {
197-
const valueAt = at + match.length - value.length;
201+
const lastKey = chain ? value.lastIndexOf(':', value.length - 2) + 1 : 0;
202+
const valueAt = at + match.length - value.length + lastKey;
198203
out += text.slice(last, valueAt);
199204
last = valueAt;
200205
re.lastIndex = valueAt;

0 commit comments

Comments
 (0)