v2.0.11

Security

Last updated


AST.html() escapes content and sanitizes raw HTML at output; this page lists how each kind of input is handled.

Escaping


PositionHandling
Text, code spans, math, code blocks& and < become entities
Attributes (href, src, title, alt, data-*, language tags)&, < and " become entities

The AST keeps the original text and escaping happens only in html(); code that renders the AST itself must escape on its own.

InputResult
[x](javascript:alert(1)) and other schemes besides http(s), mailto: and tel:Not linked; stays as plain text
Relative paths, #anchorRendered as links
mailto: / tel: linksNo target or rel; open in the same page
Other linksGet target="_blank" rel="noopener"

Raw HTML Sanitization


Lines that start with an HTML tag go through these rules before output:
RuleExamples
Remove dangerous tags (with paired content)<script>, <style>, <iframe>, <object>, <embed>, <form>, <link>, <meta>, <base>
Remove event attributesonclick, onerror and every other on* preceded by whitespace, in single-quoted, double-quoted and unquoted forms; an attribute joined to the tag name by / (such as <svg/onload=...>) is not matched
Remove dangerous URL attributeshref, src, xlink:href, action, formaction whose literal value starts with javascript:, vbscript: or data:text/html; values are not entity-decoded first, so jav&#x61;script: is kept

This is rule-based sanitization; when displaying fully untrusted content, pair it with a dedicated sanitizer such as DOMPurify.

Examples


Raw HTML


Event attributes and javascript: URLs are stripped from raw HTML lines.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. <div onclick="alert(1)">raw HTML (onclick removed)</div>
  4. <a href="javascript:alert(1)">javascript: href (removed)</a>
  5. [x](javascript:alert(1)) stays text, and <b> in text is escaped
  6. </script>
  7. <script>
  8. const out = document.getElementById("out");
  9. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  10. MDParser.Refresh(out);
  11. console.log(out.innerHTML);
  12. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文