Skip to content

Commit ea65d01

Browse files
ggiccibradfitz
authored andcommitted
net/http: clean the path of the stripped URL by StripPrefix
The path of the new stripped URL should also be cleaned. Since an empty path may cause unexpected errors in some HTTP handlers, e.g. http.ServeFile. Fixes #30165 Change-Id: Ib44fdce6388b5d62ffbcab5266925ef8f13f26e2 Reviewed-on: https://go-review.googlesource.com/c/161738 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 5a7e8f4 commit ea65d01

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/net/http/serve_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,15 @@ func TestStripPrefix(t *testing.T) {
29002900
t.Errorf("test 2: got status %v, want %v", g, e)
29012901
}
29022902
res.Body.Close()
2903+
2904+
res, err = c.Get(ts.URL + "/foo")
2905+
if err != nil {
2906+
t.Fatal(err)
2907+
}
2908+
if g, e := res.Header.Get("X-Path"), "/"; g != e {
2909+
t.Errorf("test 3: got %s, want %s", g, e)
2910+
}
2911+
res.Body.Close()
29032912
}
29042913

29052914
// https://golang.org/issue/18952.

src/net/http/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ func StripPrefix(prefix string, h Handler) Handler {
20302030
*r2 = *r
20312031
r2.URL = new(url.URL)
20322032
*r2.URL = *r.URL
2033-
r2.URL.Path = p
2033+
r2.URL.Path = cleanPath(p)
20342034
h.ServeHTTP(w, r2)
20352035
} else {
20362036
NotFound(w, r)

0 commit comments

Comments
 (0)