Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/buildProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
effectiveBaseUrl,
getProvider,
resolveApiKey,
sanitizeApiKey,
type Config,
} from '../config/index.ts';
import {
Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions src/providers/anthropic/AnthropicProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -73,17 +73,23 @@ export class AnthropicProvider implements ModelProvider {
async *stream(request: ModelRequest, signal: AbortSignal): AsyncIterable<ModelEvent> {
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);
Expand Down
15 changes: 15 additions & 0 deletions src/providers/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ export async function safeText(res: Response): Promise<string> {
return '<no body>';
}
}

/**
* 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 <key>` /
* the `x-api-key` — so a raw provider error can leak the API key. This removes
* the exact key and any `Bearer <token>` 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('<redacted>');
return out.replace(/Bearer\s+\S+/gi, 'Bearer <redacted>');
}
2 changes: 1 addition & 1 deletion src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 19 additions & 11 deletions src/providers/openai/OpenAiCompatibleProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
92 changes: 92 additions & 0 deletions tests/api-key.test.ts
Original file line number Diff line number Diff line change
@@ -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('<redacted>');
});

it('redacts any Bearer token even without the key in hand', () => {
expect(redactSecrets('auth failed: Bearer sk-live-999 rejected')).toBe(
'auth failed: Bearer <redacted> 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('<redacted>');
});
});