Skip to content
Open
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
13 changes: 12 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ function createRuntimeConfig(
authBaseUrl?: string,
): CliproxyConfig {
const baseUrl = getBaseUrl(options, authBaseUrl);
const hasExplicitApiKey = getStringOption(options, 'apiKey') !== undefined;
const originMatchesCredential =
!authBaseUrl || sameOrigin(baseUrl, authBaseUrl);
return {
baseUrl,
apiKey,
apiKey: hasExplicitApiKey || originMatchesCredential ? apiKey : '',
modelCacheTtl: getPositiveNumber(options, 'modelCacheTtl'),
refreshOnList: getBoolean(options, 'refreshOnList'),
modelsDev: getModelsDevConfig(options),
Expand Down Expand Up @@ -246,6 +249,14 @@ async function readAuthFromStore(
}
}

function sameOrigin(left: string, right: string): boolean {
try {
return new URL(left).origin === new URL(right).origin;
} catch {
return false;
}
}

export function getBaseUrl(
options?: Record<string, unknown>,
authBaseUrl?: string,
Expand Down
25 changes: 24 additions & 1 deletion test/plugin.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,27 @@ test('authorize stores baseURL and optional apiKey as JSON', async () => {
const parsed = JSON.parse(result.key);
assert.equal(parsed.baseURL, 'http://127.0.0.1:8317/v1');
assert.equal(parsed.apiKey, '');
});
});
test('config hook never sends a stored key to a project-overridden origin', async () => {
const tempHome = await createTempAuthHome({
cliproxy: {
type: 'api',
key: JSON.stringify({ baseURL: 'https://trusted.example/v1', apiKey: 'global-secret' }),
},
});
const calls = [];
global.fetch = async (input, init) => {
calls.push({ url: input instanceof Request ? input.url : String(input), headers: new Headers(init?.headers) });
return new Response(JSON.stringify({ object: 'list', data: [] }), { status: 200 });
};
try {
const plugin = await CliproxyAuthPlugin({});
const config = { provider: { cliproxy: { options: { baseURL: 'https://attacker.example/v1' } } } };
await plugin.config(config);
const attacker = calls.find((call) => call.url.startsWith('https://attacker.example/'));
assert.ok(attacker, 'project endpoint may still be queried without credentials');
assert.equal(attacker.headers.get('Authorization'), null);
} finally {
await rm(tempHome, { recursive: true, force: true });
}
});