DOM XSS Prevention with Trusted Types
Trusted Types eliminates DOM-based Cross-Site Scripting by locking ~60 hazardous sinks (innerHTML, document.write, script.src, eval) at the browser engine boundary. When enforced via CSP, raw strings throw a TypeError; only TrustedHTML, TrustedScript, or TrustedScriptURL objects minted by named policies succeed.
DOM Injection Sinks: Raw String vs TrustedHTML Policy
const bio = new URLSearchParams(location.search).get('bio');
profileEl.innerHTML = bio; // Executes <img onerror=...>!Any attacker-controlled string flows straight into the HTML parser.
const policy = trustedTypes.createPolicy('app-html', {
createHTML: (dirty) => DOMPurify.sanitize(dirty),
});
profileEl.innerHTML = policy.createHTML(bio);Raw strings throw TypeError; only TrustedHTML from app-html compiles.
Under the Hood
Trusted Types acts like a factory safety interlock on a hydraulic press: the machine refuses to cycle unless the material carries a stamped inspection tag (TrustedHTML) from an approved quality gate (trusted-types CSP allowlist).
- Sink Lockdown: Adding
require-trusted-types-for 'script'to Content Security Policy instructs the DOM bindings to reject plainstringarguments at every injection sink. - Named Policies:
trustedTypes.createPolicy('app-html', { createHTML })centralizes sanitization (such as DOMPurify) into one auditable call site. - Parser Isolation: Even if an attacker controls URL parameters or
postMessagedata, the Browser Rendering Pipeline never parses raw markup unless it passes through a policy.
// Requires CSP: require-trusted-types-for 'script'; trusted-types app-sanitizer;
const sanitizer = trustedTypes.createPolicy('app-sanitizer', {
createHTML: (input) => DOMPurify.sanitize(input),
});
container.innerHTML = sanitizer.createHTML(userBio);
// container.innerHTML = userBio; -> throws TypeError!Problem Genealogy
- 2005–2018 (Manual Sanitization): Teams sprinkled
sanitize()calls ad hoc; one forgotteninnerHTMLin 500 components reopened DOM XSS. - 2019 (Google Trusted Types): Shifted DOM XSS defense from "find every sink" to "type-lock all sinks in the engine."
- 2024+ (W3C Standardization): Expanded beyond Chromium into cross-engine interop baselines.
Production Pitfalls
- Registering a pass-through
defaultpolicy: WritingcreateHTML: (s) => ssilences errors while keeping every XSS vulnerability wide open. - Enabling enforcement without Report-Only rollout: Turning on
require-trusted-types-for 'script'immediately breaks third-party scripts that assign strings toinnerHTML; audit withContent-Security-Policy-Report-Onlyfirst.
- When not to use
innerHTMLat all: For plain text or structured UI updates, assignelement.textContentor render framework JSX nodes, they never invoke the HTML parser and need zero policies.
Connections & Logical Next Steps
DOM sinks invoke the HTML parser and script execution pipeline directly from JavaScript.
Trusted Types are activated and policy-allowlisted via Content-Security-Policy headers.
Combining nonce-based CSP with Trusted Types blocks both initial script injection and runtime DOM sinks.
Non-extractable CryptoKeys resist exfiltration even if an XSS payload executes in the page.