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(providers): bearer depends on the credential, not the keyless flag (#1331) (#1332)
tashdroidc9fd050d6489
diff --git a/server/src/__tests__/providers/keyless-bearer-1331.test.ts b/server/src/__tests__/providers/keyless-bearer-1331.test.tsnew file mode 100644index 00000000..423275b6--- /dev/null+++ b/server/src/__tests__/providers/keyless-bearer-1331.test.ts@@ -0,0 +1,75 @@+import { describe, it, expect, vi, afterEach } from 'vitest';+import { OpenAICompatProvider, isAnonymousCredential } from '../../providers/openai-compat.js';++// #1331: a real API key saved on a keyless platform (Kilo, OVH) was silently+// routed over the anonymous path, and a custom endpoint stored with the+// `no-key` sentinel sent `Authorization: Bearer no-key` upstream — which+// upstreams read as an invalid key. The Authorization header must depend on+// the CREDENTIAL, not on the provider's keyless flag.+describe('keyless bearer handling (#1331)', () => {+ afterEach(() => vi.restoreAllMocks());++ function okFetch(capture: { headers?: Record<string, string> }): void {+ vi.spyOn(global, 'fetch').mockImplementation((async (_url: any, init: any) => {+ capture.headers = (init as any).headers;+ return {+ ok: true,+ json: () => Promise.resolve({+ id: 'x', object: 'chat.completion', created: 1, model: 'm',+ choices: [{ index: 0, message: { role: 'assistant', content: 'hi' }, finish_reason: 'stop' }],+ usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },+ }),+ headers: new Headers(),+ };+ }) as any);+ }++ function keyless(platform: 'kilo' | 'ovh'): OpenAICompatProvider {+ return new OpenAICompatProvider({+ platform: platform as any, name: 'Keyless', baseUrl: 'https://x.test/v1', keyless: true,+ });+ }+ function custom(): OpenAICompatProvider {+ return new OpenAICompatProvider({+ platform: 'custom-endpoint' as any, name: 'Local', baseUrl: 'http://127.0.0.1:11434/v1',+ });+ }++ const sentBearer = (h?: Record<string, string>) =>+ (h ?? {}).Authorization ?? (h ?? {}).authorization;++ it('keyless provider with a REAL key sends the bearer (was: anonymous)', async () => {+ const cap: { headers?: Record<string, string> } = {};+ okFetch(cap);+ await keyless('kilo').chatCompletion('real-kilo-key-123', [{ role: 'user', content: 'hi' }], 'm');+ expect(sentBearer(cap.headers)).toBe('Bearer real-kilo-key-123');+ });++ it('keyless provider with the no-key sentinel sends NO Authorization header', async () => {+ const cap: { headers?: Record<string, string> } = {};+ okFetch(cap);+ await keyless('kilo').chatCompletion('no-key', [{ role: 'user', content: 'hi' }], 'm');+ expect(sentBearer(cap.headers)).toBeUndefined();+ });++ it('custom endpoint with auth off (no-key sentinel) sends NO bearer', async () => {+ const cap: { headers?: Record<string, string> } = {};+ okFetch(cap);+ await custom().chatCompletion('no-key', [{ role: 'user', content: 'hi' }], 'm');+ expect(sentBearer(cap.headers)).toBeUndefined();+ });++ it('custom endpoint with a real key still sends the bearer', async () => {+ const cap: { headers?: Record<string, string> } = {};+ okFetch(cap);+ await custom().chatCompletion('sk-local-999', [{ role: 'user', content: 'hi' }], 'm');+ expect(sentBearer(cap.headers)).toBe('Bearer sk-local-999');+ });++ it('credential test is trimmed and covers empty/missing', () => {+ expect(isAnonymousCredential(' no-key ')).toBe(true);+ expect(isAnonymousCredential('')).toBe(true);+ expect(isAnonymousCredential(undefined)).toBe(true);+ expect(isAnonymousCredential('sk-1')).toBe(false);+ });+});diff --git a/server/src/providers/openai-compat.ts b/server/src/providers/openai-compat.tsindex d1a9977b..2733b53a 100644--- a/server/src/providers/openai-compat.ts+++ b/server/src/providers/openai-compat.ts@@ -16,6 +16,16 @@ import { providerTimeoutMs } from '../lib/provider-timeout.js';import { isAbortLikeError } from '../lib/error-classify.js';import { contentToString } from '../lib/content.js';+/** The sentinel the Keys page stores for keyless/custom endpoints (routes/+ * keys.ts keeps `no-key` for "auth off"), plus the empty string a caller can+ * hand a provider when no credential exists. A keyless provider sends no+ * Authorization header for these and a real bearer for anything else (#1331).+ * Exported so validateKey and the request path share one definition. */+export function isAnonymousCredential(apiKey: string | null | undefined): boolean {+ const v = apiKey?.trim() ?? '';+ return v === '' || v === 'no-key';+}+/** Hosts that ARE Moonshot's OpenAI-compatible API (api.moonshot.ai,* api.moonshot.cn, api.kimi.com and their subdomains). */const MOONSHOT_HOST_SUFFIXES = ['moonshot.ai', 'moonshot.cn', 'kimi.com'];@@ -162,11 +172,17 @@ export class OpenAICompatProvider extends BaseProvider {return res.statusText;}- /** Keyless providers (Kilo's anonymous free tier) must send NO Authorization- * header — a stored sentinel like `Bearer no-key` could be treated as an- * invalid key. Everyone else sends the bearer as usual. */+ /** Anonymous access sends NO Authorization header: keyless providers+ * (Kilo's anonymous free tier) holding the stored `no-key` sentinel, and+ * custom endpoints whose stored credential is the same sentinel with auth+ * off. Sending `Bearer no-key` upstream is never right — upstreams read it+ * as an invalid key. A REAL key saved on a keyless platform is used instead+ * of the anonymous path, and a real key on a custom endpoint still gets its+ * bearer (#1331): the presence of a credential decides at request time —+ * Kilo, OVH and AI Horde all accept both modes per their docs. */private authHeader(apiKey: string): Record<string, string> {- return this.keyless ? {} : { 'Authorization': `Bearer ${apiKey}` };+ if (isAnonymousCredential(apiKey)) return {};+ return { 'Authorization': `Bearer ${apiKey.trim()}` };}/** Requesty's Leanstral route rejects greedy sampling when temperature=0.