Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readWeb Platform

Guard innerHTML with Trusted Types

Enforce Trusted Types for an innerHTML preview, let a named policy return fixed markup, and insert the summary as text.

Your preview code assigns an HTML string to innerHTML. That line is an injection sink, a place where the browser parses the input as markup. If the preview needs a layout as well as user text, decide which code can supply the layout and insert the text on its own.

Authorize a named policy

Serve the example page with this response header.

Content-Security-Policy: require-trusted-types-for 'script'; trusted-types preview-template

In a browser that enforces Trusted Types, an ordinary string assignment to innerHTML now throws a TypeError on this page.

The Trusted Types directives do two jobs. require-trusted-types-for 'script' turns on enforcement at covered sinks, including innerHTML. trusted-types preview-template limits policy creation to that name. Add these directives to your site's existing CSP and account for any policy names the app already uses.

Give the policy an input it can check

This policy accepts a layout name and returns fixed markup from a map. Insert the summary afterward as text.

<section id="preview"></section>
<script>
  const preview = document.querySelector('#preview')
  const templates = new Map([
    ['compact', '<p class="summary"></p>'],
    ['card', '<article><p class="summary"></p></article>'],
  ])
  function templateFor(name) {
    if (!templates.has(name)) throw new TypeError('Unknown template')
    return templates.get(name)
  }

  const policy = window.trustedTypes?.createPolicy('preview-template', {
    createHTML: templateFor,
  })
  const name = 'card'
  preview.innerHTML = policy ? policy.createHTML(name) : templateFor(name)
  preview.querySelector('.summary').textContent = 'Small & <large>'
</script>

The card wraps the summary, and the page displays Small & <large> as text.

diagram

Two inputs at the preview sink

The policy turns a known template name into TrustedHTML before the preview assignment.

createHTML wraps the policy's return value as TrustedHTML. The policy has to decide which markup is safe. Here, templateFor can return only the two strings in the map. A miss throws, and the summary never goes through the template policy.

Use textContent by itself if the preview needs plain text. For a feature that accepts rich HTML from users, have a maintained sanitizer process that HTML inside its own narrow policy. Passing the original string through unchanged would put the risky sink back within reach.

The example also calls templateFor on browsers without the API, so the same fixed markup and text insertion rules hold there. Enforcement can expose other assignments in your app or its dependencies. Try the directives as Content-Security-Policy-Report-Only first in a real rollout, exercise the pages that write HTML, and fix the violations before moving the directives to the enforced policy.