diff --git a/bin/sync-models.mjs b/bin/sync-models.mjs index c96ed98c1..9b04be76f 100755 --- a/bin/sync-models.mjs +++ b/bin/sync-models.mjs @@ -3,6 +3,14 @@ import fs from 'node:fs'; import path from 'node:path'; import { execSync } from 'node:child_process'; +const KNOWN_ARGS = new Set(['--dry-run', '--verbose', '-v']); +const unknownArgs = process.argv.slice(2).filter(arg => !KNOWN_ARGS.has(arg)); +if (unknownArgs.length > 0) { + console.error(`[sync-models] Error: unknown argument '${unknownArgs[0]}'`); + console.error('Usage: sync-models.mjs [--dry-run] [--verbose|-v]'); + process.exit(2); +} + const DRY_RUN = process.argv.includes('--dry-run'); const VERBOSE = process.argv.includes('--verbose') || process.argv.includes('-v'); diff --git a/bin/sync-models.test.mjs b/bin/sync-models.test.mjs index e7e08e26f..f81a56585 100644 --- a/bin/sync-models.test.mjs +++ b/bin/sync-models.test.mjs @@ -7,7 +7,9 @@ // If parseJsonc's declaration shape changes, this extraction fails loudly // rather than silently testing nothing. import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; import fs from 'node:fs'; +import os from 'node:os'; import path from 'node:path'; import test from 'node:test'; import { fileURLToPath } from 'node:url'; @@ -68,3 +70,60 @@ test('still throws on a genuinely malformed config', () => { assert.throws(() => parseJsonc('{ "a": }')); assert.throws(() => parseJsonc('{ "a": 1')); }); + +// Strict argv validation (#3297): unknown arguments are a usage error, +// reported before any fetch/discovery/write. These probes run the real +// script with an isolated HOME and an explicit OPENCODE_CONFIG_PATH so the +// temp target config — never the live config — is the only write candidate. +const scriptPath = path.join(binDir, 'sync-models.mjs'); + +function runWithTempConfig(args) { + const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-models-test-home-')); + const configPath = path.join(tmpHome, 'opencode.jsonc'); + const before = '{\n // temp probe config\n "provider": {}\n}\n'; + fs.writeFileSync(configPath, before); + const result = spawnSync(process.execPath, [scriptPath, ...args], { + encoding: 'utf-8', + timeout: 60000, + env: { + ...process.env, + HOME: tmpHome, + OPENCODE_CONFIG_PATH: configPath, + }, + }); + const after = fs.readFileSync(configPath, 'utf-8'); + const backupExists = fs.existsSync(`${configPath}.backup`); + fs.rmSync(tmpHome, { recursive: true, force: true }); + return { result, before, after, backupExists }; +} + +test('rejects --bogus-flag non-zero and leaves the target config byte-identical', () => { + const { result, before, after, backupExists } = runWithTempConfig(['--bogus-flag']); + assert.notEqual(result.status, 0); + assert.match(`${result.stderr}${result.stdout}`, /--bogus-flag/); + assert.equal(after, before); + assert.equal(backupExists, false); +}); + +test('rejects typo --dry-rnu non-zero, names it, and leaves the target config byte-identical', () => { + const { result, before, after, backupExists } = runWithTempConfig(['--dry-rnu']); + assert.notEqual(result.status, 0); + assert.match(`${result.stderr}${result.stdout}`, /--dry-rnu/); + assert.equal(after, before); + assert.equal(backupExists, false); +}); + +test('rejects a positional argument non-zero and leaves the target config byte-identical', () => { + const { result, before, after, backupExists } = runWithTempConfig(['some-positional']); + assert.notEqual(result.status, 0); + assert.match(`${result.stderr}${result.stdout}`, /some-positional/); + assert.equal(after, before); + assert.equal(backupExists, false); +}); + +test('--dry-run still exits 0 writing nothing', () => { + const { result, before, after, backupExists } = runWithTempConfig(['--dry-run']); + assert.equal(result.status, 0); + assert.equal(after, before); + assert.equal(backupExists, false); +});