Files
coder/aibridge/intercept/client_headers.go
T
Susana Ferreira b7635b5036 fix(aibridge): strip proxy headers from bridge requests to fix Bedrock SigV4 signing (#26019)
## Problem

On bridge routes, aibridge acts as a client and originates new outbound
requests via the SDK. Proxy headers (`X-Forwarded-For`,
`X-Forwarded-Host`, etc.) from the inbound client request were forwarded
on the outbound request. The SigV4 signer signs all headers present, so
any in-transit modification by an egress proxy (e.g. appending an IP to
`X-Forwarded-For`) invalidated the signature, causing AWS Bedrock to
reject the request with:

> 403: "The request signature we calculated does not match the signature
you provided."

## Changes

- Strip proxy headers in `PrepareClientHeaders` on bridge routes
- Add unit test for proxy header stripping in `client_headers_test.go`
- Add integration test that verifies SigV4 signature remains valid after
an egress proxy modifies headers in transit
- Add integration test that verifies passthrough routes still set
forwarded headers correctly

Related to internal [Slack
thread](https://codercom.slack.com/archives/C096PFVBZKN/p1779919049215969).

> 🤖 Generated by Coder Agents, modified and reviewed by @ssncferreira
2026-06-04 10:15:38 +02:00

89 lines
2.5 KiB
Go

package intercept
import (
"net/http"
)
// hopByHopHeaders are connection-level headers specific to the connection
// between client and AI Bridge, not meant for the upstream.
// See https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1
var hopByHopHeaders = []string{
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te",
"Trailer",
"Transfer-Encoding",
"Upgrade",
}
// nonForwardedHeaders are transport-level headers managed by aibridge or
// Go's HTTP transport that must not be forwarded to the upstream provider.
var nonForwardedHeaders = []string{
"Host",
"Accept-Encoding",
"Content-Length",
}
// authHeaders are headers that carry authentication credentials from the
// client. The upstream request is built by the SDK, which sets the correct
// provider credentials via option.WithAPIKey. Client auth headers are
// stripped here and the provider credentials are re-injected by
// BuildUpstreamHeaders from the SDK-built request.
var authHeaders = []string{
"Authorization",
"X-Api-Key",
}
// proxyHeaders describe the path the inbound request took to reach
// aibridge. On bridge routes aibridge acts as a client, not a proxy,
// so these headers are not meaningful on the outbound request.
var proxyHeaders = []string{
"X-Forwarded-For",
"X-Forwarded-Host",
"X-Forwarded-Proto",
"X-Forwarded-Port",
"Forwarded",
}
// PrepareClientHeaders returns a copy of the client headers with hop-by-hop,
// transport, auth, and proxy headers removed.
func PrepareClientHeaders(clientHeaders http.Header) http.Header {
prepared := clientHeaders.Clone()
for _, h := range hopByHopHeaders {
prepared.Del(h)
}
for _, h := range nonForwardedHeaders {
prepared.Del(h)
}
for _, h := range authHeaders {
prepared.Del(h)
}
for _, h := range proxyHeaders {
prepared.Del(h)
}
return prepared
}
// BuildUpstreamHeaders produces the header set for an upstream SDK request.
// It starts from the prepared client headers, then preserves specific
// headers from the SDK-built request that must not be overwritten.
func BuildUpstreamHeaders(sdkHeader http.Header, clientHeaders http.Header, authHeaderName string) http.Header {
headers := PrepareClientHeaders(clientHeaders)
// Preserve the auth header set by the SDK from the provider configuration.
if v := sdkHeader.Get(authHeaderName); v != "" {
headers.Set(authHeaderName, v)
}
// Preserve actor headers injected by aibridge as per-request SDK options.
for name, values := range sdkHeader {
if IsActorHeader(name) {
headers[name] = values
}
}
return headers
}