Repository navigation
Support RFC 9110 Forwarded header in scheme extraction #3248
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,33 @@ func isTrustedPeer(remoteAddr string, checker *ipChecker) bool { | |
| } | ||
|
|
||
| func schemeFromHeaders(req *http.Request) string { | ||
| // RFC 9110 §7.6.1 defines a structured Forwarded header whose proto | ||
| // parameter carries the scheme used on the incoming connection. Like | ||
| // X-Forwarded-Proto, the last element is the one added by the nearest proxy. | ||
| if values := req.Header.Values(HeaderForwarded); len(values) > 0 { | ||
| proto := "" | ||
| for _, value := range values { | ||
| for _, element := range strings.Split(value, ",") { | ||
|
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. Select the last element before reading For
|
||
| for _, parameter := range strings.Split(element, ";") { | ||
|
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. Do not split parameters inside quoted strings. For the valid value
|
||
| key, val, ok := strings.Cut(strings.TrimSpace(parameter), "=") | ||
| if !ok || !strings.EqualFold(strings.TrimSpace(key), "proto") { | ||
| continue | ||
| } | ||
| val = strings.TrimSpace(val) | ||
| if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' { | ||
| val = val[1 : len(val)-1] | ||
| } | ||
| proto = val | ||
| } | ||
| } | ||
| } | ||
| if proto != "" { | ||
|
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. Distinguish an absent With
|
||
| if scheme, ok := canonicalProto(proto); ok { | ||
| return scheme | ||
| } | ||
| return "http" | ||
| } | ||
| } | ||
| // When X-Forwarded-Proto is present it is the only header used: a trusted proxy sets it, and the other headers | ||
| // might be client-supplied values that the proxy did not remove. With several values (repeated header lines or a | ||
| // comma-separated list) the last one was added by the nearest proxy. | ||
|
|
||
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.
Reachability: External
Exploitability: Trivial
CWE: CWE-807
View Security blast radius
Keep
X-Forwarded-Protoahead ofForwardedinschemeFromHeaders.The new
Forwardedblock runs before theX-Forwarded-Protocheck and returns itsprotovalue first. The documented contract forExtractSchemeFromHeaderssays that whenX-Forwarded-Protois present, the other headers are ignored, and that the trusted proxy must overwrite that header. A trusted loopback proxy that setsX-Forwarded-Proto: httpbut passes through a client'sForwarded: proto=httpsnow produceshttpsfor a non-TLS request. Before this change, the same request producedhttp. A client can therefore set the scheme thatContext.Schemereports.Return the
X-Forwarded-Protoresult when that header is present, even ifForwardedalso carries aprotovalue.Forwarded-only requests can still use the new parser. IfForwardedmust become a trusted source with higher precedence, make that choice an explicit trust option.