diff --git a/bin/opencode-acp-adapter.mjs b/bin/opencode-acp-adapter.mjs index 3b50cb077c..ecb498f695 100755 --- a/bin/opencode-acp-adapter.mjs +++ b/bin/opencode-acp-adapter.mjs @@ -61,17 +61,59 @@ const debug = (...args) => { } }; +// Strict argv (#6939, umbrella #3352): --server is this adapter's one +// operator flag, and every value for it is validated HERE, at startup, +// before the event stream or any ACP traffic — a bad value used to be +// silently dropped (trailing --server), silently dialled forever in a +// per-request "Failed to parse URL" reconnect loop (--server=, a +// flag-like or non-URL value), or silently ignored (a typo'd flag +// falling back to the default serve). All of those now fail fast: a +// usage error on stderr naming the flag and the offending value, and +// a non-zero exit. +function usageError(message) { + process.stderr.write(`[${AGENT_NAME}] usage error: ${message}\n`); + process.stderr.write(`usage: ${AGENT_NAME} [--server ]\n`); + process.exit(2); +} + +function validateServerValue(value, source) { + if (!value) { + usageError(`${source} requires a non-empty URL value (got an empty value)`); + } + if (value.startsWith('--')) { + usageError(`${source} requires a URL value, not another flag (got "${value}")`); + } + let parsed; + try { + parsed = new URL(value); + } catch { + usageError(`${source} value is not a parseable URL (got "${value}")`); + } + if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { + usageError(`${source} value must be an http: or https: URL (got "${value}")`); + } + return value.replace(/\/+$/, ''); +} + function parseServerUrl(argv) { let server = process.env.OPENCODE_ACP_SERVER || 'http://127.0.0.1:13420'; + let source = 'OPENCODE_ACP_SERVER'; for (let i = 0; i < argv.length; i += 1) { - if (argv[i] === '--server' && argv[i + 1]) { + if (argv[i] === '--server') { + if (i + 1 >= argv.length) { + usageError('--server requires a URL value (got end of arguments)'); + } server = argv[i + 1]; + source = '--server'; i += 1; } else if (argv[i].startsWith('--server=')) { server = argv[i].slice('--server='.length); + source = '--server'; + } else { + usageError(`unknown argument "${argv[i]}" (the only supported flag is --server)`); } } - return server.replace(/\/+$/, ''); + return validateServerValue(server, source); } const serverBase = parseServerUrl(process.argv.slice(2)); diff --git a/bin/opencode-acp-adapter.test.mjs b/bin/opencode-acp-adapter.test.mjs index fcead2204a..7f738da813 100644 --- a/bin/opencode-acp-adapter.test.mjs +++ b/bin/opencode-acp-adapter.test.mjs @@ -373,3 +373,89 @@ test('malformed credential state file warns on stderr and sends no auth header', await acp.waitFor(() => acp.stderr().includes('not valid JSON') || null, 'stderr warning'); assert.ok(acp.stderr().includes(authFile), 'warning names the state file'); }); + +// --------------------------------------------------------------------------- +// --server validation (#6939): bad values fail fast at startup with a +// usage error on stderr naming --server, before any ACP traffic. The +// helper spawns the adapter with the given argv and collects its exit. +// --------------------------------------------------------------------------- +function runAdapterRaw(args, env = {}) { + return new Promise((resolve, reject) => { + const child = spawn(process.execPath, [path.join(binDir, 'opencode-acp-adapter.mjs'), ...args], { + env: { + ...process.env, + OPENCODE_SERVER_PASSWORD: '', + OPENCODE_AUTH_FILE: path.join(binDir, 'nonexistent-auth-fixture.json'), + ...env, + }, + }); + let stderr = ''; + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (chunk) => { + stderr += chunk; + }); + child.stdin.end(); + const timer = setTimeout(() => { + child.kill('SIGKILL'); + reject(new Error(`adapter did not exit fast for argv ${JSON.stringify(args)}; stderr so far: ${stderr}`)); + }, 5000); + child.on('exit', (code) => { + clearTimeout(timer); + resolve({ code, stderr }); + }); + }); +} + +test('#6939 trailing --server fails fast naming --server', async () => { + const { code, stderr } = await runAdapterRaw(['--server']); + assert.notEqual(code, 0); + assert.match(stderr, /usage error/); + assert.match(stderr, /--server/); +}); + +test('#6939 --server= (empty) fails fast naming --server', async () => { + const { code, stderr } = await runAdapterRaw(['--server=']); + assert.notEqual(code, 0); + assert.match(stderr, /usage error/); + assert.match(stderr, /--server/); +}); + +test('#6939 --server "" (empty value) fails fast naming --server', async () => { + const { code, stderr } = await runAdapterRaw(['--server', '']); + assert.notEqual(code, 0); + assert.match(stderr, /--server/); +}); + +test('#6939 flag-like --server value fails fast naming the value', async () => { + const { code, stderr } = await runAdapterRaw(['--server', '--bogus-flag']); + assert.notEqual(code, 0); + assert.match(stderr, /--server/); + assert.match(stderr, /--bogus-flag/); +}); + +test('#6939 non-URL --server value fails fast naming the value', async () => { + const { code, stderr } = await runAdapterRaw(['--server', 'notaurl']); + assert.notEqual(code, 0); + assert.match(stderr, /--server/); + assert.match(stderr, /notaurl/); +}); + +test('#6939 non-http(s) --server value fails fast', async () => { + const { code, stderr } = await runAdapterRaw(['--server', 'ftp://example.invalid']); + assert.notEqual(code, 0); + assert.match(stderr, /--server/); + assert.match(stderr, /http/); +}); + +test('#6939 a typo\'d flag fails fast instead of silently dialling the default', async () => { + const { code, stderr } = await runAdapterRaw(['--sever', 'http://127.0.0.1:9']); + assert.notEqual(code, 0); + assert.match(stderr, /unknown argument/); + assert.match(stderr, /--sever/); +}); + +test('#6939 a non-URL OPENCODE_ACP_SERVER env value fails fast naming the env var', async () => { + const { code, stderr } = await runAdapterRaw([], { OPENCODE_ACP_SERVER: 'also-not-a-url' }); + assert.notEqual(code, 0); + assert.match(stderr, /OPENCODE_ACP_SERVER/); +});