From 8f7de8c4d3ee320b64a7f40b812e1045a7f75cfb Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Tue, 10 Nov 2026 15:39:53 -0600 Subject: [PATCH] fix(cli): redact config and organization JSON parse refusals (#1873) --- packages/cli/src/org.mjs | 2 +- packages/cli/tests/json-redaction.test.mjs | 59 ++++++++++++++++++++++ scripts/mosaic-config.mjs | 6 +-- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 packages/cli/tests/json-redaction.test.mjs diff --git a/packages/cli/src/org.mjs b/packages/cli/src/org.mjs index 07b914d8a..7315ae5e5 100644 --- a/packages/cli/src/org.mjs +++ b/packages/cli/src/org.mjs @@ -11,7 +11,7 @@ import { consoleBusRestart } from './org-console.mjs'; import { refuseInsideAgent } from './transport.mjs'; export const ORG_USAGE = 'mosaic org system-types installed | system-types enabled ID | lock show|clear ID | reload ID | archive ID --if-revision N [--json] | restore ID --if-revision N [--json] | delete ID --confirm ID [--if-revision N] [--json] | template render ID [--fixture local] --output FILE | validate FILE | create ID --from FILE | show ID [--json] | list [--json] | update ID --from FILE --if-revision N | queue init ID (--local-only --accept-local-loss-risk | --remote URL) | queue show ID [--json] | project repo init ID PROJECT (--local-only --accept-local-loss-risk | --remote URL) | project repo show ID PROJECT [--json] | config validate ID | config resolve ID INSTANCE [--project ID]'; function parse(args) { const pos = [], values = {}, flags = new Set(); for (let i = 0; i < args.length; i++) { const a = args[i]; if (['--json', '--local-only', '--accept-local-loss-risk'].includes(a)) { if (flags.has(a)) throw new CliError(`duplicate ${a}`, 4); flags.add(a); } else if (['--fixture', '--output', '--from', '--if-revision', '--remote', '--confirm'].includes(a)) { if (values[a] !== undefined || !args[i+1] || args[i+1].startsWith('--')) throw new CliError(`${a} needs one value`, 4); values[a] = args[++i]; } else if (a.startsWith('--')) throw new CliError(`unknown option ${a}`, 4); else pos.push(a); } return { pos, values, flags }; } -async function document(file) { try { return JSON.parse(await readFile(file, 'utf8')); } catch (e) { throw new CliError(`cannot read document ${file}: ${e.message}`, 2); } } +async function document(file) { try { return JSON.parse(await readFile(file, 'utf8')); } catch (e) { throw new CliError(`cannot read document ${file}: ${e instanceof SyntaxError ? 'not valid JSON' : e.message}`, 2); } } export async function orgCommand(args, io, deps = {}) { try { const p = parse(args); const words = p.pos; let result; let accepted = []; diff --git a/packages/cli/tests/json-redaction.test.mjs b/packages/cli/tests/json-redaction.test.mjs new file mode 100644 index 000000000..b028356ad --- /dev/null +++ b/packages/cli/tests/json-redaction.test.mjs @@ -0,0 +1,59 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; +import { loadSystem } from '../src/config.mjs'; +import { tmp } from './helpers.mjs'; + +const repo = resolve(import.meta.dirname, '../../..'); +const configScript = join(repo, 'scripts/mosaic-config.mjs'); +const cli = join(repo, 'packages/cli/src/cli.mjs'); +const secret = 'sk-test-NOT-A-SECRET-1234'; +function fixture(t) { + const root = tmp(t, 'jr-'), home = join(root, 'home'); + const file = join(home, '.config/mosaic-redacttest/config.json'); + const env = { PATH: process.env.PATH, HOME: home, TMPDIR: process.env.TMPDIR, LANG: 'C.UTF-8', MOSAIC_CONFIG: file }; + const run = (script, ...args) => spawnSync(process.execPath, [script, ...args], { env, cwd: repo, encoding: 'utf8' }); + const boot = () => { + const result = run(configScript, 'bootstrap', '--instance', 'redacttest'); + assert.equal(result.status, 0, result.stderr); + }; + return { root, file, env, run, boot }; +} +function redacted(result, file, status) { + assert.equal(result.status, status, result.stderr); + assert.equal(result.stdout, ''); + assert.ok(result.stderr.includes(file), result.stderr); + assert.match(result.stderr, /not valid JSON/); + assert.doesNotMatch(result.stderr, /sk-test|Unexpected token/); +} + +test('system config parse refusal and CLI relay omit parser bytes', t => { + const f = fixture(t); + mkdirSync(dirname(f.file), { recursive: true }); writeFileSync(f.file, secret); + redacted(f.run(configScript, 'validate', '--instance', 'redacttest'), f.file, 2); + assert.throws(() => loadSystem({ env: f.env, instance: 'redacttest' }), error => { + assert.equal(error.exitCode, 3); + assert.ok(error.message.includes(f.file)); + assert.match(error.message, /not valid JSON/); + assert.doesNotMatch(error.message, /sk-test|Unexpected token/); + return true; + }); + redacted(f.run(cli, 'org', 'config', 'validate', 'alpha'), f.file, 3); +}); + +test('named instance marker parse refusal omits parser bytes', t => { + const f = fixture(t); f.boot(); + const { dataRoot } = JSON.parse(readFileSync(f.file, 'utf8')); + const marker = join(dataRoot, 'instance.json'); writeFileSync(marker, secret); + redacted(f.run(configScript, 'validate', '--instance', 'redacttest'), marker, 3); +}); + +for (const operation of ['validate', 'create', 'update']) test(`org ${operation} document refusal omits parser bytes`, t => { + const f = fixture(t); f.boot(); + const file = join(f.root, 'alpha.json'); writeFileSync(file, secret); + const args = operation === 'validate' ? ['validate', file] + : [operation, 'alpha', '--from', file, ...(operation === 'update' ? ['--if-revision', '1'] : [])]; + redacted(f.run(cli, 'org', ...args), file, 2); +}); diff --git a/scripts/mosaic-config.mjs b/scripts/mosaic-config.mjs index b1866a890..aae41b94b 100755 --- a/scripts/mosaic-config.mjs +++ b/scripts/mosaic-config.mjs @@ -133,7 +133,7 @@ function readMarker(dataRoot) { try { marker = JSON.parse(fs.readFileSync(file, "utf8")); } catch (error) { - fail(3, `instance-root-unclaimed: ${file} is not a valid marker (${error.message})`); + fail(3, `instance-root-unclaimed: ${file} is not a valid marker (${error instanceof SyntaxError ? 'not valid JSON' : error.message})`); } if (!isPlainObject(marker) || Object.keys(marker).sort().join(",") !== "instanceVersion,name" || marker.instanceVersion !== 1 || typeof marker.name !== "string" || !INSTANCE_PATTERN.test(marker.name)) { @@ -382,8 +382,8 @@ async function load(file, instance = null) { let document; try { document = JSON.parse(raw); - } catch (error) { - fail(2, `configuration is not valid JSON (${file}): ${error.message}`); + } catch { + fail(2, `configuration is not valid JSON (${file})`); } return await validate(document, file); }