]> Cypherpunks repositories - gostls13.git/commitdiff
http: don't Clean query string in relative redirects
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 11 May 2011 11:30:05 +0000 (04:30 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 11 May 2011 11:30:05 +0000 (04:30 -0700)
R=adg, rsc, kevlar, r
CC=golang-dev
https://golang.org/cl/4476045

src/pkg/http/serve_test.go
src/pkg/http/server.go

index 7ff6ef04b1af78a241e8f2064a24b187764442fd..f2fb98e3e2de26c03244dc7adcb80c5a9621f962 100644 (file)
@@ -693,3 +693,20 @@ func TestTimeoutHandler(t *testing.T) {
                t.Errorf("expected Write error of %v; got %v", e, g)
        }
 }
+
+// Verifies we don't path.Clean() on the wrong parts in redirects.
+func TestRedirectMunging(t *testing.T) {
+       req, _ := NewRequest("GET", "http://example.com/", nil)
+
+       resp := httptest.NewRecorder()
+       Redirect(resp, req, "/foo?next=http://bar.com/", 302)
+       if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e {
+               t.Errorf("Location header was %q; want %q", g, e)
+       }
+
+       resp = httptest.NewRecorder()
+       Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302)
+       if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e {
+               t.Errorf("Location header was %q; want %q", g, e)
+       }
+}
index d155f06a2d2d0a3c37671431fb507900bc169ac0..eb5a3a365e8bd912562e239259f9487a7eb58f71 100644 (file)
@@ -581,12 +581,18 @@ func Redirect(w ResponseWriter, r *Request, url string, code int) {
                                url = olddir + url
                        }
 
+                       var query string
+                       if i := strings.Index(url, "?"); i != -1 {
+                               url, query = url[:i], url[i:]
+                       }
+
                        // clean up but preserve trailing slash
                        trailing := url[len(url)-1] == '/'
                        url = path.Clean(url)
                        if trailing && url[len(url)-1] != '/' {
                                url += "/"
                        }
+                       url += query
                }
        }