Skip to content

Commit ecdbffd

Browse files
committed
net/http/httputil: don't append to X-Forwarded-For in ReverseProxy when nil
Fixes #38079 Change-Id: Iac02d7f9574061bb26d1d9a41bb6ee6cc38934e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/230937 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1d98012 commit ecdbffd

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

doc/go1.15.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
200200

201201
<dl id="net"><dt><a href="/pkg/net/">net</a></dt>
202202
<dd>
203-
<p><!-- CL -->
203+
<p><!-- CL 228645 -->
204204
If an I/O operation exceeds a deadline set by
205205
the <a href="/pkg/net/#Conn"><code>Conn.SetDeadline</code></a>,
206206
<code>Conn.SetReadDeadline</code>,
@@ -217,12 +217,23 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
217217
</dd>
218218
</dl>
219219

220+
<dl id="net/http/httputil"><dt><a href="/pkg/net/http/httputil/">net/http/httputil</a></dt>
221+
<dd>
222+
<p><!-- CL 230937 -->
223+
<a href="/pkg/net/http/httputil/#ReverseProxy"><code>ReverseProxy</code></a>
224+
now supports not modifying the <code>X-Forwarded-For</code>
225+
header when the incoming <code>Request.Header</code> map entry
226+
for that field is <code>nil</code>.
227+
</p>
228+
</dd>
229+
</dl>
230+
220231
<dl id="net/http/pprof"><dt><a href="/pkg/net/http/pprof/">net/http/pprof</a></dt>
221232
<dd>
222-
<p><!-- CL 147598, 229537 -->
223-
All profile endpoints now support a "seconds" parameter. When present,
233+
<p><!-- CL 147598, CL 229537 -->
234+
All profile endpoints now support a "<code>seconds</code>" parameter. When present,
224235
the endpoint profiles for the specified number of seconds and reports the difference.
225-
The meaning of the "seconds" parameter in the <code>cpu</code> profile and
236+
The meaning of the "<code>seconds</code>" parameter in the <code>cpu</code> profile and
226237
the trace endpoints is unchanged.
227238
</p>
228239
</dd>

src/net/http/httputil/reverseproxy.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ import (
2525
// sends it to another server, proxying the response back to the
2626
// client.
2727
//
28-
// ReverseProxy automatically sets the client IP as the value of the
28+
// ReverseProxy by default sets the client IP as the value of the
2929
// X-Forwarded-For header.
30+
//
3031
// If an X-Forwarded-For header already exists, the client IP is
31-
// appended to the existing values.
32+
// appended to the existing values. As a special case, if the header
33+
// exists in the Request.Header map but has a nil value (such as when
34+
// set by the Director func), the X-Forwarded-For header is
35+
// not modified.
36+
//
3237
// To prevent IP spoofing, be sure to delete any pre-existing
3338
// X-Forwarded-For header coming from the client or
3439
// an untrusted proxy.
@@ -248,10 +253,14 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
248253
// If we aren't the first proxy retain prior
249254
// X-Forwarded-For information as a comma+space
250255
// separated list and fold multiple headers into one.
251-
if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
256+
prior, ok := outreq.Header["X-Forwarded-For"]
257+
omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
258+
if len(prior) > 0 {
252259
clientIP = strings.Join(prior, ", ") + ", " + clientIP
253260
}
254-
outreq.Header.Set("X-Forwarded-For", clientIP)
261+
if !omit {
262+
outreq.Header.Set("X-Forwarded-For", clientIP)
263+
}
255264
}
256265

257266
res, err := transport.RoundTrip(outreq)

src/net/http/httputil/reverseproxy_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,39 @@ func TestXForwardedFor(t *testing.T) {
277277
}
278278
}
279279

280+
// Issue 38079: don't append to X-Forwarded-For if it's present but nil
281+
func TestXForwardedFor_Omit(t *testing.T) {
282+
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
283+
if v := r.Header.Get("X-Forwarded-For"); v != "" {
284+
t.Errorf("got X-Forwarded-For header: %q", v)
285+
}
286+
w.Write([]byte("hi"))
287+
}))
288+
defer backend.Close()
289+
backendURL, err := url.Parse(backend.URL)
290+
if err != nil {
291+
t.Fatal(err)
292+
}
293+
proxyHandler := NewSingleHostReverseProxy(backendURL)
294+
frontend := httptest.NewServer(proxyHandler)
295+
defer frontend.Close()
296+
297+
oldDirector := proxyHandler.Director
298+
proxyHandler.Director = func(r *http.Request) {
299+
r.Header["X-Forwarded-For"] = nil
300+
oldDirector(r)
301+
}
302+
303+
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
304+
getReq.Host = "some-name"
305+
getReq.Close = true
306+
res, err := frontend.Client().Do(getReq)
307+
if err != nil {
308+
t.Fatalf("Get: %v", err)
309+
}
310+
res.Body.Close()
311+
}
312+
280313
var proxyQueryTests = []struct {
281314
baseSuffix string // suffix to add to backend URL
282315
reqSuffix string // suffix to add to frontend's request URL

0 commit comments

Comments
 (0)