You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First time here? This is a repository hosting the Trusted Types specification draft and the polyfill code. You might want to check out other resources about Trusted Types:
Explainer - introductory explainer (what problem is the API solving?).
Specification draft - a more comprehensive and formalized description of the Trusted Types API.
Browser Support - The API is available natively in browsers based on Chromium version 83 and up.
Polyfill
This repository contains a polyfill implementation that allows you to use the API in all web browsers. The compiled versions are stored in dist directory.
Browsers
The ES5 / ES6 builds can be loaded directly in the browsers. There are two variants of the browser polyfill - api_only (light) and full. The api_only variant defines the API, so you can create policies and types. Full version also enables the type enforcement in the DOM, based on the CSP policy it infers from the current document (see src/polyfill/full.js).
<!-- API only --><scriptsrc="https://w3c.github.io/webappsec-trusted-types/dist/es5/trustedtypes.api_only.build.js"></script><script>constp=trustedTypes.createPolicy('foo', ...)document.body.innerHTML=p.createHTML('foo');// worksdocument.body.innerHTML='foo';// but this one works too (no enforcement).</script>
Polyfill is published as an npm package trusted-types:
$ npm install trusted-types
The polyfill supports both CommonJS and ES Modules.
consttt=require('trusted-types');// or import { trustedTypes } from 'trusted-types'tt.createPolicy(...);
Tinyfill
Due to the way the API is designed, it's possible to polyfill the most important
API surface (trustedTypes.createPolicy function) with the following snippet:
It does not enable the enforcement, but allows the creation of policies that
return string values instead of Trusted Types in non-supporting browsers. Since
the injection sinks in those browsers accept strings, the values will be accepted
unless the policy throws an error. This tinyfill code allows most applications
to work in both Trusted-Type-enforcing and a legacy environment.
Building
To build the polyfill yourself (Java required):
$ git clone https://github.com/w3c/webappsec-trusted-types/
$ cd trusted-types
$ npm install
$ npm run build
Demo
To see the polyfill in action, visit the demo page.