DOM XSS Prevention with Trusted Types
Type-enforced browser defense that locks dangerous DOM sinks (innerHTML, eval, script.src) against raw strings, permitting only objects minted by audited TrustedTypePolicy sanitizers.
About this page
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.
Where this connects
Prerequisites
Browser Rendering Pipeline (Critical Rendering Path)
DOM sinks invoke the HTML parser and script execution pipeline directly from JavaScript.
Content Security Policy Nonces and strict-dynamic
Trusted Types are activated and policy-allowlisted via Content-Security-Policy headers.
Seen in Architecture Autopsies
Autopsy #11: The Chat Bubble That Left With the Session Tokens
Enforces TrustedHTML objects on innerHTML sinks so raw attacker strings throw a TypeError.
Mechanical Comparisons
- Contrasts WithWeb Crypto API and SubtleCrypto vs DOM XSS Prevention with Trusted Types
Non-extractable CryptoKeys resist exfiltration even if an XSS payload executes in the page.