Files
CatoandClaude Opus 5.5 4fd7cf0195 fix(settings): API keys are sealed at rest and checked before they are saved (#208)
* fix(settings): API keys are sealed at rest and checked before they are saved

Readers' Groq/OpenRouter keys sat in user_settings as plaintext, and GET
/api/settings returned the whole row — keys included — to the browser.

- Keys are sealed with @bitbaum/ai-kit/seal (BYOK_SEAL_SECRET); GET returns
  only a hint (…abcd). Without the secret nothing new is stored (503).
- Legacy plaintext rows keep working and are sealed in place on first read.
- Save checks a new key with probeByokKey first; a key the provider refuses is
  never stored, and the reason is the provider's own words.
- An empty key field keeps the saved key (removing is explicit); the old PUT
  wrote key || null, which would have wiped keys once they stopped being sent.
- The key check now runs on its own after pasting, and actually passes:
  both pages read the jsonSuccess payload at the top level, so settings never
  loaded and every key — good or bad — was reported invalid.
- OpenRouter is checked on /key: its /models is public and 200s a dead key.

ai-kit ^1.18.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(settings): type the seal-secret lookup for plain env objects

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 12:35:30 -07:00

50 lines
1.5 KiB
TypeScript

/**
* Shared user LLM settings lookup
* @module lib/chat/settings
*/
import { getServiceClient } from '@/lib/supabase';
import type { ModelProvider } from '@/lib/llm-client';
import { KEY_COLUMN, readStoredKey, sealKey, sealingSecret } from '@/lib/byok-vault';
export interface LLMSettings {
provider: ModelProvider;
apiKey: string | null | undefined;
ollamaUrl: string | null | undefined;
}
/**
* Look up a user's preferred LLM provider and API keys.
* Falls back to 'groq' if no settings exist.
*
* Keys are stored sealed (lib/byok-vault.ts). A key an older version stored in
* the clear still works, and is sealed in place here the first time it is read.
*/
export async function getUserLLMSettings(userId: string): Promise<LLMSettings> {
const supabase = getServiceClient();
const { data: settings } = await supabase
.from('user_settings')
.select('*')
.eq('id', userId)
.single();
const provider: ModelProvider = settings?.preferred_model || 'groq';
const ollamaUrl = settings?.ollama_url;
if (provider !== 'groq' && provider !== 'openrouter') {
return { provider, apiKey: null, ollamaUrl };
}
const column = KEY_COLUMN[provider];
const secret = sealingSecret();
const { key, legacy } = readStoredKey(settings?.[column], secret);
if (key && legacy && secret) {
// Best effort: a failed write leaves the key usable and tries again next read.
await supabase
.from('user_settings')
.update({ [column]: sealKey(key, secret) })
.eq('id', userId);
}
return { provider, apiKey: key, ollamaUrl };
}