---
title: "Lock down risky DOM writes · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/browser-security-headers/trusted-types
id: APS-35
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 784
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Lock down risky DOM writes

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-35
- Level: Recommended
- Chapter: [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
- Rule: Require Trusted Types in the CSP, so scripts can't write raw HTML into the page. Send every remaining case through one small named policy.
- Check: With the header on, a browser test visits every page and uses its main features. The console shows no Trusted Types violations.
- How: Add `require-trusted-types-for 'script'` and `trusted-types <name>` to the CSP. Remove `innerHTML` and similar calls. Send the few you need through one policy that checks its input. Script addresses, such as a service worker's, need a policy too. Escape text first, as [Escape everything you show](https://agentwranglers.se/playbook/application-security/input-and-injection/cross-site-scripting.md) says.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### HTTP example

```http
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app
```

### JavaScript example

```js
const policy = trustedTypes.createPolicy('app', {
  createHTML: s => s.replace(/[<>&"']/g, c => `&#${c.charCodeAt(0)};`),
});
el.innerHTML = policy.createHTML(userText);
```

## Sources

- [W3C Trusted Types](https://www.w3.org/TR/trusted-types/)

## Related rules

- CSS-24 [Clone markup that scripts add from a template element](https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/template-element.md) in HTML and CSS
- APS-3 [Escape everything you show](https://agentwranglers.se/playbook/application-security/input-and-injection/cross-site-scripting.md) in Application security
- APS-100 [Send the security headers as one set](https://agentwranglers.se/playbook/application-security/browser-security-headers/header-set.md) in Application security

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/browser-security-headers/trusted-types">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Send a hash of the body](https://agentwranglers.se/playbook/application-security/browser-security-headers/digest-fields.md)
- Next: [Sending the old XSS filter header](https://agentwranglers.se/playbook/application-security/browser-security-headers/x-xss-protection.md)
- [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
