MCPcopy Create free account
hub / github.com/cure53/DOMPurify / _createTrustedTypesPolicy

Function _createTrustedTypesPolicy

src/purify.ts:132–172  ·  view source on GitHub ↗
(
  trustedTypes: TrustedTypePolicyFactory,
  purifyHostElement: HTMLScriptElement
)

Source from the content-addressed store, hash-verified

130 * are not supported or creating the policy failed).
131 */
132const _createTrustedTypesPolicy = function (
133 trustedTypes: TrustedTypePolicyFactory,
134 purifyHostElement: HTMLScriptElement
135) {
136 if (
137 typeof trustedTypes !== 'object' ||
138 typeof trustedTypes.createPolicy !== 'function'
139 ) {
140 return null;
141 }
142
143 // Allow the callers to control the unique policy name
144 // by adding a data-tt-policy-suffix to the script element with the DOMPurify.
145 // Policy creation with duplicate names throws in Trusted Types.
146 let suffix = null;
147 const ATTR_NAME = 'data-tt-policy-suffix';
148 if (purifyHostElement && purifyHostElement.hasAttribute(ATTR_NAME)) {
149 suffix = purifyHostElement.getAttribute(ATTR_NAME);
150 }
151
152 const policyName = 'dompurify' + (suffix ? '#' + suffix : '');
153
154 try {
155 return trustedTypes.createPolicy(policyName, {
156 createHTML(html) {
157 return html;
158 },
159 createScriptURL(scriptUrl) {
160 return scriptUrl;
161 },
162 });
163 } catch (_) {
164 // Policy creation failed (most likely another DOMPurify script has
165 // already run). Skip creating the policy, as this will only cause errors
166 // if TT are enforced.
167 console.warn(
168 'TrustedTypes policy ' + policyName + ' could not be created.'
169 );
170 return null;
171 }
172};
173
174const _createHooksMap = function (): HooksMap {
175 return {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected