The proxy sets Forwarded, X-Forwarded-For/-Host/-Proto and X-Real-IP from the connection, but applied a route's request header filters after that, so whoever could write a route to a backend could tell it any client address (a backend that trusts the gateway's X-Forwarded-For believes it). An HTTPRoute whose request header filter sets, adds or removes one of them (names compared case-insensitively) is now not accepted (UnsupportedValue), like the other filters we do not serve. The proxy also sets them after the filters, clearing them first: SetXForwarded appends to an X-Forwarded-For already there. The proxy test's backend now prints every X-Forwarded-For value; it printed only the first, which hid an appended one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UHXzGrsxfkGk8LmwrBbHNZ
This commit is contained in:
1 parent
e7583c4177
commit
5d6b0719b3
6 files changed
+124
-9
No files matched your search
+10
-5
@@ -293,11 +293,6 @@ func (p *Proxy) serve(s *portServer, w http.ResponseWriter, r *http.Request) {
|
||||
Rewrite: func(pr *httputil.ProxyRequest) {
|
||||
pr.SetURL(&url.URL{Scheme: "http", Host: ep.String()})
|
||||
pr.Out.Host = pr.In.Host // forwarded unmodified unless a filter says otherwise
|
||||
// SetXForwarded replaces whatever the client sent: only this hop's view is trusted
|
||||
pr.SetXForwarded()
|
||||
if ip, _, err := net.SplitHostPort(pr.In.RemoteAddr); err == nil {
|
||||
pr.Out.Header.Set("X-Real-IP", ip)
|
||||
}
|
||||
for _, f := range filters {
|
||||
switch {
|
||||
case f.Type == filterRequestHeaderModifier && f.RequestHeaderModifier != nil:
|
||||
@@ -312,6 +307,16 @@ func (p *Proxy) serve(s *portServer, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// After the filters, so none can change these (the model already refuses routes that try): only this
|
||||
// hop's view of the connection is passed on. Cleared first, as SetXForwarded appends to an
|
||||
// X-Forwarded-For already there.
|
||||
for _, h := range forwardingHeaders {
|
||||
pr.Out.Header.Del(h)
|
||||
}
|
||||
pr.SetXForwarded()
|
||||
if ip, _, err := net.SplitHostPort(pr.In.RemoteAddr); err == nil {
|
||||
pr.Out.Header.Set("X-Real-IP", ip)
|
||||
}
|
||||
},
|
||||
ModifyResponse: func(resp *http.Response) error {
|
||||
for _, f := range filters {
|
||||
|
||||
Reference in new issue
Block a user