Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bin/check-opencode-metered-providers.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import { realpathSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -356,6 +357,22 @@ async function main() {
console.log(`Metered-provider config guard passed: ${configPath}`);
}

if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
// Entry-point check: compare realpaths, not the raw strings (#4694;
// same pattern as proxy-supervise.mjs / #2083 and
// provider-catalog-sync.mjs / #2235). process.argv[1] keeps the path
// exactly as invoked (e.g. through a file or directory symlink) while
// import.meta.url is the resolved module URL, so a resolve() string
// compare silently never matches and main() never runs via a symlink --
// the guard exited 0 without checking anything.
const isMain = (() => {
try {
if (!process.argv[1]) return false;
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
} catch {
return false;
}
})();

if (isMain) {
await main();
}
58 changes: 58 additions & 0 deletions bin/check-opencode-metered-providers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,61 @@ test('rejects a PAYG provider in a provider group named providers', () => {
assert.match(issues[0], /provider-route/);
assert.ok(issues[0].includes('$.experimental.provider_routes.providers.providers[0]'));
});

// #4694: the entry-point check must run main() when the guard is invoked
// through a symlink (file or directory). Before the realpath fix both
// invocations silently exited 0 without checking anything.
import { spawnSync } from 'node:child_process';
import { mkdtempSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';

const guardPath = fileURLToPath(new URL('./check-opencode-metered-providers.mjs', import.meta.url));
const meteredConfig = '{ "model": "deepseek/deepseek-chat" }';
const cleanCliConfig = '{ "model": "openai/gpt-6" }';

function runGuard(scriptPath, configSource) {
const dir = mkdtempSync(join(tmpdir(), 'metered-guard-'));
const configPath = join(dir, 'config.jsonc');
writeFileSync(configPath, configSource);
return spawnSync(process.execPath, [scriptPath, configPath], { encoding: 'utf8' });
}

test('guard runs when invoked through a file symlink (metered config fails)', () => {
const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-'));
const linkPath = join(dir, 'guard-link.mjs');
symlinkSync(guardPath, linkPath);
const res = runGuard(linkPath, meteredConfig);
assert.equal(res.status, 1);
assert.match(res.stderr, /Metered-provider config guard failed/);
assert.match(res.stderr, /deepseek/);
});

test('guard runs when invoked through a file symlink (clean config passes)', () => {
const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-'));
const linkPath = join(dir, 'guard-link.mjs');
symlinkSync(guardPath, linkPath);
const res = runGuard(linkPath, cleanCliConfig);
assert.equal(res.status, 0);
assert.match(res.stdout, /Metered-provider config guard passed/);
});

test('guard runs when invoked through a symlinked directory (metered config fails)', () => {
const dir = mkdtempSync(join(tmpdir(), 'metered-guard-dirlink-'));
const linkDir = join(dir, 'bin-link');
symlinkSync(dirname(guardPath), linkDir, 'dir');
const res = runGuard(join(linkDir, 'check-opencode-metered-providers.mjs'), meteredConfig);
assert.equal(res.status, 1);
assert.match(res.stderr, /Metered-provider config guard failed/);
});

test('importing the guard module does not run main()', () => {
const res = spawnSync(
process.execPath,
['--input-type=module', '-e', `import ${JSON.stringify(pathToFileURL(guardPath).href)};`],
{ encoding: 'utf8' },
);
assert.equal(res.status, 0);
assert.doesNotMatch(res.stdout + res.stderr, /Metered-provider config guard/);
});
Loading