|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { ProviderFailure } from "../../src/util/provider-failure" |
| 3 | + |
| 4 | +// Verbatim record taken from the `message` table of a real failed session; the |
| 5 | +// URL is what revealed the request had gone to an Anthropic path while an |
| 6 | +// OpenAI-compatible provider was configured. |
| 7 | +const RECORDED = { |
| 8 | + name: "APIError", |
| 9 | + data: { |
| 10 | + message: "404 Page not found", |
| 11 | + statusCode: 404, |
| 12 | + isRetryable: false, |
| 13 | + responseHeaders: { |
| 14 | + "minimax-request-id": "fdf4f50a92ec5a88f79125fedaa3ed3b", |
| 15 | + authorization: "Bearer sk-live-do-not-print-me", |
| 16 | + }, |
| 17 | + responseBody: "404 page not found", |
| 18 | + metadata: { |
| 19 | + providerID: "minimax", |
| 20 | + modelID: "MiniMax-M3", |
| 21 | + url: "https://api.minimax.chat/v1/messages", |
| 22 | + }, |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +describe("ProviderFailure.describe", () => { |
| 27 | + test("turns the recorded failure into a diagnosable message", () => { |
| 28 | + expect(ProviderFailure.describe("404 Page not found", RECORDED)).toBe( |
| 29 | + [ |
| 30 | + "404 Page not found", |
| 31 | + " provider minimax/MiniMax-M3", |
| 32 | + " request https://api.minimax.chat/v1/messages", |
| 33 | + " status 404", |
| 34 | + ].join("\n"), |
| 35 | + ) |
| 36 | + }) |
| 37 | + |
| 38 | + test("never echoes response headers, where bearer tokens live", () => { |
| 39 | + expect(ProviderFailure.describe("404 Page not found", RECORDED)).not.toContain("sk-live-do-not-print-me") |
| 40 | + expect(ProviderFailure.describe("404 Page not found", RECORDED)).not.toContain("authorization") |
| 41 | + }) |
| 42 | + |
| 43 | + test("falls back to the caller's provider and model for records written before they were recorded", () => { |
| 44 | + const legacy = { name: "APIError", data: { message: "boom", metadata: { url: "https://api.acme.dev/v1/chat" } } } |
| 45 | + expect(ProviderFailure.describe("boom", legacy, { providerID: "acme", modelID: "acme-1" })).toContain( |
| 46 | + "provider acme/acme-1", |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + test("prefers the recorded provider over the caller's fallback", () => { |
| 51 | + expect(ProviderFailure.describe("404 Page not found", RECORDED, { providerID: "wrong", modelID: "wrong" })).toContain( |
| 52 | + "provider minimax/MiniMax-M3", |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + test("returns the message untouched when there is no transport context", () => { |
| 57 | + expect(ProviderFailure.describe("Aborted", { name: "MessageAbortedError", data: { message: "Aborted" } })).toBe( |
| 58 | + "Aborted", |
| 59 | + ) |
| 60 | + expect(ProviderFailure.describe("nope", undefined)).toBe("nope") |
| 61 | + expect(ProviderFailure.describe("nope", "a string")).toBe("nope") |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +describe("ProviderFailure.redactURL", () => { |
| 66 | + test("keeps scheme, host, port and path, which are the diagnosis", () => { |
| 67 | + expect(ProviderFailure.redactURL("https://api.minimax.chat:8443/v1/messages")).toBe( |
| 68 | + "https://api.minimax.chat:8443/v1/messages", |
| 69 | + ) |
| 70 | + }) |
| 71 | + |
| 72 | + test("redacts a key passed as a query parameter", () => { |
| 73 | + // Google's Generative Language API takes the API key this way. |
| 74 | + expect(ProviderFailure.redactURL("https://generativelanguage.googleapis.com/v1/models:generate?key=AIzaSyREAL")).toBe( |
| 75 | + `https://generativelanguage.googleapis.com/v1/models:generate?key=${ProviderFailure.REDACTED}`, |
| 76 | + ) |
| 77 | + }) |
| 78 | + |
| 79 | + test("redacts query values it has never seen, not just key-shaped names", () => { |
| 80 | + const redacted = ProviderFailure.redactURL("https://api.acme.dev/v1?wildcard=SECRET&sig=SECRET&token=SECRET") |
| 81 | + expect(redacted).not.toContain("SECRET") |
| 82 | + }) |
| 83 | + |
| 84 | + test("redacts credentials in userinfo", () => { |
| 85 | + expect(ProviderFailure.redactURL("https://admin:[email protected]/v1/chat")).toBe("https://api.acme.dev/v1/chat") |
| 86 | + }) |
| 87 | + |
| 88 | + test("drops the fragment, which can carry a token", () => { |
| 89 | + expect(ProviderFailure.redactURL("https://api.acme.dev/v1/chat#access_token=SECRET")).toBe( |
| 90 | + "https://api.acme.dev/v1/chat", |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + test("collapses a repeated credential parameter so no copy survives", () => { |
| 95 | + expect(ProviderFailure.redactURL("https://api.acme.dev/v1?key=SECRET&key=SECRET2")).not.toContain("SECRET") |
| 96 | + }) |
| 97 | + |
| 98 | + test("keeps the allowlisted api-version, which is itself a diagnosis", () => { |
| 99 | + expect( |
| 100 | + ProviderFailure.redactURL("https://acme.openai.azure.com/openai/deployments/gpt/chat?api-version=2024-06-01"), |
| 101 | + ).toBe("https://acme.openai.azure.com/openai/deployments/gpt/chat?api-version=2024-06-01") |
| 102 | + }) |
| 103 | + |
| 104 | + test("matches the allowlist case-insensitively", () => { |
| 105 | + expect(ProviderFailure.redactURL("https://api.acme.dev/v1?API-Version=2024-06-01")).toContain("2024-06-01") |
| 106 | + }) |
| 107 | + |
| 108 | + test("withholds the query of an unparseable URL rather than guessing", () => { |
| 109 | + expect(ProviderFailure.redactURL("api.acme.dev/v1/chat?key=SECRET")).toBe( |
| 110 | + `api.acme.dev/v1/chat?${ProviderFailure.REDACTED}`, |
| 111 | + ) |
| 112 | + expect(ProviderFailure.redactURL("?key=SECRET")).toBe(ProviderFailure.REDACTED) |
| 113 | + expect(ProviderFailure.redactURL("")).toBe(ProviderFailure.REDACTED) |
| 114 | + }) |
| 115 | + |
| 116 | + test("no query value survives for any parameter name a provider might invent", () => { |
| 117 | + // The allowlist is the guarantee: an unknown name can never leak its value. |
| 118 | + const names = ["key", "api_key", "apikey", "access_token", "sig", "signature", "password", "x-goog-api-key", "zzz"] |
| 119 | + for (const name of names) { |
| 120 | + expect(ProviderFailure.redactURL(`https://api.acme.dev/v1?${name}=SUPERSECRET`)).not.toContain("SUPERSECRET") |
| 121 | + } |
| 122 | + }) |
| 123 | +}) |
0 commit comments