Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(proxy): a revocable URL token never reads the unified-only /v1/usage ledger (#1663)

tashdroid2d27c9f62321
diff --git a/server/src/__tests__/routes/url-tokens.test.ts b/server/src/__tests__/routes/url-tokens.test.ts
index ec6c2373..f8a42102 100644
--- a/server/src/__tests__/routes/url-tokens.test.ts
+++ b/server/src/__tests__/routes/url-tokens.test.ts
@@ -116,4 +116,24 @@ describe('revocable URL-token aliases', () => {
const response = await request(app, 'GET', `/v1/t/${getUnifiedApiKey()}/models`);
expect(response.status).toBe(401);
});
+
+ it('does not let a URL token read the unified-only /v1/usage ledger', async () => {
+ const minted = await request(app, 'POST', '/api/settings/url-tokens', { label: 'widget' }, {
+ Authorization: `Bearer ${dashboardToken}`,
+ });
+ expect(minted.status).toBe(201);
+
+ // The middleware injects the unified key for headerless clients — the
+ // usage route must not treat that injection as the real unified key.
+ const viaToken = await request(app, 'GET', `/v1/t/${minted.body.token}/usage`);
+ expect(viaToken.status).toBe(403);
+ expect(viaToken.body.error?.type).toBe('permission_error');
+
+ // The genuine unified key keeps its read.
+ const viaUnified = await request(app, 'GET', '/v1/usage', undefined, {
+ Authorization: `Bearer ${getUnifiedApiKey()}`,
+ });
+ expect(viaUnified.status).toBe(200);
+ expect(typeof viaUnified.body.requests).toBe('number');
+ });
});
diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts
index 752fcdda..b2ca76b0 100644
--- a/server/src/routes/proxy.ts
+++ b/server/src/routes/proxy.ts
@@ -541,6 +541,15 @@ function buildCatalogEntries(filters: { onlyAvailable?: boolean; executionStatus
// (sk-cp-...) is handed to a downstream client and must not see what every
// other client spent.
proxyRouter.get('/usage', (req: Request, res: Response) => {
+ // A URL-token request carries the injected unified key (routes/url-tokens.ts)
+ // — which is precisely why it must not pass a unified-key gate. The token is
+ // handed to a headerless client (a teammate, a widget, a phone app); the
+ // ledger behind this route is every provider key's spend. Same reasoning as
+ // the client-profile refusal below.
+ if ((req as Request & { urlTokenAuthenticated?: boolean }).urlTokenAuthenticated) {
+ res.status(403).json({ error: { message: 'Usage is only available to the unified API key', type: 'permission_error' } });
+ return;
+ }
const auth = requireInferenceAuth(req, res);
if (!auth) return;
if (auth.kind !== 'unified') {
diff --git a/server/src/routes/url-tokens.ts b/server/src/routes/url-tokens.ts
index 6fb3f9a6..05946dbe 100644
--- a/server/src/routes/url-tokens.ts
+++ b/server/src/routes/url-tokens.ts
@@ -19,6 +19,13 @@ urlTokenRouter.use((req: Request, res: Response, next: NextFunction) => {
});
return;
}
+ // A URL token buys headerless INFERENCE, not the operator's console. The
+ // injection below makes the request look unified to every downstream
+ // router, so routes that read only for the unified key check THIS flag and
+ // refuse — otherwise a shareable, revocable token handed to a headerless
+ // client reads exactly what the client-profile-key rule in routes/proxy.ts
+ // forbids: the install-wide spend ledger (/v1/usage reports every key's
+ // monthly spend, caps and which account is nearly drained).
// Downstream protocol routers retain their normal authentication checks.
// Supply the unified key internally only after the separately revocable URL
// token was verified; it never appears in the public URL.