# Trusted Types

> The Trusted Types is a browser security API that prevents DOM-based XSS by requiring typed values for dangerous sinks.

## What are Trusted Types and how do they prevent DOM XSS?

The API enabled through CSP that forbids passing plain strings to dangerous sinks like `innerHTML`. Values must first pass through a policy that sanitizes them, which makes DOM-based XSS much harder.

- Topic: Security
- Type: technique

## Example

Wrong:

```js
const html = await res.text();
el.innerHTML = html;
```

Right:

```js
const policy = trustedTypes.createPolicy('sanitize', {
  createHTML: (s) => DOMPurify.sanitize(s),
});
const html = await res.text();
el.innerHTML = policy.createHTML(html);
```

With Trusted Types enforced, `innerHTML` accepts only values from a policy, so every HTML string goes through the sanitizer.

## Common mistakes

- Creating a policy that returns input as is, which keeps the XSS hole open.

## Key points

- The CSP header includes `require-trusted-types-for 'script'`.
- Policies from `trustedTypes.createPolicy()` sanitize input, they do not return it unchanged.
- The `trusted-types` directive lists the allowed policy names.
- A 'default' policy is not used to pass every string through and silence errors.

## Interview questions

### Do Trusted Types replace HTML sanitizing?

No. They force every value for a dangerous sink to go through a policy, and the policy itself must still sanitize.

### How do I roll out Trusted Types without breaking my site?

Send `require-trusted-types-for 'script'` in a `Content-Security-Policy-Report-Only` header first, fix the reported violations, then enforce.

### Which sinks do Trusted Types protect?

HTML sinks like `innerHTML` and `insertAdjacentHTML`, script sinks like `eval()`, and script URL sinks like `script.src`.

## Related cards

- [CSP](https://wonderium.dev/web/csp.md)
- [.innerHTML](https://wonderium.dev/js/inner-html.md)
- [Reflected XSS](https://wonderium.dev/web/xss-reflected.md)
- [Stored XSS](https://wonderium.dev/web/xss-stored.md)
- [.getHTML()](https://wonderium.dev/js/get-html.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/trusted-types
- Last updated: 2026-11-25
