diff --git a/src/app/buildProvider.ts b/src/app/buildProvider.ts index 28a4ecd..b926919 100644 --- a/src/app/buildProvider.ts +++ b/src/app/buildProvider.ts @@ -3,6 +3,7 @@ import { effectiveBaseUrl, getProvider, resolveApiKey, + sanitizeApiKey, type Config, } from '../config/index.ts'; import { @@ -58,7 +59,10 @@ export function buildProvider(config: Config, options: { apiKey?: string } = {}) throw new UnsupportedProviderError(config.provider); } - const apiKey = options.apiKey ?? resolveApiKey(config.provider); + // Sanitize whether the key came from the env, the credentials store, or the + // `/model` dialog — all can carry a stray newline/space that would make the + // auth header illegal (and leak the key via undici's error). + const apiKey = sanitizeApiKey(options.apiKey ?? resolveApiKey(config.provider)); if (!apiKey) { throw new MissingApiKeyError(config.provider, apiKeyEnvHint(config.provider)); } diff --git a/src/config/index.ts b/src/config/index.ts index 9f9712b..6cd9a33 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -8,7 +8,7 @@ export { type Config, type ProviderName, } from './schema.ts'; -export { loadConfig, resolveApiKey, resolveSearchApiKey } from './loadConfig.ts'; +export { loadConfig, resolveApiKey, resolveSearchApiKey, sanitizeApiKey } from './loadConfig.ts'; export { configHome, databasePath, diff --git a/src/config/loadConfig.ts b/src/config/loadConfig.ts index a5e6193..f6e0075 100644 --- a/src/config/loadConfig.ts +++ b/src/config/loadConfig.ts @@ -52,6 +52,23 @@ export function resolveApiKey(provider: string): string | undefined { return getCredential(provider); } +/** + * Scrub an API key so it is always a legal HTTP header value. API keys are + * visible-ASCII tokens (`[!-~]`); anything else — a trailing newline from + * `.env`/`credentials.json`, a line break from a wrapped paste, a stray space, + * or a smart-quote — is removed. This matters because undici *throws* on a + * header value containing a control character (e.g. an embedded `\n`) and its + * error message echoes the key verbatim, so an unsanitized key both breaks the + * request and risks leaking the secret into logs. Removing (not rejecting) the + * junk means a wrapped paste "just works". Returns `undefined` if nothing is + * left (the key was only whitespace). + */ +export function sanitizeApiKey(raw: string | undefined): string | undefined { + if (raw === undefined) return undefined; + const cleaned = raw.replace(/[^\x21-\x7e]/g, ''); + return cleaned === '' ? undefined : cleaned; +} + /** * Resolve the web-search (Tavily) API key: the `TAVILY_API_KEY` environment * variable first, then the on-disk credentials store under the `tavily` key diff --git a/src/providers/anthropic/AnthropicProvider.ts b/src/providers/anthropic/AnthropicProvider.ts index 7ccb4d1..01c3928 100644 --- a/src/providers/anthropic/AnthropicProvider.ts +++ b/src/providers/anthropic/AnthropicProvider.ts @@ -2,7 +2,7 @@ import { newToolCallId, type ToolCall } from '../../protocol/index.ts'; import type { ModelEvent } from '../ModelEvent.ts'; import type { ModelRequest } from '../ModelRequest.ts'; import type { ModelCapabilities, ModelProvider } from '../ModelProvider.ts'; -import { ProviderHttpError, safeText } from '../errors.ts'; +import { ProviderHttpError, redactSecrets, safeText } from '../errors.ts'; import { parseSseStream } from '../openai/sse.ts'; import { toMessagesRequest } from './messagesRequest.ts'; @@ -73,17 +73,23 @@ export class AnthropicProvider implements ModelProvider { async *stream(request: ModelRequest, signal: AbortSignal): AsyncIterable { const body = toMessagesRequest(request, { maxTokens: this.maxTokens }); - const res = await this.fetchImpl(`${this.baseUrl}/messages`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Accept: 'text/event-stream', - 'x-api-key': this.apiKey, - 'anthropic-version': this.anthropicVersion, - }, - body: JSON.stringify(body), - signal, - }); + let res: Response; + try { + res = await this.fetchImpl(`${this.baseUrl}/messages`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'text/event-stream', + 'x-api-key': this.apiKey, + 'anthropic-version': this.anthropicVersion, + }, + body: JSON.stringify(body), + signal, + }); + } catch (err) { + const msg = redactSecrets(err instanceof Error ? err.message : String(err), this.apiKey); + throw new ProviderHttpError(0, `${this.name} request failed: ${msg}`); + } if (!res.ok) { const detail = await safeText(res); diff --git a/src/providers/errors.ts b/src/providers/errors.ts index 9500cf7..0d1e1c3 100644 --- a/src/providers/errors.ts +++ b/src/providers/errors.ts @@ -21,3 +21,18 @@ export async function safeText(res: Response): Promise { return ''; } } + +/** + * Strip secrets from an error message before it is surfaced (to logs, the + * `turn.failed` event, or the UI). undici's "invalid header value" TypeError + * echoes the offending header value verbatim — which for us is `Bearer ` / + * the `x-api-key` — so a raw provider error can leak the API key. This removes + * the exact key and any `Bearer ` sequence. Keys are sanitized upstream + * so this error should not normally fire, but it is the safety net that + * guarantees a key never reaches a log line. + */ +export function redactSecrets(message: string, secret?: string): string { + let out = message; + if (secret && secret.length >= 4) out = out.split(secret).join(''); + return out.replace(/Bearer\s+\S+/gi, 'Bearer '); +} diff --git a/src/providers/index.ts b/src/providers/index.ts index b9b4b88..26b42e6 100644 --- a/src/providers/index.ts +++ b/src/providers/index.ts @@ -15,4 +15,4 @@ export { type FetchLike, } from './openai/OpenAiCompatibleProvider.ts'; export { AnthropicProvider, type AnthropicOptions } from './anthropic/AnthropicProvider.ts'; -export { ProviderHttpError } from './errors.ts'; +export { ProviderHttpError, redactSecrets } from './errors.ts'; diff --git a/src/providers/openai/OpenAiCompatibleProvider.ts b/src/providers/openai/OpenAiCompatibleProvider.ts index 094b998..2a351b3 100644 --- a/src/providers/openai/OpenAiCompatibleProvider.ts +++ b/src/providers/openai/OpenAiCompatibleProvider.ts @@ -2,7 +2,7 @@ import { newToolCallId, type ToolCall } from '../../protocol/index.ts'; import type { ModelEvent } from '../ModelEvent.ts'; import type { ModelRequest } from '../ModelRequest.ts'; import type { ModelCapabilities, ModelProvider } from '../ModelProvider.ts'; -import { ProviderHttpError, safeText } from '../errors.ts'; +import { ProviderHttpError, redactSecrets, safeText } from '../errors.ts'; import { toChatRequest } from './chatRequest.ts'; import { parseSseStream } from './sse.ts'; @@ -67,16 +67,24 @@ export class OpenAiCompatibleProvider implements ModelProvider { ...(this.maxTokens !== undefined ? { maxTokens: this.maxTokens } : {}), }); - const res = await this.fetchImpl(`${this.baseUrl}/chat/completions`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Accept: 'text/event-stream', - Authorization: `Bearer ${this.apiKey}`, - }, - body: JSON.stringify(body), - signal, - }); + let res: Response; + try { + res = await this.fetchImpl(`${this.baseUrl}/chat/completions`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'text/event-stream', + Authorization: `Bearer ${this.apiKey}`, + }, + body: JSON.stringify(body), + signal, + }); + } catch (err) { + // Never let a transport error carry the key (undici's invalid-header-value + // TypeError echoes the Authorization value verbatim). + const msg = redactSecrets(err instanceof Error ? err.message : String(err), this.apiKey); + throw new ProviderHttpError(0, `${this.name} request failed: ${msg}`); + } if (!res.ok) { const detail = await safeText(res); diff --git a/tests/api-key.test.ts b/tests/api-key.test.ts new file mode 100644 index 0000000..d56e491 --- /dev/null +++ b/tests/api-key.test.ts @@ -0,0 +1,92 @@ +import { describe, it, expect } from 'vitest'; +import { sanitizeApiKey } from '../src/config/index.ts'; +import { redactSecrets, OpenAiCompatibleProvider, type FetchLike } from '../src/providers/index.ts'; +import type { ModelRequest } from '../src/providers/index.ts'; + +describe('sanitizeApiKey', () => { + it('leaves a clean key untouched', () => { + expect(sanitizeApiKey('nvapi-abc123')).toBe('nvapi-abc123'); + }); + + it('strips a trailing newline (common from .env / echo > file)', () => { + expect(sanitizeApiKey('nvapi-abc123\n')).toBe('nvapi-abc123'); + expect(sanitizeApiKey('nvapi-abc123\r\n')).toBe('nvapi-abc123'); + }); + + it('strips an EMBEDDED newline (the wrapped-paste case that broke the header)', () => { + // undici rejects a header value with an interior control char; removing it + // reassembles the intended key. + expect(sanitizeApiKey('nvapi-abc\ndef')).toBe('nvapi-abcdef'); + expect(sanitizeApiKey('nvapi-abc\r\n def')).toBe('nvapi-abcdef'); + }); + + it('strips surrounding whitespace, tabs, and non-ASCII', () => { + expect(sanitizeApiKey(' nvapi-abc ')).toBe('nvapi-abc'); + expect(sanitizeApiKey('\tnvapi-abc\t')).toBe('nvapi-abc'); + expect(sanitizeApiKey('nvapi abc')).toBe('nvapiabc'); // non-breaking space + }); + + it('returns undefined for undefined or whitespace-only input', () => { + expect(sanitizeApiKey(undefined)).toBeUndefined(); + expect(sanitizeApiKey(' \n\t ')).toBeUndefined(); + expect(sanitizeApiKey('')).toBeUndefined(); + }); + + it('produces a value that is a legal HTTP header (the actual invariant)', () => { + const key = sanitizeApiKey('nvapi-abc\ndef\n')!; + // Would throw "invalid header value" before sanitizing. + expect(() => new Headers({ Authorization: `Bearer ${key}` })).not.toThrow(); + }); +}); + +describe('redactSecrets', () => { + it('removes the exact key when present in an error message', () => { + const msg = 'Headers.append: "Bearer nvapi-supersecret" is an invalid header value.'; + const out = redactSecrets(msg, 'nvapi-supersecret'); + expect(out).not.toContain('nvapi-supersecret'); + expect(out).toContain(''); + }); + + it('redacts any Bearer token even without the key in hand', () => { + expect(redactSecrets('auth failed: Bearer sk-live-999 rejected')).toBe( + 'auth failed: Bearer rejected', + ); + }); + + it('does not redact short or absent secrets', () => { + expect(redactSecrets('plain error', undefined)).toBe('plain error'); + expect(redactSecrets('key ab here', 'ab')).toBe('key ab here'); // too short to redact + }); +}); + +describe('provider never leaks the key in a transport error', () => { + const request: ModelRequest = { + model: 'm', + system: 's', + messages: [{ role: 'user', content: 'hi' }], + tools: [], + }; + + it('scrubs an undici-style invalid-header error that echoes the key', async () => { + const key = 'nvapi-leaky-secret-key'; + // Simulate undici throwing with the Authorization value in the message. + const fetchImpl: FetchLike = () => { + throw new TypeError(`Headers.append: "Bearer ${key}" is an invalid header value.`); + }; + const provider = new OpenAiCompatibleProvider({ + name: 'nvidia', + baseUrl: 'https://x.test/v1', + apiKey: key, + fetchImpl, + }); + let caught: unknown; + try { + for await (const _ of provider.stream(request, new AbortController().signal)) void _; + } catch (e) { + caught = e; + } + const message = caught instanceof Error ? caught.message : String(caught); + expect(message).not.toContain(key); + expect(message).toContain(''); + }); +});