Skip to content

fix: harden Matrix HTML sanitization#571

Merged
7w1 merged 4 commits intodevfrom
hazre/fix/matrix-html-sanitization
Mar 30, 2026
Merged

fix: harden Matrix HTML sanitization#571
7w1 merged 4 commits intodevfrom
hazre/fix/matrix-html-sanitization

Conversation

@hazre
Copy link
Copy Markdown
Member

@hazre hazre commented Mar 28, 2026

Description

  • switch Matrix custom HTML sanitization from sanitize-html to DOMPurify
  • enforce the Matrix v1.18 tag and attribute allowlist exactly, including scheme and mxc:// restrictions
  • sanitize reply previews before trimming/parsing so untrusted formatted_body never reaches the reply extraction path unsanitized
  • move data-mx-color and data-mx-bg-color translation to render time and explicitly ignore incoming style in the HTML parser
  • use sanitizeText for plain-text interpolation paths that were previously running through the HTML sanitizer
  • add regression tests for sanitizer allowlist behavior, reply preview sanitization, parser color translation, and parser-side style stripping

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

AI disclosure:

  • Partially AI assisted (clarify which code was AI assisted and briefly explain what it does).
  • Fully AI generated (explain what all the generated code does in moderate detail).

This PR replaces the Matrix custom HTML sanitizer with a DOMPurify-based implementation that matches the Matrix v1.18 allowlist instead of the broader legacy rules. The sanitizer now validates tag-specific attributes, rejects incoming style, limits links to https/http/ftp/mailto/magnet, limits images to mxc://, strips mx-reply, and keeps the existing nesting guard. Reply previews now sanitize formatted_body before any reply trimming or parsing. The React HTML parser now turns data-mx-color and data-mx-bg-color into React styles at render time and ignores raw incoming style attributes even if unsanitized HTML somehow reaches the parser. The plain-text HTML interpolation paths in forwarding, the room composer, and the message editor now use text escaping instead of the HTML allowlist sanitizer. The new tests lock down the sanitizer contract, the reply-preview regression, parser color translation, and parser-side style stripping.

@hazre hazre requested a review from 7w1 as a code owner March 28, 2026 16:28
…sanitization

# Conflicts:
#	src/app/components/message/Reply.tsx
#	src/app/features/room/RoomInput.tsx
@hazre
Copy link
Copy Markdown
Member Author

hazre commented Mar 28, 2026

Should I write a changeset for this?

@7w1
Copy link
Copy Markdown
Member

7w1 commented Mar 29, 2026

Appears to break some (?) custom emoji formatting.
image

@hazre
Copy link
Copy Markdown
Member Author

hazre commented Mar 29, 2026

uh yeah thats because of mxc links, let me see how I can fix it.

@hazre hazre marked this pull request as draft March 29, 2026 20:20
@hazre hazre force-pushed the hazre/fix/matrix-html-sanitization branch from 4dfc72d to fb1732c Compare March 29, 2026 21:42
@hazre
Copy link
Copy Markdown
Member Author

hazre commented Mar 29, 2026

okay it should be fixed now.

@hazre hazre marked this pull request as ready for review March 29, 2026 21:43
@github-actions
Copy link
Copy Markdown
Contributor

Deploying with  Cloudflare Workers  Cloudflare Workers

Status Preview URL Commit Alias Updated (UTC)
✅ Deployment successful! https://pr-571-sable.raspy-dream-bb1d.workers.dev fb1732c pr-571 Sun, 29 Mar 2026 21:44:51 GMT

Copy link
Copy Markdown
Member

@7w1 7w1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@7w1 7w1 added this pull request to the merge queue Mar 30, 2026
Merged via the queue into dev with commit 4d2fcfc Mar 30, 2026
9 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Mar 30, 2026
> [!IMPORTANT]
> Merging this PR will create a new release.

## Fixes

* Add youtube shorts support to stop it from crashing sable.
([#578](#578) by @nushea)
* Fix rich-text reply previews and custom-formatted messages so unsafe
HTML is filtered more strictly and Matrix colors render correctly.
([#571](#571) by @hazre)
* Fix crash when previewing non-video YouTube URLs (channels, @Handles,
etc.) that lack query parameters.
([#584](#584) by @Just-Insane)
* fix id handling and id generation for Personas
([#583](#583) by @dozro)
@hazre hazre deleted the hazre/fix/matrix-html-sanitization branch March 31, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants