Skip to content

Sanitize HTML rendered from third-party markdown on catalog pages - #1518

Merged
dfreedm merged 2 commits into
webcomponents:mainfrom
smalls:fix-markdown
Nov 14, 2026
Merged

dfreedm merged 2 commits into
webcomponents:mainfrom
smalls:fix-markdown

Conversation

@smalls

@smalls smalls commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Package descriptions and custom element descriptions come from third-party npm package metadata and custom elements manifests. The catalog renders them with marked() and inserts the result with unsafeHTML(), but marked passes raw HTML through unchanged and nothing was sanitizing it.

This change:

  • Adds renderUntrustedMarkdown() in packages/site-server/src/lib/catalog/markdown.ts. It runs the marked() output through sanitize-html with a tight allowlist:
    • tags: the sanitize-html defaults, plus img and del
    • attributes: only those needed for links, images, code-language classes and table alignment
    • URL schemes: only http, https and mailto (no protocol-relative URLs)
    • no SVG, MathML or inline styles
  • Uses the helper in the package and element routes instead of calling marked() directly. Sanitizing on the server covers both the SSR output and the hydration data in __ssrData.
  • Adds unit tests for the helper (src/test/catalog/markdown_test.ts). These don't need the dev servers.

I chose sanitize-html over DOMPurify so the site server doesn't need jsdom: it adds about 15 packages to the lockfile instead of about 38.

Package descriptions and custom element descriptions come from npm
package metadata and custom elements manifests, and were rendered with
marked() and inserted via unsafeHTML() without sanitization.

Add renderUntrustedMarkdown(), which runs marked() output through
sanitize-html with a tight allowlist (default tags plus img/del; link,
image, code-language and table-align attributes only; http, https and
mailto URLs), and use it in the package and element routes. Sanitizing
on the server covers both the SSR output and the hydration data in
__ssrData.
@kevinpschaaf
kevinpschaaf enabled auto-merge (squash) November 13, 2026 22:08
@kevinpschaaf
kevinpschaaf enabled auto-merge (squash) November 13, 2026 22:10
auto-merge was automatically disabled November 13, 2026 22:19

Head branch was pushed to by a user without write access

@dfreedm
dfreedm merged commit 2659d95 into webcomponents:main Nov 14, 2026
11 checks passed
@smalls
smalls deleted the fix-markdown branch November 14, 2026 00:04
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.

3 participants