Skip to content

Commit 3e0a8e7

Browse files
committed
net/http: preserve original path encoding in redirects
Fixes #70758 Change-Id: I9fc6fe98c194351557c6219513918b7593899bc1 Reviewed-on: https://go-review.googlesource.com/c/go/+/720821 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mark Freeman <[email protected]>
1 parent 831af61 commit 3e0a8e7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/net/http/serve_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,19 @@ func TestRedirectBadPath(t *testing.T) {
28812881
}
28822882
}
28832883

2884+
func TestRedirectEscapedPath(t *testing.T) {
2885+
baseURL, redirectURL := "http://example.com/foo%2Fbar/", "qux%2Fbaz"
2886+
req := httptest.NewRequest("GET", baseURL, NoBody)
2887+
2888+
rr := httptest.NewRecorder()
2889+
Redirect(rr, req, redirectURL, StatusMovedPermanently)
2890+
2891+
wantURL := "/foo%2Fbar/qux%2Fbaz"
2892+
if got := rr.Result().Header.Get("Location"); got != wantURL {
2893+
t.Errorf("Redirect(%s, %s) = %s, want = %s", baseURL, redirectURL, got, wantURL)
2894+
}
2895+
}
2896+
28842897
// Test different URL formats and schemes
28852898
func TestRedirect(t *testing.T) {
28862899
req, _ := NewRequest("GET", "http://example.com/qux/", nil)

src/net/http/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ func Redirect(w ResponseWriter, r *Request, url string, code int) {
24082408
// but doing it ourselves is more reliable.
24092409
// See RFC 7231, section 7.1.2
24102410
if u.Scheme == "" && u.Host == "" {
2411-
oldpath := r.URL.Path
2411+
oldpath := r.URL.EscapedPath()
24122412
if oldpath == "" { // should not happen, but avoid a crash if it does
24132413
oldpath = "/"
24142414
}

0 commit comments

Comments
 (0)