Dev.Lib
arrow_backBack to Blogs
LEVEL_066.2June 27, 20261 min read

Trusted Types

Web Security

Enforces typed DOM sinks — `innerHTML` requires TrustedHTML from policy, blocking DOM XSS injection.

Summary

Enforces typed DOM sinks — innerHTML requires TrustedHTML from policy, blocking DOM XSS injection. Supported Chromium; polyfill/fallback strategies elsewhere. Integrates with CSP require-trusted-types-for 'script'. Libraries must be compatible or use policy createHTML wrappers.

Code example

if (window.trustedTypes) {
  const policy = trustedTypes.createPolicy('app', {
    createHTML: (input) => DOMPurify.sanitize(input),
  });
  element.innerHTML = policy.createHTML(untrustedHtml);
}

Key points

  • Enforces typed DOM sinks — innerHTML requires TrustedHTML from policy, blocking DOM XSS injection.
  • Supported Chromium; polyfill/fallback strategies elsewhere.
  • Integrates with CSP require-trusted-types-for 'script'.
  • Libraries must be compatible or use policy createHTML wrappers.

Further reading

Related concepts