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(google): correct validateKey error taxonomy so good keys aren't auto-disabled (#268)
Tashfeend87a7175efa4
diff --git a/server/src/__tests__/providers/google.test.ts b/server/src/__tests__/providers/google.test.tsindex 8c4677fe..c3b7dee1 100644--- a/server/src/__tests__/providers/google.test.ts+++ b/server/src/__tests__/providers/google.test.ts@@ -89,6 +89,44 @@ describe('GoogleProvider', () => {expect(await provider.validateKey('invalid-key')).toBe(false);});+ // #268: Google reports a bad key as HTTP 400 INVALID_ARGUMENT / API_KEY_INVALID,+ // not 401/403. A confirmed-bad key must return false (→ auto-disable counter).+ it('validateKey returns false for a genuinely invalid key (HTTP 400 API_KEY_INVALID)', async () => {+ vi.spyOn(global, 'fetch').mockResolvedValueOnce({+ ok: false,+ status: 400,+ json: () => Promise.resolve({+ error: {+ code: 400,+ message: 'API key not valid. Please pass a valid API key.',+ status: 'INVALID_ARGUMENT',+ details: [{ '@type': 'type.googleapis.com/google.rpc.ErrorInfo', reason: 'API_KEY_INVALID' }],+ },+ }),+ } as any);+ expect(await provider.validateKey('bad-key')).toBe(false);+ });++ // #268: a permission/region/restriction 403 (e.g. API not enabled on the project,+ // or an IP/API-key restriction on the proxy host) must NOT auto-disable a key that+ // may still work for generateContent elsewhere — validateKey throws so health.ts+ // records status='error' instead of incrementing the disable counter.+ it('validateKey throws (does not return false) on a permission/region 403', async () => {+ vi.spyOn(global, 'fetch').mockResolvedValueOnce({+ ok: false,+ status: 403,+ json: () => Promise.resolve({+ error: {+ code: 403,+ message: 'Generative Language API has not been used in project before or it is disabled.',+ status: 'PERMISSION_DENIED',+ details: [{ '@type': 'type.googleapis.com/google.rpc.ErrorInfo', reason: 'SERVICE_DISABLED' }],+ },+ }),+ } as any);+ await expect(provider.validateKey('region-blocked-key')).rejects.toThrow(/inconclusive/i);+ });+it('should translate system messages to systemInstruction', async () => {let capturedBody: any;vi.spyOn(global, 'fetch').mockImplementation(async (_url, init) => {diff --git a/server/src/providers/google.ts b/server/src/providers/google.tsindex 97f4cc3e..c9f83b2c 100644--- a/server/src/providers/google.ts+++ b/server/src/providers/google.ts@@ -573,12 +573,48 @@ export class GoogleProvider extends BaseProvider {async validateKey(apiKey: string): Promise<boolean> {// Transport errors propagate — health.ts marks status='error' without- // counting toward auto-disable. Only confirmed 401/403 disables a key.+ // counting toward auto-disable.const res = await this.fetchWithTimeout(`${API_BASE}/models?key=${apiKey}`,{ method: 'GET' },10000,);- return res.status !== 401 && res.status !== 403;+ if (res.ok) return true;++ // Google's error taxonomy is NOT the usual 401/403-means-bad-key (#268):+ // - bad/expired key → HTTP 400 INVALID_ARGUMENT / reason API_KEY_INVALID+ // - API not enabled on project → HTTP 403 PERMISSION_DENIED / reason SERVICE_DISABLED+ // - IP / referrer / API-key restriction, or empty key → HTTP 403 PERMISSION_DENIED+ // - unsupported region → HTTP 400 FAILED_PRECONDITION ("User location is not supported")+ // The old check (`status !== 401 && status !== 403`) had this exactly+ // backwards: it marked a genuinely-bad 400 key as HEALTHY, and auto-disabled+ // a perfectly good key that merely hit a permission/region/restriction 403 on+ // the host running the proxy (the key still works for generateContent from+ // another network). So only a CONFIRMED bad credential returns false (which+ // counts toward auto-disable); every other non-2xx is inconclusive and throws+ // so health.ts records status='error' WITHOUT disabling a usable key.+ type GoogleErrorBody = { error?: { message?: unknown; status?: unknown; details?: unknown } };+ let body: GoogleErrorBody | null = null;+ try { body = (await res.json()) as GoogleErrorBody; } catch { /* non-JSON error body */ }+ const err = body?.error;+ const details = Array.isArray(err?.details) ? err!.details as Array<{ reason?: unknown }> : [];+ const reason = details.find(d => typeof d?.reason === 'string')?.reason as string | undefined;+ const message = typeof err?.message === 'string' ? err.message : '';+ const gStatus = typeof err?.status === 'string' ? err.status : undefined;++ const badCredentials =+ res.status === 401 ||+ reason === 'API_KEY_INVALID' ||+ /API key not valid|API key expired|API_KEY_INVALID/i.test(message);+ if (badCredentials) {+ console.warn(`[Google] validateKey: key rejected as invalid (HTTP ${res.status}${reason ? ` ${reason}` : ''})`);+ return false;+ }++ console.warn(+ `[Google] validateKey: inconclusive HTTP ${res.status} (${gStatus ?? 'UNKNOWN'}${reason ? `/${reason}` : ''}): ${message.slice(0, 200)} ` ++ `— treating as 'error', not auto-disabling (the key may be valid but blocked by region/permission/restriction on this host).`,+ );+ throw new Error(`Google key validation inconclusive (HTTP ${res.status}${gStatus ? ` ${gStatus}` : ''})`);}}