Skip to content

A platform for community discussion. Free, open, simple.

FIX: Avoid quadratic attribute normalization in xss

Jake Goldsborough2a5ae845d78e
diff --git a/frontend/discourse-markdown-it/package.json b/frontend/discourse-markdown-it/package.json
index 3176a2695c0..face754cb03 100644
--- a/frontend/discourse-markdown-it/package.json
+++ b/frontend/discourse-markdown-it/package.json
@@ -22,7 +22,7 @@
"discourse-i18n": "workspace:1.0.0",
"markdown-it": "^15.0.2",
"pretty-text": "workspace:1.0.0",
- "xss": "^1.0.15",
+ "xss": "1.0.15",
"discourse": "workspace:0.0.0"
},
"peerDependencies": {
diff --git a/frontend/discourse/package.json b/frontend/discourse/package.json
index 0cadb937b7d..1a66c683f84 100644
--- a/frontend/discourse/package.json
+++ b/frontend/discourse/package.json
@@ -175,7 +175,7 @@
"tracked-built-ins": "^4.1.2",
"typescript": "7.1.0-dev.20260930.4",
"util": "^0.12.5",
- "xss": "^1.0.15"
+ "xss": "1.0.15"
},
"engines": {
"node": ">= 20",
diff --git a/frontend/discourse/tests/unit/lib/sanitizer-test.js b/frontend/discourse/tests/unit/lib/sanitizer-test.js
index 29b4b21e0c0..8114f97a7e2 100644
--- a/frontend/discourse/tests/unit/lib/sanitizer-test.js
+++ b/frontend/discourse/tests/unit/lib/sanitizer-test.js
@@ -2,6 +2,7 @@ import { setupTest } from "ember-qunit";
import AllowLister from "pretty-text/allow-lister";
import { hrefAllowed, sanitize } from "pretty-text/sanitizer";
import { module, test } from "qunit";
+import sinon from "sinon";
import DiscourseMarkdownIt from "discourse-markdown-it";
function build(options) {
@@ -11,6 +12,47 @@ function build(options) {
module("Unit | Utility | sanitizer", function (hooks) {
setupTest(hooks);
+ test("preserves attribute whitespace normalization", function (assert) {
+ const allowLister = new AllowLister();
+
+ for (const whitespace of ["\t", "\n", "\r", "\f", "\u00a0"]) {
+ assert.strictEqual(
+ sanitize(
+ `<a title="first${whitespace}value" title="second${whitespace}value">link</a>`,
+ allowLister
+ ),
+ `<a title="first${whitespace}value" title="second value">link</a>`,
+ "preserves quoted whitespace before normalization starts"
+ );
+ assert.strictEqual(
+ sanitize(`<a title = "first${whitespace}value">link</a>`, allowLister),
+ '<a title="first value">link</a>',
+ "normalizes quoted whitespace after unquoted whitespace"
+ );
+ }
+ });
+
+ test("does not repeatedly normalize the entire attribute string", function (assert) {
+ const attributes = Array(128).fill('title="value"').join(" ");
+ const input = `<a ${attributes}>link</a>`;
+ const replace = sinon.spy(String.prototype, "replace");
+ let output;
+ let replacements;
+
+ try {
+ output = sanitize(input, new AllowLister());
+ // Count full-input scans instead of asserting a machine-dependent duration.
+ replacements = replace.getCalls().filter((call) => {
+ return call.thisValue === attributes && call.args[0]?.global;
+ }).length;
+ } finally {
+ replace.restore();
+ }
+
+ assert.strictEqual(output, input);
+ assert.strictEqual(replacements, 1, "normalizes the attribute string once");
+ });
+
test("sanitize", function (assert) {
const engine = build({
siteSettings: {
diff --git a/frontend/pretty-text-processor/package.json b/frontend/pretty-text-processor/package.json
index 6e183fab4d8..d932a8bd00f 100644
--- a/frontend/pretty-text-processor/package.json
+++ b/frontend/pretty-text-processor/package.json
@@ -13,7 +13,7 @@
"markdown-it": "^15.0.2",
"pretty-text": "workspace:*",
"rolldown": "1.2.7",
- "xss": "^1.0.15"
+ "xss": "1.0.15"
},
"engines": {
"node": ">= 20",
diff --git a/frontend/pretty-text/package.json b/frontend/pretty-text/package.json
index 0d2eec30801..e11f6804bd1 100644
--- a/frontend/pretty-text/package.json
+++ b/frontend/pretty-text/package.json
@@ -12,7 +12,7 @@
"dependencies": {
"discourse": "workspace:*",
"discourse-i18n": "workspace:1.0.0",
- "xss": "^1.0.15"
+ "xss": "1.0.15"
},
"devDependencies": {
"ember-source": "~7.3.0"
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 00000000000..610dba0a0fd
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,24 @@
+diff --git a/lib/parser.js b/lib/parser.js
+--- a/lib/parser.js
++++ b/lib/parser.js
+@@ -128,6 +128,7 @@
+ var lastPos = 0;
+ var lastMarkPos = 0;
+ var retAttrs = [];
++ var whitespaceNormalized = false;
+ var tmpName = false;
+ var len = html.length;
+
+@@ -167,7 +168,11 @@
+ }
+ }
+ if (/\s|\n|\t/.test(c)) {
+- html = html.replace(/\s|\n|\t/g, " ");
++ // Normalize lazily: doing this before parsing changes earlier quoted values.
++ if (!whitespaceNormalized) {
++ html = html.replace(/\s|\n|\t/g, " ");
++ whitespaceNormalized = true;
++ }
+ if (tmpName === false) {
+ j = findNextEqual(html, i);
+ if (j === -1) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4fab81b5690..84a940e3537 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -176,6 +176,7 @@ patchedDependencies:
[email protected]: 3032f3a47f33911d5c278ec6f2d4e0e4afc8f977baea1012613b13de23831c73
licensee: 566956dc682db703e899e5b01c65b129e434ad9f0848f5b9f62c3c28afd086e9
pikaday: ab597bca99e3c8cc091cd804d003516de8a3744472e0a4b881e598a38b51dc83
+ [email protected]: c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf
importers:
@@ -792,8 +793,8 @@ importers:
specifier: ^0.12.5
version: 0.12.5
xss:
- specifier: ^1.0.15
- version: 1.0.15
+ specifier: 1.0.15
+ version: 1.0.15(patch_hash=c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf)
frontend/discourse-i18n:
dependencies:
@@ -825,8 +826,8 @@ importers:
specifier: workspace:1.0.0
version: link:../pretty-text
xss:
- specifier: ^1.0.15
- version: 1.0.15
+ specifier: 1.0.15
+ version: 1.0.15(patch_hash=c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf)
frontend/discourse-types:
devDependencies:
@@ -858,8 +859,8 @@ importers:
specifier: workspace:1.0.0
version: link:../discourse-i18n
xss:
- specifier: ^1.0.15
- version: 1.0.15
+ specifier: 1.0.15
+ version: 1.0.15(patch_hash=c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf)
devDependencies:
ember-source:
specifier: ~7.3.0
@@ -886,8 +887,8 @@ importers:
specifier: 1.2.7
version: 1.2.7
xss:
- specifier: ^1.0.15
- version: 1.0.15
+ specifier: 1.0.15
+ version: 1.0.15(patch_hash=c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf)
plugins/automation:
dependencies:
@@ -16874,7 +16875,7 @@ snapshots:
+ [email protected](patch_hash=c255740ccc35e471df9a3ace55ec09e2b54dc3a6f9258354311e30c96ff716bf):
dependencies:
commander: 2.20.3
cssfilter: 0.0.10
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 0ead5cdb73f..cd64abf6c5e 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -82,6 +82,7 @@ patchedDependencies:
"pikaday": "patches/pikaday.patch"
peerDependencyRules:
allowedVersions:
"@mixer/parallel-prettier>prettier": "*"