-
Notifications
You must be signed in to change notification settings - Fork 5
feat: forward client request headers to upstream providers in bridge routes #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ssncferreira
wants to merge
2
commits into
main
Choose a base branch
from
ssncf/feat-forward-client-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| 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 headers that should not be forwarded to the upstream provider: | ||
| // - Connection-specific headers managed by AI Bridge or the HTTP transport. | ||
| // - Auth headers that are re-injected by the SDK from the provider configuration. | ||
| // - User-Agent is set by the SDK to identify AI Bridge as the upstream client. | ||
| var nonForwardedHeaders = []string{ | ||
| "Host", | ||
| "Accept-Encoding", | ||
| "Content-Length", | ||
| "Content-Type", | ||
| "Authorization", | ||
| "X-Api-Key", | ||
| "User-Agent", | ||
| } | ||
|
|
||
| // SanitizeClientHeaders clones headers and returns a sanitized copy suitable | ||
| // for forwarding to an upstream provider. | ||
| // | ||
| // It removes: | ||
| // - Hop-by-hop headers | ||
| // - Non-forwarded headers (connection-specific, transport-managed, auth, and SDK-managed headers) | ||
| // | ||
| // Callers should apply these headers first, so that any subsequently | ||
| // added headers take priority in case of conflict. | ||
| func SanitizeClientHeaders(headers http.Header) http.Header { | ||
| if headers == nil { | ||
| return http.Header{} | ||
| } | ||
|
|
||
| outHeaders := headers.Clone() | ||
|
|
||
| for _, h := range hopByHopHeaders { | ||
| outHeaders.Del(h) | ||
| } | ||
|
|
||
| for _, h := range nonForwardedHeaders { | ||
| outHeaders.Del(h) | ||
| } | ||
|
|
||
| return outHeaders | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package intercept | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSanitizeClientHeaders(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input http.Header | ||
| expectAbsent []string | ||
| expectPresent map[string][]string | ||
| expectEmpty bool | ||
| }{ | ||
| { | ||
| name: "no headers returns empty header", | ||
| input: nil, | ||
| expectEmpty: true, | ||
| }, | ||
| { | ||
| name: "hop-by-hop headers are removed", | ||
| input: http.Header{ | ||
| "Connection": []string{"keep-alive"}, | ||
| "Keep-Alive": []string{"timeout=5"}, | ||
| "Transfer-Encoding": []string{"chunked"}, | ||
| "Upgrade": []string{"websocket"}, | ||
| }, | ||
| expectAbsent: []string{"Connection", "Keep-Alive", "Transfer-Encoding", "Upgrade"}, | ||
| }, | ||
| { | ||
| name: "bridge headers are removed", | ||
| input: http.Header{ | ||
| "Host": []string{"example.com"}, | ||
| "Accept-Encoding": []string{"gzip"}, | ||
| "Content-Length": []string{"42"}, | ||
| "Content-Type": []string{"application/json"}, | ||
| "User-Agent": []string{"client/1.0"}, | ||
| }, | ||
| expectAbsent: []string{"Host", "Accept-Encoding", "Content-Length", "Content-Type", "User-Agent"}, | ||
| }, | ||
| { | ||
| name: "auth headers are removed", | ||
| input: http.Header{ | ||
| "Authorization": []string{"Bearer some-token"}, | ||
| "X-Api-Key": []string{"some-key"}, | ||
| }, | ||
| expectAbsent: []string{"Authorization", "X-Api-Key"}, | ||
| }, | ||
| { | ||
| name: "custom headers are preserved", | ||
| input: http.Header{ | ||
| "X-Custom-Header": []string{"custom-value"}, | ||
| "X-Request-Id": []string{"req-123"}, | ||
| }, | ||
| expectPresent: map[string][]string{ | ||
| "X-Custom-Header": {"custom-value"}, | ||
| "X-Request-Id": {"req-123"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "multi-value headers are preserved", | ||
| input: http.Header{ | ||
| "X-Custom-Header": []string{"value-1", "value-2"}, | ||
| }, | ||
| expectPresent: map[string][]string{ | ||
| "X-Custom-Header": {"value-1", "value-2"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "input is not mutated", | ||
| input: http.Header{ | ||
| "Connection": []string{"keep-alive"}, | ||
| "X-Custom-Header": []string{"custom-value"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Capture original state to verify no mutation. | ||
| originalCopy := tc.input.Clone() | ||
|
|
||
| result := SanitizeClientHeaders(tc.input) | ||
|
|
||
| if tc.expectEmpty { | ||
| require.Empty(t, result) | ||
| return | ||
| } | ||
|
|
||
| for _, h := range tc.expectAbsent { | ||
| require.Empty(t, result.Get(h), "expected header %q to be absent", h) | ||
| } | ||
|
|
||
| for h, vals := range tc.expectPresent { | ||
| require.Equal(t, vals, result[h], "expected header %q to be present", h) | ||
| } | ||
|
|
||
| // Verify input was not mutated. | ||
| require.Equal(t, originalCopy, tc.input) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |
| "github.com/aws/aws-sdk-go-v2/credentials" | ||
| aibconfig "github.com/coder/aibridge/config" | ||
| aibcontext "github.com/coder/aibridge/context" | ||
| "github.com/coder/aibridge/intercept" | ||
| "github.com/coder/aibridge/intercept/apidump" | ||
| "github.com/coder/aibridge/mcp" | ||
| "github.com/coder/aibridge/recorder" | ||
|
|
@@ -40,6 +41,10 @@ type interceptionBase struct { | |
| cfg aibconfig.Anthropic | ||
| bedrockCfg *aibconfig.AWSBedrock | ||
|
|
||
| // clientHeaders holds the original client request headers to forward | ||
| // to upstream providers. | ||
| clientHeaders http.Header | ||
|
|
||
| tracer trace.Tracer | ||
| logger slog.Logger | ||
|
|
||
|
|
@@ -178,6 +183,15 @@ func (i *interceptionBase) isSmallFastModel() bool { | |
| } | ||
|
|
||
| func (i *interceptionBase) newMessagesService(ctx context.Context, opts ...option.RequestOption) (anthropic.MessageService, error) { | ||
| // Forward sanitized client headers to the upstream provider. | ||
| // Client headers are added first so that SDK auth appended | ||
| // below takes priority on any conflict. | ||
| for k, vals := range intercept.SanitizeClientHeaders(i.clientHeaders) { | ||
| for _, v := range vals { | ||
| opts = append(opts, option.WithHeader(k, v)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| opts = append(opts, option.WithAPIKey(i.cfg.Key)) | ||
| opts = append(opts, option.WithBaseURL(i.cfg.BaseURL)) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optionorder matters? I'd assume it should not so old way of initializingoptscould stay?