Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(providers): hand the REAL api key to provider clients

purplee0e0e3a653c6
diff --git a/src/api/providers/__tests__/registry-key.test.ts b/src/api/providers/__tests__/registry-key.test.ts
new file mode 100644
index 00000000..642415c0
--- /dev/null
+++ b/src/api/providers/__tests__/registry-key.test.ts
@@ -0,0 +1,62 @@
+/**
+ * Registry API-key handling — the store keeps keys obfuscated in memory;
+ * getProvider must hand the REAL key to the client. Regression test for the
+ * m9mx report (Discord 2026-07-26): Test button + chat sent a garbled Bearer
+ * token, so Groq said "Invalid API Key" and OpenRouter chat 401ed while its
+ * public /models let the Test button claim "connected".
+ * Run: npx vitest run src/api/providers/__tests__/registry-key.test.ts
+ */
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { useProviderStore } from '../../../stores/providerStore'
+import { getProvider, clearProviderCache } from '../registry'
+
+const REAL_KEY = 'sk-or-real-key-123456'
+
+describe('getProvider api key', () => {
+ beforeEach(() => {
+ clearProviderCache()
+ useProviderStore.getState().setProviderConfig('openai', {
+ enabled: true,
+ name: 'OpenRouter',
+ baseUrl: 'https://openrouter.ai/api/v1',
+ isLocal: false,
+ managed: false,
+ })
+ useProviderStore.getState().setProviderApiKey('openai', REAL_KEY)
+ })
+
+ it('store holds the key obfuscated in memory (test premise)', () => {
+ const raw = useProviderStore.getState().providers['openai'].apiKey
+ expect(raw).not.toBe(REAL_KEY)
+ expect(useProviderStore.getState().getProviderApiKey('openai')).toBe(REAL_KEY)
+ })
+
+ it('checkConnection sends the deobfuscated key as Bearer token', async () => {
+ let seenAuth: string | undefined
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init?: RequestInit) => {
+ seenAuth = (init?.headers as Record<string, string> | undefined)?.['Authorization']
+ return new Response(JSON.stringify({ data: [] }), { status: 200 })
+ }))
+
+ const ok = await getProvider('openai').checkConnection()
+ expect(ok).toBe(true)
+ expect(seenAuth).toBe(`Bearer ${REAL_KEY}`)
+
+ vi.unstubAllGlobals()
+ })
+
+ it('a key change invalidates the cached client', async () => {
+ const seen: Array<string | undefined> = []
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init?: RequestInit) => {
+ seen.push((init?.headers as Record<string, string> | undefined)?.['Authorization'])
+ return new Response(JSON.stringify({ data: [] }), { status: 200 })
+ }))
+
+ await getProvider('openai').checkConnection()
+ useProviderStore.getState().setProviderApiKey('openai', 'sk-rotated-999')
+ await getProvider('openai').checkConnection()
+ expect(seen).toEqual([`Bearer ${REAL_KEY}`, 'Bearer sk-rotated-999'])
+
+ vi.unstubAllGlobals()
+ })
+})
diff --git a/src/api/providers/registry.ts b/src/api/providers/registry.ts
index 56e20292..dfb1b082 100644
--- a/src/api/providers/registry.ts
+++ b/src/api/providers/registry.ts
@@ -48,8 +48,17 @@ function createClient(config: ProviderConfig): ProviderClient {
* Get a provider client by ID. Uses cached instance if config hasn't changed.
*/
export function getProvider(id: ProviderId): ProviderClient {
- const config = useProviderStore.getState().providers[id]
- if (!config) throw new Error(`Provider not configured: ${id}`)
+ const state = useProviderStore.getState()
+ const stored = state.providers[id]
+ if (!stored) throw new Error(`Provider not configured: ${id}`)
+
+ // The store keeps apiKey OBFUSCATED in memory (see providerStore). Handing
+ // the raw state to the client sent a garbled Bearer token on every chat /
+ // agent / Test-button request, while the model list (getEnabledProviders,
+ // which deobfuscates) worked — Groq answered "Invalid API Key", OpenRouter's
+ // public /models made Test claim "connected" and chat then 401ed
+ // (m9mx Discord 2026-07-26, four providers).
+ const config = { ...stored, apiKey: state.getProviderApiKey(id) }
const key = cacheKey(config)
let client = clientCache.get(key)