Skip to content

Commit c2791d1

Browse files
fix: reconcile with BuildUpstreamHeaders
1 parent 5a0b836 commit c2791d1

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

provider/anthropic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,20 @@ func (p *Anthropic) CreateInterceptor(w http.ResponseWriter, r *http.Request, tr
121121
// WithAuthToken(), and clear the centralized key.
122122
// - X-Api-Key: <api-key> → personal API key. Overwrite the
123123
// centralized key with the user's key.
124+
authHeaderName := p.AuthHeader()
124125
if bearer := r.Header.Get("Authorization"); bearer != "" {
125126
cfg.BYOKBearerToken = strings.TrimPrefix(bearer, "Bearer ")
126127
cfg.Key = ""
128+
authHeaderName = "Authorization"
127129
} else if apiKey := r.Header.Get("X-Api-Key"); apiKey != "" {
128130
cfg.Key = apiKey
129131
}
130132

131133
var interceptor intercept.Interceptor
132134
if req.Stream {
133-
interceptor = messages.NewStreamingInterceptor(id, &req, payload, cfg, p.bedrockCfg, r.Header, p.AuthHeader(), tracer)
135+
interceptor = messages.NewStreamingInterceptor(id, &req, payload, cfg, p.bedrockCfg, r.Header, authHeaderName, tracer)
134136
} else {
135-
interceptor = messages.NewBlockingInterceptor(id, &req, payload, cfg, p.bedrockCfg, r.Header, p.AuthHeader(), tracer)
137+
interceptor = messages.NewBlockingInterceptor(id, &req, payload, cfg, p.bedrockCfg, r.Header, authHeaderName, tracer)
136138
}
137139
span.SetAttributes(interceptor.TraceAttributes(r)...)
138140
return interceptor, nil

provider/anthropic_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ func TestAnthropic_CreateInterceptor(t *testing.T) {
8686
body := `{"model": "claude-opus-4-5", "max_tokens": 1024, "messages": [{"role": "user", "content": "hello"}], "stream": false}`
8787
req := httptest.NewRequest(http.MethodPost, routeMessages, bytes.NewBufferString(body))
8888
req.Header.Set("Anthropic-Beta", betaHeader)
89-
// Simulate a client sending its own auth credential, which must be replaced
90-
// by aibridge with the configured provider key.
91-
req.Header.Set("Authorization", "Bearer fake-client-bearer")
89+
req.Header.Set("X-Custom-Header", "should-be-forwarded")
90+
// In centralized mode, http.go strips Authorization and X-Api-Key
91+
// before CreateInterceptor is called, so neither is set here.
9292
w := httptest.NewRecorder()
9393

9494
interceptor, err := provider.CreateInterceptor(w, req, testTracer)
@@ -105,9 +105,12 @@ func TestAnthropic_CreateInterceptor(t *testing.T) {
105105
// Verify the full Anthropic-Beta header (all betas) was forwarded unchanged.
106106
assert.Equal(t, betaHeader, receivedHeaders.Get("Anthropic-Beta"), "Anthropic-Beta header must be forwarded unchanged to upstream")
107107

108-
// Verify aibridge's configured key was used and the client's auth credential was not forwarded.
108+
// Verify aibridge's configured key was used.
109109
assert.Equal(t, "test-key", receivedHeaders.Get("X-Api-Key"), "upstream must receive configured provider key")
110-
assert.Empty(t, receivedHeaders.Get("Authorization"), "client Authorization header must not reach upstream")
110+
assert.Empty(t, receivedHeaders.Get("Authorization"), "Authorization should not be set in centralized mode")
111+
112+
// Verify custom client headers are forwarded.
113+
assert.Equal(t, "should-be-forwarded", receivedHeaders.Get("X-Custom-Header"), "custom client headers should be forwarded")
111114
})
112115

113116
byokTests := []struct {

0 commit comments

Comments
 (0)