From 74d0f80f2efa20b29438ffe22d64c306dea3f3f2 Mon Sep 17 00:00:00 2001 From: 0libote <79215282+0libote@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:08:23 +0000 Subject: [PATCH 1/2] fix(credentials): mask encoded secret forms in service logs, refuse secrets too short to mask --- docker/services/logs.mjs | 10 ++- docker/setup/client/credential-model.js | 3 + docker/setup/credentials/model.mjs | 4 ++ docker/setup/gateways.mjs | 3 + tests/credential-model.test.mjs | 8 ++- tests/credentials-run.test.mjs | 8 +-- tests/credentials.test.mjs | 7 +++ tests/gateways-service.test.mjs | 82 +++++++++++++------------ tests/services.test.mjs | 11 ++++ 9 files changed, 88 insertions(+), 48 deletions(-) diff --git a/docker/services/logs.mjs b/docker/services/logs.mjs index 5a669356..9f132531 100644 --- a/docker/services/logs.mjs +++ b/docker/services/logs.mjs @@ -2,13 +2,17 @@ import { mkdirSync, statSync, renameSync, writeFileSync, appendFileSync, readFil import path from "node:path"; import { StringDecoder } from "node:string_decoder"; import { ServiceError } from './store.mjs'; +import { maskValues } from "../setup/credentials/redact.mjs"; const LIMIT = 256 * 1024; export function logWriter(root, id, runId, stream, masks = []) { const dir = path.join(root, "logs", id); mkdirSync(dir, { recursive: true, mode: 0o700 }); const file = path.join(dir, stream + ".jsonl"), decoder = new StringDecoder("utf8"); let buffered = "", pending = '', sequence = 0; - const redact = value => masks.filter(Boolean).sort((a, b) => b.length - a.length).reduce((result, mask) => result.split(mask).join("[redacted]"), value); + // The same encoded forms the Vault hides in command output: a service that + // prints `echo $TOKEN | base64` must not leave the value in its log. + const values = maskValues(masks); + const redact = value => values.reduce((result, mask) => result.split(mask).join("[redacted]"), value); const line = text => { const record = JSON.stringify({ at: Date.now(), runId, stream, sequence: ++sequence, text: text.slice(0, 8192) }) + "\n"; try { @@ -19,12 +23,12 @@ export function logWriter(root, id, runId, stream, masks = []) { const drain = final => { // Redact before splitting lines, including multiline values and values // split across writes. Keep enough raw suffix for the next chunk. - const overlap = Math.max(1, ...masks.map(mask => mask.length)); + const overlap = Math.max(1, ...values.map(value => value.length)); let cut = final ? pending.length : Math.max(0, pending.length - overlap + 1); let changed; do { changed = false; - for (const mask of masks.filter(Boolean)) { + for (const mask of values) { const at = pending.lastIndexOf(mask, cut - 1); if (at >= 0 && at < cut && at + mask.length > cut) { cut = at; changed = true; } } diff --git a/docker/setup/client/credential-model.js b/docker/setup/client/credential-model.js index 1c052c08..61d0baab 100644 --- a/docker/setup/client/credential-model.js +++ b/docker/setup/client/credential-model.js @@ -161,6 +161,7 @@ const T3CredentialModel = (() => { if (!validEnvName(row.name)) errors.vars = (row.name || 'Each variable') + ' needs a name of letters, numbers and underscores.'; else if (seen.has(row.name)) errors.vars = row.name + ' is listed twice.'; else if (row.secret && !row.value && !row.saved) errors.vars = 'Enter a value for ' + row.name + '.'; + else if (row.secret && row.value && row.value.trim().length < 8) errors.vars = 'Use at least 8 characters for ' + row.name + '.'; seen.add(row.name); } return errors; @@ -174,7 +175,9 @@ const T3CredentialModel = (() => { if (!applies(entry, draft.fields) || entry.optional) continue; const value = secrets[entry.key]; if (!value && !savedSecrets.includes(entry.key)) errors['secrets.' + entry.key] = 'Enter ' + entry.label.toLowerCase() + '.'; + else if (value && !entry.multiline && value.replace(/\r?\n$/, '').length < 8) errors['secrets.' + entry.key] = 'Use at least 8 characters for ' + entry.label.toLowerCase() + '.'; else if (value && !entry.multiline && !text(value.replace(/\r?\n$/, ''), 8192)) errors['secrets.' + entry.key] = 'Use up to 8192 characters, without line breaks.'; + else if (value && entry.multiline && value.trim().length < 8) errors['secrets.' + entry.key] = 'Use at least 8 characters for ' + entry.label.toLowerCase() + '.'; else if (value && entry.multiline && value.length > 48 * 1024) errors['secrets.' + entry.key] = 'Use up to 48 KiB.'; } for (const entry of [...type.fields, ...type.secrets]) { diff --git a/docker/setup/credentials/model.mjs b/docker/setup/credentials/model.mjs index 594f5451..7f2c81d9 100644 --- a/docker/setup/credentials/model.mjs +++ b/docker/setup/credentials/model.mjs @@ -5,6 +5,8 @@ import { TYPES, applies, isType, withDefaults } from './types.mjs'; const fail = (message, field) => { throw new CredentialError(message, 400, 'invalid', field ? { field } : {}); }; const MULTILINE_MAX = 48 * 1024; const SECRET_MAX = 8192; +// Short values can't be masked without hiding ordinary text (see redact.mjs), +// so nothing this short is accepted as a secret in the first place. const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/; // A saved value under these names would break the command, or change what it runs. const RESERVED_ENV = /^(?:PATH|HOME|USER|SHELL|PWD|IFS|LD_.*|DYLD_.*|NODE_OPTIONS|BASH_ENV|ENV)$/; @@ -64,11 +66,13 @@ function normalizeSecret(entry, raw, previous) { if (value === undefined) { if (entry.optional) return undefined; fail(`Enter ${entry.label.toLowerCase()}.`, entry.key); } if (typeof value !== 'string' || !value.isWellFormed()) fail(`Enter ${entry.label.toLowerCase()} as plain text.`, entry.key); if (entry.multiline) { + if (value.trim().length < 8) fail(`Use at least 8 characters for ${entry.label.toLowerCase()}.`, entry.key); if (value.length > MULTILINE_MAX || value.includes('\0') || !value.trim()) fail(`Enter ${entry.label.toLowerCase()} as text of up to 48 KiB.`, entry.key); return value; } // A pasted value or a file often ends in a line break that isn't part of it. const line = value.replace(/\r?\n$/, ''); + if (line.length < 8) fail(`Use at least 8 characters for ${entry.label.toLowerCase()}.`, entry.key); if (!text(line, SECRET_MAX)) fail(`Enter ${entry.label.toLowerCase()}, up to ${SECRET_MAX} characters without line breaks.`, entry.key); return line; } diff --git a/docker/setup/gateways.mjs b/docker/setup/gateways.mjs index d2824de7..16998f01 100644 --- a/docker/setup/gateways.mjs +++ b/docker/setup/gateways.mjs @@ -63,12 +63,15 @@ const cleanKey = value => { if (value === undefined || value === null) return undefined; const key = String(value).trim(); if (key.length > 4096 || /[^\x21-\x7e]/.test(key)) throw new GatewayError('An API key is one line of plain characters, without spaces.', 400, 'invalid', { field: 'key' }); + // Short keys can't be masked in output without hiding ordinary text. + if (key && key.length < 8) throw new GatewayError('An API key is at least 8 characters of plain text, without spaces.', 400, 'invalid', { field: 'key' }); return key; }; const cleanManagementKey = value => { if (value === undefined || value === null || value === '') return undefined; const key = String(value).trim(); if (!key || key.length > 4096 || /[^\x21-\x7e]/.test(key)) throw new GatewayError('A management key is one line of plain characters, without spaces.', 400, 'invalid', { field: 'managementKey' }); + if (key.length < 8) throw new GatewayError('A management key is at least 8 characters of plain text, without spaces.', 400, 'invalid', { field: 'managementKey' }); return key; }; const allowPrivate = input => (input?.allowPrivateHost ?? input?.gateway?.allowPrivateHost) === true; diff --git a/tests/credential-model.test.mjs b/tests/credential-model.test.mjs index dc3cc136..2834ffbf 100644 --- a/tests/credential-model.test.mjs +++ b/tests/credential-model.test.mjs @@ -34,7 +34,7 @@ test('commands and snippets follow what the server can actually do for each cred }); test('draft validation mirrors saved path and authentication restrictions with field errors', () => { - const valid = S.draftOf(api), secrets = { token: 'fixture' }; + const valid = S.draftOf(api), secrets = { token: 'fixture-value' }; assert.deepEqual(plain(S.validate(valid, { secrets })), {}); assert.ok(S.validate({ ...valid, id: 'Bad ID' }, { secrets }).id); assert.match(S.validate({ ...valid, id: 'new' }, { secrets }).id, /reserved/); @@ -53,6 +53,10 @@ test('the page and the server agree about what a credential may hold', () => { const page = (input, secrets) => Object.keys(S.validate({ ...S.draftOf(input), id: input.id }, { secrets })).length === 0; const cases = [ [{ id: 'fine', type: 'token', fields: {} }, { token: 'fixture-value' }, true], + [{ id: 'short', type: 'token', fields: {} }, { token: 'abc' }, false], + [{ id: 'seven', type: 'token', fields: {} }, { token: '1234567' }, false], + [{ id: 'eight', type: 'token', fields: {} }, { token: '12345678' }, true], + [{ id: 'short-file', type: 'file', fields: {} }, { content: 'tiny' }, false], [{ id: 'bad-env', type: 'token', fields: {}, env: { token: 'PATH' } }, { token: 'fixture-value' }, false], [{ id: 'box', type: 'ssh', fields: { host: '2001:db8::1', username: 'deploy', auth: 'password' } }, { password: 'fixture-value' }, true], [{ id: 'box', type: 'ssh', fields: { host: '-oProxyCommand=bad', username: 'deploy', auth: 'password' } }, { password: 'fixture-value' }, false], @@ -103,6 +107,8 @@ test('drafts and payloads hold only what each type needs, and a saved secret is assert.match(S.validate({ ...custom, vars: [{ name: 'PATH', value: 'x', secret: true }] }).vars, /letters, numbers/); assert.match(S.validate({ ...custom, vars: [{ name: 'SAME', value: 'x' }, { name: 'SAME', value: 'y' }] }).vars, /twice/); assert.match(S.validate({ ...custom, vars: [{ name: 'EMPTY', value: '', secret: true }] }).vars, /Enter a value/); + assert.match(S.validate({ ...custom, vars: [{ name: 'API_KEY', value: 'abc', secret: true }] }).vars, /at least 8/); + assert.throws(() => normalize({ id: 'mixed', type: 'custom', secretNames: ['API_KEY'], secrets: { API_KEY: 'abc' } }), { status: 400, extra: { field: 'API_KEY' } }); assert.deepEqual(plain(S.givesPreview(S.draftOf({ id: 'app-db', type: 'database', fields: { engine: 'postgres', host: 'x' } }))), [{ name: 'DATABASE_URL' }, { name: 'PGPASSWORD' }]); assert.deepEqual(plain(S.givesPreview({ ...S.defaults('kubernetes'), id: 'kube' })), [{ name: 'KUBECONFIG', file: true }]); assert.deepEqual(plain(S.givesPreview({ ...S.defaults('custom'), vars: [{ name: 'ONE' }, { name: '' }] })), [{ name: 'ONE' }]); diff --git a/tests/credentials-run.test.mjs b/tests/credentials-run.test.mjs index 966296a9..dda4874a 100644 --- a/tests/credentials-run.test.mjs +++ b/tests/credentials-run.test.mjs @@ -178,7 +178,7 @@ test('temporary files exist only during the command, at mode 600 in a 700 folder test('temporary files go even when the command fails or is killed', async () => { for (const ending of ['process.exit(3)', `process.kill(process.pid, 'SIGKILL'); setTimeout(() => {}, 5000);`]) { - const { out } = await run([normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture' } })], script(`require('node:fs').writeSync(1, process.env.CERT_FILE); ${ending}`)); + const { out } = await run([normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture-content' } })], script(`require('node:fs').writeSync(1, process.env.CERT_FILE); ${ending}`)); await assert.rejects(fs.stat(out), { code: 'ENOENT' }); } }); @@ -208,9 +208,9 @@ test('two credentials claiming one variable is an error, before anything starts' }); assert.equal(started, false); // A file variable counts too. - const file = id => normalize({ id, type: 'file', env: { content: 'SHARED_FILE' }, secrets: { content: 'fixture' } }); + const file = id => normalize({ id, type: 'file', env: { content: 'SHARED_FILE' }, secrets: { content: 'fixture-content' } }); await assert.rejects(run([file('one'), file('two')], ['/bin/true']), /both set SHARED_FILE/); - await assert.rejects(run([shared('one'), normalize({ id: 'two', type: 'file', env: { content: 'SHARED_TOKEN' }, secrets: { content: 'fixture' } })], ['/bin/true']), /both set SHARED_TOKEN/); + await assert.rejects(run([shared('one'), normalize({ id: 'two', type: 'file', env: { content: 'SHARED_TOKEN' }, secrets: { content: 'fixture-content' } })], ['/bin/true']), /both set SHARED_TOKEN/); }); test('NAME=VAR gives the main secret another name, which also settles a clash', async () => { @@ -219,7 +219,7 @@ test('NAME=VAR gives the main secret another name, which also settles a clash', const shared = (id, value) => credential(id, value, { env: { token: 'SHARED_TOKEN' } }); const { code, out } = await run([shared('one', token + '-one'), { credential: shared('two', token + '-two'), rename: 'OTHER_TOKEN' }], script(`process.stdout.write(JSON.stringify([process.env.SHARED_TOKEN.endsWith('-one'), process.env.OTHER_TOKEN.endsWith('-two')]))`)); assert.equal(code, 0); assert.deepEqual(JSON.parse(out), [true, true]); - const file = normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture' } }); + const file = normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture-content' } }); assert.equal((await run([{ credential: file, rename: 'MY_CERT' }], script(`process.stdout.write(String(require('node:fs').existsSync(process.env.MY_CERT)))`))).out, 'true'); }); diff --git a/tests/credentials.test.mjs b/tests/credentials.test.mjs index 72535fcc..135413af 100644 --- a/tests/credentials.test.mjs +++ b/tests/credentials.test.mjs @@ -119,6 +119,13 @@ test('normalizing keeps only what a type holds and treats a pasted value the way assert.deepEqual([edited.fields, edited.secrets], [{ REGION: 'us' }, { KEEP: secret }]); }); +test('secrets too short to mask are refused instead of stored in the clear', () => { + for (const secrets of [{ token: 'abc' }, { token: '1234567' }]) assert.throws(() => normalize({ id: 'tiny', type: 'token', secrets }), { status: 400, extra: { field: 'token' } }); + assert.throws(() => normalize({ id: 'tiny', type: 'file', secrets: { content: 'tiny\n' } }), { status: 400, extra: { field: 'content' } }); + assert.equal(normalize({ id: 'tiny', type: 'token', secrets: { token: '12345678' } }).secrets.token, '12345678', 'eight characters is enough to mask'); + assert.throws(() => normalize({ id: 'tiny', type: 'token', secrets: {} }, { id: 'tiny', type: 'token', secrets: { token: 'abc' } }), { status: 400 }, 'a kept value is re-checked like any other'); +}); + test('an omitted or blank optional secret is kept, an explicit null removes it', () => { const aws = secrets => ({ id: 'cloud', type: 'aws', fields: { accessKeyId: 'AKIAFIXTURE' }, secrets }); const previous = normalize(aws({ secretAccessKey: 'fixture-secret', sessionToken: 'fixture-session' })); diff --git a/tests/gateways-service.test.mjs b/tests/gateways-service.test.mjs index 037c2f3e..8b98eaf4 100644 --- a/tests/gateways-service.test.mjs +++ b/tests/gateways-service.test.mjs @@ -91,7 +91,7 @@ async function harness(t, options = {}) { const service = createGatewayService({ baseDir: path.join(root, 'state'), home, env: {}, io: options.io ?? fs, t3, vault, processes, lookup: options.lookup, nativeCodexModels: options.nativeCodexModels ?? (async () => nativeModels), wait: options.wait ?? (() => Promise.resolve()), ...(options.now ? { now: options.now } : {}), ...(options.getHarness ? { getHarness: options.getHarness } : {}) }); return { service, t3, vault, home, root, stopped, processes, gateway: await gatewayServer(t, options.gatewayModels) }; } -const add = (h, agents = ['claude', 'codex', 'opencode'], over = {}) => h.service.create({ allowPrivateHost: true, gateway: { name: 'My Proxy', preset: 'cliproxyapi', url: h.gateway.url, agents, ...over }, key: 'sk-one' }); +const add = (h, agents = ['claude', 'codex', 'opencode'], over = {}) => h.service.create({ allowPrivateHost: true, gateway: { name: 'My Proxy', preset: 'cliproxyapi', url: h.gateway.url, agents, ...over }, key: 'sk-first-key' }); const turn = async (h, id, agent, connected, more = {}) => { const { revision } = await h.service.status(); return h.service.setAgent({ id, agent, connected, revision, ...more }); }; const settled = async h => { if (!h) return new Promise(resolve => setTimeout(resolve, 30)); @@ -148,10 +148,10 @@ test('Pi and Grok lifecycle stays scoped through reapply, rotation, model change const dir = path.join(h.root, 'state/setup/gateways/agents'), piFile = path.join(dir, `${id}.pi.mjs`), grokHome = path.join(dir, `${id}.grok`); assert.match(h.t3.state.instances[`pi_gw_${id}`].config.launchArgs, /^--offline --extension /); assert.equal(h.t3.state.instances[`grok_gw_${id}`].config.binaryPath, '/launchers/grok'); - assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-one'); - assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-one'); + assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-first-key'); + assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-first-key'); for (const file of [piFile, path.join(grokHome, 'config.toml')]) { - assert.doesNotMatch(await fs.readFile(file, 'utf8'), /sk-one/); + assert.doesNotMatch(await fs.readFile(file, 'utf8'), /sk-first-key/); assert.equal((await fs.stat(file)).mode & 0o777, 0o600); } assert.equal((await fs.stat(grokHome)).mode & 0o777, 0o700); @@ -160,11 +160,11 @@ test('Pi and Grok lifecycle stays scoped through reapply, rotation, model change assert.ok((await h.service.reapply({ id, revision: view.revision })).results.every(row => row.action === 'none')); assert.deepEqual(h.t3.writes, []); assert.deepEqual(h.stopped, []); - await h.vault.replace(`${id}-gateway`, 'sk-two'); + await h.vault.replace(`${id}-gateway`, 'sk-second-key'); view = await h.service.status(); await h.service.reapply({ id, revision: view.revision }); - assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-two'); - assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-two'); + assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-second-key'); + assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-second-key'); assert.deepEqual(h.stopped.sort(), [`grok_gw_${id}`, `pi_gw_${id}`]); await settled(); h.gateway.state.models.push('grok-new'); @@ -272,17 +272,17 @@ test('CLIProxyAPI catalog discovery feeds every supported gateway adapter', asyn test('a typed key goes to the Vault and each instance’s secret, and to nowhere else', async t => { const h = await harness(t), { id } = await add(h); - assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-one'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one'); - assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-one'); + assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-first-key'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key'); + assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-first-key'); const keyFile = path.join(h.root, `state/setup/gateways/agents/${id}.key`); - assert.equal(await fs.readFile(keyFile, 'utf8'), 'sk-one'); + assert.equal(await fs.readFile(keyFile, 'utf8'), 'sk-first-key'); assert.equal((await fs.stat(keyFile)).mode & 0o777, 0o600); - assert.doesNotMatch(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.json`), 'utf8'), /sk-one/); + assert.doesNotMatch(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.json`), 'utf8'), /sk-first-key/); const file = await fs.readFile(path.join(h.root, 'state/setup/gateways/gateways.json'), 'utf8'); - assert.doesNotMatch(file, /sk-one/); + assert.doesNotMatch(file, /sk-first-key/); assert.equal((await fs.stat(path.join(h.root, 'state/setup/gateways/gateways.json'))).mode & 0o777, 0o600); - assert.doesNotMatch(JSON.stringify(await h.service.status()), /sk-one/); + assert.doesNotMatch(JSON.stringify(await h.service.status()), /sk-first-key/); }); test('a saved Vault key can be chosen instead of typing one', async t => { @@ -336,7 +336,7 @@ test('an instance still starting reads as starting, whatever T3’s placeholder h.t3.read = async () => ({ ...await read(), providers: Object.keys(h.t3.state.instances).filter(id => id.includes('_gw_')).map(instanceId => ({ instanceId, status: 'warning', installed: false, checkedAt: '2000-01-01T00:00:00.000Z' })) }); h.waitFor = new Promise(resolve => { release = resolve; }); const service = createGatewayService({ baseDir: path.join(h.root, 'state2'), home: h.home, env: {}, io: fs, t3: h.t3, vault: h.vault, processes: h.processes, wait: () => h.waitFor }); - const { gateways } = await service.create({ allowPrivateHost: true, gateway: { name: 'Slow', url: h.gateway.url, agents: ['claude'] }, key: 'sk-one' }); + const { gateways } = await service.create({ allowPrivateHost: true, gateway: { name: 'Slow', url: h.gateway.url, agents: ['claude'] }, key: 'sk-first-key' }); assert.equal(gateways[0].agents[0].state, 'starting'); assert.equal(gateways[0].settling, true); h.t3.read = read; @@ -347,17 +347,17 @@ test('adding an agent later reads the key from the Vault', async t => { const h = await harness(t), { id } = await add(h, ['claude']); await settled(); await turn(h, id, 'codex', true); - assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-one'); + assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-first-key'); }); test('rotating the key here reaches every connected instance', async t => { const h = await harness(t), { id } = await add(h); await settled(); const { revision } = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-two' }); - assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-two'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-two'); - assert.equal(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.key`), 'utf8'), 'sk-two'); + await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-second-key' }); + assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-second-key'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-second-key'); + assert.equal(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.key`), 'utf8'), 'sk-second-key'); }); test('a process that outlives its instance is retired when it holds something old', async t => { @@ -366,7 +366,7 @@ test('a process that outlives its instance is retired when it holds something ol h.stopped.length = 0; const all = ['claudeAgent_gw_my-proxy', 'codex_gw_my-proxy', 'opencode_gw_my-proxy']; let view = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision: view.revision, gateway: {}, key: 'sk-two' }); + await h.service.save({ allowPrivateHost: true, id, revision: view.revision, gateway: {}, key: 'sk-second-key' }); assert.deepEqual(h.stopped.sort(), all, 'a new key'); h.stopped.length = 0; view = await h.service.status(); @@ -405,7 +405,7 @@ test('processes are stopped before the write, because T3 checks the new instance h.processes.stop = async (...args) => { order.push('stop'); return stop(...args); }; h.t3.upsert = async (...args) => { order.push('write'); return upsert(...args); }; const { revision } = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-two' }); + await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-second-key' }); assert.deepEqual(order, ['stop', 'write']); }); @@ -493,7 +493,7 @@ test('an owned setting changed in T3 Code is flagged, and additions are kept whe const restored = h.t3.state.instances['claudeAgent_gw_my-proxy'].environment; assert.equal(restored.find(row => row.name === 'ANTHROPIC_BASE_URL').value, h.gateway.url); assert.equal(restored.find(row => row.name === 'MY_FLAG').value, '1'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one', 'the saved secret survives'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key', 'the saved secret survives'); }); test('renaming an instance in T3 Code is not drift, and survives a later apply', async t => { @@ -800,11 +800,11 @@ test('names become unique ids, and each gateway keeps its own instances', async const h = await harness(t); const first = await add(h, ['codex'], { name: 'Proxy!' }); await settled(); - const second = await h.service.create({ allowPrivateHost: true, gateway: { name: 'proxy', url: h.gateway.url, agents: ['codex'] }, key: 'sk-two' }); + const second = await h.service.create({ allowPrivateHost: true, gateway: { name: 'proxy', url: h.gateway.url, agents: ['codex'] }, key: 'sk-second-key' }); assert.equal(first.id, 'proxy'); assert.equal(second.id, 'proxy-2'); - assert.equal(h.t3.secrets.get('codex_gw_proxy/T3_GATEWAY_KEY'), 'sk-one'); - assert.equal(h.t3.secrets.get('codex_gw_proxy-2/T3_GATEWAY_KEY'), 'sk-two'); + assert.equal(h.t3.secrets.get('codex_gw_proxy/T3_GATEWAY_KEY'), 'sk-first-key'); + assert.equal(h.t3.secrets.get('codex_gw_proxy-2/T3_GATEWAY_KEY'), 'sk-second-key'); }); test('Claude’s settings file is reported with no values, and cleared key by key', async t => { @@ -841,15 +841,17 @@ test('bad input is refused with a sentence and the field it belongs to', async t assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'a'.repeat(41), url: h.gateway.url, agents: [] } }))).field, 'name'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: ['nobody'] } }))).field, 'agents'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [] }, key: 'has space' }))).field, 'key'); + assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [] }, key: 'short' }))).field, 'key', 'a key too short to mask is refused'); + assert.equal((await failure(h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try-key', managementKey: 'short' }))).field, 'managementKey'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [], manual: ['bad id'] } }))).field, 'manual'); }); test('testing an unsaved address changes nothing', async t => { const h = await harness(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: `${h.gateway.url}/v1`, key: 'sk-try' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: `${h.gateway.url}/v1`, key: 'sk-try-key' }); assert.equal(result.ok, true); assert.equal(h.gateway.state.keys[0], undefined, 'The keyless connectivity check runs first'); - assert.ok(h.gateway.state.keys.includes('Bearer sk-try'), 'The key goes out only after the host answers'); + assert.ok(h.gateway.state.keys.includes('Bearer sk-try-key'), 'The key goes out only after the host answers'); assert.deepEqual(result.offers, { claude: 1, codex: 1, opencode: 2, pi: 2, grok: 0 }); assert.equal((await h.service.status()).gateways.length, 0); assert.equal(h.vault.rows.size, 0); @@ -858,20 +860,20 @@ test('testing an unsaved address changes nothing', async t => { test('a test needs a private-host confirm before any key goes out', async t => { const h = await harness(t); const before = h.gateway.state.keys.length; - const denied = await h.service.test({ url: h.gateway.url, key: 'sk-try' }); + const denied = await h.service.test({ url: h.gateway.url, key: 'sk-try-key' }); assert.equal(denied.result.code, 'private-host'); assert.match(denied.result.message, /Confirm that you trust/); assert.equal(denied.result.confirmHost, '127.0.0.1'); assert.equal(h.gateway.state.keys.length, before, 'Zero requests, keyed or not, without the confirm'); - const allowed = await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try' }); + const allowed = await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try-key' }); assert.equal(allowed.result.ok, true); - assert.ok(h.gateway.state.keys.slice(before).includes('Bearer sk-try'), 'The key goes out only after the confirm'); + assert.ok(h.gateway.state.keys.slice(before).includes('Bearer sk-try-key'), 'The key goes out only after the confirm'); assert.equal(h.gateway.state.keys[before], undefined, 'Even then the keyless check runs first'); }); test('connecting reuses the test just shown instead of probing again', async t => { const h = await harness(t); - await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-one' }); + await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-first-key' }); const seen = h.gateway.state.keys.length; await add(h, ['claude']); assert.equal(h.gateway.state.keys.length, seen); @@ -962,7 +964,7 @@ test('Restore puts back a key row someone deleted in T3 Code', async t => { assert.equal(view.gateways[0].agents[0].state, 'drift'); const { results } = await h.service.reapply({ id, revision: view.revision }); assert.deepEqual(results.map(result => result.ok), [true]); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key'); }); test('an agent can be turned off even when its instance has lost its key row', async t => { @@ -1048,7 +1050,7 @@ test('ids stay valid for T3 and the Vault, and never collide with the Add sheet' assert.notEqual(first.id, 'new'); const long = 'a'.repeat(27) + ' b ' + 'c'.repeat(8); await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] } }); - const second = await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] }, key: 'sk-x' }); + const second = await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] }, key: 'sk-extra-key' }); assert.match(second.id, /^[a-z0-9]+(?:-[a-z0-9]+)*$/); }); @@ -1076,7 +1078,7 @@ async function hubServer(t, { accounts = [{ provider: 'claude' }, { provider: 'c t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); })); return { state, url: `http://127.0.0.1:${server.address().port}` }; } -const createInput = (hub, extra = {}) => ({ allowPrivateHost: true, gateway: { name: 'Hub', preset: 'cliproxyapi', url: hub.url, agents: ['claude'] }, key: 'sk-api', ...extra }); +const createInput = (hub, extra = {}) => ({ allowPrivateHost: true, gateway: { name: 'Hub', preset: 'cliproxyapi', url: hub.url, agents: ['claude'] }, key: 'sk-hub-key', ...extra }); test('adding a management key gives T3 a verified hub, and the key stays out of the store', async t => { const h = await harness(t), hub = await hubServer(t); @@ -1095,7 +1097,7 @@ test('adding a management key gives T3 a verified hub, and the key stays out of test('a wrong management key adds nothing, costs one attempt, and never reaches T3', async t => { const h = await harness(t), hub = await hubServer(t); - await assert.rejects(h.service.create(createInput(hub, { managementKey: 'wrong' })), error => error.code === 'limits' && error.extra.field === 'managementKey' && /not the hash/.test(error.extra.hint) && /30 minutes/.test(error.extra.hint)); + await assert.rejects(h.service.create(createInput(hub, { managementKey: 'wrong-management-key' })), error => error.code === 'limits' && error.extra.field === 'managementKey' && /not the hash/.test(error.extra.hint) && /30 minutes/.test(error.extra.hint)); assert.equal(hub.state.attempts, 1, 'Five of these lock the server out'); assert.deepEqual(h.t3.hubWrites, [], 'T3 polls with whatever it holds, so a bad key would spend the rest'); assert.deepEqual(h.t3.writes, [], 'And the gateway isn’t half added'); @@ -1104,7 +1106,7 @@ test('a wrong management key adds nothing, costs one attempt, and never reaches test('a management key is sent only when asked, so a plain test or health check costs nothing', async t => { const h = await harness(t), hub = await hubServer(t); - await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-api' }); + await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-hub-key' }); const { id } = await h.service.create(createInput(hub)); await h.service.test({ id }); await h.service.status(); @@ -1115,7 +1117,7 @@ test('a management key is sent only when asked, so a plain test or health check test('a key shown as good isn’t spent again when the gateway is added', async t => { const h = await harness(t), hub = await hubServer(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-api', managementKey: 'the-management-key' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-hub-key', managementKey: 'the-management-key' }); assert.deepEqual(result.limits, { ok: true, code: 'ok', accounts: { claude: 1, codex: 1 } }, 'Disabled accounts don’t count, and only what Limits can show does'); assert.ok(!JSON.stringify(result).includes('never-read'), 'The list can hold credentials, and none of it leaves'); await h.service.create(createInput(hub, { managementKey: 'the-management-key' })); @@ -1124,7 +1126,7 @@ test('a key shown as good isn’t spent again when the gateway is added', async test('a gateway can’t vouch for a management key when it didn’t answer', async t => { const h = await harness(t), hub = await hubServer(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: 'http://127.0.0.1:1', key: 'sk-api', managementKey: 'the-management-key' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: 'http://127.0.0.1:1', key: 'sk-hub-key', managementKey: 'the-management-key' }); assert.equal(result.ok, false); assert.equal(result.limits, undefined); assert.equal(hub.state.attempts, 0); }); @@ -1291,7 +1293,7 @@ test('limits report what T3 sees, and only for a gateway that can have them', as h.t3.reports = [{ id: sourceId, kind: 'cliproxy', label: 'Hub', checkedAt: '2026-11-06T00:00:00Z', accounts: [], error: 'The hub rejected the key.' }]; await h.service.setLimits({ id, revision: (await h.service.status()).revision, managementKey: 'the-management-key' }); assert.equal((await h.service.status()).gateways[0].limits.state, 'error'); - const litellm = await h.service.create({ allowPrivateHost: true, gateway: { name: 'Other', preset: 'litellm', url: hub.url, agents: [] }, key: 'sk-api' }); + const litellm = await h.service.create({ allowPrivateHost: true, gateway: { name: 'Other', preset: 'litellm', url: hub.url, agents: [] }, key: 'sk-hub-key' }); assert.equal(litellm.gateways.find(row => row.id !== id).limits.supported, false); }); diff --git a/tests/services.test.mjs b/tests/services.test.mjs index e5920235..7bbfde2c 100644 --- a/tests/services.test.mjs +++ b/tests/services.test.mjs @@ -169,6 +169,17 @@ test("logs redact split secrets, remain bounded and separate stdout from stderr" const dir = path.join(f.root, "logs/preview"); for (const file of await fs.readdir(dir)) assert.ok((await fs.stat(path.join(dir, file))).size <= 256 * 1024); }); +test("logs hide the encoded forms of a secret, even split across writes", async t => { + const f = await fixture(t), secret = "p@ss w0rd/val+ue"; + const writer = logWriter(f.root, "preview", "run", "stdout", [secret]); + const base64 = Buffer.from(secret).toString("base64"), url = encodeURIComponent(secret); + const form = new URLSearchParams([["", secret]]).toString().slice(1); + writer.write(Buffer.from("a " + base64.slice(0, 10))); writer.write(Buffer.from(base64.slice(10) + " b " + url.slice(0, 8))); + writer.write(Buffer.from(url.slice(8) + " c " + form + " d " + secret + "\n")); writer.end(); + const text = readLogs(f.root, "preview").entries.map(entry => entry.text).join("\n"); + for (const value of [secret, base64, url, form]) assert.ok(!text.includes(value), value); + assert.equal(text.match(/\[redacted\]/g).length, 4); +}); test("disabled registrations retain exact tool and Vault dependency references", async t => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "t3-services-refs-")); t.after(() => fs.rm(root, { recursive: true, force: true })); From 082a385b898b5a8f7589a3ab1ab6107ad6c66520 Mon Sep 17 00:00:00 2001 From: Ziyang Zeng Date: Sat, 14 Nov 2026 14:40:57 -0800 Subject: [PATCH 2/2] fix: preserve Vault compatibility and Services image imports --- docker/services/logs.mjs | 9 ++- docker/services/redact.mjs | 6 ++ docker/setup/client/credential-model.js | 3 - docker/setup/credentials/model.mjs | 4 -- docker/setup/credentials/redact.mjs | 6 +- docker/setup/gateways.mjs | 3 - tests/credential-model.test.mjs | 8 +-- tests/credentials-run.test.mjs | 8 +-- tests/credentials.test.mjs | 7 --- tests/gateways-service.test.mjs | 82 ++++++++++++------------- tests/services-redaction.test.mjs | 40 ++++++++++++ 11 files changed, 100 insertions(+), 76 deletions(-) create mode 100644 docker/services/redact.mjs create mode 100644 tests/services-redaction.test.mjs diff --git a/docker/services/logs.mjs b/docker/services/logs.mjs index 9f132531..2d07010d 100644 --- a/docker/services/logs.mjs +++ b/docker/services/logs.mjs @@ -2,7 +2,7 @@ import { mkdirSync, statSync, renameSync, writeFileSync, appendFileSync, readFil import path from "node:path"; import { StringDecoder } from "node:string_decoder"; import { ServiceError } from './store.mjs'; -import { maskValues } from "../setup/credentials/redact.mjs"; +import { maskValues } from './redact.mjs'; const LIMIT = 256 * 1024; export function logWriter(root, id, runId, stream, masks = []) { const dir = path.join(root, "logs", id); @@ -11,8 +11,11 @@ export function logWriter(root, id, runId, stream, masks = []) { let buffered = "", pending = '', sequence = 0; // The same encoded forms the Vault hides in command output: a service that // prints `echo $TOKEN | base64` must not leave the value in its log. - const values = maskValues(masks); - const redact = value => values.reduce((result, mask) => result.split(mask).join("[redacted]"), value); + // Preserve the old literal masking of short service passwords too. The + // external service owns its password policy; saved values must stay usable. + const values = maskValues(masks, { minimumLength: 1 }); + const pattern = values.length ? new RegExp(values.map(value => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g') : null; + const redact = value => pattern ? value.replace(pattern, '[redacted]') : value; const line = text => { const record = JSON.stringify({ at: Date.now(), runId, stream, sequence: ++sequence, text: text.slice(0, 8192) }) + "\n"; try { diff --git a/docker/services/redact.mjs b/docker/services/redact.mjs new file mode 100644 index 00000000..fa31adcb --- /dev/null +++ b/docker/services/redact.mjs @@ -0,0 +1,6 @@ +// Services and Setup ship in sibling /opt/t3-* directories in the image. +// Share the Vault's variants while also supporting the source-tree layout. +import { existsSync } from 'node:fs'; +const source = new URL('../setup/credentials/redact.mjs', import.meta.url); +const installed = new URL('../t3-setup/credentials/redact.mjs', import.meta.url); +export const { maskValues } = await import(existsSync(source) ? source.href : installed.href); diff --git a/docker/setup/client/credential-model.js b/docker/setup/client/credential-model.js index 61d0baab..1c052c08 100644 --- a/docker/setup/client/credential-model.js +++ b/docker/setup/client/credential-model.js @@ -161,7 +161,6 @@ const T3CredentialModel = (() => { if (!validEnvName(row.name)) errors.vars = (row.name || 'Each variable') + ' needs a name of letters, numbers and underscores.'; else if (seen.has(row.name)) errors.vars = row.name + ' is listed twice.'; else if (row.secret && !row.value && !row.saved) errors.vars = 'Enter a value for ' + row.name + '.'; - else if (row.secret && row.value && row.value.trim().length < 8) errors.vars = 'Use at least 8 characters for ' + row.name + '.'; seen.add(row.name); } return errors; @@ -175,9 +174,7 @@ const T3CredentialModel = (() => { if (!applies(entry, draft.fields) || entry.optional) continue; const value = secrets[entry.key]; if (!value && !savedSecrets.includes(entry.key)) errors['secrets.' + entry.key] = 'Enter ' + entry.label.toLowerCase() + '.'; - else if (value && !entry.multiline && value.replace(/\r?\n$/, '').length < 8) errors['secrets.' + entry.key] = 'Use at least 8 characters for ' + entry.label.toLowerCase() + '.'; else if (value && !entry.multiline && !text(value.replace(/\r?\n$/, ''), 8192)) errors['secrets.' + entry.key] = 'Use up to 8192 characters, without line breaks.'; - else if (value && entry.multiline && value.trim().length < 8) errors['secrets.' + entry.key] = 'Use at least 8 characters for ' + entry.label.toLowerCase() + '.'; else if (value && entry.multiline && value.length > 48 * 1024) errors['secrets.' + entry.key] = 'Use up to 48 KiB.'; } for (const entry of [...type.fields, ...type.secrets]) { diff --git a/docker/setup/credentials/model.mjs b/docker/setup/credentials/model.mjs index 7f2c81d9..594f5451 100644 --- a/docker/setup/credentials/model.mjs +++ b/docker/setup/credentials/model.mjs @@ -5,8 +5,6 @@ import { TYPES, applies, isType, withDefaults } from './types.mjs'; const fail = (message, field) => { throw new CredentialError(message, 400, 'invalid', field ? { field } : {}); }; const MULTILINE_MAX = 48 * 1024; const SECRET_MAX = 8192; -// Short values can't be masked without hiding ordinary text (see redact.mjs), -// so nothing this short is accepted as a secret in the first place. const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/; // A saved value under these names would break the command, or change what it runs. const RESERVED_ENV = /^(?:PATH|HOME|USER|SHELL|PWD|IFS|LD_.*|DYLD_.*|NODE_OPTIONS|BASH_ENV|ENV)$/; @@ -66,13 +64,11 @@ function normalizeSecret(entry, raw, previous) { if (value === undefined) { if (entry.optional) return undefined; fail(`Enter ${entry.label.toLowerCase()}.`, entry.key); } if (typeof value !== 'string' || !value.isWellFormed()) fail(`Enter ${entry.label.toLowerCase()} as plain text.`, entry.key); if (entry.multiline) { - if (value.trim().length < 8) fail(`Use at least 8 characters for ${entry.label.toLowerCase()}.`, entry.key); if (value.length > MULTILINE_MAX || value.includes('\0') || !value.trim()) fail(`Enter ${entry.label.toLowerCase()} as text of up to 48 KiB.`, entry.key); return value; } // A pasted value or a file often ends in a line break that isn't part of it. const line = value.replace(/\r?\n$/, ''); - if (line.length < 8) fail(`Use at least 8 characters for ${entry.label.toLowerCase()}.`, entry.key); if (!text(line, SECRET_MAX)) fail(`Enter ${entry.label.toLowerCase()}, up to ${SECRET_MAX} characters without line breaks.`, entry.key); return line; } diff --git a/docker/setup/credentials/redact.mjs b/docker/setup/credentials/redact.mjs index 0ddcb0cc..738c655c 100644 --- a/docker/setup/credentials/redact.mjs +++ b/docker/setup/credentials/redact.mjs @@ -6,10 +6,10 @@ const MIN_LINE = 16; // The forms a value commonly takes in output. A file's long lines count as well, // so printing part of a private key is caught too. -export function maskValues(secrets) { +export function maskValues(secrets, { minimumLength = MIN_LENGTH } = {}) { const values = new Set(); for (const secret of [secrets].flat()) { - if (typeof secret !== 'string' || secret.length < MIN_LENGTH) continue; + if (typeof secret !== 'string' || secret.length < minimumLength) continue; values.add(secret); values.add(secret.trim()); values.add(JSON.stringify(secret).slice(1, -1)); values.add(encodeURIComponent(secret)); @@ -17,7 +17,7 @@ export function maskValues(secrets) { values.add(Buffer.from(secret).toString('base64')); if (secret.includes('\n')) for (const line of secret.split(/\r?\n/)) if (line.trim().length >= MIN_LINE) values.add(line.trim()); } - return [...values].filter(value => value.length >= MIN_LENGTH).sort((a, b) => b.length - a.length); + return [...values].filter(value => value.length >= minimumLength).sort((a, b) => b.length - a.length); } function filterValues(values, write) { diff --git a/docker/setup/gateways.mjs b/docker/setup/gateways.mjs index 16998f01..d2824de7 100644 --- a/docker/setup/gateways.mjs +++ b/docker/setup/gateways.mjs @@ -63,15 +63,12 @@ const cleanKey = value => { if (value === undefined || value === null) return undefined; const key = String(value).trim(); if (key.length > 4096 || /[^\x21-\x7e]/.test(key)) throw new GatewayError('An API key is one line of plain characters, without spaces.', 400, 'invalid', { field: 'key' }); - // Short keys can't be masked in output without hiding ordinary text. - if (key && key.length < 8) throw new GatewayError('An API key is at least 8 characters of plain text, without spaces.', 400, 'invalid', { field: 'key' }); return key; }; const cleanManagementKey = value => { if (value === undefined || value === null || value === '') return undefined; const key = String(value).trim(); if (!key || key.length > 4096 || /[^\x21-\x7e]/.test(key)) throw new GatewayError('A management key is one line of plain characters, without spaces.', 400, 'invalid', { field: 'managementKey' }); - if (key.length < 8) throw new GatewayError('A management key is at least 8 characters of plain text, without spaces.', 400, 'invalid', { field: 'managementKey' }); return key; }; const allowPrivate = input => (input?.allowPrivateHost ?? input?.gateway?.allowPrivateHost) === true; diff --git a/tests/credential-model.test.mjs b/tests/credential-model.test.mjs index 2834ffbf..dc3cc136 100644 --- a/tests/credential-model.test.mjs +++ b/tests/credential-model.test.mjs @@ -34,7 +34,7 @@ test('commands and snippets follow what the server can actually do for each cred }); test('draft validation mirrors saved path and authentication restrictions with field errors', () => { - const valid = S.draftOf(api), secrets = { token: 'fixture-value' }; + const valid = S.draftOf(api), secrets = { token: 'fixture' }; assert.deepEqual(plain(S.validate(valid, { secrets })), {}); assert.ok(S.validate({ ...valid, id: 'Bad ID' }, { secrets }).id); assert.match(S.validate({ ...valid, id: 'new' }, { secrets }).id, /reserved/); @@ -53,10 +53,6 @@ test('the page and the server agree about what a credential may hold', () => { const page = (input, secrets) => Object.keys(S.validate({ ...S.draftOf(input), id: input.id }, { secrets })).length === 0; const cases = [ [{ id: 'fine', type: 'token', fields: {} }, { token: 'fixture-value' }, true], - [{ id: 'short', type: 'token', fields: {} }, { token: 'abc' }, false], - [{ id: 'seven', type: 'token', fields: {} }, { token: '1234567' }, false], - [{ id: 'eight', type: 'token', fields: {} }, { token: '12345678' }, true], - [{ id: 'short-file', type: 'file', fields: {} }, { content: 'tiny' }, false], [{ id: 'bad-env', type: 'token', fields: {}, env: { token: 'PATH' } }, { token: 'fixture-value' }, false], [{ id: 'box', type: 'ssh', fields: { host: '2001:db8::1', username: 'deploy', auth: 'password' } }, { password: 'fixture-value' }, true], [{ id: 'box', type: 'ssh', fields: { host: '-oProxyCommand=bad', username: 'deploy', auth: 'password' } }, { password: 'fixture-value' }, false], @@ -107,8 +103,6 @@ test('drafts and payloads hold only what each type needs, and a saved secret is assert.match(S.validate({ ...custom, vars: [{ name: 'PATH', value: 'x', secret: true }] }).vars, /letters, numbers/); assert.match(S.validate({ ...custom, vars: [{ name: 'SAME', value: 'x' }, { name: 'SAME', value: 'y' }] }).vars, /twice/); assert.match(S.validate({ ...custom, vars: [{ name: 'EMPTY', value: '', secret: true }] }).vars, /Enter a value/); - assert.match(S.validate({ ...custom, vars: [{ name: 'API_KEY', value: 'abc', secret: true }] }).vars, /at least 8/); - assert.throws(() => normalize({ id: 'mixed', type: 'custom', secretNames: ['API_KEY'], secrets: { API_KEY: 'abc' } }), { status: 400, extra: { field: 'API_KEY' } }); assert.deepEqual(plain(S.givesPreview(S.draftOf({ id: 'app-db', type: 'database', fields: { engine: 'postgres', host: 'x' } }))), [{ name: 'DATABASE_URL' }, { name: 'PGPASSWORD' }]); assert.deepEqual(plain(S.givesPreview({ ...S.defaults('kubernetes'), id: 'kube' })), [{ name: 'KUBECONFIG', file: true }]); assert.deepEqual(plain(S.givesPreview({ ...S.defaults('custom'), vars: [{ name: 'ONE' }, { name: '' }] })), [{ name: 'ONE' }]); diff --git a/tests/credentials-run.test.mjs b/tests/credentials-run.test.mjs index dda4874a..966296a9 100644 --- a/tests/credentials-run.test.mjs +++ b/tests/credentials-run.test.mjs @@ -178,7 +178,7 @@ test('temporary files exist only during the command, at mode 600 in a 700 folder test('temporary files go even when the command fails or is killed', async () => { for (const ending of ['process.exit(3)', `process.kill(process.pid, 'SIGKILL'); setTimeout(() => {}, 5000);`]) { - const { out } = await run([normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture-content' } })], script(`require('node:fs').writeSync(1, process.env.CERT_FILE); ${ending}`)); + const { out } = await run([normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture' } })], script(`require('node:fs').writeSync(1, process.env.CERT_FILE); ${ending}`)); await assert.rejects(fs.stat(out), { code: 'ENOENT' }); } }); @@ -208,9 +208,9 @@ test('two credentials claiming one variable is an error, before anything starts' }); assert.equal(started, false); // A file variable counts too. - const file = id => normalize({ id, type: 'file', env: { content: 'SHARED_FILE' }, secrets: { content: 'fixture-content' } }); + const file = id => normalize({ id, type: 'file', env: { content: 'SHARED_FILE' }, secrets: { content: 'fixture' } }); await assert.rejects(run([file('one'), file('two')], ['/bin/true']), /both set SHARED_FILE/); - await assert.rejects(run([shared('one'), normalize({ id: 'two', type: 'file', env: { content: 'SHARED_TOKEN' }, secrets: { content: 'fixture-content' } })], ['/bin/true']), /both set SHARED_TOKEN/); + await assert.rejects(run([shared('one'), normalize({ id: 'two', type: 'file', env: { content: 'SHARED_TOKEN' }, secrets: { content: 'fixture' } })], ['/bin/true']), /both set SHARED_TOKEN/); }); test('NAME=VAR gives the main secret another name, which also settles a clash', async () => { @@ -219,7 +219,7 @@ test('NAME=VAR gives the main secret another name, which also settles a clash', const shared = (id, value) => credential(id, value, { env: { token: 'SHARED_TOKEN' } }); const { code, out } = await run([shared('one', token + '-one'), { credential: shared('two', token + '-two'), rename: 'OTHER_TOKEN' }], script(`process.stdout.write(JSON.stringify([process.env.SHARED_TOKEN.endsWith('-one'), process.env.OTHER_TOKEN.endsWith('-two')]))`)); assert.equal(code, 0); assert.deepEqual(JSON.parse(out), [true, true]); - const file = normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture-content' } }); + const file = normalize({ id: 'cert', type: 'file', secrets: { content: 'fixture' } }); assert.equal((await run([{ credential: file, rename: 'MY_CERT' }], script(`process.stdout.write(String(require('node:fs').existsSync(process.env.MY_CERT)))`))).out, 'true'); }); diff --git a/tests/credentials.test.mjs b/tests/credentials.test.mjs index 135413af..72535fcc 100644 --- a/tests/credentials.test.mjs +++ b/tests/credentials.test.mjs @@ -119,13 +119,6 @@ test('normalizing keeps only what a type holds and treats a pasted value the way assert.deepEqual([edited.fields, edited.secrets], [{ REGION: 'us' }, { KEEP: secret }]); }); -test('secrets too short to mask are refused instead of stored in the clear', () => { - for (const secrets of [{ token: 'abc' }, { token: '1234567' }]) assert.throws(() => normalize({ id: 'tiny', type: 'token', secrets }), { status: 400, extra: { field: 'token' } }); - assert.throws(() => normalize({ id: 'tiny', type: 'file', secrets: { content: 'tiny\n' } }), { status: 400, extra: { field: 'content' } }); - assert.equal(normalize({ id: 'tiny', type: 'token', secrets: { token: '12345678' } }).secrets.token, '12345678', 'eight characters is enough to mask'); - assert.throws(() => normalize({ id: 'tiny', type: 'token', secrets: {} }, { id: 'tiny', type: 'token', secrets: { token: 'abc' } }), { status: 400 }, 'a kept value is re-checked like any other'); -}); - test('an omitted or blank optional secret is kept, an explicit null removes it', () => { const aws = secrets => ({ id: 'cloud', type: 'aws', fields: { accessKeyId: 'AKIAFIXTURE' }, secrets }); const previous = normalize(aws({ secretAccessKey: 'fixture-secret', sessionToken: 'fixture-session' })); diff --git a/tests/gateways-service.test.mjs b/tests/gateways-service.test.mjs index 8b98eaf4..037c2f3e 100644 --- a/tests/gateways-service.test.mjs +++ b/tests/gateways-service.test.mjs @@ -91,7 +91,7 @@ async function harness(t, options = {}) { const service = createGatewayService({ baseDir: path.join(root, 'state'), home, env: {}, io: options.io ?? fs, t3, vault, processes, lookup: options.lookup, nativeCodexModels: options.nativeCodexModels ?? (async () => nativeModels), wait: options.wait ?? (() => Promise.resolve()), ...(options.now ? { now: options.now } : {}), ...(options.getHarness ? { getHarness: options.getHarness } : {}) }); return { service, t3, vault, home, root, stopped, processes, gateway: await gatewayServer(t, options.gatewayModels) }; } -const add = (h, agents = ['claude', 'codex', 'opencode'], over = {}) => h.service.create({ allowPrivateHost: true, gateway: { name: 'My Proxy', preset: 'cliproxyapi', url: h.gateway.url, agents, ...over }, key: 'sk-first-key' }); +const add = (h, agents = ['claude', 'codex', 'opencode'], over = {}) => h.service.create({ allowPrivateHost: true, gateway: { name: 'My Proxy', preset: 'cliproxyapi', url: h.gateway.url, agents, ...over }, key: 'sk-one' }); const turn = async (h, id, agent, connected, more = {}) => { const { revision } = await h.service.status(); return h.service.setAgent({ id, agent, connected, revision, ...more }); }; const settled = async h => { if (!h) return new Promise(resolve => setTimeout(resolve, 30)); @@ -148,10 +148,10 @@ test('Pi and Grok lifecycle stays scoped through reapply, rotation, model change const dir = path.join(h.root, 'state/setup/gateways/agents'), piFile = path.join(dir, `${id}.pi.mjs`), grokHome = path.join(dir, `${id}.grok`); assert.match(h.t3.state.instances[`pi_gw_${id}`].config.launchArgs, /^--offline --extension /); assert.equal(h.t3.state.instances[`grok_gw_${id}`].config.binaryPath, '/launchers/grok'); - assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-first-key'); - assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-first-key'); + assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-one'); + assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-one'); for (const file of [piFile, path.join(grokHome, 'config.toml')]) { - assert.doesNotMatch(await fs.readFile(file, 'utf8'), /sk-first-key/); + assert.doesNotMatch(await fs.readFile(file, 'utf8'), /sk-one/); assert.equal((await fs.stat(file)).mode & 0o777, 0o600); } assert.equal((await fs.stat(grokHome)).mode & 0o777, 0o700); @@ -160,11 +160,11 @@ test('Pi and Grok lifecycle stays scoped through reapply, rotation, model change assert.ok((await h.service.reapply({ id, revision: view.revision })).results.every(row => row.action === 'none')); assert.deepEqual(h.t3.writes, []); assert.deepEqual(h.stopped, []); - await h.vault.replace(`${id}-gateway`, 'sk-second-key'); + await h.vault.replace(`${id}-gateway`, 'sk-two'); view = await h.service.status(); await h.service.reapply({ id, revision: view.revision }); - assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-second-key'); - assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-second-key'); + assert.equal(h.t3.secrets.get(`pi_gw_${id}/T3_GATEWAY_KEY`), 'sk-two'); + assert.equal(h.t3.secrets.get(`grok_gw_${id}/XAI_API_KEY`), 'sk-two'); assert.deepEqual(h.stopped.sort(), [`grok_gw_${id}`, `pi_gw_${id}`]); await settled(); h.gateway.state.models.push('grok-new'); @@ -272,17 +272,17 @@ test('CLIProxyAPI catalog discovery feeds every supported gateway adapter', asyn test('a typed key goes to the Vault and each instance’s secret, and to nowhere else', async t => { const h = await harness(t), { id } = await add(h); - assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-first-key'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key'); - assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-first-key'); + assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-one'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one'); + assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-one'); const keyFile = path.join(h.root, `state/setup/gateways/agents/${id}.key`); - assert.equal(await fs.readFile(keyFile, 'utf8'), 'sk-first-key'); + assert.equal(await fs.readFile(keyFile, 'utf8'), 'sk-one'); assert.equal((await fs.stat(keyFile)).mode & 0o777, 0o600); - assert.doesNotMatch(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.json`), 'utf8'), /sk-first-key/); + assert.doesNotMatch(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.json`), 'utf8'), /sk-one/); const file = await fs.readFile(path.join(h.root, 'state/setup/gateways/gateways.json'), 'utf8'); - assert.doesNotMatch(file, /sk-first-key/); + assert.doesNotMatch(file, /sk-one/); assert.equal((await fs.stat(path.join(h.root, 'state/setup/gateways/gateways.json'))).mode & 0o777, 0o600); - assert.doesNotMatch(JSON.stringify(await h.service.status()), /sk-first-key/); + assert.doesNotMatch(JSON.stringify(await h.service.status()), /sk-one/); }); test('a saved Vault key can be chosen instead of typing one', async t => { @@ -336,7 +336,7 @@ test('an instance still starting reads as starting, whatever T3’s placeholder h.t3.read = async () => ({ ...await read(), providers: Object.keys(h.t3.state.instances).filter(id => id.includes('_gw_')).map(instanceId => ({ instanceId, status: 'warning', installed: false, checkedAt: '2000-01-01T00:00:00.000Z' })) }); h.waitFor = new Promise(resolve => { release = resolve; }); const service = createGatewayService({ baseDir: path.join(h.root, 'state2'), home: h.home, env: {}, io: fs, t3: h.t3, vault: h.vault, processes: h.processes, wait: () => h.waitFor }); - const { gateways } = await service.create({ allowPrivateHost: true, gateway: { name: 'Slow', url: h.gateway.url, agents: ['claude'] }, key: 'sk-first-key' }); + const { gateways } = await service.create({ allowPrivateHost: true, gateway: { name: 'Slow', url: h.gateway.url, agents: ['claude'] }, key: 'sk-one' }); assert.equal(gateways[0].agents[0].state, 'starting'); assert.equal(gateways[0].settling, true); h.t3.read = read; @@ -347,17 +347,17 @@ test('adding an agent later reads the key from the Vault', async t => { const h = await harness(t), { id } = await add(h, ['claude']); await settled(); await turn(h, id, 'codex', true); - assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-first-key'); + assert.equal(h.t3.secrets.get('codex_gw_my-proxy/T3_GATEWAY_KEY'), 'sk-one'); }); test('rotating the key here reaches every connected instance', async t => { const h = await harness(t), { id } = await add(h); await settled(); const { revision } = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-second-key' }); - assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-second-key'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-second-key'); - assert.equal(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.key`), 'utf8'), 'sk-second-key'); + await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-two' }); + assert.equal(h.vault.rows.get(`${id}-gateway`).key, 'sk-two'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-two'); + assert.equal(await fs.readFile(path.join(h.root, `state/setup/gateways/agents/${id}.key`), 'utf8'), 'sk-two'); }); test('a process that outlives its instance is retired when it holds something old', async t => { @@ -366,7 +366,7 @@ test('a process that outlives its instance is retired when it holds something ol h.stopped.length = 0; const all = ['claudeAgent_gw_my-proxy', 'codex_gw_my-proxy', 'opencode_gw_my-proxy']; let view = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision: view.revision, gateway: {}, key: 'sk-second-key' }); + await h.service.save({ allowPrivateHost: true, id, revision: view.revision, gateway: {}, key: 'sk-two' }); assert.deepEqual(h.stopped.sort(), all, 'a new key'); h.stopped.length = 0; view = await h.service.status(); @@ -405,7 +405,7 @@ test('processes are stopped before the write, because T3 checks the new instance h.processes.stop = async (...args) => { order.push('stop'); return stop(...args); }; h.t3.upsert = async (...args) => { order.push('write'); return upsert(...args); }; const { revision } = await h.service.status(); - await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-second-key' }); + await h.service.save({ allowPrivateHost: true, id, revision, gateway: {}, key: 'sk-two' }); assert.deepEqual(order, ['stop', 'write']); }); @@ -493,7 +493,7 @@ test('an owned setting changed in T3 Code is flagged, and additions are kept whe const restored = h.t3.state.instances['claudeAgent_gw_my-proxy'].environment; assert.equal(restored.find(row => row.name === 'ANTHROPIC_BASE_URL').value, h.gateway.url); assert.equal(restored.find(row => row.name === 'MY_FLAG').value, '1'); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key', 'the saved secret survives'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one', 'the saved secret survives'); }); test('renaming an instance in T3 Code is not drift, and survives a later apply', async t => { @@ -800,11 +800,11 @@ test('names become unique ids, and each gateway keeps its own instances', async const h = await harness(t); const first = await add(h, ['codex'], { name: 'Proxy!' }); await settled(); - const second = await h.service.create({ allowPrivateHost: true, gateway: { name: 'proxy', url: h.gateway.url, agents: ['codex'] }, key: 'sk-second-key' }); + const second = await h.service.create({ allowPrivateHost: true, gateway: { name: 'proxy', url: h.gateway.url, agents: ['codex'] }, key: 'sk-two' }); assert.equal(first.id, 'proxy'); assert.equal(second.id, 'proxy-2'); - assert.equal(h.t3.secrets.get('codex_gw_proxy/T3_GATEWAY_KEY'), 'sk-first-key'); - assert.equal(h.t3.secrets.get('codex_gw_proxy-2/T3_GATEWAY_KEY'), 'sk-second-key'); + assert.equal(h.t3.secrets.get('codex_gw_proxy/T3_GATEWAY_KEY'), 'sk-one'); + assert.equal(h.t3.secrets.get('codex_gw_proxy-2/T3_GATEWAY_KEY'), 'sk-two'); }); test('Claude’s settings file is reported with no values, and cleared key by key', async t => { @@ -841,17 +841,15 @@ test('bad input is refused with a sentence and the field it belongs to', async t assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'a'.repeat(41), url: h.gateway.url, agents: [] } }))).field, 'name'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: ['nobody'] } }))).field, 'agents'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [] }, key: 'has space' }))).field, 'key'); - assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [] }, key: 'short' }))).field, 'key', 'a key too short to mask is refused'); - assert.equal((await failure(h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try-key', managementKey: 'short' }))).field, 'managementKey'); assert.equal((await failure(h.service.create({ allowPrivateHost: true, gateway: { name: 'x', url: h.gateway.url, agents: [], manual: ['bad id'] } }))).field, 'manual'); }); test('testing an unsaved address changes nothing', async t => { const h = await harness(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: `${h.gateway.url}/v1`, key: 'sk-try-key' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: `${h.gateway.url}/v1`, key: 'sk-try' }); assert.equal(result.ok, true); assert.equal(h.gateway.state.keys[0], undefined, 'The keyless connectivity check runs first'); - assert.ok(h.gateway.state.keys.includes('Bearer sk-try-key'), 'The key goes out only after the host answers'); + assert.ok(h.gateway.state.keys.includes('Bearer sk-try'), 'The key goes out only after the host answers'); assert.deepEqual(result.offers, { claude: 1, codex: 1, opencode: 2, pi: 2, grok: 0 }); assert.equal((await h.service.status()).gateways.length, 0); assert.equal(h.vault.rows.size, 0); @@ -860,20 +858,20 @@ test('testing an unsaved address changes nothing', async t => { test('a test needs a private-host confirm before any key goes out', async t => { const h = await harness(t); const before = h.gateway.state.keys.length; - const denied = await h.service.test({ url: h.gateway.url, key: 'sk-try-key' }); + const denied = await h.service.test({ url: h.gateway.url, key: 'sk-try' }); assert.equal(denied.result.code, 'private-host'); assert.match(denied.result.message, /Confirm that you trust/); assert.equal(denied.result.confirmHost, '127.0.0.1'); assert.equal(h.gateway.state.keys.length, before, 'Zero requests, keyed or not, without the confirm'); - const allowed = await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try-key' }); + const allowed = await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-try' }); assert.equal(allowed.result.ok, true); - assert.ok(h.gateway.state.keys.slice(before).includes('Bearer sk-try-key'), 'The key goes out only after the confirm'); + assert.ok(h.gateway.state.keys.slice(before).includes('Bearer sk-try'), 'The key goes out only after the confirm'); assert.equal(h.gateway.state.keys[before], undefined, 'Even then the keyless check runs first'); }); test('connecting reuses the test just shown instead of probing again', async t => { const h = await harness(t); - await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-first-key' }); + await h.service.test({ allowPrivateHost: true, url: h.gateway.url, key: 'sk-one' }); const seen = h.gateway.state.keys.length; await add(h, ['claude']); assert.equal(h.gateway.state.keys.length, seen); @@ -964,7 +962,7 @@ test('Restore puts back a key row someone deleted in T3 Code', async t => { assert.equal(view.gateways[0].agents[0].state, 'drift'); const { results } = await h.service.reapply({ id, revision: view.revision }); assert.deepEqual(results.map(result => result.ok), [true]); - assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-first-key'); + assert.equal(h.t3.secrets.get('claudeAgent_gw_my-proxy/ANTHROPIC_AUTH_TOKEN'), 'sk-one'); }); test('an agent can be turned off even when its instance has lost its key row', async t => { @@ -1050,7 +1048,7 @@ test('ids stay valid for T3 and the Vault, and never collide with the Add sheet' assert.notEqual(first.id, 'new'); const long = 'a'.repeat(27) + ' b ' + 'c'.repeat(8); await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] } }); - const second = await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] }, key: 'sk-extra-key' }); + const second = await h.service.create({ allowPrivateHost: true, gateway: { name: long, url: h.gateway.url, agents: [] }, key: 'sk-x' }); assert.match(second.id, /^[a-z0-9]+(?:-[a-z0-9]+)*$/); }); @@ -1078,7 +1076,7 @@ async function hubServer(t, { accounts = [{ provider: 'claude' }, { provider: 'c t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); })); return { state, url: `http://127.0.0.1:${server.address().port}` }; } -const createInput = (hub, extra = {}) => ({ allowPrivateHost: true, gateway: { name: 'Hub', preset: 'cliproxyapi', url: hub.url, agents: ['claude'] }, key: 'sk-hub-key', ...extra }); +const createInput = (hub, extra = {}) => ({ allowPrivateHost: true, gateway: { name: 'Hub', preset: 'cliproxyapi', url: hub.url, agents: ['claude'] }, key: 'sk-api', ...extra }); test('adding a management key gives T3 a verified hub, and the key stays out of the store', async t => { const h = await harness(t), hub = await hubServer(t); @@ -1097,7 +1095,7 @@ test('adding a management key gives T3 a verified hub, and the key stays out of test('a wrong management key adds nothing, costs one attempt, and never reaches T3', async t => { const h = await harness(t), hub = await hubServer(t); - await assert.rejects(h.service.create(createInput(hub, { managementKey: 'wrong-management-key' })), error => error.code === 'limits' && error.extra.field === 'managementKey' && /not the hash/.test(error.extra.hint) && /30 minutes/.test(error.extra.hint)); + await assert.rejects(h.service.create(createInput(hub, { managementKey: 'wrong' })), error => error.code === 'limits' && error.extra.field === 'managementKey' && /not the hash/.test(error.extra.hint) && /30 minutes/.test(error.extra.hint)); assert.equal(hub.state.attempts, 1, 'Five of these lock the server out'); assert.deepEqual(h.t3.hubWrites, [], 'T3 polls with whatever it holds, so a bad key would spend the rest'); assert.deepEqual(h.t3.writes, [], 'And the gateway isn’t half added'); @@ -1106,7 +1104,7 @@ test('a wrong management key adds nothing, costs one attempt, and never reaches test('a management key is sent only when asked, so a plain test or health check costs nothing', async t => { const h = await harness(t), hub = await hubServer(t); - await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-hub-key' }); + await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-api' }); const { id } = await h.service.create(createInput(hub)); await h.service.test({ id }); await h.service.status(); @@ -1117,7 +1115,7 @@ test('a management key is sent only when asked, so a plain test or health check test('a key shown as good isn’t spent again when the gateway is added', async t => { const h = await harness(t), hub = await hubServer(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-hub-key', managementKey: 'the-management-key' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: hub.url, key: 'sk-api', managementKey: 'the-management-key' }); assert.deepEqual(result.limits, { ok: true, code: 'ok', accounts: { claude: 1, codex: 1 } }, 'Disabled accounts don’t count, and only what Limits can show does'); assert.ok(!JSON.stringify(result).includes('never-read'), 'The list can hold credentials, and none of it leaves'); await h.service.create(createInput(hub, { managementKey: 'the-management-key' })); @@ -1126,7 +1124,7 @@ test('a key shown as good isn’t spent again when the gateway is added', async test('a gateway can’t vouch for a management key when it didn’t answer', async t => { const h = await harness(t), hub = await hubServer(t); - const { result } = await h.service.test({ allowPrivateHost: true, url: 'http://127.0.0.1:1', key: 'sk-hub-key', managementKey: 'the-management-key' }); + const { result } = await h.service.test({ allowPrivateHost: true, url: 'http://127.0.0.1:1', key: 'sk-api', managementKey: 'the-management-key' }); assert.equal(result.ok, false); assert.equal(result.limits, undefined); assert.equal(hub.state.attempts, 0); }); @@ -1293,7 +1291,7 @@ test('limits report what T3 sees, and only for a gateway that can have them', as h.t3.reports = [{ id: sourceId, kind: 'cliproxy', label: 'Hub', checkedAt: '2026-11-06T00:00:00Z', accounts: [], error: 'The hub rejected the key.' }]; await h.service.setLimits({ id, revision: (await h.service.status()).revision, managementKey: 'the-management-key' }); assert.equal((await h.service.status()).gateways[0].limits.state, 'error'); - const litellm = await h.service.create({ allowPrivateHost: true, gateway: { name: 'Other', preset: 'litellm', url: hub.url, agents: [] }, key: 'sk-hub-key' }); + const litellm = await h.service.create({ allowPrivateHost: true, gateway: { name: 'Other', preset: 'litellm', url: hub.url, agents: [] }, key: 'sk-api' }); assert.equal(litellm.gateways.find(row => row.id !== id).limits.supported, false); }); diff --git a/tests/services-redaction.test.mjs b/tests/services-redaction.test.mjs new file mode 100644 index 00000000..0538278c --- /dev/null +++ b/tests/services-redaction.test.mjs @@ -0,0 +1,40 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import * as fs from 'node:fs/promises'; +import path from 'node:path'; +import os from 'node:os'; +import {pathToFileURL} from 'node:url'; +import {normalize} from '../docker/setup/credentials/model.mjs'; +import {logWriter,readLogs} from '../docker/services/logs.mjs'; + +test('valid existing short credentials remain readable and editable',()=>{ + for(const token of ['abc','sk-one','1234567']){ + const saved=normalize({id:'existing',type:'token',secrets:{token}}); + assert.equal(normalize({...saved,description:'edited'},saved).secrets.token,token); + } +}); + +test('service logs redact short and encoded secrets across every split',async t=>{ + const root=await fs.mkdtemp(path.join(os.tmpdir(),'t3-services-redaction-')); + t.after(()=>fs.rm(root,{recursive:true,force:true})); + for(const secret of ['abc','a','p@ss word','quote"\\line\nnext']){ + const forms=[secret,Buffer.from(secret).toString('base64'),encodeURIComponent(secret),new URLSearchParams([['',secret]]).toString().slice(1),JSON.stringify(secret).slice(1,-1)]; + for(const value of new Set(forms))for(let at=0;at<=value.length;at++){ + const writer=logWriter(root,'test','run','stdout',[secret]); + writer.write(Buffer.from(value.slice(0,at)));writer.write(Buffer.from(value.slice(at)+'\n'));writer.end(); + assert.equal(readLogs(root,'test').entries.at(-1).text,'[redacted]',`split ${at} for ${JSON.stringify(secret)}`); + } + } +}); + +test('Services imports and masks logs in the installed image directory layout',async t=>{ + const root=await fs.mkdtemp(path.join(os.tmpdir(),'t3-services-image-layout-')); + t.after(()=>fs.rm(root,{recursive:true,force:true})); + await fs.cp(new URL('../docker/services/',import.meta.url),path.join(root,'t3-services'),{recursive:true}); + const setup=path.join(root,'t3-setup','credentials');await fs.mkdir(setup,{recursive:true}); + await fs.copyFile(new URL('../docker/setup/credentials/redact.mjs',import.meta.url),path.join(setup,'redact.mjs')); + const {logWriter:writer,readLogs:logs}=await import(pathToFileURL(path.join(root,'t3-services','logs.mjs'))); + const output=writer(path.join(root,'logs'),'demo','run','stdout',['private-token']); + output.write(Buffer.from(Buffer.from('private-token').toString('base64')+'\n'));output.end(); + assert.equal(logs(path.join(root,'logs'),'demo').entries[0].text,'[redacted]'); +});