]> Cypherpunks repositories - gostls13.git/commitdiff
net/url: don't escape sub-delims in fragment
authorKunpei Sakai <namusyaka@gmail.com>
Fri, 8 Sep 2017 15:39:20 +0000 (00:39 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 13 Jul 2018 03:53:00 +0000 (03:53 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

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

index 6608dbd74a8967a3f50c5bfe74f19355d46e0ff5..80eb7a86c8de26f3e3110c1f0da58a4281d8b2ef 100644 (file)
@@ -158,6 +158,19 @@ func shouldEscape(c byte, mode encoding) bool {
                }
        }
 
+       if mode == encodeFragment {
+               // RFC 3986 §2.2 allows not escaping sub-delims. A subset of sub-delims are
+               // included in reserved from RFC 2396 §2.2. The remaining sub-delims do not
+               // need to be escaped. To minimize potential breakage, we apply two restrictions:
+               // (1) we always escape sub-delims outside of the fragment, and (2) we always
+               // escape single quote to avoid breaking callers that had previously assumed that
+               // single quotes would be escaped. See issue #19917.
+               switch c {
+               case '!', '(', ')', '*':
+                       return false
+               }
+       }
+
        // Everything else must be escaped.
        return true
 }
index 7f03d2b9def425b3bfa8b757b09c9967ec57f73a..9043a844e88f800e8d5b206fba665ccbc2187efe 100644 (file)
@@ -1075,6 +1075,7 @@ var resolveReferenceTests = []struct {
 
        // Fragment
        {"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
+       {"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="},
 
        // Paths with escaping (issue 16947).
        {"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"},