]> Cypherpunks repositories - gostls13.git/commitdiff
net/url: preserve leading slashes when resolving path
authorMark Theunissen <mark.theunissen@gmail.com>
Tue, 25 Jul 2017 10:47:39 +0000 (12:47 +0200)
committerRuss Cox <rsc@golang.org>
Mon, 30 Oct 2017 21:00:06 +0000 (21:00 +0000)
When doing resolvePath, if there are multiple leading slashes in the
target, preserve them. This prevents an issue where the Go http.Client
cleans up multiple leading slashes in the Location header in a
redirect, resulting in a redirection to the incorrect target.

Fixes #21158.

Change-Id: I6a21ea61ca3bc7033f3c8a6ccc21ecaa3e996fa8
Reviewed-on: https://go-review.googlesource.com/51050
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>

src/net/url/url.go
src/net/url/url_test.go

index c9353ab080d72abaf669ab8874037982a737e8ce..509cec3ba0a33c998ce8ab618fbfd7cec04b6469 100644 (file)
@@ -911,7 +911,7 @@ func resolvePath(base, ref string) string {
                // Add final slash to the joined path.
                dst = append(dst, "")
        }
-       return "/" + strings.TrimLeft(strings.Join(dst, "/"), "/")
+       return "/" + strings.TrimPrefix(strings.Join(dst, "/"), "/")
 }
 
 // IsAbs reports whether the URL is absolute.
index 5f03200d94bd2aca6cd85d44c5c66ad3a00b6dc5..604b323601faec7b307b8d1d0ebf61261b2480b3 100644 (file)
@@ -1032,6 +1032,10 @@ var resolveReferenceTests = []struct {
        {"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"},
        {"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
 
+       // Multiple slashes
+       {"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"},
+       {"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"},
+
        // Scheme-relative
        {"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},