-
Notifications
You must be signed in to change notification settings - Fork 730
fix(adk): match Python's MCP header precedence - dynamic overrides static #2556
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
onematchfox
wants to merge
1
commit into
kagent-dev:release/v0.10.x
Choose a base branch
from
onematchfox:fix-golang-header-precedence
base: release/v0.10.x
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.
+107
−20
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,9 +79,49 @@ func TestAllowedRequestHeaders_ForwardsMatchingHeaders(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // TestAllowedRequestHeaders_StaticOverridesDynamic verifies that a statically | ||
| // configured header wins over the same header forwarded from the A2A request. | ||
| func TestAllowedRequestHeaders_StaticOverridesDynamic(t *testing.T) { | ||
| // TestUnlistedRequestHeader_DoesNotOverrideStatic verifies the override in | ||
| // TestAllowedRequestHeaders_DynamicOverridesStatic is gated on the header | ||
| // being explicitly opted in. An incoming request carrying the same header | ||
| // name as a static default, with neither allowedHeaders nor propagateToken | ||
| // configured for it, must not override the static value — a request cannot | ||
| // silently clobber a static header just by sending one with a matching name. | ||
| func TestUnlistedRequestHeader_DoesNotOverrideStatic(t *testing.T) { | ||
| t.Parallel() | ||
| var capturedAuth string | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| capturedAuth = r.Header.Get("Authorization") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| ctx := a2aCtx(map[string][]string{ | ||
| "Authorization": {"Bearer incoming"}, | ||
| }) | ||
|
|
||
| rt := &headerRoundTripper{ | ||
| base: newTestTransport(t), | ||
| headers: map[string]string{"Authorization": "Bearer static"}, | ||
| // Deliberately no allowedHeaders, no propagateToken, no headerProvider: | ||
| // nothing opts Authorization into being forwarded from the request. | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil) | ||
| resp, err := rt.RoundTrip(req) | ||
| if err != nil { | ||
| t.Fatalf("RoundTrip failed: %v", err) | ||
| } | ||
| resp.Body.Close() | ||
|
|
||
| if capturedAuth != "Bearer static" { | ||
| t.Errorf("Authorization: got %q, want %q", capturedAuth, "Bearer static") | ||
| } | ||
| } | ||
|
|
||
| // TestAllowedRequestHeaders_DynamicOverridesStatic verifies that a header | ||
| // forwarded from the A2A request (via allowedHeaders) wins over a statically | ||
| // configured header of the same name — the static header is only a default. | ||
| func TestAllowedRequestHeaders_DynamicOverridesStatic(t *testing.T) { | ||
| t.Parallel() | ||
| var capturedAuth string | ||
|
|
||
|
|
@@ -108,8 +148,8 @@ func TestAllowedRequestHeaders_StaticOverridesDynamic(t *testing.T) { | |
| } | ||
| resp.Body.Close() | ||
|
|
||
| if capturedAuth != "Bearer static" { | ||
|
Contributor
Author
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. This assertion was wrong to begin with. Header round-tripper is configured above with |
||
| t.Errorf("Authorization: got %q, want %q", capturedAuth, "Bearer static") | ||
| if capturedAuth != "Bearer incoming" { | ||
| t.Errorf("Authorization: got %q, want %q", capturedAuth, "Bearer incoming") | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -535,9 +575,12 @@ func TestDynamicHeaders_OverridePropagatedAndAllowedHeaders(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // TestStaticHeaders_OverrideDynamic verifies static configured headers remain | ||
| // the highest-precedence source. | ||
| func TestStaticHeaders_OverrideDynamic(t *testing.T) { | ||
| // TestDynamicHeaders_OverrideStatic verifies a headerProvider-sourced header | ||
| // (e.g. an STS-exchanged or propagated Authorization) wins over a static header | ||
| // of the same name configured on the MCP server spec. This is what lets a | ||
| // per-user token override a RemoteMCPServer's static headersFrom Authorization | ||
| // that the controller needs for its own tool-discovery handshake. | ||
| func TestDynamicHeaders_OverrideStatic(t *testing.T) { | ||
| t.Parallel() | ||
| var capturedAuth string | ||
|
|
||
|
|
@@ -562,7 +605,47 @@ func TestStaticHeaders_OverrideDynamic(t *testing.T) { | |
| } | ||
| resp.Body.Close() | ||
|
|
||
| if capturedAuth != "Bearer dynamic" { | ||
| t.Errorf("Authorization: got %q, want %q", capturedAuth, "Bearer dynamic") | ||
| } | ||
| } | ||
|
|
||
| // TestStaticHeaders_ApplyWhenNoDynamicSource verifies the static header is | ||
| // still used when no dynamic source sets the same header name — e.g. an | ||
| // autonomous run with no caller token, or an unrelated static header. | ||
| func TestStaticHeaders_ApplyWhenNoDynamicSource(t *testing.T) { | ||
| t.Parallel() | ||
| var capturedAuth, capturedOther string | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| capturedAuth = r.Header.Get("Authorization") | ||
| capturedOther = r.Header.Get("X-Other") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| rt := &headerRoundTripper{ | ||
| base: newTestTransport(t), | ||
| headers: map[string]string{ | ||
| "Authorization": "Bearer static", | ||
| "X-Other": "static-other", | ||
| }, | ||
| headerProvider: func(context.Context) map[string]string { | ||
| return map[string]string{"X-Different": "dynamic-value"} | ||
| }, | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil) | ||
| resp, err := rt.RoundTrip(req) | ||
| if err != nil { | ||
| t.Fatalf("RoundTrip failed: %v", err) | ||
| } | ||
| resp.Body.Close() | ||
|
|
||
| if capturedAuth != "Bearer static" { | ||
| t.Errorf("Authorization: got %q, want %q", capturedAuth, "Bearer static") | ||
| } | ||
| if capturedOther != "static-other" { | ||
| t.Errorf("X-Other: got %q, want %q", capturedOther, "static-other") | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.