Skip to content
Open
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
21 changes: 20 additions & 1 deletion packages/proxy/src/providers/anthropic.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { it, expect, describe } from "vitest";
import { callProxyV1, createCapturingFetch } from "../../utils/tests";
import { FetchFn } from "../proxy";
import { anthropicAuthHeaders, FetchFn } from "../proxy";
import {
OpenAIChatCompletion,
OpenAIChatCompletionChunk,
OpenAIChatCompletionCreateParams,
} from "@types";
import { APISecretSchema } from "@schema";
import { omitUnsupportedAnthropicParams } from "./anthropic";
import {
IMAGE_DATA_URL,
Expand Down Expand Up @@ -72,6 +73,24 @@ it("should request identity encoding for streaming Anthropic chat completions",
expect(requests[0].headers["accept-encoding"]).toBe("identity");
});

it("should use bearer authorization for Anthropic WIF secrets", () => {
const headers = anthropicAuthHeaders(
APISecretSchema.parse({
type: "anthropic",
secret: "test-wif-access-token",
name: "anthropic",
metadata: {
auth_type: "oauth_bearer",
auth_source: "anthropic_workload_identity_federation",
},
}),
);

expect(headers).toEqual({
authorization: "Bearer test-wif-access-token",
});
});

it("should mark streaming responses as no-transform", async () => {
const encoder = new TextEncoder();
const anthropicEvents = [
Expand Down
28 changes: 26 additions & 2 deletions packages/proxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ const GOOGLE_URL_REGEX =

const GOOGLE_API_KEY_HEADER = "x-goog-api-key";

function isAnthropicOAuthBearerSecret(secret: APISecret) {
return (
secret.type === "anthropic" &&
secret.metadata !== null &&
secret.metadata !== undefined &&
"auth_type" in secret.metadata &&
secret.metadata.auth_type === "oauth_bearer"
);
}

export function anthropicAuthHeaders(
secret: APISecret,
): Record<string, string> {
if (isAnthropicOAuthBearerSecret(secret)) {
return {
authorization: `Bearer ${secret.secret}`,
};
}

return {
"x-api-key": secret.secret,
};
}

// Options to control how the cache key is generated.
export interface CacheKeyOptions {
excludeAuthToken?: boolean;
Expand Down Expand Up @@ -2666,7 +2690,7 @@ async function fetchAnthropicMessages({
{
method: "POST",
headers: {
"x-api-key": secret.secret,
...anthropicAuthHeaders(secret),
"content-type": "application/json",
"anthropic-version": "2023-06-01",
},
Expand Down Expand Up @@ -2769,7 +2793,7 @@ async function fetchAnthropicChatCompletions({
headers["accept"] = "application/json";
headers["anthropic-version"] = "2023-06-01";
headers["host"] = fullURL.host;
headers["x-api-key"] = secret.secret;
Object.assign(headers, anthropicAuthHeaders(secret));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Drop stale x-api-key before setting Anthropic bearer auth

In the /chat/completions Anthropic path, this line only merges authorization for WIF secrets, but the mutable headers object can already contain the caller's proxy auth x-api-key. When clients authenticate to this proxy with x-api-key, that token is forwarded upstream together with the new bearer token, which can break Anthropic auth and leaks the proxy credential to the model provider. Clear x-api-key before applying bearer auth for this flow.

Useful? React with 👍 / 👎.

}

if (isEmpty(bodyData)) {
Expand Down
Loading