Skip to content

Commit a32232c

Browse files
rolandshoemakercagedmantis
authored andcommitted
html/template: handle all JS whitespace characters
Rather than just a small set. Character class as defined by \s [0]. Thanks to Juho Nurminen of Mattermost for reporting this. Fixes #59721 Fixes CVE-2023-24540 [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes Change-Id: I56d4fa1ef08125b417106ee7dbfb5b0923b901ba Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1821459 Reviewed-by: Julie Qiu <julieqiu@google.com> Run-TryBot: Roland Shoemaker <bracewell@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/491616 Run-TryBot: Carlos Amedee <carlos@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
1 parent 8673ca8 commit a32232c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/html/template/js.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"unicode/utf8"
1414
)
1515

16+
// jsWhitespace contains all of the JS whitespace characters, as defined
17+
// by the \s character class.
18+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes.
19+
const jsWhitespace = "\f\n\r\t\v\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
20+
1621
// nextJSCtx returns the context that determines whether a slash after the
1722
// given run of tokens starts a regular expression instead of a division
1823
// operator: / or /=.
@@ -26,7 +31,8 @@ import (
2631
// JavaScript 2.0 lexical grammar and requires one token of lookbehind:
2732
// https://www.mozilla.org/js/language/js20-2000-07/rationale/syntax.html
2833
func nextJSCtx(s []byte, preceding jsCtx) jsCtx {
29-
s = bytes.TrimRight(s, "\t\n\f\r \u2028\u2029")
34+
// Trim all JS whitespace characters
35+
s = bytes.TrimRight(s, jsWhitespace)
3036
if len(s) == 0 {
3137
return preceding
3238
}

src/html/template/js_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ func TestNextJsCtx(t *testing.T) {
8080
{jsCtxDivOp, "0"},
8181
// Dots that are part of a number are div preceders.
8282
{jsCtxDivOp, "0."},
83+
// Some JS interpreters treat NBSP as a normal space, so
84+
// we must too in order to properly escape things.
85+
{jsCtxRegexp, "=\u00A0"},
8386
}
8487

8588
for _, test := range tests {
86-
if nextJSCtx([]byte(test.s), jsCtxRegexp) != test.jsCtx {
87-
t.Errorf("want %s got %q", test.jsCtx, test.s)
89+
if ctx := nextJSCtx([]byte(test.s), jsCtxRegexp); ctx != test.jsCtx {
90+
t.Errorf("%q: want %s got %s", test.s, test.jsCtx, ctx)
8891
}
89-
if nextJSCtx([]byte(test.s), jsCtxDivOp) != test.jsCtx {
90-
t.Errorf("want %s got %q", test.jsCtx, test.s)
92+
if ctx := nextJSCtx([]byte(test.s), jsCtxDivOp); ctx != test.jsCtx {
93+
t.Errorf("%q: want %s got %s", test.s, test.jsCtx, ctx)
9194
}
9295
}
9396

0 commit comments

Comments
 (0)