From cce366f71a7eb01021cbbc241e7ef8ac9244d014 Mon Sep 17 00:00:00 2001 From: hanchao Date: Mon, 22 May 2023 22:21:52 +0800 Subject: [PATCH] bugfix: fix CVE-2023-29400,CVE-2023-24539,CVE-2023-24540 CVE:CVE-2023-29400,CVE-2023-24539,CVE-2023-24540 Reference:https://go-review.googlesource.com/c/go/+/491615,https://go-review.googlesource.com/c/go/+/491616,https://go-review.googlesource.com/c/go/+/491617 Type:CVE Reason:fix CVE-2023-29400,CVE-2023-24539,CVE-2023-24540 --- ...mplate-emit-filterFailsafe-for-empty.patch | 101 ++++++++++++++++++ ...mplate-handle-all-JS-whitespace-char.patch | 98 +++++++++++++++++ ...mplate-disallow-angle-brackets-in-CS.patch | 69 ++++++++++++ golang.spec | 11 +- 4 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 0040-Backport-html-template-emit-filterFailsafe-for-empty.patch create mode 100644 0041-Backport-html-template-handle-all-JS-whitespace-char.patch create mode 100644 0042-Backport-html-template-disallow-angle-brackets-in-CS.patch diff --git a/0040-Backport-html-template-emit-filterFailsafe-for-empty.patch b/0040-Backport-html-template-emit-filterFailsafe-for-empty.patch new file mode 100644 index 0000000..a7a3516 --- /dev/null +++ b/0040-Backport-html-template-emit-filterFailsafe-for-empty.patch @@ -0,0 +1,101 @@ +From 269ef3bb401deab32dcbce3ee24e0cf64e94b825 Mon Sep 17 00:00:00 2001 +From: Roland Shoemaker +Date: Wed, 10 May 2023 16:33:50 +0800 +Subject: [PATCH 1/3] [Backport] html/template: emit filterFailsafe for empty + unquoted attr value + +Offering: Cloud Core Network +CVE: CVE-2023-29400 +Reference: https://go-review.googlesource.com/c/go/+/491357 + +An unquoted action used as an attribute value can result in unsafe +behavior if it is empty, as HTML normalization will result in unexpected +attributes, and may allow attribute injection. If executing a template +results in a empty unquoted attribute value, emit filterFailsafe +instead. + +Thanks to Juho Nurminen of Mattermost for reporting this issue. + +For #59722 +Fixes #59815 +Fixes CVE-2023-29400 + +Change-Id: Ia38d1b536ae2b4af5323a6c6d861e3c057c2570a +Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1826631 +Reviewed-by: Julie Qiu +Run-TryBot: Roland Shoemaker +Reviewed-by: Damien Neil +Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1851498 +Reviewed-by: Roland Shoemaker +Run-TryBot: Damien Neil +Reviewed-on: https://go-review.googlesource.com/c/go/+/491357 +Run-TryBot: Carlos Amedee +TryBot-Result: Gopher Robot +Reviewed-by: Dmitri Shuralyov + +Signed-off-by: Li Bi Chen libichen@huawei.com +--- + src/html/template/escape.go | 5 ++--- + src/html/template/escape_test.go | 15 +++++++++++++++ + src/html/template/html.go | 3 +++ + 3 files changed, 20 insertions(+), 3 deletions(-) + +diff --git a/src/html/template/escape.go b/src/html/template/escape.go +index ca078f40ea..bdccc65a57 100644 +--- a/src/html/template/escape.go ++++ b/src/html/template/escape.go +@@ -362,9 +362,8 @@ func normalizeEscFn(e string) string { + // for all x. + var redundantFuncs = map[string]map[string]bool{ + "_html_template_commentescaper": { +- "_html_template_attrescaper": true, +- "_html_template_nospaceescaper": true, +- "_html_template_htmlescaper": true, ++ "_html_template_attrescaper": true, ++ "_html_template_htmlescaper": true, + }, + "_html_template_cssescaper": { + "_html_template_attrescaper": true, +diff --git a/src/html/template/escape_test.go b/src/html/template/escape_test.go +index 9d7749fc9c..3e17aee8f2 100644 +--- a/src/html/template/escape_test.go ++++ b/src/html/template/escape_test.go +@@ -678,6 +678,21 @@ func TestEscape(t *testing.T) { + ``, + ``, + }, ++ { ++ "unquoted empty attribute value (plaintext)", ++ "

", ++ "

", ++ }, ++ { ++ "unquoted empty attribute value (url)", ++ "

", ++ "

", ++ }, ++ { ++ "quoted empty attribute value", ++ "

", ++ "

", ++ }, + } + + for _, test := range tests { +diff --git a/src/html/template/html.go b/src/html/template/html.go +index 356b8298ae..636bc21069 100644 +--- a/src/html/template/html.go ++++ b/src/html/template/html.go +@@ -14,6 +14,9 @@ import ( + // htmlNospaceEscaper escapes for inclusion in unquoted attribute values. + func htmlNospaceEscaper(args ...interface{}) string { + s, t := stringify(args...) ++ if s == "" { ++ return filterFailsafe ++ } + if t == contentTypeHTML { + return htmlReplacer(stripTags(s), htmlNospaceNormReplacementTable, false) + } +-- +2.33.0 + diff --git a/0041-Backport-html-template-handle-all-JS-whitespace-char.patch b/0041-Backport-html-template-handle-all-JS-whitespace-char.patch new file mode 100644 index 0000000..71e121b --- /dev/null +++ b/0041-Backport-html-template-handle-all-JS-whitespace-char.patch @@ -0,0 +1,98 @@ +From 9b226a94a44d53ca8f740e9807f8236a8bb7dfc3 Mon Sep 17 00:00:00 2001 +From: Roland Shoemaker +Date: Wed, 10 May 2023 16:37:48 +0800 +Subject: [PATCH 2/3] [Backport] html/template: handle all JS whitespace + characters +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Offering: Cloud Core Network +CVE: CVE-2023-24540 +Reference: https://go-review.googlesource.com/c/go/+/491355 + +Rather than just a small set. Character class as defined by \s [0]. + +Thanks to Juho Nurminen of Mattermost for reporting this. + +For #59721 +Fixes  #59813 +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 +Run-TryBot: Roland Shoemaker +Reviewed-by: Damien Neil +Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1851497 +Run-TryBot: Damien Neil +Reviewed-by: Roland Shoemaker +Reviewed-on: https://go-review.googlesource.com/c/go/+/491355 +Reviewed-by: Dmitri Shuralyov +Reviewed-by: Carlos Amedee +TryBot-Bypass: Carlos Amedee +Run-TryBot: Carlos Amedee + +Signed-off-by: Li Bi Chen libichen@huawei.com +--- + src/html/template/js.go | 8 +++++++- + src/html/template/js_test.go | 11 +++++++---- + 2 files changed, 14 insertions(+), 5 deletions(-) + +diff --git a/src/html/template/js.go b/src/html/template/js.go +index b888eaf8b7..35994f076e 100644 +--- a/src/html/template/js.go ++++ b/src/html/template/js.go +@@ -13,6 +13,11 @@ import ( + "unicode/utf8" + ) + ++// jsWhitespace contains all of the JS whitespace characters, as defined ++// by the \s character class. ++// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes. ++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" ++ + // nextJSCtx returns the context that determines whether a slash after the + // given run of tokens starts a regular expression instead of a division + // operator: / or /=. +@@ -26,7 +31,8 @@ import ( + // JavaScript 2.0 lexical grammar and requires one token of lookbehind: + // https://www.mozilla.org/js/language/js20-2000-07/rationale/syntax.html + func nextJSCtx(s []byte, preceding jsCtx) jsCtx { +- s = bytes.TrimRight(s, "\t\n\f\r \u2028\u2029") ++ // Trim all JS whitespace characters ++ s = bytes.TrimRight(s, jsWhitespace) + if len(s) == 0 { + return preceding + } +diff --git a/src/html/template/js_test.go b/src/html/template/js_test.go +index 7d963ae6f1..de9ef28410 100644 +--- a/src/html/template/js_test.go ++++ b/src/html/template/js_test.go +@@ -81,14 +81,17 @@ func TestNextJsCtx(t *testing.T) { + {jsCtxDivOp, "0"}, + // Dots that are part of a number are div preceders. + {jsCtxDivOp, "0."}, ++ // Some JS interpreters treat NBSP as a normal space, so ++ // we must too in order to properly escape things. ++ {jsCtxRegexp, "=\u00A0"}, + } + + for _, test := range tests { +- if nextJSCtx([]byte(test.s), jsCtxRegexp) != test.jsCtx { +- t.Errorf("want %s got %q", test.jsCtx, test.s) ++ if ctx := nextJSCtx([]byte(test.s), jsCtxRegexp); ctx != test.jsCtx { ++ t.Errorf("%q: want %s got %s", test.s, test.jsCtx, ctx) + } +- if nextJSCtx([]byte(test.s), jsCtxDivOp) != test.jsCtx { +- t.Errorf("want %s got %q", test.jsCtx, test.s) ++ if ctx := nextJSCtx([]byte(test.s), jsCtxDivOp); ctx != test.jsCtx { ++ t.Errorf("%q: want %s got %s", test.s, test.jsCtx, ctx) + } + } + +-- +2.33.0 + diff --git a/0042-Backport-html-template-disallow-angle-brackets-in-CS.patch b/0042-Backport-html-template-disallow-angle-brackets-in-CS.patch new file mode 100644 index 0000000..dfc666c --- /dev/null +++ b/0042-Backport-html-template-disallow-angle-brackets-in-CS.patch @@ -0,0 +1,69 @@ +From be128e70a9a7f52f9ff33fceb2139b5769da0112 Mon Sep 17 00:00:00 2001 +From: Roland Shoemaker +Date: Wed, 10 May 2023 16:43:52 +0800 +Subject: [PATCH 3/3] [Backport] html/template: disallow angle brackets in CSS + values + +Offering: Cloud Core Network +CVE: CVE-2023-24539 +Reference: https://go-review.googlesource.com/c/go/+/491335 + +Angle brackets should not appear in CSS contexts, as they may affect +token boundaries (such as closing a