Skip to content

Commit e7ca0b0

Browse files
committed
fix(api): echo only the endpoint origin in the credential-drop warning
CodeRabbit (security minor): the drop warning interpolated ollamaBaseUrl verbatim, so URL credentials (user:pass@) or sensitive query values in the base URL leaked to the log. The warning now derives the WHATWG origin (protocol//host:port) via new URL() and falls back to a fixed phrase for an unparseable host. Tests cover a base URL carrying credentials and a query token: the warning must contain the origin and must not contain the secret, the token value, or the word "token"; the secure-endpoint test now also pins the attached Authorization header. Negative control: echoing the raw host fails exactly the drop-warning test.
1 parent 6e216e2 commit e7ca0b0

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,9 @@ describe("NativeOllamaHandler", () => {
22102210
const handler = new NativeOllamaHandler({
22112211
apiModelId: "llama2",
22122212
ollamaModelId: "llama2",
2213-
ollamaBaseUrl: "http://ollama.example.com:11434",
2213+
// Credentials and a sensitive query value ride along: the warning
2214+
// must echo only the origin, never these.
2215+
ollamaBaseUrl: "http://user:[email protected]:11434/v1?token=***",
22142216
ollamaApiKey: "test-api-key-123",
22152217
})
22162218

@@ -2221,9 +2223,15 @@ describe("NativeOllamaHandler", () => {
22212223
const callArgs = OllamaMock.mock.calls[0][0]
22222224
expect(callArgs.headers).toBeUndefined()
22232225
// The silent drop is a diagnosis trap (bare 401 behind an auth
2224-
// proxy), so the gate warns - without echoing the key itself.
2226+
// proxy), so the gate warns - without echoing the key or any URL
2227+
// credential/query material, only the protocol/host:port origin.
2228+
const warned = warnSpy.mock.calls.flat().join(" ")
22252229
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("ollamaApiKey"))
2226-
expect(warnSpy.mock.calls.flat().join(" ")).not.toContain("test-api-key-123")
2230+
expect(warned).not.toContain("test-api-key-123")
2231+
expect(warned).toContain("ollama.example.com:11434")
2232+
expect(warned).not.toContain("secret")
2233+
expect(warned).not.toContain("abc123")
2234+
expect(warned).not.toContain("token")
22272235
})
22282236

22292237
it("should not warn when the API key is attached to a secure endpoint", async () => {
@@ -2235,12 +2243,16 @@ describe("NativeOllamaHandler", () => {
22352243
const handler = new NativeOllamaHandler({
22362244
apiModelId: "llama2",
22372245
ollamaModelId: "llama2",
2238-
ollamaBaseUrl: "https://ollama.example.com:11434",
2246+
// Credentials on a secure endpoint must not trigger the drop
2247+
// warning either.
2248+
ollamaBaseUrl: "https://user:[email protected]:11434",
22392249
ollamaApiKey: "test-api-key-123",
22402250
})
22412251

22422252
await handler.completePrompt("Test prompt")
22432253

2254+
const callArgs = OllamaMock.mock.calls[0][0]
2255+
expect(callArgs.headers).toEqual({ Authorization: "Bearer test-api-key-123" })
22442256
expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining("ollamaApiKey"))
22452257
})
22462258

‎src/api/providers/native-ollama.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,20 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
271271
} else {
272272
// The credential is dropped to avoid leaking it over plaintext
273273
// HTTP (CWE-319); surface the drop so a bare 401 from an auth
274-
// proxy is diagnosable. The key itself is never logged.
274+
// proxy is diagnosable. Neither the key nor the raw URL is
275+
// logged: a base URL can embed credentials or sensitive query
276+
// values, so only the WHATWG origin (protocol/host:port) is
277+
// echoed, and an unparseable host falls back to a fixed phrase.
278+
let endpointOrigin = "the configured endpoint"
279+
try {
280+
const parsed = new URL(clientOptions.host)
281+
endpointOrigin = `${parsed.protocol}//${parsed.host}`
282+
} catch {
283+
// Unparseable host: keep the fixed phrase rather than echo
284+
// a raw value that could carry credentials.
285+
}
275286
console.warn(
276-
`[NativeOllama] ollamaApiKey is set but the endpoint "${clientOptions.host}" is not https or loopback; the Authorization header is not sent (the credential would leak over plaintext). Use an https:// or loopback endpoint to authenticate.`,
287+
`[NativeOllama] ollamaApiKey is set but the endpoint "${endpointOrigin}" is not https or loopback; the Authorization header is not sent (the credential would leak over plaintext). Use an https:// or loopback endpoint to authenticate.`,
277288
)
278289
}
279290
}

0 commit comments

Comments
 (0)