Skip to content
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

net/http/httputil: NewSingleHostReverseProxy returns proxy that sets default User-Agent header if client did not provided one #15524

Closed
artyom opened this issue May 3, 2016 · 4 comments

Comments

@artyom
Copy link
Member

artyom commented May 3, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?

    go version devel +babdbfb Tue May 3 12:35:09 2016 +0000 darwin/amd64
    
  2. What operating system and processor architecture are you using (go env)?

    darwin/amd64, OS X 10.11.4
    
  3. What did you do?

  4. What did you expect to see?

    The program reports that User-Agent header is empty: User-Agent is ""

  5. What did you see instead?

    The program reports that User-Agent is set to default value: User-Agent is "Go-http-client/1.1"

@artyom
Copy link
Member Author

artyom commented May 3, 2016

Possible fix is trivial:

diff --git i/src/net/http/httputil/reverseproxy.go w/src/net/http/httputil/reverseproxy.go
index 44d15ff..8c471a9 100644
--- i/src/net/http/httputil/reverseproxy.go
+++ w/src/net/http/httputil/reverseproxy.go
@@ -90,6 +90,11 @@ 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}
 }
diff --git i/src/net/http/httputil/reverseproxy_test.go w/src/net/http/httputil/reverseproxy_test.go
index e9c0658..1065561 100644
--- i/src/net/http/httputil/reverseproxy_test.go
+++ w/src/net/http/httputil/reverseproxy_test.go
@@ -41,6 +41,9 @@ func TestReverseProxy(t *testing.T) {
        if len(r.TransferEncoding) > 0 {
            t.Errorf("backend got unexpected TransferEncoding: %v", r.TransferEncoding)
        }
+       if c := r.Header.Get("User-Agent"); c != "" {
+           t.Errorf("handler got User-Agent header value %q", c)
+       }
        if r.Header.Get("X-Forwarded-For") == "" {
            t.Errorf("didn't get X-Forwarded-For header")
        }
@@ -80,6 +83,7 @@ func TestReverseProxy(t *testing.T) {

    getReq, _ := http.NewRequest("GET", frontend.URL, nil)
    getReq.Host = "some-name"
+   getReq.Header.Set("User-Agent", "")
    getReq.Header.Set("Connection", "close")
    getReq.Header.Set("Proxy-Connection", "should be deleted")
    getReq.Header.Set("Upgrade", "foo")

Please let me know if this change makes sense, I can proceed with CL then.

@bradfitz bradfitz added this to the Go1.7Maybe milestone May 4, 2016
@bradfitz bradfitz self-assigned this May 4, 2016
@bradfitz
Copy link
Contributor

@artyom, sure, send a CL. Be sure to also add a test that the original User-Agent from a user is preserved.

@bradfitz bradfitz removed their assignment May 10, 2016
@artyom
Copy link
Member Author

artyom commented May 11, 2016

Sure, will do this later this week, likely over the weekend. Please feel free to assing this to me.

@gopherbot
Copy link

CL https://golang.org/cl/23089 mentions this issue.

@golang golang locked and limited conversation to collaborators May 16, 2017
@rsc rsc unassigned artyom Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants