diff --git a/packages/model-proxy/src/admin.mjs b/packages/model-proxy/src/admin.mjs index f93d55a20..94ddf7d90 100644 --- a/packages/model-proxy/src/admin.mjs +++ b/packages/model-proxy/src/admin.mjs @@ -149,10 +149,16 @@ export function recentReceipts(dataRoot, limit = 10) { // Console's user, no group or other bits) and backward from `before` in 64 KB chunks. A partial // last line (a write in progress) waits for a later read; a line that does not parse, or is longer // than 64 KB, is skipped. Each entry is redacted (redact.mjs) before it is answered, so a receipt -// that ever held key text shows none. A missing file is an empty history; any other failure to -// read refuses outcome-unknown. `uid` is a test seam. +// that ever held key text shows none. An entry whose redacted values read together, or whose keys +// and values read together, still hold an environment secret split across them is answered as +// { withheld: true }; pieces kept apart by another whole field are not seen (known limit). A missing +// file is an empty history; any other failure to read refuses outcome-unknown. `uid` is a test seam. export const HISTORY_LIMIT = Object.freeze({ min: 1, max: 100, default: 25 }); const HISTORY_CHUNK = 65536, HISTORY_LINE_MAX = 65536, HISTORY_SCAN_MAX = 2000; +// A redacted entry's values joined in order, with each object key before its value when `keys`. +const entryText = (value, keys) => (value !== null && typeof value === 'object' + ? Object.entries(value).map(([key, item]) => (keys && !Array.isArray(value) ? key : '') + entryText(item, keys)).join('') + : String(value)); export function receiptHistory(dataRoot, { limit = HISTORY_LIMIT.default, before = null, uid = process.getuid(), secrets = [] } = {}) { if (!Number.isSafeInteger(limit)) refuse('invalid-request'); const n = Math.min(HISTORY_LIMIT.max, Math.max(HISTORY_LIMIT.min, limit)); @@ -202,7 +208,11 @@ export function receiptHistory(dataRoot, { limit = HISTORY_LIMIT.default, before if (lineEnd - lineStart <= HISTORY_LINE_MAX) { let value = null; try { value = JSON.parse(bytes(lineStart, lineEnd).toString('utf8')); } catch { /* torn line */ } - if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.op === 'string') entries.push(redactValue(value, secrets)); + if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.op === 'string') { + const safe = redactValue(value, secrets); + const split = holdsSecret(entryText(safe, false), secrets) || holdsSecret(entryText(safe, true), secrets); + entries.push(split ? { withheld: true } : safe); + } } cursor = lineStart; } @@ -255,6 +265,8 @@ export async function unitLog({ run = defaultRun, unit, lines = LOG_LINES.defaul out.push(safe.text); } // The lines that will be shown, read together: a secret split across them drops them all. + // Only environment secrets are checked this way. A key pattern broken across two journal + // lines, each part too short for the long-run rules, is a known limit shared with chat-fail. if (holdsSecret(out.join(''), secrets)) { dropped += out.length; out.length = 0; } } catch { refuse('outcome-unknown'); } return { unit, lines: out, redacted, dropped }; diff --git a/packages/model-proxy/src/redact.mjs b/packages/model-proxy/src/redact.mjs index 7a064ea22..9c52c8b0d 100644 --- a/packages/model-proxy/src/redact.mjs +++ b/packages/model-proxy/src/redact.mjs @@ -3,7 +3,9 @@ // // It is the redaction chat-fail landed for a failed turn's cause (packages/harness/src/failure-notice.mjs, // its REDACT list), with Mosaic's own key prefixes added to the key-prefix line: mso- (an organization's -// model key), mst- (the Triage key) and msp-, beside msk- (the owner's). failure-notice.mjs does not +// model key), mst- (the Triage key) and msp-, beside msk- (the owner's). One line is added after it: +// those four Mosaic prefixes with no word boundary, so a key glued to a word (xmso-...) is caught +// too. Over-redaction there is the safe side. failure-notice.mjs does not // export its list, and packages/harness/ belongs to another lane now, so the list is copied here. // tests/redact.test.mjs fails when a copied line no longer matches failure-notice.mjs. // @@ -18,7 +20,8 @@ export { secretValues } from '../../harness/src/failure-notice.mjs'; export const LINE_MAX = 300; export const REDACTED = '[redacted]'; -// Copied from failure-notice.mjs. Only the key-prefix line differs: it adds mso, mst and msp. +// Copied from failure-notice.mjs. The key-prefix line adds mso, mst and msp, and the line after +// it is Mosaic's own; every other line is failure-notice's. const KEY_WORD = "(?:\\w*[_-])?(?:api[_-]?key|key|token|secret|password|passwd|authorization|cookie|session)"; const REDACT = [ /(? { + const dataRoot = scratch(t); + appendReceipt(dataRoot, { op: 'fetch', n: 1 }); + appendReceipt(dataRoot, { op: 'x', a: 'plainword', b: 'sonly' }); + appendReceipt(dataRoot, { op: 'x', plainw: 'ordsonly' }); + appendReceipt(dataRoot, { op: 'x', list: ['plainwo', 'rdsonly'] }); + appendReceipt(dataRoot, { op: 'fetch', n: 2 }); + const all = receiptHistory(dataRoot, { limit: 10, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) }); + assert.deepEqual(all.entries, [{ op: 'fetch', n: 1 }, { withheld: true }, { withheld: true }, { withheld: true }, { op: 'fetch', n: 2 }]); + assert.equal(receiptHistory(dataRoot, { limit: 10 }).entries[1].a, 'plainword', 'with no secrets nothing is withheld'); +}); + test('receiptHistory: a missing file is an empty history; a symlink, FIFO, directory, group-readable or foreign file refuses', t => { const dataRoot = scratch(t); assert.deepEqual(receiptHistory(dataRoot), { entries: [], next: null }); diff --git a/packages/model-proxy/tests/redact.test.mjs b/packages/model-proxy/tests/redact.test.mjs index 010d77b2b..8fe07c038 100644 --- a/packages/model-proxy/tests/redact.test.mjs +++ b/packages/model-proxy/tests/redact.test.mjs @@ -22,6 +22,17 @@ for (const [prefix, key] of Object.entries(SHORT)) { }); } +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}`]) { @@ -92,8 +103,9 @@ test('a 128000-character run with no spaces is redacted in well under a second', }); // 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. -test('the redaction list matches failure-notice.mjs line for line, with only the prefixes added', () => { +// 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]; @@ -104,5 +116,7 @@ test('the redaction list matches failure-notice.mjs line for line, with only the 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); }); diff --git a/packages/webui/src/proxy-http.mjs b/packages/webui/src/proxy-http.mjs index 79d83cf37..73d56f7e4 100644 --- a/packages/webui/src/proxy-http.mjs +++ b/packages/webui/src/proxy-http.mjs @@ -62,6 +62,7 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted, if (req.method === 'POST' && typeof record === 'function') { let recorded; try { recorded = record({ verb: route[1], args, result: value, user: user ?? null }); } catch { recorded = false; } + // All four recorded verbs answer a plain object; any other answer is passed on unmarked. if (recorded === false && value && typeof value === 'object' && !Array.isArray(value)) value = { ...value, recorded: false }; } return done(200, value);