Skip to content

Commit 84e91e1

Browse files
marktheunissenrsc
authored andcommitted
net/url: preserve leading slashes when resolving path
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 <[email protected]> Run-TryBot: Russ Cox <[email protected]>
1 parent b4c3fe7 commit 84e91e1

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/net/url/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ func resolvePath(base, ref string) string {
911911
// Add final slash to the joined path.
912912
dst = append(dst, "")
913913
}
914-
return "/" + strings.TrimLeft(strings.Join(dst, "/"), "/")
914+
return "/" + strings.TrimPrefix(strings.Join(dst, "/"), "/")
915915
}
916916

917917
// IsAbs reports whether the URL is absolute.

src/net/url/url_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ var resolveReferenceTests = []struct {
10321032
{"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"},
10331033
{"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
10341034

1035+
// Multiple slashes
1036+
{"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"},
1037+
{"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"},
1038+
10351039
// Scheme-relative
10361040
{"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
10371041

0 commit comments

Comments
 (0)