Repository navigation
🐛 fix: filter headers at agent HTTP relay boundaries #19264
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Parsed agent HTTP relays now remove proxy credentials, forwarding metadata, internal Hive headers, and other hop-by-hop metadata before sending requests upstream. |
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,38 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| // stripRelayHopHeaders removes metadata scoped to the inbound connection. | ||
| // The request/response writers generate framing for the outbound connection. | ||
| func stripRelayHopHeaders(h http.Header) { | ||
| for _, value := range h.Values("Connection") { | ||
| for name := range strings.SplitSeq(value, ",") { | ||
| h.Del(strings.TrimSpace(name)) | ||
| } | ||
| } | ||
| for _, name := range [...]string{ | ||
| "Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", | ||
| "Proxy-Connection", "Te", "Trailer", "Transfer-Encoding", "Upgrade", | ||
| } { | ||
| delete(h, name) | ||
| } | ||
| } | ||
|
|
||
| func stripRelayRequestHeaders(req *http.Request) { | ||
| stripRelayHopHeaders(req.Header) | ||
| for name := range req.Header { | ||
| if strings.HasPrefix(name, "X-Forwarded-") || strings.HasPrefix(name, "X-Hive-") { | ||
| delete(req.Header, name) | ||
| } | ||
| } | ||
| // Trailers arrive after the headers have been filtered; do not forward them. | ||
| req.Trailer = nil | ||
| } | ||
|
|
||
| func stripRelayResponseHeaders(resp *http.Response) { | ||
| stripRelayHopHeaders(resp.Header) | ||
| resp.Trailer = nil | ||
| } |
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,47 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| // Use ordinary HTTP messages with inert metadata. Each relay must pass the | ||
| // end-to-end field while removing the other fields before the next hop. | ||
| func relayTestRequestHeaders() http.Header { | ||
| h := make(http.Header) | ||
| setRelayTestResponseHeaders(h) | ||
| h.Set("Proxy-Authorization", "Basic dGVzdDpmaXh0dXJl") | ||
| h.Set("Proxy-Connection", "keep-alive") | ||
| h.Set("x-forwarded-example", "fixture") | ||
| h.Set("x-hive-example", "fixture") | ||
| return h | ||
| } | ||
|
|
||
| func setRelayTestResponseHeaders(h http.Header) { | ||
| h.Add("Connection", "Keep-Alive, x-relay-hop") | ||
| h.Add("Connection", "X-Second-Hop") | ||
| h.Set("Keep-Alive", "timeout=5") | ||
| h.Set("X-Relay-Hop", "fixture") | ||
| h.Set("X-Second-Hop", "fixture") | ||
| h.Set("X-End-To-End", "preserved") | ||
| } | ||
|
|
||
| func assertRelayHeaderHygiene(t *testing.T, got, sent http.Header) { | ||
| t.Helper() | ||
| for name := range sent { | ||
| if name == "X-End-To-End" { | ||
| if value := got.Get(name); value != "preserved" { | ||
| t.Errorf("%s = %q, want preserved", name, value) | ||
| } | ||
| } else if len(got.Values(name)) != 0 { | ||
| t.Errorf("%s was forwarded", name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func assertRelayResponseHeaderHygiene(t *testing.T, resp *http.Response) { | ||
| t.Helper() | ||
| sent := make(http.Header) | ||
| setRelayTestResponseHeaders(sent) | ||
| assertRelayHeaderHygiene(t, resp.Header, sent) | ||
| } |
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
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.
The doc comment above says this only rewrites Authorization, but it now also runs stripRelayRequestHeaders. Please update the comment.