Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): close the redaction and containment holes left open in the debug hook (#5929)

Joey48cb3a4ec23b
diff --git a/packages/cli/authoring/config/config.doc.mjs b/packages/cli/authoring/config/config.doc.mjs
index 0cb5be9f25..fc8a308ebf 100644
--- a/packages/cli/authoring/config/config.doc.mjs
+++ b/packages/cli/authoring/config/config.doc.mjs
@@ -48,8 +48,9 @@ export const doc = {
name: 'debug',
type: '(event: DebugEvent) => void',
description:
- 'Record every astryx command run in this project and hand each one to this function. Setting it is the whole opt-in; leaving it out records nothing. It runs synchronously at process exit, so a returned promise is never awaited — use it for synchronous work only. Write the key literally in the config file: so that a project which has not opted in never pays to evaluate its config, the CLI only loads the config before parsing when the file itself contains the word "debug", which a handler spread in from another module would not.',
- example: "event => appendFileSync('runs.ndjson', JSON.stringify(event) + '\\n')",
+ 'Record every command run. The handler is synchronous; promises are not awaited and output goes to stderr. Declare `debug` directly in this file so early commands can discover it.',
+ example:
+ "event => appendFileSync('runs.ndjson', JSON.stringify(event) + '\\n')",
},
{
name: 'experimental',
diff --git a/packages/cli/authoring/debug/type.ts b/packages/cli/authoring/debug/type.ts
index f7bd68e208..f3c9735976 100644
--- a/packages/cli/authoring/debug/type.ts
+++ b/packages/cli/authoring/debug/type.ts
@@ -189,10 +189,7 @@ export interface DebugEvent {
* reaches the log. Anything the handler throws is swallowed; recording must
* never fail a command.
*
- * Two things a handler still CAN do to its own command, because they happen
- * after the CLI has finished and outside anything this module controls:
- * calling `process.exit` from in here replaces the command's exit code, and
- * writing to stdout appends to the command's own output — which will break a
- * `--json` consumer parsing that stream. Don't do either.
+ * `process.exit` is ignored and synchronous stdout writes go to stderr.
+ * Saved writers, deferred writes, and inherited child stdio are not contained.
*/
export type DebugEventHandler = (event: DebugEvent) => void;
diff --git a/packages/cli/clients/cli/index.mjs b/packages/cli/clients/cli/index.mjs
index 1d99931e2e..6c2ac79130 100644
--- a/packages/cli/clients/cli/index.mjs
+++ b/packages/cli/clients/cli/index.mjs
@@ -136,7 +136,10 @@ export async function loadProjectDebugHandler() {
);
const configPath = findConfigPath(process.cwd());
if (!configPath) return;
- if (!fs.readFileSync(configPath, 'utf-8').includes('debug')) return;
+ if (!fs.readFileSync(configPath, 'utf-8').includes('debug')) {
+ debug.noteConfigGateSkipped();
+ return;
+ }
await Project.load(process.cwd());
} catch {
// A broken config is the command's problem to report, not ours.
diff --git a/packages/cli/foundation/debug/index.mjs b/packages/cli/foundation/debug/index.mjs
index 3da8e13b0e..db5c9dde2c 100644
--- a/packages/cli/foundation/debug/index.mjs
+++ b/packages/cli/foundation/debug/index.mjs
@@ -33,6 +33,7 @@ export {
setGlobalOptions,
setProject,
setEventHandler,
+ noteConfigGateSkipped,
setOutcome,
recordEnvelope,
recordHelp,
@@ -47,4 +48,9 @@ export {
toEventError,
} from './event.mjs';
-export {createRedactor, isSensitiveKey, redactArgv, REDACTED} from './redact.mjs';
+export {
+ createRedactor,
+ isSensitiveKey,
+ redactArgv,
+ REDACTED,
+} from './redact.mjs';
diff --git a/packages/cli/foundation/debug/on-event.test.mjs b/packages/cli/foundation/debug/on-event.test.mjs
index 9599336bda..fd268b5119 100644
--- a/packages/cli/foundation/debug/on-event.test.mjs
+++ b/packages/cli/foundation/debug/on-event.test.mjs
@@ -25,6 +25,7 @@ import {
setGlobalOptions,
setOutcome,
setEventHandler,
+ noteConfigGateSkipped,
recordEnvelope,
recordHelp,
resetRecorder,
@@ -33,8 +34,6 @@ import {
import {parseDebugEvent} from '../../authoring/debug/parse.mjs';
import {parseConfig} from '../../authoring/config/parse.mjs';
-
-
/** Collect every event a run delivers. */
function collect() {
/** @type {any[]} */
@@ -77,8 +76,6 @@ describe('delivery', () => {
});
it('accepts a function registered after the run started', () => {
- // This is the real order: the config is read during the command, not
- // before it, so a handler always arrives mid-flight.
begin({argv: ['docs']});
setCommand('docs');
const seen = collect();
@@ -130,8 +127,6 @@ describe('what the event carries', () => {
begin({argv: ['docs']});
setCommand('docs');
finish({exitCode: 0});
- // Drift-locked to the exported type, so this is the same guarantee a
- // consumer gets from importing DebugEvent.
expect(() => parseDebugEvent(seen[0])).not.toThrow();
});
@@ -205,7 +200,6 @@ describe('scrubbing', () => {
expect(JSON.stringify(seen[0])).not.toContain('ghp_abcdef');
});
-
it('clamps an oversized value instead of dropping the run', () => {
const seen = collect();
begin({argv: []});
@@ -217,10 +211,6 @@ describe('scrubbing', () => {
});
it('scrubs the value after a sensitive flag in argv', () => {
- // argv is a flat array: `--token` and its value are separate elements, and
- // scrubbing element by element gives the value no key to match against.
- // `--flag value` is the ordinary CLI spelling, so redacting it in options
- // while printing it in argv would be the worst of both.
const seen = collect();
begin({argv: ['docs', '--token', 'hunter2SECRETVALUE']});
finish({exitCode: 0});
@@ -237,15 +227,7 @@ describe('scrubbing', () => {
});
describe('what a run with no handler costs', () => {
- // The merge condition for this feature was that nothing changes for a user
- // who has not set `debug`. These are the two ways that quietly stopped being
- // true once, so they are pinned.
-
it('does not probe the environment while collecting', () => {
- // captureEnv's first Intl.DateTimeFormat().resolvedOptions() initialises
- // ICU, and detectPackageManager and isCliOneOff touch the filesystem.
- // Doing that in begin() charged every run for something most runs never
- // deliver — ~9% of the CLI's startup.
begin({argv: ['--version']});
expect(currentEvent()?.env).toBe(null);
});
@@ -286,7 +268,6 @@ describe('a broken handler cannot break the CLI', () => {
begin({argv: []});
setCommand('docs');
finish({exitCode: 0});
- // It got a copy; the sealed record is untouched.
expect(seen[0].command).toBe('MUTATED');
});
@@ -319,9 +300,143 @@ describe('a broken handler cannot break the CLI', () => {
recordHelp();
}).not.toThrow();
});
+
+ it('cannot change the exit code by calling process.exit', () => {
+ const exits = [];
+ const realExit = process.exit;
+ process.exit = /** @type {any} */ (code => exits.push(code));
+ try {
+ setEventHandler(() => {
+ process.exit(7);
+ });
+ begin({argv: []});
+ setCommand('docs');
+ finish({exitCode: 0});
+ } finally {
+ process.exit = realExit;
+ }
+ expect(exits).toEqual([]);
+ });
+
+ it('cannot change the exit code by assigning process.exitCode', () => {
+ const before = process.exitCode;
+ setEventHandler(() => {
+ process.exitCode = 42;
+ });
+ begin({argv: []});
+ finish({exitCode: 0});
+ expect(process.exitCode).toBe(before);
+ });
+
+ it('sends what it prints to stderr, because stdout belongs to the command', () => {
+ const out = [];
+ const err = [];
+ const realOut = process.stdout.write;
+ const realErr = process.stderr.write;
+ process.stdout.write = /** @type {any} */ (
+ c => (out.push(String(c)), true)
+ );
+ process.stderr.write = /** @type {any} */ (
+ c => (err.push(String(c)), true)
+ );
+ try {
+ setEventHandler(() => {
+ process.stdout.write('handler diagnostics\n');
+ });
+ begin({argv: []});
+ finish({exitCode: 0});
+ } finally {
+ process.stdout.write = realOut;
+ process.stderr.write = realErr;
+ }
+ expect(out.join('')).not.toContain('handler diagnostics');
+ expect(err.join('')).toContain('handler diagnostics');
+ });
+
+ it('puts the real writers back after the handler returns', () => {
+ const before = {out: process.stdout.write, err: process.stderr.write};
+ setEventHandler(() => {
+ process.stdout.write('x');
+ });
+ begin({argv: []});
+ finish({exitCode: 0});
+ expect(process.stdout.write).toBe(before.out);
+ expect(process.stderr.write).toBe(before.err);
+ });
});
+describe('the config gate, when it guesses wrong', () => {
+ function withStderr(fn) {
+ const lines = [];
+ const real = process.stderr.write;
+ process.stderr.write = /** @type {any} */ (
+ c => (lines.push(String(c)), true)
+ );
+ try {
+ fn();
+ } finally {
+ process.stderr.write = real;
+ }
+ return lines.join('');
+ }
+ it('says so when a handler turns up after the gate declined', () => {
+ const said = withStderr(() => {
+ begin({argv: []});
+ noteConfigGateSkipped();
+ setEventHandler(() => {});
+ });
+ expect(said).toMatch(/astryx\.config sets `debug`/);
+ });
+
+ it('stays quiet when the gate was right', () => {
+ const said = withStderr(() => {
+ begin({argv: []});
+ noteConfigGateSkipped();
+ });
+ expect(said).toBe('');
+ });
+
+ it.each([undefined, null, 'not a function'])(
+ 'stays quiet when what arrives is %s rather than a handler',
+ value => {
+ const said = withStderr(() => {
+ begin({argv: []});
+ noteConfigGateSkipped();
+ setEventHandler(/** @type {any} */ (value));
+ });
+ expect(said).toBe('');
+ },
+ );
+
+ it('stays quiet when the gate let the config through', () => {
+ const said = withStderr(() => {
+ begin({argv: []});
+ setEventHandler(() => {});
+ });
+ expect(said).toBe('');
+ });
+
+ it('says it once, not once per lifecycle call', () => {
+ const said = withStderr(() => {
+ begin({argv: []});
+ noteConfigGateSkipped();
+ setEventHandler(() => {});
+ setEventHandler(() => {});
+ });
+ expect(said.match(/astryx\.config sets/g)).toHaveLength(1);
+ });
+
+ it('stays quiet under --json, where stderr is still not the place', () => {
+ const said = withStderr(() => {
+ begin({argv: ['--json']});
+ setGlobalOptions({json: true});
+ noteConfigGateSkipped();
+ setEventHandler(() => {});
+ });
+ expect(said).toBe('');
+ });
+});
describe('config: the whole surface is one function', () => {
it('accepts a function', () => {
@@ -345,11 +460,6 @@ describe('config: the whole surface is one function', () => {
});
describe('captured output — what the CLI answered', () => {
- // Writes go straight to the streams rather than through console.log,
- // because Vitest replaces console.* with its own collectors and they never
- // reach process.stdout — the very seam the tee sits on. In a real CLI run
- // console.log does bottom out here; the subprocess case at the end of this
- // block proves that path.
const say = (out = '', err = '') => {
if (out) process.stdout.write(`${out}\n`);
if (err) process.stderr.write(`${err}\n`);
@@ -377,7 +487,6 @@ describe('captured output — what the CLI answered', () => {
begin({argv: []});
say('12345');
finish({exitCode: 0});
- // console.log appends a newline.
expect(seen[0].output.stdoutBytes).toBe(6);
expect(seen[0].output.truncated).toBe(false);
});
@@ -404,8 +513,6 @@ describe('captured output — what the CLI answered', () => {
});
it('keeps a long answer intact below the cap, unlike other fields', () => {
- // Ordinary values clamp at ~2KB; an answer is the point of the record, so
- // it only clamps at MAX_CAPTURED_OUTPUT.
const seen = collect();
begin({argv: []});
say('y'.repeat(8000));
@@ -416,7 +523,6 @@ describe('captured output — what the CLI answered', () => {
it('still lets the output reach the real stream', () => {
const written = [];
const original = process.stdout.write;
- // @ts-expect-error - test-only monkeypatch, installed before the tee
process.stdout.write = chunk => {
written.push(String(chunk));
return true;
@@ -457,11 +563,6 @@ describe('captured output — what the CLI answered', () => {
});
describe('signals — the runs that never reach `exit`', () => {
- // `process.on('exit')` does not fire for a signal, so this seam is the only
- // thing standing between "the user gave up on a watch command" and no record
- // at all. It cannot be exercised in-process: raising a real signal ends the
- // worker, so each case is a child that signals itself and reports what its
- // handler was given.
const debugDir = path.dirname(fileURLToPath(import.meta.url));
/**
@@ -469,7 +570,7 @@ describe('signals — the runs that never reach `exit`', () => {
* @returns {{status: number | null, signal: string | null, event: any, raw: string}}
*/
function signalledRun({owner = false, handlerFirst = false} = {}) {
- const install = `d.setEventHandler(e => process.stdout.write('EVENT ' + JSON.stringify(e) + '\\n'));`;
+ const install = `d.setEventHandler(e => process.stderr.write('EVENT ' + JSON.stringify(e) + '\\n'));`;
const source = `
const d = await import(${JSON.stringify(path.join(debugDir, 'index.mjs'))});
${handlerFirst ? install : ''}
@@ -478,21 +579,24 @@ describe('signals — the runs that never reach `exit`', () => {
${handlerFirst ? '' : install}
${owner ? `process.on('SIGINT', () => process.exit(0));` : ''}
process.kill(process.pid, 'SIGINT');
- // Only reached if the signal was swallowed, which is itself a failure.
setTimeout(() => process.exit(7), 2000);
`;
- const res = spawnSync(process.execPath, ['--input-type=module', '-e', source], {
- encoding: 'utf8',
- timeout: 30_000,
- });
- const line = (res.stdout || '')
+ const res = spawnSync(
+ process.execPath,
+ ['--input-type=module', '-e', source],
+ {
+ encoding: 'utf8',
+ timeout: 30_000,
+ },
+ );
+ const line = (res.stderr || '')
.split('\n')
.find(l => l.startsWith('EVENT '));
return {
status: res.status,
signal: res.signal,
event: line ? JSON.parse(line.slice(6)) : null,
- raw: res.stdout || '',
+ raw: `${res.stdout || ''}${res.stderr || ''}`,
};
}
@@ -506,30 +610,21 @@ describe('signals — the runs that never reach `exit`', () => {
});
it('still lets the signal kill the process', () => {
- // Adding a listener suppresses Node's default disposition, so the handler
- // has to hand termination back. A parent must still see signal death.
const {status, signal} = signalledRun();
expect(signal).toBe('SIGINT');
expect(status).toBe(null);
});
it('leaves the outcome to the command that owns the signal', () => {
- // Watch mode traps SIGINT, prints "Stopped watching." and returns 0.
- // Sealing an error here would file the ordinary way out of `--watch` as a
- // failure, with an exit code the process never returned.
const {status, event} = signalledRun({owner: true});
expect(status).toBe(0);
expect(event.outcome).toBe('ok');
expect(event.exitCode).toBe(0);
expect(event.error).toBe(null);
- // The signal is still on the record — that is how you find these runs.
expect(event.signal).toBe('SIGINT');
});
it('arms the signal handlers whichever order the lifecycle runs in', () => {
- // The bin calls begin() first and supplies the handler once the config is
- // read. A caller that already has one must not silently lose every
- // signal-terminated run.
const {event} = signalledRun({handlerFirst: true});
expect(event).toBeTruthy();
expect(event.signal).toBe('SIGINT');
diff --git a/packages/cli/foundation/debug/recorder.mjs b/packages/cli/foundation/debug/recorder.mjs
index f10b72cdd0..6bba4f9397 100644
--- a/packages/cli/foundation/debug/recorder.mjs
+++ b/packages/cli/foundation/debug/recorder.mjs
@@ -33,7 +33,12 @@
* @position packages/cli/foundation/debug — runtime
*/
-import {createEvent, toEventError, captureProject, captureEnv} from './event.mjs';
+import {
+ createEvent,
+ toEventError,
+ captureProject,
+ captureEnv,
+} from './event.mjs';
import {createRedactor, redactArgv} from './redact.mjs';
import {ERROR_CODES} from '../response/error-codes.mjs';
@@ -66,6 +71,7 @@ let _handler = null;
let _finished = false;
let _listenerInstalled = false;
let _signalsArmed = false;
+let _configGateSkipped = false;
let _startedAt = 0;
/** @type {string | undefined} */
let _cliVersion;
@@ -177,10 +183,32 @@ export function currentEvent() {
export function setEventHandler(handler) {
guard(() => {
_handler = typeof handler === 'function' ? handler : null;
- if (_event && _handler) armSignalHandlers();
+ if (!_event || !_handler) return;
+ armSignalHandlers();
+ if (_configGateSkipped) reportConfigGateMiss();
});
}
+export function noteConfigGateSkipped() {
+ _configGateSkipped = true;
+}
+
+function reportConfigGateMiss() {
+ _configGateSkipped = false;
+ if (_event?.output.jsonMode) return;
+ try {
+ const write = _originalWrites?.err ?? process.stderr.write;
+ write.call(
+ process.stderr,
+ 'astryx: your astryx.config sets `debug`, but the file does not contain ' +
+ 'the word, so commands that do not otherwise read the config are not ' +
+ 'recorded. Name the key literally in the config file.\n',
+ );
+ } catch {
+ /* best effort */
+ }
+}
+
/**
* Start collecting.
*
@@ -352,7 +380,12 @@ export function setOutcome(outcome, details = {}) {
_event.error = toEventError(details.error);
if (_event.error && details.code) _event.error.code = details.code;
} else if (details.code) {
- _event.error = {name: 'CliError', message: '', code: details.code, stack: null};
+ _event.error = {
+ name: 'CliError',
+ message: '',
+ code: details.code,
+ stack: null,
+ };
}
});
}
@@ -459,7 +492,8 @@ export function finish({exitCode} = {}) {
_event.output.stdoutBytes = _stdout.bytes;
_event.output.stderrBytes = _stderr.bytes;
_event.output.truncated =
- _stdout.bytes > MAX_CAPTURED_OUTPUT || _stderr.bytes > MAX_CAPTURED_OUTPUT;
+ _stdout.bytes > MAX_CAPTURED_OUTPUT ||
+ _stderr.bytes > MAX_CAPTURED_OUTPUT;
const endedAt = new Date().toISOString();
const durationMs = Math.max(0, Date.now() - _startedAt);
@@ -527,14 +561,60 @@ export function finish({exitCode} = {}) {
// A COPY. Handing over the live object would let third-party code mutate
// the record mid-flight; its errors are swallowed for the same reason.
+ delivered = callHandler(_handler, structuredClone(sealed));
+ });
+ return delivered;
+}
+
+/**
+ * @param {import('../../authoring/debug/type').DebugEventHandler} handler
+ * @param {import('./event.mjs').DebugEvent} event
+ * @returns {boolean}
+ */
+function callHandler(handler, event) {
+ const realExit = process.exit;
+ const realStdoutWrite = process.stdout.write;
+ const realStderrWrite = process.stderr.write;
+ const exitCodeBefore = process.exitCode;
+ let exitAttempt = /** @type {number | string | null | undefined} */ (
+ undefined
+ );
+ let ran = false;
+
+ try {
+ process.exit = /** @type {any} */ (
+ (/** @type {any} */ code) => {
+ exitAttempt = code;
+ }
+ );
+ process.stdout.write = /** @type {any} */ (
+ function (/** @type {any} */ chunk, /** @type {any[]} */ ...rest) {
+ return realStderrWrite.call(process.stderr, chunk, ...rest);
+ }
+ );
+ handler(event);
+ ran = true;
+ } catch {
+ /* handler errors are isolated */
+ } finally {
+ process.exit = realExit;
+ process.stdout.write = realStdoutWrite;
+ process.exitCode = exitCodeBefore;
+ }
+
+ if (exitAttempt !== undefined) {
try {
- _handler(structuredClone(sealed));
- delivered = true;
+ realStderrWrite.call(
+ process.stderr,
+ `astryx: the debug handler called process.exit(${String(exitAttempt)}); ` +
+ `ignored so it cannot change what the command returned.\n`,
+ );
} catch {
- /* a broken handler must not fail the command */
+ /* best effort */
}
- });
- return delivered;
+ }
+
+ return ran;
}
/** Reset all recorder state. Tests call this between cases. */
@@ -557,5 +637,6 @@ export function resetRecorder() {
_event = null;
_handler = null;
_finished = false;
+ _configGateSkipped = false;
_startedAt = 0;
}
diff --git a/packages/cli/foundation/debug/redact.mjs b/packages/cli/foundation/debug/redact.mjs
index d5eaccc4a1..91537995e1 100644
--- a/packages/cli/foundation/debug/redact.mjs
+++ b/packages/cli/foundation/debug/redact.mjs
@@ -64,10 +64,6 @@ const SENSITIVE_KEY_PARTS = [
'token',
];
-/**
- * Names that are sensitive on their own but are substrings of ordinary words,
- * so they are matched WHOLE (again after stripping `-`/`_`).
- */
const SENSITIVE_KEY_EXACT = new Set([
'creds',
'key',
@@ -80,48 +76,78 @@ const SENSITIVE_KEY_EXACT = new Set([
/** Flags whose NEXT argv element is the value, e.g. `--token hunter2`. */
const SENSITIVE_FLAG_RE = /^--?([\w-]{1,64})$/;
-/**
- * `--flag=value` / `KEY=value`, ANY key. Whether the key is sensitive is
- * decided per match by {@link isSensitiveKey}, not encoded in the pattern —
- * one keyword list, and `--api-key`, `--api_key` and `--apiKey` are all the
- * same entry instead of three alternations to keep in step.
- *
- * Global and unanchored, because the same text arrives twice: once as its own
- * argv element, and again quoted inside an error message, a stack frame and
- * the captured stderr — `error: unknown option '--token=…'`. An anchored rule
- * scrubs the first and leaves the other three, which is the same secret,
- * written to the same record.
- *
- * The key half is BOUNDED. An unbounded `[\w-]*` before a keyword makes an
- * unanchored scan quadratic, and this runs over captured output that can be
- * tens of kilobytes — 500KB of word characters followed by `token=` took over
- * seven minutes.
- *
- * The value half takes an optional matching quote, so `--token="s"` and the
- * JSON spelling `"token":"s"` lose their value like the bare form. Without it
- * the value stops AT the opening quote and the secret survives with quotes
- * around it, which is the shape a config blob or a quoted shell argument
- * arrives in.
- */
const ASSIGNMENT_RE =
- /(--?[\w-]{1,64}|"[\w.-]{1,64}"|[\w.]{1,64})(\s*[=:]\s*)("[^"]*"|'[^']*'|[^\s'"`,}\]]*)/g;
+ /(--?[\w-]{1,128}|"[\w.-]{1,128}"|[\w.]{1,128})(\s*[=:]\s*)("[^"]*"|'[^']*'|[^\s'"`,}\]]*)/g;
/** Well-known credential formats worth catching wherever they appear. */
const CREDENTIAL_PATTERNS = [
- // GitHub personal access / OAuth / app tokens.
/\bgh[pousr]_[A-Za-z0-9]{16,}\b/g,
- // Slack tokens.
+ /\bglpat-[A-Za-z0-9_-]{16,}/g,
/\bxox[abposr]-[A-Za-z0-9-]{10,}\b/g,
- // AWS access key ids.
/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g,
- // Generic "Bearer <token>".
/\bBearer\s+[A-Za-z0-9._~+/-]{16,}=*/gi,
- // JSON Web Tokens.
/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,
- // OpenAI-style keys.
+ /\bsk-(?:ant|proj|live|test)-[A-Za-z0-9_-]{16,}/g,
/\bsk-[A-Za-z0-9]{20,}\b/g,
+ /\b[sr]k_(?:live|test)_[A-Za-z0-9]{10,}\b/g,
+ /\bAIza[A-Za-z0-9_-]{35}\b/g,
+ /\bnpm_[A-Za-z0-9]{30,}\b/g,
+ /\bhf_[A-Za-z0-9]{30,}\b/g,
+ /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/g,
+ /\b(?:AC|SK)[0-9a-f]{32}\b/g,
+ /-----BEGIN[A-Z ]*PRIVATE KEY-----[\s\S]*?-----END[A-Z ]*PRIVATE KEY-----/g,
];
+const ENTROPY_MIN_CHARS = 24;
+const HIGH_ENTROPY_CANDIDATE_RE = new RegExp(
+ `[A-Za-z0-9+=_-]{${ENTROPY_MIN_CHARS},512}`,
+ 'g',
+);
+
+const ENCODED_PAYLOAD_RE =
+ /(?:;base64,|\bsha(?:256|384|512)-|\bsourceMappingURL=)[A-Za-z0-9+/=_-]+/g;
+
+/**
+ * @param {string} value
+ * @returns {number}
+ */
+function entropyBits(value) {
+ /** @type {Map<string, number>} */
+ const counts = new Map();
+ for (const ch of value) counts.set(ch, (counts.get(ch) ?? 0) + 1);
+ let bits = 0;
+ for (const n of counts.values()) {
+ const p = n / value.length;
+ bits -= p * Math.log2(p);
+ }
+ return bits;
+}
+
+/**
+ * @param {string} word
+ * @returns {boolean}
+ */
+function looksLikeSecret(word) {
+ if (isOrdered(word)) return false;
+ return entropyBits(word) >= 0.9 * Math.log2(Math.min(word.length, 32));
+}
+
+/**
+ * @param {string} word
+ * @returns {boolean}
+ */
+function isOrdered(word) {
+ if (word.length < 4) return false;
+ let turns = 0;
+ for (let i = 2; i < word.length; i += 1) {
+ const a = word.charCodeAt(i - 2);
+ const b = word.charCodeAt(i - 1);
+ const c = word.charCodeAt(i);
+ if ((b > a && c < b) || (b < a && c > b)) turns += 1;
+ }
+ return turns / (word.length - 2) < 0.25;
+}
+
/** Credentials embedded in a URL's userinfo component. */
const URL_USERINFO_RE = /(\b[a-z][a-z0-9+.-]*:\/\/)[^/\s:@]+(?::[^/\s@]*)?@/gi;
@@ -129,25 +155,18 @@ const URL_USERINFO_RE = /(\b[a-z][a-z0-9+.-]*:\/\/)[^/\s:@]+(?::[^/\s@]*)?@/gi;
const EMAIL_RE = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g;
/**
- * Does this key name mean "the value is a secret"?
* @param {string | undefined} key
+ * @param {boolean} [named]
* @returns {boolean}
*/
-export function isSensitiveKey(key) {
+export function isSensitiveKey(key, named = true) {
if (!key) return false;
const lower = String(key).toLowerCase().replace(/[-_]/g, '');
- if (SENSITIVE_KEY_EXACT.has(lower)) return true;
+ if (named && SENSITIVE_KEY_EXACT.has(lower)) return true;
return SENSITIVE_KEY_PARTS.some(part => lower.includes(part));
}
/**
- * Scrub an argv array, where a flag and its value are two separate elements.
- *
- * `redact` walks a plain array element by element with no key context, so
- * `['--token', 'hunter2']` reaches it as two ordinary strings and the second
- * one matches nothing. `--flag value` is the ordinary CLI spelling, and it
- * would be perverse for `options` to redact what `argv` prints in full.
- *
* @param {string[]} argv
* @param {Redactor} redact
* @returns {unknown[]}
@@ -173,7 +192,7 @@ export function redactArgv(argv, redact) {
* revealing where on the machine it lives.
*
* @param {string} value
- * @param {{home: string, cwd: string}} ctx
+ * @param {{home: string, cwd: string, user: RegExp | null}} ctx
* @returns {string}
*/
function scrubPaths(value, ctx) {
@@ -204,13 +223,26 @@ function scrubPaths(value, ctx) {
},
);
+ out = out.replace(
+ /(^|[\s"'`([{<=,;])([A-Za-z]:\\|\\\\)([^\s"'`)\]}>]{2,})/g,
+ (match, lead, root, rest) => {
+ const parts = String(rest).split('\\').filter(Boolean);
+ if (parts.length <= 2) return match;
+ return `${lead}${root}…\\${parts.slice(-2).join('\\')}`;
+ },
+ );
+
+ if (ctx.user) {
+ out = out.replace(ctx.user, (whole, before) => `${before}${REDACTED}`);
+ }
+
return out;
}
/**
* Apply every content rule to a single string.
* @param {string} value
- * @param {{home: string, cwd: string}} ctx
+ * @param {{home: string, cwd: string, user: RegExp | null}} ctx
* @returns {string}
*/
function scrubString(value, ctx) {
@@ -222,22 +254,57 @@ function scrubString(value, ctx) {
out = out.replace(URL_USERINFO_RE, (_m, scheme) => `${scheme}${REDACTED}@`);
out = out.replace(EMAIL_RE, REDACTED);
- // `--token=abc` / `GITHUB_TOKEN=abc` / `"token": "abc"` survive the patterns
- // above when the value is an unrecognized format, so drop the right-hand
- // side by key name. The separator test is the cheap way out: most strings
- // have neither and skip the scan entirely.
if (out.includes('=') || out.includes(':')) {
out = out.replace(ASSIGNMENT_RE, (whole, key, sep, value) => {
- const name = String(key).replace(/^--?/, '').replace(/^"|"$/g, '');
- if (!isSensitiveKey(name)) return whole;
- // Keep the value's quoting so the surrounding shape — a JSON blob, a
- // quoted shell argument — still parses as what it was.
+ const raw = String(key);
+ const flagged = raw.startsWith('-');
+ const name = raw.replace(/^--?/, '').replace(/^"|"$/g, '');
+ if (!isSensitiveKey(name, flagged)) return whole;
const q = /^["']/.test(value) ? value[0] : '';
return `${key}${sep}${q}${REDACTED}${q}`;
});
}
- return scrubPaths(out, ctx);
+ out = scrubPaths(out, ctx);
+
+ if (out.length >= ENTROPY_MIN_CHARS) {
+ const skip = encodedPayloadRanges(out);
+ out = out.replace(HIGH_ENTROPY_CANDIDATE_RE, (word, offset) => {
+ if (inAnyRange(offset, skip)) return word;
+ return looksLikeSecret(word) ? REDACTED : word;
+ });
+ }
+
+ return out;
+}
+
+/**
+ * @param {string} text
+ * @returns {Array<[number, number]>}
+ */
+function encodedPayloadRanges(text) {
+ /** @type {Array<[number, number]>} */
+ const ranges = [];
+ if (!/base64,|sha(?:256|384|512)-|sourceMappingURL=/.test(text))
+ return ranges;
+ ENCODED_PAYLOAD_RE.lastIndex = 0;
+ let m;
+ while ((m = ENCODED_PAYLOAD_RE.exec(text)) !== null) {
+ ranges.push([m.index, m.index + m[0].length]);
+ }
+ return ranges;
+}
+
+/**
+ * @param {number} offset
+ * @param {Array<[number, number]>} ranges
+ * @returns {boolean}
+ */
+function inAnyRange(offset, ranges) {
+ for (const [start, end] of ranges) {
+ if (offset >= start && offset < end) return true;
+ }
+ return false;
}
/**
@@ -265,6 +332,7 @@ function clamp(value, max) {
* rules while still applying the depth and length limits.
* @param {string} [options.home] - overridable for tests.
* @param {string} [options.cwd] - overridable for tests.
+ * @param {string} [options.user] - overridable for tests.
* @param {number} [options.maxLength] - Per-value character cap.
* @returns {Redactor}
*/
@@ -272,12 +340,14 @@ export function createRedactor({
enabled = true,
home,
cwd,
+ user,
maxLength = MAX_VALUE_CHARS,
} = {}) {
- /** @type {{home: string, cwd: string}} */
+ /** @type {{home: string, cwd: string, user: RegExp | null}} */
const ctx = {
home: home ?? safeHomedir(),
cwd: cwd ?? safeCwd(),
+ user: usernamePattern(user ?? safeUsername()),
};
/**
@@ -340,3 +410,38 @@ function safeCwd() {
return '';
}
}
+
+/** @returns {string} */
+function safeUsername() {
+ try {
+ return os.userInfo().username || '';
+ } catch {
+ return '';
+ }
+}
+
+/**
+ * @param {string} name
+ * @returns {RegExp | null}
+ */
+function usernamePattern(name) {
+ const value = String(name ?? '');
+ if (value.length < 3 || COMMON_WORD_USERNAMES.has(value.toLowerCase())) {
+ return null;
+ }
+ const escaped = value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ return new RegExp(`([/\\\\~])${escaped}(?![A-Za-z0-9_-])`, 'gi');
+}
+
+const COMMON_WORD_USERNAMES = new Set([
+ 'admin',
+ 'build',
+ 'core',
+ 'dev',
+ 'docs',
+ 'node',
+ 'root',
+ 'test',
+ 'user',
+ 'www',
+]);
diff --git a/packages/cli/foundation/debug/redact.test.mjs b/packages/cli/foundation/debug/redact.test.mjs
index 40402476b3..74bbdf9267 100644
--- a/packages/cli/foundation/debug/redact.test.mjs
+++ b/packages/cli/foundation/debug/redact.test.mjs
@@ -8,21 +8,60 @@ import {
REDACTED,
} from './redact.mjs';
-const ctx = {home: '/Users/ada', cwd: '/Users/ada/projects/app'};
+/** @param {string[]} parts */
+const cred = parts => parts.join('');
+
+const ctx = {home: '/Users/ada', cwd: '/Users/ada/projects/app', user: 'ada'};
const scrub = createRedactor({enabled: true, ...ctx});
describe('isSensitiveKey', () => {
- it.each(['token', 'authToken', 'GITHUB_TOKEN', 'password', 'apiSecret', 'sessionId', 'x-signature', 'api-key', 'apiKey', 'API_KEY', 'accessKey', 'key', 'pat', 'pw'])(
- 'treats %s as sensitive',
+ it.each([
+ 'token',
+ 'authToken',
+ 'GITHUB_TOKEN',
+ 'password',
+ 'apiSecret',
+ 'sessionId',
+ 'x-signature',
+ 'api-key',
+ 'apiKey',
+ 'API_KEY',
+ 'accessKey',
+ 'key',
+ 'pat',
+ 'pw',
+ ])('treats %s as sensitive', key => {
+ expect(isSensitiveKey(key)).toBe(true);
+ });
+
+ it.each([
+ 'out',
+ 'detail',
+ 'component',
+ 'limit',
+ 'path',
+ 'keyboard',
+ 'sortkey',
+ 'pattern',
+ 'compatible',
+ undefined,
+ ])('leaves %s alone', key => {
+ expect(isSensitiveKey(key)).toBe(false);
+ });
+
+ it.each(['key', 'pwd', 'sig', 'pat', 'pw', 'creds'])(
+ 'treats %s as sensitive only when the name was given, not guessed from text',
key => {
- expect(isSensitiveKey(key)).toBe(true);
+ expect(isSensitiveKey(key, true)).toBe(true);
+ expect(isSensitiveKey(key, false)).toBe(false);
},
);
- it.each(['out', 'detail', 'component', 'limit', 'path', 'keyboard', 'sortkey', 'pattern', 'compatible', undefined])(
- 'leaves %s alone',
+ it.each(['token', 'password', 'apiKey', 'auth'])(
+ 'treats %s as sensitive either way — nothing ordinary is called that',
key => {
- expect(isSensitiveKey(key)).toBe(false);
+ expect(isSensitiveKey(key, true)).toBe(true);
+ expect(isSensitiveKey(key, false)).toBe(true);
},
);
});
@@ -37,7 +76,6 @@ describe('path scrubbing', () => {
});
it('keeps only the tail of a path outside home and cwd', () => {
- // The shape of the operation survives; where it lives on disk does not.
expect(scrub('/mnt/corp/secret-project/themes/ocean.ts')).toBe(
'/…/themes/ocean.ts',
);
@@ -48,8 +86,6 @@ describe('path scrubbing', () => {
});
it('rewrites a path inside a stack frame', () => {
- // The paths that carry a username are the ones in a stack, and a stack
- // never puts whitespace before them: `at fn (file:///Users/ada/…)`.
const frame =
' at cliError (file:///opt/tools/astryx/packages/cli/lib/x.mjs:12:5)';
const out = String(scrub(frame));
@@ -62,6 +98,45 @@ describe('path scrubbing', () => {
'read (/…/build/out.css)',
);
});
+
+ it('removes the username left in the middle of a collapsed path', () => {
+ expect(String(scrub('/mnt/corp/ada/notes.txt'))).toBe(
+ `/…/${REDACTED}/notes.txt`,
+ );
+ });
+
+ it.each([
+ [
+ 'a theme the project ships',
+ 'import {adaTheme} from "@astryxdesign/theme-ada"',
+ ],
+ ['a table row', 'Ada | a calm neutral theme'],
+ ['prose', 'the Ada theme is best for dashboards'],
+ ['a component name', 'see the Ada component and ada layout docs'],
+ ])('leaves %s alone — the name is only sensitive in a path', (_l, input) => {
+ expect(String(scrub(input))).toBe(input);
+ });
+
+ it.each(['ad', 'root', 'build', 'test', 'node', ''])(
+ 'skips the username %s — too short, or an ordinary directory name',
+ user => {
+ const s = createRedactor({enabled: true, ...ctx, user});
+ const p = `/srv/${user || 'x'}/notes.txt`;
+ expect(String(s(p))).toContain(user || 'x');
+ },
+ );
+
+ it('rewrites a Windows path', () => {
+ expect(String(scrub('at fn (C:\\Users\\ada\\proj\\src\\x.ts:3:1)'))).toBe(
+ 'at fn (C:\\…\\src\\x.ts:3:1)',
+ );
+ });
+
+ it('rewrites a UNC path', () => {
+ expect(String(scrub('copy \\\\server\\share\\team\\dist\\out.css'))).toBe(
+ 'copy \\\\…\\dist\\out.css',
+ );
+ });
});
describe('credential scrubbing', () => {
@@ -70,17 +145,43 @@ describe('credential scrubbing', () => {
['xoxb-1234567890-abcdefghij', 'Slack token'],
['AKIAIOSFODNN7EXAMPLE', 'AWS key id'],
['sk-abcdefghijklmnopqrstuvwxyz012345', 'OpenAI key'],
+ [cred(['glp', 'at-', 'AbCdEfGhIjKlMnOpQrSt']), 'GitLab token'],
+ [
+ cred(['sk-', 'ant-', 'api03-', 'AbCdEfGh_IjKlMnOpQrStUv']),
+ 'Anthropic key',
+ ],
+ [cred(['sk', '_live_', '4eC39HqLyjWDarjtT1zdp7dc']), 'Stripe key'],
+ [cred(['AIza', 'SyA1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P67']), 'Google API key'],
+ [cred(['npm', '_', 'AbCdEfGhIjKlMnOpQrStUvWxYz0123456789']), 'npm token'],
+ [
+ cred(['hf', '_', 'AbCdEfGhIjKlMnOpQrStUvWxYz0123456789']),
+ 'HuggingFace token',
+ ],
+ [cred(['AC', '0123456789abcdef0123456789abcdef']), 'Twilio sid'],
])('removes a %s', value => {
expect(String(scrub(`--key ${value}`))).not.toContain(value);
});
+ it('removes a PEM private key however it was wrapped', () => {
+ const pem = [
+ `-----BEGIN RSA ${cred(['PRIV', 'ATE KEY'])}-----`,
+ 'MIIEow',
+ 'IBAAKC',
+ `-----END RSA ${cred(['PRIV', 'ATE KEY'])}-----`,
+ ].join('\n');
+ expect(String(scrub(pem))).toBe(REDACTED);
+ });
+
it('removes a bearer token', () => {
- const out = String(scrub('Authorization: Bearer abcdefghijklmnopqrstuvwxyz'));
+ const out = String(
+ scrub('Authorization: Bearer abcdefghijklmnopqrstuvwxyz'),
+ );
expect(out).not.toContain('abcdefghijklmnopqrstuvwxyz');
});
it('removes a JWT', () => {
- const jwt = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U';
+ const jwt =
+ 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U';
expect(String(scrub(jwt))).toBe(REDACTED);
});
@@ -103,9 +204,6 @@ describe('credential scrubbing', () => {
});
it('drops it inside a larger string too', () => {
- // A rejected flag comes back quoted in the error message, the stack and
- // the captured stderr. Scrubbing only the standalone argv element writes
- // the same secret to the record three more times.
const out = String(
scrub("error: unknown option '--token=whatever-shape-this-is'"),
);
@@ -114,9 +212,6 @@ describe('credential scrubbing', () => {
});
it('drops every assignment in one string', () => {
- // The anchored rule this replaced would swallow everything after the first
- // `=`, so a case with only sensitive keys passes either way. This one has
- // an ordinary flag in front, which the anchored rule never gets past.
const out = String(scrub('--out=dist and --secret=two and TOKEN=three'));
expect(out).toContain('--out=dist');
expect(out).not.toContain('two');
@@ -130,8 +225,6 @@ describe('credential scrubbing', () => {
it.each(['--api-key', '--api_key', '--apiKey', '--APIKEY'])(
'drops the value of %s',
flag => {
- // `key` is not a substring rule — it would take `--keyboard` with it —
- // so the spellings have to normalize to one entry.
const out = String(scrub(`unknown option '${flag}=shhSECRETVALUE'`));
expect(out).not.toContain('shhSECRETVALUE');
},
@@ -146,6 +239,47 @@ describe('credential scrubbing', () => {
},
);
+ it.each([
+ ['a React key prop', '<Avatar key={user.id} src={user.src} />'],
+ [
+ 'the help text describing key: value output',
+ 'shape: aligned "key: value" lines',
+ ],
+ ['a JSON key field', '{"key":"listItem"}'],
+ ['a signature note', 'sig=(a: string) => void'],
+ ])(
+ 'leaves %s alone — a bare word is not a name someone chose',
+ (_l, input) => {
+ expect(String(scrub(input))).toBe(input);
+ },
+ );
+
+ it('leaves shell pwd output readable', () => {
+ expect(String(scrub('pwd=/srv/www/build'))).toBe('pwd=/…/www/build');
+ });
+
+ it('still drops a bare assignment whose name is unambiguous', () => {
+ expect(String(scrub('token=shhSECRETVALUE'))).toBe(`token=${REDACTED}`);
+ expect(String(scrub('{"password":"shhSECRETVALUE"}'))).toBe(
+ `{"password":"${REDACTED}"}`,
+ );
+ });
+
+ it('still redacts a real field named key, however it is nested', () => {
+ expect(scrub({key: 'shhSECRETVALUE'})).toEqual({key: REDACTED});
+ expect(scrub({a: {key: 'shhSECRETVALUE'}})).toEqual({a: {key: REDACTED}});
+ expect(scrub([{pwd: 'shhSECRETVALUE'}])).toEqual([{pwd: REDACTED}]);
+ });
+
+ it('catches a bare key= in text anyway when the value looks like a secret', () => {
+ expect(String(scrub("key: 'aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS'"))).toBe(
+ `key: '${REDACTED}'`,
+ );
+ expect(String(scrub('key=sk-abcdefghijklmnopqrstuvwxyz012345'))).toBe(
+ `key=${REDACTED}`,
+ );
+ });
+
it.each(['--keyboard', '--path', '--sortkey', '--pattern', '--signal'])(
'leaves %s alone — a whole-word rule must not eat ordinary flags',
flag => {
@@ -160,9 +294,6 @@ describe('credential scrubbing', () => {
['a spaced JSON pair', '{ "apiKey": "shhSECRETVALUE" }'],
['a yaml pair', 'password: shhSECRETVALUE'],
])('drops %s', (_label, input) => {
- // The value half has to take the quotes with it. Stopping AT the opening
- // quote leaves the secret standing with quotes around it — which is the
- // shape a config blob or a quoted argument arrives in.
expect(String(scrub(input))).not.toContain('shhSECRETVALUE');
});
@@ -180,15 +311,106 @@ describe('credential scrubbing', () => {
['a time', 'elapsed 12:30 done'],
['a ratio', 'aspect: 16/9'],
['an ordinary JSON pair', '{"name":"astryx"}'],
- ])('leaves %s untouched — `:` is a separator, not an invitation', (_l, input) => {
+ ])(
+ 'leaves %s untouched — `:` is a separator, not an invitation',
+ (_l, input) => {
+ expect(String(scrub(input))).toBe(input);
+ },
+ );
+});
+
+describe('a bare secret, with nothing to identify it by', () => {
+ it.each([
+ ['a random alphanumeric token', 'aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS'],
+ ['a longer one', 'Xk7Pq2Lm9Rt4Ws8Nb3Vc6Zy1Ha5Jd0Ge7Fu4Io2M'],
+ ['a base64url secret', 'dBjftJeZ4CVPmB92K27uhbUJU1p1r-wW1gFWFOEjXkQ'],
+ ['one at the length floor', 'M4nP8qR2tV6xZ9bD3fH7jL5s'],
+ [
+ 'a generated password',
+ 'k2Qz9zx3w7ep1r5t8yu4io6pa0sdF2ghj9kl3zxc7vbn5m1qwe',
+ ],
+ ])('redacts %s passed as a bare argument', (_label, secret) => {
+ expect(String(scrub(`docs ${secret}`))).toBe(`docs ${REDACTED}`);
+ });
+
+ it('redacts one quoted inside an error message', () => {
+ const secret = 'aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS';
+ expect(String(scrub(`Unknown topic "${secret}"`))).not.toContain(secret);
+ });
+
+ it.each([
+ ['a component name', 'RichTextEditorAutoLinkPlugin'],
+ ['a hook name', 'useTableGroupedRowsWithSelection'],
+ ['a commit sha', 'a3e3d179dcb1f4e5a2b8c7d6e9f0a1b2c3d4e5f6'],
+ ['a uuid', '550e8400-e29b-41d4-a716-446655440000'],
+ ['a prerelease version', '1.10.0-canary.20260902.1'],
+ ['a timestamp', '2026-09-02T07:25:14.000Z'],
+ ['a branch name', 'fix-debug-redaction-followups-2'],
+ ['a StyleX class', 'x1n2onr6x1ja2u2zx78zum5x1q0g3np'],
+ ['a file name', 'DateRangeInput.stories.tsx'],
+ ['a package tarball', 'astryxdesign-core-0.5.2.tgz'],
+ ['a relative path', 'packages/cli/foundation/debug/recorder.mjs'],
+ ])('leaves %s alone', (_label, value) => {
+ expect(String(scrub(value))).toBe(value);
+ });
+
+ it.each([
+ ['an ordered alphabet', 'abcdefghijklmnopqrstuvwxyz0123456789'],
+ ['a reversed one', 'zyxwvutsrqponmlkjihgfedcba9876543210'],
+ ['digits then letters', '0123456789abcdefghijklmnopqrstuvwxyz'],
+ ])(
+ 'leaves %s alone — entropy ignores order, and a credential is never ordered',
+ (_l, input) => {
+ expect(String(scrub(input))).toBe(input);
+ },
+ );
+
+ it('leaves a whole line of ordinary output alone', () => {
+ const line = 'Compile one or more defineTheme files to CSS and JS';
+ expect(String(scrub(line))).toBe(line);
+ });
+
+ it.each([
+ [
+ 'an inline PNG data URI',
+ '<Thumbnail src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAwFBMVEWPnaqJmKe4wsuvusS' +
+ 'Rn6yhrbfP1dvo6+7c4eXFztXm6ez18/Hy8O3d3drGxsO5ubbR0c7q6un08/L7+/rj4+H29vX9/f3Q0M2rq6i0tLGYmJWFhYKJiYb' +
+ 'w8O/Y2NWjo6CQkI2Ojov5+fj4+Pf39/YAAAB0dHFsbGlkZGFcXFlUVFFMTElERD8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />',
+ ],
+ [
+ 'a lockfile integrity hash',
+ 'resolution: {integrity: sha512-9CjPWnUqmyBcJLwPnQ4RmBpqgpTsNMOgWkO3sYNMBoWtOgqBqOxBcXvVQx7bMvJhCXrVHOm+HTVHTfPu9pJ0Kw==}',
+ ],
+ [
+ 'an inline source map',
+ '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdfQ==',
+ ],
+ ])('leaves %s intact — it is an answer, not a secret', (_l, input) => {
expect(String(scrub(input))).toBe(input);
});
+
+ it('still redacts a secret sitting next to an encoded payload', () => {
+ const png =
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4Hs';
+ const out = String(scrub(`${png} token=aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS`));
+ expect(out).toContain('iVBORw0KGgo');
+ expect(out).not.toContain('aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS');
+ });
+
+ it('catches a letters-only passphrase and a lowercase token too', () => {
+ expect(String(scrub('QwErTyUiOpAsDfGhJkLzXcVbNm'))).toBe(REDACTED);
+ expect(String(scrub('k3m9x2qb7ft4wz8vn5hd1jr6ps0y'))).toBe(REDACTED);
+ });
+
+ it('reports the margin the ordinary strings survive on', () => {
+ const closest = 'RichTextEditorAutoLinkPlugin';
+ expect(String(scrub(closest))).toBe(closest);
+ expect(String(scrub(`${closest}1`))).toBe(`${closest}1`);
+ });
});
describe('argv, where the flag and its value are separate elements', () => {
it('drops the element after a sensitive flag', () => {
- // `--flag value` is the ordinary CLI spelling. Redacting it in `options`
- // and printing it in `argv` would be the worst of both.
expect(redactArgv(['docs', '--token', 'hunter2SECRET'], scrub)).toEqual([
'docs',
'--token',
@@ -221,7 +443,6 @@ describe('argv, where the flag and its value are separate elements', () => {
});
it('does not treat the value after `--token=x` as sensitive', () => {
- // The `=` form carries its own value; the NEXT element is unrelated.
expect(redactArgv(['--token=x', 'docs'], scrub)).toEqual([
`--token=${REDACTED}`,
'docs',
@@ -266,13 +487,6 @@ describe('structure preservation', () => {
});
describe('cost on hostile input', () => {
- // Scrubbing runs before the length clamp and over captured output, so every
- // rule has to stay linear in the size of the string. Two guards keep the
- // assignment rule linear and BOTH are load-bearing: a cheap keyword
- // pre-check skips strings that cannot match, and bounded quantifiers either
- // side of the keyword save the ones that get past it. Measured on the
- // unbounded version, a 500KB word run followed by `token=` took over seven
- // minutes; it is 200ms here.
const big = createRedactor({...ctx, maxLength: Number.MAX_SAFE_INTEGER});
it.each([
@@ -281,7 +495,16 @@ describe('cost on hostile input', () => {
['a word run then a colon keyword', `${'y'.repeat(500_000)} token:abc`],
['colons', ':'.repeat(500_000)],
['quoted pairs', '"k":"xxxxxxxxxxxxxxxxxxxx" '.repeat(19_000)],
- ['a keyword between two word runs', `${'x'.repeat(250_000)}token=${'y'.repeat(250_000)}`],
+ [
+ 'random-looking tokens',
+ 'aB3xK9mQ7pL2vN8rT5wY6zC4dF1gH0jS '.repeat(15_000),
+ ],
+ ['hex runs', 'a3e3d179dcb1f4e5a2b8c7d6e9f0a1b2c3d4e5f6 '.repeat(12_000)],
+ ['one long base64 run', 'QWxhZGRpbjpvcGVuIHNlc2FtZQ'.repeat(19_000)],
+ [
+ 'a keyword between two word runs',
+ `${'x'.repeat(250_000)}token=${'y'.repeat(250_000)}`,
+ ],
['a word run then an equals sign', `${'x'.repeat(500_000)}=`],
['repeated assignments', 'token=abc '.repeat(50_000)],
['deep paths', '/a/b/c/d/e/f '.repeat(38_000)],
@@ -293,7 +516,8 @@ describe('cost on hostile input', () => {
});
});
-describe('disabled mode', () => { it('keeps sensitive content verbatim', () => {
+describe('disabled mode', () => {
+ it('keeps sensitive content verbatim', () => {
const raw = createRedactor({enabled: false});
expect(raw({token: 'ghp_secret', path: '/Users/ada/x'})).toEqual({
token: 'ghp_secret',
@@ -302,8 +526,6 @@ describe('disabled mode', () => { it('keeps sensitive content verbatim', () =>
});
it('still clamps oversized values', () => {
- // Length limits are a size guard, not a privacy one, so they survive
- // turning scrubbing off — otherwise one huge value costs the whole event.
const raw = createRedactor({enabled: false, maxLength: 10});
expect(String(raw({big: 'y'.repeat(500)}).big)).toMatch(
/^y{10}…\[\+490 chars\]$/,