Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Chrome refuses an innerHTML string under a Trusted Types policy

Your error

This document requires 'TrustedHTML' assignment.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

Your own code writes an HTML string into a sink

Most of these writes only set text or empty an element, and need no policy: use textContent, createElement or trustedTypes.emptyHTML.

Fix
// text and clearing need no policy
el.textContent = message;
list.innerHTML = trustedTypes.emptyHTML;

// real markup goes through one sanitizing policy
const htmlPolicy = trustedTypes.createPolicy('app-html', {
  createHTML: (input) => DOMPurify.sanitize(input),
});
el.innerHTML = htmlPolicy.createHTML(markupFromServer);

# response header
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app-html
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

This document requires 'TrustedHTML' assignment is the TypeError Chrome throws when the page's Content Security Policy has require-trusted-types-for 'script' and code hands a plain string to innerHTML, insertAdjacentHTML or document.write. Write text with textContent, build markup through a named Trusted Types policy, and cover libraries you cannot edit with a default policy. Applies to current Chromium browsers.

  • Applies to Chrome and Edge (current Chromium releases), Trusted Types API, webpack 5.113
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Confirm that the document enforces Trusted Types, then read the error's stack in DevTools. A top frame in your own file points to cause 1, frames inside a library to cause 2, and a message that goes on about the 'default' policy to cause 3. A header sent as Content-Security-Policy-Report-Only only reports; it does not throw.

Run this first
curl -sI https://example.com/ | grep -i "content-security-policy"
# in the DevTools console of the failing page:
document.querySelector('meta[http-equiv="Content-Security-Policy"]')?.content

Then find the line you see in this list and open its fix:

  1. 1Your own code writes an HTML string into a sinkThe short message, with the top stack frame on one of your lines that sets innerHTML, outerHTML, insertAdjacentHTML or document.write
  2. 2A library you cannot change writes the stringThe same short message, but the top frames sit inside jQuery, AngularJS or another vendor file
  3. 3A default policy exists but gives back no HTMLThe message continues: and the 'default' policy failed to execute. or: and no 'default' policy for 'TrustedHTML' has been defined.

What does the error look like?

  • This document requires 'TrustedHTML' assignment.
  • This document requires 'TrustedHTML' assignment and the 'default' policy failed to execute.
  • This document requires 'TrustedHTML' assignment and no 'default' policy for 'TrustedHTML' has been defined.

Cause 1 of 3

Your own code writes an HTML string into a sink

You seeThe short message, with the top stack frame on one of your lines that sets innerHTML, outerHTML, insertAdjacentHTML or document.write

Check

This document requires 'TrustedHTML' assignment appears when require-trusted-types-for 'script' is enforced and a plain string reaches an HTML sink. Under that rule Chrome accepts only TrustedHTML objects for innerHTML, outerHTML, insertAdjacentHTML, iframe srcdoc, document.write and DOMParser.parseFromString, and throws a TypeError for anything else.

Check
grep -rnE "innerHTML|outerHTML|insertAdjacentHTML|document\.write|parseFromString" src/

Fix

Most of these writes only set text or empty an element, and need no policy: use textContent, createElement or trustedTypes.emptyHTML. For real markup, create one named policy that sanitizes and pass its output to the sink. When the CSP also has a trusted-types directive, list the policy name there.

Fix
// text and clearing need no policy
el.textContent = message;
list.innerHTML = trustedTypes.emptyHTML;

// real markup goes through one sanitizing policy
const htmlPolicy = trustedTypes.createPolicy('app-html', {
  createHTML: (input) => DOMPurify.sanitize(input),
});
el.innerHTML = htmlPolicy.createHTML(markupFromServer);

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

Verify

After a reload, DevTools shows no TypeError and the element renders the cleaned markup, while the response header lists your policy name.

Verify
curl -sI https://example.com/ | grep -i "trusted-types app-html"

Confirmed by web.dev

Did this fix it?

Cause 2 of 3

A library you cannot change writes the string

You seeThe same short message, but the top frames sit inside jQuery, AngularJS or another vendor file

Check

Here the TrustedHTML assignment error starts in third-party code, so there is no line of yours to rewrite. Trusted Types has a hook for exactly this case: when a policy named default exists and enforcement is on, every plain string sent to a sink passes through that policy first.

Check
grep -rn "createPolicy" src/

Fix

Register a default policy that sanitizes, and load it before the library runs. The web.dev guide asks you to use it sparingly and to prefer named policies in code you control; it exists for code you cannot refactor. If the CSP lists policy names, add default to the list.

Fix
// trusted-types-default.js, loaded before the library
if (window.trustedTypes && trustedTypes.createPolicy) {
  trustedTypes.createPolicy('default', {
    createHTML: (input) => DOMPurify.sanitize(input),
  });
}

# response header
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app-html default

Verify

The widget the library draws renders again, and the console shows no Trusted Types error.

Verify
curl -sI https://example.com/ | grep -i "trusted-types"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

A default policy exists but gives back no HTML

You seeThe message continues: and the 'default' policy failed to execute. or: and no 'default' policy for 'TrustedHTML' has been defined.

Check

These longer forms of the TrustedHTML assignment error mean a default policy is registered but could not turn the string into HTML. In Chromium's source, the no-policy-defined wording is used when the default policy has no createHTML function, and the failed-to-execute wording when createHTML returned nothing. MDN states that a default policy returning null or undefined makes the browser throw a TypeError.

Check
grep -rn -A4 "createPolicy('default'" src/

Fix

Give the default policy a createHTML function and make it return a string on every path. Return null only for input you deliberately want blocked, because that is what makes the write throw.

Fix
trustedTypes.createPolicy('default', {
  createHTML: (input) => DOMPurify.sanitize(input), // always a string
});

Verify

Run a test write in the console of the page. It no longer throws, and any error left elsewhere is the short form from a sink you have not covered yet.

Verify
document.createElement('div').innerHTML = '<b>test</b>'; // no TypeError once the default policy returns a string

Confirmed by chromium.googlesource.com

Did this fix it?

How do I stop it coming back?

  • Ship require-trusted-types-for 'script' under Content-Security-Policy-Report-Only first, and read the reports until no new sink shows up before you enforce it.
  • List every allowed policy name in a trusted-types directive, so code you did not expect cannot create a policy of its own.
  • On webpack 5.37 or later, set output.trustedTypes (and allow its policy name), so lazily loaded chunks get their script URLs from a policy; without it the next failure reads 'TrustedScriptURL' instead.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.