Stop sending target cookies from the HTTP service #7

Merged
raucao merged 1 commits from bugfix/openrouter into master 2026-11-16 08:25:55 +00:00
4 changed files with 76 additions and 6 deletions

No files matched your search

+10 -1
View File
@@ -67,7 +67,16 @@ export default class HttpService extends Service {
let response;
try {
response = await fetch(target, { ...options, headers });
// Extension pages with host permissions are treated as same-origin, so
// the target origin's cookies would ride along. Cookie-first APIs (e.g.
// OpenRouter's Clerk-protected analytics endpoint) then try to read our
// Authorization key as a session JWT and reject it. We authenticate with
// headers, never cookies.
response = await fetch(target, {
credentials: 'omit',
...options,
headers,
});
} catch (err) {
// A navigation (or bfcache entry) cancels in-flight requests; that is not
// a real network failure and must not surface as an error state.
+10 -5
View File
@@ -247,23 +247,28 @@ export default class OpenRouterService extends Service {
return 'Unable to reach OpenRouter';
}
// Prefer the API's own message (e.g. "Only management keys can perform
// this operation") so auth and permission failures stay diagnosable,
// falling back to a status-based summary.
const apiMessage = err?.body?.error?.message;
if (err?.status === 401) {
return 'Invalid or expired management key';
return apiMessage ?? 'Invalid or expired management key';
}
if (err?.status === 403) {
return 'Management key required';
return apiMessage ?? 'Management key required';
}
if (err?.status === 408) {
return 'OpenRouter timed out';
return apiMessage ?? 'OpenRouter timed out';
}
if (err?.status === 429) {
return 'Rate limited by OpenRouter';
return apiMessage ?? 'Rate limited by OpenRouter';
}
return 'OpenRouter returned an error';
return apiMessage ?? 'OpenRouter returned an error';
}
startPolling() {
+32
View File
@@ -218,4 +218,36 @@ module('Unit | Service | http', function (hooks) {
assert.strictEqual(requestedUrl, 'https://example.com/data.json');
assert.false(CORS_PROXY_HEADER in requestedOptions.headers);
});
test('requestJson omits credentials so target cookies are not sent', async function (assert) {
let requestedOptions;
globalThis.fetch = async (url, options) => {
requestedOptions = options;
return { ok: true, json: async () => ({}) };
};
const http = this.owner.lookup('service:http');
http.corsProxyBase = null;
await http.requestJson('https://example.com/data.json');
assert.strictEqual(requestedOptions.credentials, 'omit');
});
test('requestJson lets callers override the credentials mode', async function (assert) {
let requestedOptions;
globalThis.fetch = async (url, options) => {
requestedOptions = options;
return { ok: true, json: async () => ({}) };
};
const http = this.owner.lookup('service:http');
http.corsProxyBase = null;
await http.requestJson('https://example.com/data.json', {
credentials: 'include',
});
assert.strictEqual(requestedOptions.credentials, 'include');
});
});
+24
View File
@@ -240,6 +240,30 @@ module('Unit | Service | openrouter', function (hooks) {
assert.strictEqual(service.error, 'Management key required');
});
test('fetchUsage surfaces the API error message on failure', async function (assert) {
globalThis.fetch = async () => ({
ok: false,
status: 403,
json: async () => ({
error: {
code: 403,
message: 'Only management keys can perform this operation',
},
}),
});
const service = this.owner.lookup('service:openrouter');
service.apiKey = 'sk-or-v1-test';
service.enabled = true;
await service.fetchUsage();
assert.strictEqual(
service.error,
'Only management keys can perform this operation'
);
});
test('fetchKeys returns the keys and reads the response data', async function (assert) {
globalThis.fetch = async () => ({
ok: true,