Skip to content

fix(security): escape mXSS-detection text fallback in sanitizeHtmlCore - #182

Merged
JosunLP merged 1 commit into
devfrom
fix/162-mxss-fallback-escape
Jul 6, 2026
Merged

fix(security): escape mXSS-detection text fallback in sanitizeHtmlCore#182
JosunLP merged 1 commit into
devfrom
fix/162-mxss-fallback-escape

Conversation

@JosunLP

@JosunLP JosunLP commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #162 (Critical).

The anti-mXSS double-parse in sanitizeHtmlCore returned raw, unescaped fragment.textContent when the serialize→re-parse output was unstable. Every caller assigns the return value to an HTML sink (innerHTML/insertAdjacentHTML via .html(), .append(), bq-html, …), so an attacker could smuggle an entity-encoded payload (&lt;img src=x onerror=...&gt;) plus a construct that forces re-parse instability (<a><table><a>… foster-parenting) and get live markup back — the mXSS defense itself was the injection sink.

Fix

The text fallback is now HTML-escaped before being returned (local escapeHtmlText helper in sanitize-core.ts to avoid a circular import with sanitize.ts).

Verification

  • New regression test with the exact audit payload <a><table><a>&lt;img src=x onerror=alert(1)&gt;; asserts no <img> element materializes when the result is assigned to innerHTML.
  • Verified the fallback branch actually fires under happy-dom: output is now &lt;img src=x onerror=alert(1)&gt; (inert text).
  • bun test tests/security.test.ts: 59 pass / 0 fail. tsc --noEmit clean.

🤖 Generated with Claude Code

The double-parse stability check returned raw fragment.textContent when
serialize->re-parse output differed. Since every caller assigns the result
to an HTML sink (innerHTML/insertAdjacentHTML), entity-smuggled payloads
(e.g. &lt;img onerror=...&gt; combined with table foster-parenting to force
instability) were handed back as live markup - the mXSS defense itself
became the injection sink.

The fallback is now HTML-escaped before being returned.

Fixes #162

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added security Changes to the security module tests Chenges to the tests labels Jul 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3cc63b74-945d-40fe-8ec2-4c99acf8222b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/162-mxss-fallback-escape

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@JosunLP
JosunLP merged commit 1e59261 into dev Jul 6, 2026
9 checks passed
@JosunLP
JosunLP deleted the fix/162-mxss-fallback-escape branch July 6, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Changes to the security module tests Chenges to the tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant