Render Untrusted Markdown Safely: Parse First, Sanitize HTML After
Convert user-supplied Markdown to HTML with a CommonMark parser, then sanitize the output—never the source—to block XSS while keeping code blocks and emphasis.
08 Jul 2026, 20:38 UTC

Your comment system accepts Markdown. A user submits a post that includes . Because many Markdown parsers pass raw HTML through to the rendered page, that markup can execute JavaScript in your visitors’ browsers. This is cross-site scripting (XSS): attacker-controlled code running with your site’s origin.
The practical takeaway is a two-stage pipeline: parse Markdown into HTML with a CommonMark-compliant parser, then sanitize the generated HTML with a strict allowlist. Sanitize the output, never the Markdown source.
Why raw HTML in Markdown is a risk
Markdown was designed to be written in plain text and converted to HTML. Most implementations allow inline HTML, and many pass tags such as