agentleFS
fabioc-alohafabioc-aloha/Alex_ACT_ONE/skills/markdown-sanitization-chain/SKILL.md

markdown-sanitization-chain

Skill0 starsChanged 9 days agoMITView on GitHub

Render user-supplied markdown safely — marked.js → DOMPurify → Mermaid (order matters; skipping the sanitizer is XSS)

1 other project uses a version of this, 1 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never skip the sanitizer even if content is "trusted." Trust gets revoked when the threat model changes.

    line 35
  2. Very specific:

    Do not allow on* event attributes by default.

    line 81

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Markdown Sanitization Chain

Battle-tested via production XSS incident. The order of markdown → sanitize → diagram render is non-negotiable when content comes from users.

When to Use

  • An app renders markdown supplied by users (comments, docs UI, embedded editors)
  • You're about to call innerHTML with markdown-derived HTML
  • Mermaid or another diagram renderer runs in the browser
  • Security review on a markdown-rendering surface

Why It Matters

Markdown renderers (marked.js, markdown-it) convert markdown to HTML but do not sanitize it. Diagram renderers (Mermaid, PlantUML) execute after sanitizers run, which can re-introduce attack vectors. Order matters critically.

The Rule

Always: marked.js → DOMPurify → Mermaid (post-render).

1. Parse markdown to HTML        (marked.js)
2. Sanitize HTML                 (DOMPurify)
3. Insert sanitized HTML into DOM
4. Render diagrams on the now-sanitized DOM (Mermaid.run())

Never skip the sanitizer even if content is "trusted." Trust gets revoked when the threat model changes; the chain stays.

Implementation

import { marked } from 'marked';
import DOMPurify from 'dompurify';
import mermaid from 'mermaid';

async function renderMarkdown(content, container) {
  // Step 1: parse markdown to HTML
  const rawHtml = marked.parse(content);

  // Step 2: sanitize BEFORE inserting into the DOM
  const cleanHtml = DOMPurify.sanitize(rawHtml, {
    ADD_TAGS: ['mermaid'], // allow mermaid tags through
  });

  // Step 3: insert sanitized HTML
  container.innerHTML = cleanHtml;

  // Step 4: render diagrams on sanitized DOM
  await mermaid.run({ nodes: container.querySelectorAll('.mermaid') });
}

Common Mistakes

MistakeConsequence
Skip DOMPurify ("it's internal content")XSS from any content source
Sanitize after Mermaid rendersMermaid-injected scripts execute
Use innerHTML without sanitization anywhereClassic XSS
Trust localStorage / URL paramsUser-controlled XSS payloads

DOMPurify Configuration

const config = {
  ADD_TAGS: ['mermaid'],            // preserve diagram tags
  ADD_ATTR: ['class'],              // preserve diagram class hooks only
  FORBID_TAGS: ['style', 'script'], // explicit blocklist
  FORBID_ATTR: ['onclick', 'onerror', 'onload'],
};

Do not allow on* event attributes by default. If a product genuinely needs an event handler, bind it in trusted application code after sanitization instead of allowing user-supplied event attributes through DOMPurify.

Verification Checklist

  • Markdown parser runs first
  • DOMPurify runs before DOM insertion
  • Diagram renderer runs after sanitization
  • Review DOM insertion paths for unsanitized input. scripts/lint-innerhtml.cjs (in this skill's scripts/ directory) is a heuristic aid for direct dot-property innerHTML/outerHTML assignments. It recognizes whole sanitizer calls and static literals, and flags indirect or composed expressions for review. A clean scan does not establish safety: inspect data flow, dynamic/bracket-property sinks, parser limitations, and sanitizer configuration separately.
  • Tested with <img src=x onerror=alert(1)> payload

Related

  • markdown-mermaid — markdown + Mermaid style guide
  • markdown-mermaid § Mode Fragility — silent render failures
  • lint-clean-markdown — author-side hygiene

Would Revise If

Revisit the repaired configuration if DOMPurify or marked.js changes the documented chain behavior, an event attribute or unsafe URL survives the tested configuration, or Mermaid's post-sanitization output introduces executable user content.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.