A platform for community discussion. Free, open, simple.
FIX: Catch highlightHTML calls where the needle is too massive to highlight (#33776)
Gary Pendergastfd091f46e448
diff --git a/app/assets/javascripts/discourse/app/lib/highlight-html.js b/app/assets/javascripts/discourse/app/lib/highlight-html.jsindex 7fdbdfdefb6..db7b23b87fa 100644--- a/app/assets/javascripts/discourse/app/lib/highlight-html.js+++ b/app/assets/javascripts/discourse/app/lib/highlight-html.js@@ -17,7 +17,14 @@ function highlight(node, pattern, nodeName, className) {}if (node.nodeType === Node.TEXT_NODE) {- const match = node.data.match(pattern);+ let match;+ try {+ match = node.data.match(pattern);+ } catch {+ // If the regex is too large, it will throw an error.+ // In this case, we just return the original node without highlighting.+ return 0;+ }if (!match) {return 0;diff --git a/app/assets/javascripts/discourse/tests/unit/lib/highlight-html-test.js b/app/assets/javascripts/discourse/tests/unit/lib/highlight-html-test.jsnew file mode 100644index 00000000000..58c30c4c4c0--- /dev/null+++ b/app/assets/javascripts/discourse/tests/unit/lib/highlight-html-test.js@@ -0,0 +1,19 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import highlightHTML from "discourse/lib/highlight-html";++module("Unit | Utility | highlight-html", function (hooks) {+ setupTest(hooks);++ test("massive search string", function (assert) {+ const string = "a".repeat(32768);+ const elem = document.createElement("span");+ elem.innerHTML = string;+ const highlighted = highlightHTML(elem, string);+ assert.strictEqual(+ highlighted.innerHTML,+ string,+ "it should successfully bail when the string is too massive to highlight"+ );+ });+});