123 lines
7.6 KiB
JavaScript
123 lines
7.6 KiB
JavaScript
// #1873 settings-backend A1: the redaction the setup page's log and history reads use. It is
|
|
// chat-fail's list (packages/harness/src/failure-notice.mjs) with Mosaic's key prefixes added.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { redactLine, redactValue, holdsSecret, secretValues, REDACTED, LINE_MAX } from '../src/redact.mjs';
|
|
|
|
// Short keys of each Mosaic prefix, letters only, so no long-run or mixed-run rule catches them:
|
|
// only the key-prefix line does.
|
|
const SHORT = { msk: 'msk-tag-qwertyu', mso: 'mso-tag-acme-qwerty', mst: 'mst-tag-triage-zxcvb', msp: 'msp-tag-plmokn' };
|
|
|
|
for (const [prefix, key] of Object.entries(SHORT)) {
|
|
test(`a short ${prefix}- key is redacted from a line, and the rest of the line stays`, () => {
|
|
const safe = redactLine(`proxy said ${key} was refused`);
|
|
assert.equal(safe.text, `proxy said ${REDACTED} was refused`);
|
|
assert.equal(safe.redacted, true);
|
|
assert.equal(safe.text.includes(key), false);
|
|
// Upper case, and joined by _ instead of -, too.
|
|
assert.equal(redactLine(`proxy said ${key.toUpperCase()} x`).text, `proxy said ${REDACTED} x`);
|
|
assert.equal(redactLine(`proxy said ${key.replace('-', '_')} x`).text, `proxy said ${REDACTED} x`);
|
|
});
|
|
}
|
|
|
|
test('a short Mosaic key glued to a word or an underscore is redacted', () => {
|
|
for (const key of Object.values(SHORT)) {
|
|
for (const glued of [`x${key}`, `_${key}`, `id=x${key}`, `9${key.toUpperCase()}`]) {
|
|
const safe = redactLine(`proxy said ${glued} was refused`);
|
|
assert.equal(safe.redacted, true, glued);
|
|
assert.equal(safe.text.toLowerCase().includes(key.slice(4)), false, glued);
|
|
assert.match(safe.text, / was refused$/);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('the real key shapes of each prefix are redacted', () => {
|
|
const tail = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
|
for (const key of [`msk-k1a2-${tail}`, `mso-k1a2-acme-${tail}`, `mst-k1a2-triage-${tail}`, `msp-k1a2-${tail}`, `sk-ant-${tail}`]) {
|
|
const safe = redactLine(`key ${key} rejected by upstream`);
|
|
assert.equal(safe.text.includes(key.slice(0, 12)), false, key.slice(0, 4));
|
|
assert.equal(safe.text.includes(tail.slice(0, 8)), false, key.slice(0, 4));
|
|
}
|
|
});
|
|
|
|
test('bearer and basic tokens, key=value pairs, links, emails and paths never show', () => {
|
|
const line = 'POST https://u:[email protected]/v1?key=abc Authorization: Bearer abcdefgh api_key=hunter2 token "quoted value" '
|
|
+ 'by [email protected] from /home/jane/.config/mosaic-dev/proxy.json and ~/.mosaic/x C:\\Users\\j \\\\host\\share Basic dXNlcjpwYXNz eyJhbGciOi.x.y';
|
|
const safe = redactLine(line);
|
|
for (const bit of ['u:p@', 'api.example', 'abcdefgh', 'hunter2', 'quoted value', 'jane@', '/home/jane', '.config', '.mosaic', 'Users', 'share', 'dXNlcjpwYXNz', 'eyJhbGci']) {
|
|
assert.equal(safe.text.includes(bit), false, bit);
|
|
}
|
|
assert.match(safe.text, /^POST /);
|
|
});
|
|
|
|
test('a line holding an environment secret is withheld whole, even split by spaces, controls or encoding', () => {
|
|
const secrets = secretValues({ MOSAIC_PROXY_KEY: 'plainwordsonly', HOME: '/home/x', SHORT_TOKEN: 'abc' });
|
|
assert.deepEqual(secrets, ['plainwordsonly'], 'only secret-named values of 8 or more characters');
|
|
for (const line of ['started with plainwordsonly', 'started with plain words only', 'started p\u0000lainwords\u200bonly', 'started %70lainwordsonly', 'started plainwordsonly']) {
|
|
assert.deepEqual(redactLine(line, secrets), { text: null, redacted: true, withheld: true }, JSON.stringify(line));
|
|
}
|
|
assert.equal(holdsSecret('plain\nwords\nonly', secrets), true, 'split across lines');
|
|
assert.equal(holdsSecret('nothing here', secrets), false);
|
|
assert.equal(holdsSecret('plainwordsonly', []), false);
|
|
});
|
|
|
|
test('a line that is empty, blank or only redactions is dropped; a long one is cut to the line cap', () => {
|
|
assert.deepEqual(redactLine(''), { text: null, redacted: false });
|
|
assert.deepEqual(redactLine(' \u0000\t '), { text: null, redacted: false });
|
|
assert.deepEqual(redactLine(`${SHORT.mso} ${SHORT.mst}`), { text: null, redacted: true });
|
|
assert.equal(redactLine(`a ${SHORT.mso} ${SHORT.mst} b`).text, `a ${REDACTED} b`, 'repeated redactions collapse');
|
|
const long = redactLine('word '.repeat(200));
|
|
assert.equal(Array.from(long.text).length, LINE_MAX);
|
|
assert.match(long.text, /\.\.\.$/);
|
|
assert.equal(redactLine('\u001b[31mred\u001b[0m text').text, 'red text', 'ANSI goes');
|
|
});
|
|
|
|
test('a stored value is redacted all through: strings, nested strings, and keys that read as secrets', () => {
|
|
const value = { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${SHORT.mso}`, deep: { list: [SHORT.msk, 3, true, null] }, [SHORT.mst.replace(/-/g, '_')]: 'x', 'bad key': 1 };
|
|
const out = redactValue(value);
|
|
assert.deepEqual(out, { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${REDACTED}`, deep: { list: [REDACTED, 3, true, null] } });
|
|
assert.equal(JSON.stringify(out).includes('qwerty'), false);
|
|
// Depth and size are bounded.
|
|
assert.deepEqual(redactValue({ a: { b: { c: { d: { e: 1 } } } } }), { a: { b: { c: { d: null } } } });
|
|
assert.equal(redactValue(Array.from({ length: 100 }, (_, i) => i)).length, 64);
|
|
assert.equal(redactValue('plainwordsonly', ['plainwordsonly']), REDACTED);
|
|
assert.equal(redactValue(undefined), null);
|
|
});
|
|
|
|
// chat-fail's shape: redaction runs over a whole line before the cut, so a long run with no spaces
|
|
// must stay linear.
|
|
test('a 128000-character run with no spaces is redacted in well under a second', () => {
|
|
const N = 128000;
|
|
const secrets = secretValues({ API_KEY: 'plainwordsonly' });
|
|
for (const run of ['a'.repeat(N), 'a.'.repeat(N / 2), 'a-'.repeat(N / 2), 'a/'.repeat(N / 2), 'a:'.repeat(N / 2), 'a"'.repeat(N / 2), 'mso-'.repeat(N / 4), 'a@'.repeat(N / 2)]) {
|
|
const started = performance.now();
|
|
const safe = redactLine(`${run} then the unit stopped`, secrets);
|
|
const took = performance.now() - started;
|
|
assert.ok(took < 1000, `${run.slice(0, 4)}... took ${Math.round(took)} ms`);
|
|
assert.ok(safe.text === null || Array.from(safe.text).length <= LINE_MAX);
|
|
}
|
|
// A run that reads as a key is redacted whole, so the words after it show.
|
|
assert.equal(redactLine(`${'a'.repeat(N)} then the unit stopped`).text, `${REDACTED} then the unit stopped`);
|
|
});
|
|
|
|
// The list is a copy of failure-notice.mjs's. Every line must still match it, except the key-prefix
|
|
// line, which is the original with mso, mst and msp added after msk, and Mosaic's glued-prefix line
|
|
// right after it.
|
|
test('the redaction list matches failure-notice.mjs line for line, with only the prefixes and one line added', () => {
|
|
const source = file => fs.readFileSync(fileURLToPath(new URL(file, import.meta.url)), 'utf8');
|
|
const block = text => {
|
|
const body = /const REDACT = \[\n([\s\S]*?)\n\];/.exec(text)[1];
|
|
return body.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('//')).map(line => line.replace(/,\s*\/\/ .*$/, ',').replace(/,$/, ''));
|
|
};
|
|
const keyWord = text => /const KEY_WORD = (.*);/.exec(text)[1];
|
|
const theirs = source('../../harness/src/failure-notice.mjs'), ours = source('../src/redact.mjs');
|
|
assert.equal(keyWord(ours), keyWord(theirs));
|
|
const expected = block(theirs).map(line => line.replace('(?:sk|msk|rk|', '(?:sk|msk|mso|mst|msp|rk|'));
|
|
assert.equal(expected.filter((line, i) => line !== block(theirs)[i]).length, 1, 'the original has one key-prefix line');
|
|
const at = expected.findIndex(line => line.includes('(?:sk|msk|mso|mst|msp|rk|'));
|
|
expected.splice(at + 1, 0, '/ms[kotp][-_]\\S+/gi');
|
|
assert.deepEqual(block(ours), expected);
|
|
});
|