Skip to content

Commit 16cf6c0

Browse files
committed
trusted-types-eval - add
1 parent a586cfe commit 16cf6c0

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

files/en-us/web/api/trusted_types_api/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ The following sections list injection sinks that are expected to accept trusted
209209

210210
- {{domxref("Document.parseHTMLUnsafe_static()")}}
211211
- {{domxref("Document.write()")}}
212+
- {{domxref("Document.writeln()")}}
212213
- {{domxref("DOMParser.parseFromString()")}}
213214
- {{domxref("Element.innerHTML")}}
214215
- {{domxref("Element.insertAdjacentHTML")}}
@@ -232,13 +233,28 @@ The following sections list injection sinks that are expected to accept trusted
232233
- {{domxref("HTMLScriptElement.src")}}
233234
- {{domxref("SvgAnimatedString.baseVal")}}
234235

236+
<!--
237+
238+
These still require links, and possibly docs
239+
- [`ServiceWorkerContainer.register()`](https://developer.mozilla.org/docs/Web/API/ServiceWorkerContainer/register)
240+
- [`WorkerGlobalScope.importScripts`](https://developer.mozilla.org/docs/Web/API/WorkerGlobalScope/importScripts)
241+
- [`Window.trustedTypes`](https://developer.mozilla.org/docs/Web/API/Window/trustedTypes)
242+
-->
243+
235244
## Extensions to HTTP
236245

246+
{{httpheader("Content-Security-Policy")}} directives:
247+
237248
- {{CSP("require-trusted-types-for")}}
238249
- : Enforces that [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are passed to DOM XSS [injection sinks](/en-US/docs/Web/API/Trusted_Types_API#concepts_and_usage).
239250
- {{CSP("trusted-types")}}
240251
- : Used to specify an allowlist of [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) policy names.
241252

253+
{{httpheader("Content-Security-Policy")}} keywords:
254+
255+
- [`'trusted-types-eval'`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#trusted-types-eval)
256+
- : Allows [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) and similar functions to be used but only when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are supported and enabled.
257+
242258
## Examples
243259

244260
In the below example we create a policy that will create {{domxref("TrustedHTML")}} objects using {{domxref("TrustedTypePolicyFactory.createPolicy()")}}. We can then use {{domxref("TrustedTypePolicy.createHTML()")}} to create a sanitized HTML string to be inserted into the document.

files/en-us/web/http/guides/csp/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ Like inline JavaScript, if a CSP contains either a `default-src` or a `script-sr
329329
setTimeout("console.log('hello from setTimeout')", 1);
330330
```
331331

332-
The `unsafe-eval` keyword can be used to override this behavior, and as with `unsafe-inline`, and for the same reasons: **developers should avoid `unsafe-eval`**. Sometimes it can be difficult to remove usages of `eval()`: in these situations, the [Trusted Types API](/en-US/docs/Web/API/Trusted_Types_API) can make it safer, by ensuring that the input meets a defined policy.
332+
The `unsafe-eval` keyword can be used to override this behavior, and as with `unsafe-inline`, and for the same reasons: **developers should avoid `unsafe-eval`**.
333+
334+
Sometimes it can be difficult to remove usages of `eval()` and the other methods: in these situations, the [Trusted Types API](/en-US/docs/Web/API/Trusted_Types_API) can make it safer, by ensuring that the input meets a defined policy.
335+
The `trusted-types-eval` keyword should be used to override the behavior in this case!
336+
Unlike `unsafe-inline` it only overrides the behavior in browser when trusted types are supported and enabled; which ensures that the methods will remain blocked on browsers that don't support trusted types.
333337

334338
Unlike `unsafe-inline`, the `unsafe-eval` keyword does still work in a directive that contains nonce or hash expressions.
335339

files/en-us/web/http/reference/headers/content-security-policy/index.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ Reporting directives control the destination URL for CSP violation reports in `C
159159
- : Used to specify an allowlist of [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) policies.
160160
Trusted Types allows applications to lock down DOM XSS injection sinks to only accept non-spoofable, typed values in place of strings.
161161
- {{CSP("upgrade-insecure-requests")}}
162-
- : Instructs user agents to treat all of a site's insecure URLs (those served over
163-
HTTP) as though they have been replaced with secure URLs (those served over HTTPS).
164-
This directive is intended for websites with large numbers of insecure legacy URLs
165-
that need to be rewritten.
162+
- : Instructs user agents to treat all of a site's insecure URLs (those served over HTTP) as though they have been replaced with secure URLs (those served over HTTPS).
163+
This directive is intended for websites with large numbers of insecure legacy URLs that need to be rewritten.
166164
167165
### Deprecated directives
168166
@@ -272,14 +270,33 @@ Secure upgrades are allowed. For example:
272270
- If the document is served from `http://example.com`, then a CSP of `'self'` will also permit resources from `https://example.com`.
273271
- If the document is served from `ws://example.org`, then a CSP of `'self'` will also permit resources from `wss://example.org`.
274272

273+
### 'trusted-types-eval'
274+
275+
By default, if a CSP contains a `default-src` or a `script-src` directive, then JavaScript functions which evaluate their arguments as JavaScript are disabled.
276+
This includes [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval), the [`code`](/en-US/docs/Web/API/Window/setTimeout#code) argument to {{domxref("Window.setTimeout()", "setTimeout()")}}, or the {{jsxref("Function/Function()", "Function()")}} constructor.
277+
278+
The `trusted-types-eval` keyword can be used to undo this protection, but only when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are enforced and passed to these functions instead of strings.
279+
This allows dynamic evaluation of strings as JavaScript, but only after inputs have been passed through a transformation function before it is injected, which has the chance to [sanitize](/en-US/docs/Web/Security/Attacks/XSS#sanitization) the input to remove potentially dangerous markup.
280+
281+
The `trusted-types-eval` must be used instead of [`'unsafe-eval'`](#unsafe-eval) when using these methods with trusted types.
282+
This ensures that access to the methods is blocked on browsers that don't support trusted types.
283+
284+
> [!NOTE]
285+
> Developers should avoid using `trusted-types-eval` or these methods unless absolutely necessary.
286+
> Trusted types ensure that the input passes through a transformation function — they don't ensure that the transformation makes the input safe (and this can be very hard to get right).
287+
288+
See [`eval()` and similar APIs](/en-US/docs/Web/HTTP/Guides/CSP#eval_and_similar_apis) in the CSP guide for more usage information.
289+
275290
### 'unsafe-eval'
276291

277-
By default, if a CSP contains a `default-src` or a `script-src` directive, then JavaScript functions which evaluate their arguments as JavaScript are disabled. This includes [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval), the [`code`](/en-US/docs/Web/API/Window/setTimeout#code) argument to {{domxref("Window.setTimeout()", "setTimeout()")}}, or the {{jsxref("Function/Function()", "Function()")}} constructor.
292+
By default, if a CSP contains a `default-src` or a `script-src` directive, then JavaScript functions which evaluate their arguments as JavaScript are disabled.
293+
This includes [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval), the [`code`](/en-US/docs/Web/API/Window/setTimeout#code) argument to {{domxref("Window.setTimeout()", "setTimeout()")}}, or the {{jsxref("Function/Function()", "Function()")}} constructor.
278294

279295
The `unsafe-eval` keyword can be used to undo this protection, allowing dynamic evaluation of strings as JavaScript.
280296

281297
> [!WARNING]
282298
> Developers should avoid `'unsafe-eval'`, because it defeats much of the purpose of having a CSP.
299+
> ['trusted-types-eval'](#trusted-types-eval) provides a "potentially" safer alternative if using these methods is necessary.
283300
284301
See [`eval()` and similar APIs](/en-US/docs/Web/HTTP/Guides/CSP#eval_and_similar_apis) in the CSP guide for more usage information.
285302

0 commit comments

Comments
 (0)