From 080e00d55d37be67be30e2723233594e64097edf Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 9 Aug 2013 18:33:57 -0400 Subject: [PATCH] encoding/json: escape & always There are a few different places in the code that escape possibly-problematic characters like < > and &. This one was the only one missing &, so add it. This means that if you Marshal a string, you get the same answer you do if you Marshal a string and pass it through the compactor. (Ironically, the compaction makes the string longer.) Because html/template invokes json.Marshal to prepare escaped strings for JavaScript, this changes the form of some of the escaped strings, but not their meaning. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/12708044 --- src/pkg/encoding/json/encode.go | 2 +- src/pkg/html/template/content_test.go | 12 ++++++------ src/pkg/html/template/escape_test.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pkg/encoding/json/encode.go b/src/pkg/encoding/json/encode.go index 5e8020502f7..a1127072694 100644 --- a/src/pkg/encoding/json/encode.go +++ b/src/pkg/encoding/json/encode.go @@ -734,7 +734,7 @@ func (e *encodeState) string(s string) (int, error) { start := 0 for i := 0; i < len(s); { if b := s[i]; b < utf8.RuneSelf { - if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' { + if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' { i++ continue } diff --git a/src/pkg/html/template/content_test.go b/src/pkg/html/template/content_test.go index 5e130faacb2..5f3ffe2d325 100644 --- a/src/pkg/html/template/content_test.go +++ b/src/pkg/html/template/content_test.go @@ -123,29 +123,29 @@ func TestTypedContent(t *testing.T) { { ``, []string{ - `"\u003cb\u003e \"foo%\" O'Reilly &bar;"`, + `"\u003cb\u003e \"foo%\" O'Reilly \u0026bar;"`, `"a[href =~ \"//example.com\"]#foo"`, - `"Hello, \u003cb\u003eWorld\u003c/b\u003e &tc!"`, + `"Hello, \u003cb\u003eWorld\u003c/b\u003e \u0026amp;tc!"`, `" dir=\"ltr\""`, // Not escaped. `c && alert("Hello, World!");`, // Escape sequence not over-escaped. `"Hello, World & O'Reilly\x21"`, - `"greeting=H%69&addressee=(World)"`, + `"greeting=H%69\u0026addressee=(World)"`, }, }, { `