CCyberwiki

Trusted Types

Trusted Types
TypeBrowser API with two CSP directives
HeaderContent-Security-Policy: require-trusted-types-for 'script'
SpecificationW3C Trusted Types, Working Draft
CWECWE-79 (the weakness it prevents)
OWASP Top 10A05:2025 Injection
Browser supportChrome and Edge 83, Firefox 148, Safari 26
AffectsFront-end JavaScript that writes HTML, script text or script addresses into the page

The line el.innerHTML = decodeURIComponent(location.hash.slice(1)) takes whatever follows the # in the page address and parses it as HTML. Anyone who gets a visitor to open a crafted link can then run their own JavaScript in that visitor's browser, a flaw called DOM-based cross-site scripting.1 With Trusted Types switched on, the browser refuses plain strings at innerHTML and similar properties. They accept only objects made by a function the site has written and named, so every value that reaches the page as markup or code has passed through code the developers chose.2

Sinks and types

The browser functions and properties that turn text into markup or running code are called injection sinks. Trusted Types sorts them into three groups and gives each group its own object type:3

Table 1. Sink groups and the type each one accepts under enforcement
TypeExamples of sinks
TrustedHTMLinnerHTML, outerHTML, insertAdjacentHTML(), document.write(), DOMParser.parseFromString(), <iframe srcdoc>
TrustedScripteval(), new Function(), text given to setTimeout() or setInterval(), the text of a <script> element
TrustedScriptURL<script src>, the address given to new Worker(), navigator.serviceWorker.register()

Counted across all three groups there are around 60 sinks; the exact number depends on the browser engine and version.4 The browser also checks some indirect routes: a <script> element filled through a text node is checked at the moment it is added to the document and could run.3

Policies

The trusted objects can only be created by a policy, an object made with trustedTypes.createPolicy(). The site gives the policy a name and the functions that turn a string into each type. The browser ships no policy of its own; what the functions do is up to the site.3 A common HTML policy runs the input through the DOMPurify sanitizer:

const policy = trustedTypes.createPolicy("app-html", {
  createHTML: (input) => DOMPurify.sanitize(input),
});

el.innerHTML = policy.createHTML(comment.body); // accepted
el.innerHTML = comment.body;                    // TypeError

DOMPurify can also return a TrustedHTML object directly when called with { RETURN_TRUSTED_TYPE: true }.1 Where the code needs no markup, the first fix in the migration guide is to drop the sink: set textContent on an ordinary element, or build the nodes with document.createElement(), so no string is parsed as HTML.1

Turning it on

On its own the JavaScript API blocks nothing, and a string can still be assigned to innerHTML.3 Enforcement comes from a Content Security Policy directive sent in the page's response header. A second directive lists the policy names the page may create, so no other code on the page can register a policy the developers did not expect; createPolicy() with any other name throws.5

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

The value 'none' for trusted-types forbids every policy, and 'allow-duplicates' lets one name be registered more than once.5 The feature works only on pages served over HTTPS or from localhost.1

A site with existing code can first send the same directive in a Content-Security-Policy-Report-Only header. The page keeps working, and the browser sends a report for each string that reaches a sink, with the file, the line and the first characters of the value, such as Element innerHTML <img src=x. When the reports stop, the header is switched to enforcing.1

The default policy

A policy named default has a special role. Under enforcement the browser passes every plain string that reaches a sink to this policy and uses what it returns; if it returns null or no value, the assignment throws.3 It exists for code the site cannot change, such as a third-party script loaded from a CDN, and is meant as a step during migration.1

What it guarantees

With enforcement on and policy names fixed, the places where DOM-based XSS can enter shrink to the bodies of the listed policies, and a security review can start there.1 Enforcement does not check what a policy does with its input. A policy that returns its input unchanged passes an attack through, and a flaw in the sanitizer a policy calls leaves the site exposed.1 Trusted Types also does not touch HTML the server builds: Output encoding and HTML sanitization still apply there.

For browsers without support, a two-line stand-in makes createPolicy() return the policy's own functions, so the same sanitizing code paths run, provided the code has first been tested in a browser that enforces.3

if (!window.trustedTypes)
  window.trustedTypes = { createPolicy: (n, rules) => rules };

History and adoption

Chrome and Edge have supported Trusted Types since version 83. Firefox added it in version 148 and Safari in version 26, and since February 2026 it has worked in the current version of every major browser.13 The specification, edited at Google, is still a W3C Working Draft; the most recent was published on 7 October 2026.6

By mid-2021 Google enforced it on more than 130 services, among them Google Photos and Google Contacts, and had seen no DOM-based XSS in any migrated application. Old code marked as "legacy conversions" was still allowed to create trusted values, and XSS stayed possible there.4 A review of DOM XSS bugs reported to Google's bug bounty, among those its static analysis had missed, estimated that at least 61% would have been stopped. About three quarters of the code that broke under enforcement was fixed by mechanical rewrites. Visual Studio Code also enforces it, with about 100 root causes of violations found in a million lines of code. Angular and Lit support it by default; React, Webpack and DOMPurify through a setting.4

In SecFlash

On the Security page, a script that calls eval, assigns to innerHTML or uses a similar sink shows as SF-SEC-646, Dangerous JavaScript functions. A page whose own JavaScript writes part of the address into the page as HTML, the case in the first paragraph, shows as SF-SEC-610.

See also

References

  1. a b c d e f g h i Kotowicz, Krzysztof (25 March 2020). "Prevent DOM-based cross-site scripting vulnerabilities with Trusted Types". web.dev. Google.
  2. back "Content-Security-Policy: require-trusted-types-for directive". MDN Web Docs. Mozilla.
  3. a b c d e f g "Trusted Types API". MDN Web Docs. Mozilla.
  4. a b c Kotowicz, Krzysztof et al. (July 2021). "Trusted Types: mid 2021 report". Google. Announced on the public-webappsec mailing list, 7 July 2021.
  5. a b "Content-Security-Policy: trusted-types directive". MDN Web Docs. Mozilla.
  6. back Kotowicz, Krzysztof, ed. (7 October 2026). "Trusted Types". W3C Working Draft. W3C Web Application Security Working Group.