Trusted Types
ELI5 - The Vibe Check
Trusted Types is the browser refusing to let random strings get shoved into innerHTML unless you've explicitly promised they're safe. Most DOM based XSS bugs happen because some string sneaks into the DOM without anyone checking it. Trusted Types makes the browser enforce that check for you, at the platform level, instead of hoping every developer remembers to sanitize.
Real Talk
Trusted Types is a browser security API, enforced via Content Security Policy, that restricts DOM XSS sink functions like innerHTML or eval to only accept specially typed objects created through a registered policy, rather than raw strings. It forces sanitization or validation logic through an explicit, auditable chokepoint, making DOM based XSS significantly harder to introduce accidentally.
Show Me The Code
// Register a Trusted Types policy
const policy = trustedTypes.createPolicy('safeHTML', {
createHTML: (input) => DOMPurify.sanitize(input)
});
element.innerHTML = policy.createHTML(userInput);
When You'll Hear This
"We turned on Trusted Types and immediately caught three innerHTML assignments we'd forgotten to sanitize." / "The library broke under Trusted Types because it was building HTML with raw string concatenation."
Related Terms
Content Security Policy (CSP)
Content Security Policy is an HTTP header that tells the browser exactly where it's allowed to load scripts, images, and other resources from.
Input Validation
Input validation is checking that user input is what you expect before using it.
Sanitization
Sanitization is cleaning up user input before using it — stripping out anything dangerous like script tags or SQL commands.
XSS (XSS)
XSS stands for Cross-Site Scripting. Hackers inject their own JavaScript into your site so when other users visit, the evil script runs in their browser.