Skip to content

Commit 8a33045

Browse files
namusyakabradfitz
authored andcommitted
net/url: don't escape sub-delims in fragment
According to RFC-3986, the sub-delims chars should not be escaped in fragment. So this change fixes current behavior a bit. Fixes #19917 Change-Id: I1a8deb93255d979532f75bae183c3fb53a05d395 Reviewed-on: https://go-review.googlesource.com/61650 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a241922 commit 8a33045

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/net/url/url.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ func shouldEscape(c byte, mode encoding) bool {
158158
}
159159
}
160160

161+
if mode == encodeFragment {
162+
// RFC 3986 §2.2 allows not escaping sub-delims. A subset of sub-delims are
163+
// included in reserved from RFC 2396 §2.2. The remaining sub-delims do not
164+
// need to be escaped. To minimize potential breakage, we apply two restrictions:
165+
// (1) we always escape sub-delims outside of the fragment, and (2) we always
166+
// escape single quote to avoid breaking callers that had previously assumed that
167+
// single quotes would be escaped. See issue #19917.
168+
switch c {
169+
case '!', '(', ')', '*':
170+
return false
171+
}
172+
}
173+
161174
// Everything else must be escaped.
162175
return true
163176
}

src/net/url/url_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ var resolveReferenceTests = []struct {
10751075

10761076
// Fragment
10771077
{"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
1078+
{"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="},
10781079

10791080
// Paths with escaping (issue 16947).
10801081
{"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"},

0 commit comments

Comments
 (0)