1
0
mirror of https://github.com/golang/go synced 2024-11-17 14:04:48 -07:00

net/http/httputil: don't insert default User-Agent header in proxied requests

When ReverseProxy forwards a request with no User-Agent header, leave
the header in the forwarded request blank rather than inserting the
default Go HTTP clent User-Agent.

We already did this for NewSingleHostReverseProxy; generalize it to
every ReverseProxy.

Change-Id: Id81a230cb8d384acdfae190b78a4265d80720388
Reviewed-on: https://go-review.googlesource.com/c/go/+/407375
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Damien Neil 2022-05-19 12:33:05 -07:00
parent 1513e57b70
commit f001df540b
2 changed files with 25 additions and 31 deletions

View File

@ -156,10 +156,6 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
return &ReverseProxy{Director: director}
}
@ -321,6 +317,12 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}
if _, ok := outreq.Header["User-Agent"]; !ok {
// If the outbound request doesn't have a User-Agent header set,
// don't send the default Go HTTP client User-Agent.
outreq.Header.Set("User-Agent", "")
}
res, err := transport.RoundTrip(outreq)
if err != nil {
p.getErrorHandler()(rw, outreq, err)

View File

@ -614,46 +614,38 @@ func TestNilBody(t *testing.T) {
// Issue 15524
func TestUserAgentHeader(t *testing.T) {
const explicitUA = "explicit UA"
var gotUA string
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/noua" {
if c := r.Header.Get("User-Agent"); c != "" {
t.Errorf("handler got non-empty User-Agent header %q", c)
}
return
}
if c := r.Header.Get("User-Agent"); c != explicitUA {
t.Errorf("handler got unexpected User-Agent header %q", c)
}
gotUA = r.Header.Get("User-Agent")
}))
defer backend.Close()
backendURL, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
proxyHandler := NewSingleHostReverseProxy(backendURL)
proxyHandler := new(ReverseProxy)
proxyHandler.ErrorLog = log.New(io.Discard, "", 0) // quiet for tests
proxyHandler.Director = func(req *http.Request) {
req.URL = backendURL
}
frontend := httptest.NewServer(proxyHandler)
defer frontend.Close()
frontendClient := frontend.Client()
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
getReq.Header.Set("User-Agent", explicitUA)
getReq.Close = true
res, err := frontendClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
for _, sentUA := range []string{"explicit UA", ""} {
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
getReq.Header.Set("User-Agent", sentUA)
getReq.Close = true
res, err := frontendClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
res.Body.Close()
if got, want := gotUA, sentUA; got != want {
t.Errorf("got forwarded User-Agent %q, want %q", got, want)
}
}
res.Body.Close()
getReq, _ = http.NewRequest("GET", frontend.URL+"/noua", nil)
getReq.Header.Set("User-Agent", "")
getReq.Close = true
res, err = frontendClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
res.Body.Close()
}
type bufferPool struct {