-
Notifications
You must be signed in to change notification settings - Fork 354
config: strip credentials on cross-host redirects #901
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
roidelapluie
merged 1 commit into
prometheus:main
from
roidelapluie:roidelapluie/fixredirectauth
Jun 11, 2026
+525
−2
Merged
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 |
|---|---|---|
|
|
@@ -721,6 +721,14 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon | |
| } | ||
|
|
||
| if cfg.HTTPHeaders != nil { | ||
| // Strip sensitive headers added by headersRoundTripper on cross-host | ||
| // redirects before they reach the transport. Only needed when | ||
| // redirects are actually followed; when FollowRedirects is false | ||
| // CheckRedirect returns ErrUseLastResponse immediately so there are | ||
| // no subsequent requests. | ||
| if cfg.FollowRedirects { | ||
| rt = &sensitiveHeadersStripRT{next: rt} | ||
| } | ||
| rt = NewHeadersRoundTripper(cfg.HTTPHeaders, rt) | ||
| } | ||
|
|
||
|
|
@@ -862,7 +870,7 @@ func NewAuthorizationCredentialsRoundTripper(authType string, authCredentials Se | |
| } | ||
|
|
||
| func (rt *authorizationCredentialsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if len(req.Header.Get("Authorization")) != 0 { | ||
| if len(req.Header.Get("Authorization")) != 0 || isCrossHostRedirect(req) { | ||
| return rt.rt.RoundTrip(req) | ||
| } | ||
|
|
||
|
|
@@ -900,7 +908,7 @@ func NewBasicAuthRoundTripper(username, password SecretReader, rt http.RoundTrip | |
| } | ||
|
|
||
| func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if len(req.Header.Get("Authorization")) != 0 { | ||
| if len(req.Header.Get("Authorization")) != 0 || isCrossHostRedirect(req) { | ||
| return rt.rt.RoundTrip(req) | ||
| } | ||
| var username string | ||
|
|
@@ -1085,6 +1093,9 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro | |
| rt.mtx.RLock() | ||
| currentRT := rt.lastRT | ||
| rt.mtx.RUnlock() | ||
| if isCrossHostRedirect(req) { | ||
|
Member
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. I was surprised we can't cut off sooner, but I guess we don't know Base here .. or is it rt.client? |
||
| return currentRT.Base.RoundTrip(req) | ||
| } | ||
| return currentRT.RoundTrip(req) | ||
| } | ||
|
|
||
|
|
@@ -1106,6 +1117,85 @@ func mapToValues(m map[string]string) url.Values { | |
| return v | ||
| } | ||
|
|
||
| // isCrossHostRedirect reports whether req is a redirect to a different host | ||
| // than the original request. It detects this by walking the req.Response chain | ||
| // (which Go's HTTP client populates on every redirect hop) to find the original | ||
| // request's hostname, then comparing it to the current destination. | ||
| // This works regardless of whether the caller uses NewClientFromConfig or a | ||
| // custom http.Client built from NewRoundTripperFromConfigWithContext directly. | ||
| func isCrossHostRedirect(req *http.Request) bool { | ||
| if req.Response == nil { | ||
| return false | ||
| } | ||
| originalHost := strings.ToLower(originalRequestHost(req)) | ||
| return !isDomainOrSubdomain(strings.ToLower(req.URL.Hostname()), originalHost) | ||
| } | ||
|
|
||
| func originalRequestHost(req *http.Request) string { | ||
| r := req | ||
| for r.Response != nil && r.Response.Request != nil { | ||
| r = r.Response.Request | ||
| } | ||
| return r.URL.Hostname() | ||
| } | ||
|
|
||
| // sensitiveHeadersOnRedirect lists the headers that must not be forwarded when | ||
| // following a redirect to a different host, mirroring the list in | ||
| // makeHeadersCopier in net/http/client.go. | ||
| var sensitiveHeadersOnRedirect = map[string]struct{}{ | ||
| "Authorization": {}, | ||
| // "Www-Authenticate" is the canonical form produced by | ||
| // textproto.CanonicalMIMEHeaderKey; it is not a typo of "WWW-Authenticate". | ||
| "Www-Authenticate": {}, | ||
| "Cookie": {}, | ||
| "Cookie2": {}, | ||
| "Proxy-Authorization": {}, | ||
| "Proxy-Authenticate": {}, | ||
| } | ||
|
|
||
| // sensitiveHeadersStripRT strips sensitive headers from requests marked as | ||
| // cross-host redirects before passing them to the underlying transport. | ||
| type sensitiveHeadersStripRT struct { | ||
| next http.RoundTripper | ||
| } | ||
|
|
||
| func (rt *sensitiveHeadersStripRT) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if isCrossHostRedirect(req) { | ||
| req = cloneRequest(req) | ||
| for h := range sensitiveHeadersOnRedirect { | ||
| req.Header.Del(h) | ||
| } | ||
| } | ||
| return rt.next.RoundTrip(req) | ||
| } | ||
|
|
||
| func (rt *sensitiveHeadersStripRT) CloseIdleConnections() { | ||
| if ci, ok := rt.next.(closeIdler); ok { | ||
| ci.CloseIdleConnections() | ||
| } | ||
| } | ||
|
|
||
| // isDomainOrSubdomain reports whether sub is a subdomain (or exact match) of | ||
| // parent. It mirrors isDomainOrSubdomain from net/http/client.go. | ||
| func isDomainOrSubdomain(sub, parent string) bool { | ||
| if parent == "" { | ||
| return false | ||
| } | ||
| if sub == parent { | ||
| return true | ||
| } | ||
| // A colon means sub is an IPv6 address; a percent sign introduces an IPv6 | ||
| // zone ID. Neither can be a hostname, and both could otherwise pass the | ||
| // suffix check below (e.g. "::1%.www.example.com" ends with "example.com"). | ||
| if strings.ContainsAny(sub, ":%") { | ||
| return false | ||
| } | ||
| if !strings.HasSuffix(sub, parent) { | ||
| return false | ||
| } | ||
| return sub[len(sub)-len(parent)-1] == '.' | ||
| } | ||
|
|
||
| // cloneRequest returns a clone of the provided *http.Request. | ||
| // The clone is a shallow copy of the struct and its Header map. | ||
| func cloneRequest(r *http.Request) *http.Request { | ||
|
|
||
Oops, something went wrong.
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.