From 900ebcfe4d592486dd5bc50f5e8101ba65e32948 Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Thu, 10 Oct 2019 10:48:00 +0000 Subject: [PATCH] encoding/json: stop escaping U+2028 and U+2029 in Compact MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Compact has been inconsistently escaping only some problematic characters (U+2028 and U+2029), but not others (<, > and &). This change addresses this inconsistency by removing the escaping of U+2028 and U+2029. Callers who need to escape the output of Compact should use HTMLEscape which escapes <, >, &, U+2028 and U+2029. Fixes #34070 Fixes #30357 Updates #5836 Change-Id: Icfce7691d2b8b1d9b05ba7b64d2d1e4f3b67871b GitHub-Last-Rev: 38859fe3e2fd586bbd45175c2742f7b123836bf3 GitHub-Pull-Request: golang/go#34804 Reviewed-on: https://go-review.googlesource.com/c/go/+/200217 Reviewed-by: Daniel Martí Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/encoding/json/indent.go | 2 +- src/encoding/json/scanner_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/encoding/json/indent.go b/src/encoding/json/indent.go index fba19548c9..06adfc1263 100644 --- a/src/encoding/json/indent.go +++ b/src/encoding/json/indent.go @@ -28,7 +28,7 @@ func compact(dst *bytes.Buffer, src []byte, escape bool) error { start = i + 1 } // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). - if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { + if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { if start < i { dst.Write(src[start:i]) } diff --git a/src/encoding/json/scanner_test.go b/src/encoding/json/scanner_test.go index 6cdbe7d301..3737516a45 100644 --- a/src/encoding/json/scanner_test.go +++ b/src/encoding/json/scanner_test.go @@ -48,6 +48,7 @@ var examples = []example{ {`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"}, {`{"x":1}`, "{\n\t\"x\": 1\n}"}, {ex1, ex1i}, + {"{\"\":\"<>&\u2028\u2029\"}", "{\n\t\"\": \"<>&\u2028\u2029\"\n}"}, // See golang.org/issue/34070 } var ex1 = `[true,false,null,"x",1,1.5,0,-5e+2]` @@ -89,8 +90,8 @@ func TestCompactSeparators(t *testing.T) { tests := []struct { in, compact string }{ - {"{\"\u2028\": 1}", `{"\u2028":1}`}, - {"{\"\u2029\" :2}", `{"\u2029":2}`}, + {"{\"\u2028\": 1}", "{\"\u2028\":1}"}, + {"{\"\u2029\" :2}", "{\"\u2029\":2}"}, } for _, tt := range tests { var buf bytes.Buffer -- 2.50.0