Skip to content

Commit 4db798b

Browse files
hamishwilleebsmth
andauthored
FF145 Relnote: Trusted Types early beta (#41518)
* FF145 Relnote: Trusted Types early beta * trusted-types-eval - add * Add more items to trusted types sinks * Add note about indirect injection * Apply suggestions from code review Co-authored-by: Brian Smith <brian@smith.berlin> * Move the injection sink list --------- Co-authored-by: Brian Smith <brian@smith.berlin>
1 parent e80d1c1 commit 4db798b

File tree

5 files changed

+114
-22
lines changed

5 files changed

+114
-22
lines changed

files/en-us/mozilla/firefox/experimental_features/index.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -408,26 +408,20 @@ The {{domxref("CloseWatcher")}} interface allows developers to implement UI comp
408408
### Trusted Types API
409409

410410
The [Trusted Types API](/en-US/docs/Web/API/Trusted_Types_API) provides mechanisms to ensure that functions that can potentially be used as vectors for XSS attacks are only able to be called with data that has been validated or sanitized.
411+
The API is enabled in early beta releases ([Firefox bug 1992941](https://bugzil.la/1992941)).
411412

412-
> [!NOTE]
413-
> At the time of writing not enough of the API has been implemented for it to be effectively testable.
414-
> This note will be removed once it is ready.
413+
This includes (non-exhaustively):
415414

416-
This subset of the API has been implemented:
417-
418-
- {{domxref("TrustedTypePolicyFactory")}}:
419-
- {{domxref("TrustedTypePolicyFactory/getAttributeType", "getAttributeType()")}} and {{domxref("TrustedTypePolicyFactory/getPropertyType", "getPropertyType()")}} ([Firefox bug 1917783](https://bugzil.la/1917783), [Firefox bug 1917784](https://bugzil.la/1917784)).
420-
- The {{domxref("Document.write()", "write()")}} and {{domxref("Document.writeln()","writeln()")}} methods of the {{domxref("Document")}} interface now accept {{domxref("TrustedHTML")}} objects as parameters, in addition to strings. ([Firefox bug 1906301](https://bugzil.la/1906301)).
421-
- The {{domxref("HTMLScriptElement.text","text")}}, {{domxref("HTMLElement.innerText","innerText")}}, and {{domxref("Node.textContent","textContent")}} properties of the {{domxref("HTMLScriptElement")}} interface now accept {{domxref("TrustedScript")}} objects a value, while {{domxref("HTMLScriptElement.src", "src")}} accepts {{domxref("TrustedScriptURL")}} values. ([Firefox bug 1905706](https://bugzil.la/1905706)).
422-
- The {{domxref("Window.setInterval()")}} and {{domxref("Window.setTimeout()")}} methods can be called with a {{domxref("TrustedScript")}}. ([Firefox bug 1931290](https://bugzil.la/1931290)).
423-
- The global [`trustedTypes`](/en-US/docs/Web/API/Window/trustedTypes) property is available for accessing the Trusted Types API.
424-
- The properties {{domxref("Element.innerHTML")}} and {{domxref("ShadowRoot.innerHTML")}} can be called with [trusted types](/en-US/docs/Web/API/Trusted_Types_API).
415+
- Addition of the {{domxref("TrustedTypePolicyFactory")}}, {{domxref("TrustedTypePolicy")}}, {{domxref("TrustedHTML")}}, {{domxref("TrustedScript")}}, {{domxref("TrustedScriptURL")}} interfaces, and the `trustedTypes` property on {{domxref("Window/trustedTypes", "Window")}} and {{domxref("WorkerGlobalScope/trustedTypes", "WorkerGlobalScope")}}.
416+
- Updates to [injection sink interfaces](/en-US/docs/Web/API/Trusted_Types_API#injection_sink_interfaces), such as {{domxref("Element.innerHTML")}} and {{domxref("Document.write()", "document.write()")}}, to allow the `TrustedHTML`, `TrustedScript`, `TrustedScriptURL` to be passed as well as strings.
417+
- Support for the [`require-trusted-types-for`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for) and [`trusted-types`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/trusted-types) directives, and the [`'trusted-types-eval'`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#trusted-types-eval) keyword, of the {{HTTPHeader("Content-Security-Policy")}} HTTP header.
418+
These can be used to enforce trusted types instead of strings, name the specific policies that are allowed, and to enable [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) and similar functions to be used when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are supported and enforced.
425419

426420
| Release channel | Version added | Enabled by default? |
427421
| ----------------- | ------------- | ------------------- |
428-
| Nightly | 133 | No |
429-
| Developer Edition | 133 | No |
430-
| Beta | 133 | No |
422+
| Nightly | 145 | Yes |
423+
| Developer Edition | 145 | Yes |
424+
| Beta | 145 | Yes |
431425
| Release | 133 | No |
432426

433427
- `dom.security.trusted_types.enabled`

files/en-us/mozilla/firefox/releases/145/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,14 @@ You can find more such features on the [Experimental features](/en-US/docs/Mozil
117117

118118
The CSS `text-decoration-trim` property is supported but currently disabled by default.
119119
It allows you to specify {{cssxref("text-decoration")}} start and end offsets to shorten, lengthen, or shift the position of text decorations with respect to the text ([Firefox bug 1979915](https://bugzil.la/1979915)).
120+
121+
- **Trusted Types API** for scripts (Nightly/Early Beta): `dom.security.trusted_types.enabled`
122+
123+
The [Trusted Types API](/en-US/docs/Web/API/Trusted_Types_API) is now enabled in early beta releases. ([Firefox bug 1976656](https://bugzil.la/1976656)).
124+
125+
The changes include:
126+
127+
- Addition of the {{domxref("TrustedTypePolicyFactory")}}, {{domxref("TrustedTypePolicy")}}, {{domxref("TrustedHTML")}}, {{domxref("TrustedScript")}}, {{domxref("TrustedScriptURL")}} interfaces, and the `trustedTypes` property on {{domxref("Window/trustedTypes", "Window")}} and {{domxref("WorkerGlobalScope/trustedTypes", "WorkerGlobalScope")}}.
128+
- Updates to [injection sink interfaces](/en-US/docs/Web/API/Trusted_Types_API##injection_sink_interfaces) APIs, such as {{domxref("Element.innerHTML")}} and {{domxref("Document.write()", "document.write()")}}, to allow the `TrustedHTML`, `TrustedScript`, `TrustedScriptURL` to be passed as well as strings.
129+
- Support for the [`require-trusted-types-for`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for) and [`trusted-types`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/trusted-types) directives, and the [`'trusted-types-eval'`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#trusted-types-eval) keyword, of the {{HTTPHeader("Content-Security-Policy")}} HTTP header.
130+
These can be used to enforce trusted types instead of strings, name the specific policies that are allowed, and to enable [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) and similar functions to be used when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are supported and enforced.

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The **Trusted Types API** gives web developers a way to ensure that input has be
1212

1313
## Concepts and usage
1414

15-
Client-side, or DOM-based, XSS attacks happen when data crafted by an attacker is passed to a browser API that executes that data as code. These APIs are known as _injection sinks_.
15+
Client-side, or DOM-based, XSS attacks happen when data crafted by an attacker is passed to a browser API that executes that data as code. These APIs are known as [_injection sinks_](#injection_sink_interfaces).
1616

1717
The Trusted Types API distinguishes three sorts of injection sinks:
1818

@@ -134,6 +134,50 @@ element.innerHTML = userInput;
134134
> [!NOTE]
135135
> It's recommended that you use the default policy only while you are transitioning from legacy code that passes input directly to injection sinks, to code that uses trusted types explicitly.
136136
137+
### Injection sink interfaces
138+
139+
This section provides an list of "direct" injection sink interfaces.
140+
141+
Note that there are cases where untrusted strings may be "indirectly injected", such as when an untrusted string is added as the child node of a script element, and then the element is added to the document.
142+
These cases are evaluated the untrusted script is added to the document.
143+
144+
#### TrustedHTML
145+
146+
- {{domxref("Document.execCommand()")}} with a `commandName` of [`"insertHTML"`](/en-US/docs/Web/API/Document/execCommand#inserthtml)
147+
- {{domxref("Document.parseHTMLUnsafe_static()")}}
148+
- {{domxref("Document.write()")}}
149+
- {{domxref("Document.writeln()")}}
150+
- {{domxref("DOMParser.parseFromString()")}}
151+
- {{domxref("Element.innerHTML")}}
152+
- {{domxref("Element.insertAdjacentHTML")}}
153+
- {{domxref("Element.outerHTML")}}
154+
- {{domxref("Element.setHTMLUnsafe()")}}
155+
- {{domxref("HTMLIFrameElement.srcdoc")}}
156+
- {{domxref("Range.createContextualFragment()")}}
157+
- {{domxref("ShadowRoot.innerHTML")}}
158+
- {{domxref("ShadowRoot.setHTMLUnsafe()")}}
159+
160+
#### TrustedScript
161+
162+
- [`eval()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)
163+
- [`Element.setAttribute()`](/en-US/docs/Web/API/Element/setAttribute#value) (`value` argument)
164+
- [`Element.setAttributeNS()`](/en-US/docs/Web/API/Element/setAttributeNS#value) (`value` argument)
165+
- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function)
166+
- {{domxref("HTMLScriptElement.innerText")}}
167+
- {{domxref("HTMLScriptElement.textContent")}}
168+
- {{domxref("HTMLScriptElement.text")}}
169+
- [`window.setTimeout()`](/en-US/docs/Web/API/Window/setTimeout#code) and [`WorkerGlobalScope.setTimeout()`](/en-US/docs/Web/API/WorkerGlobalScope/setTimeout#code) (`code` argument)
170+
- [`window.setInterval()`](/en-US/docs/Web/API/Window/setInterval#code) and [`WorkerGlobalScope.setInterval()`](/en-US/docs/Web/API/WorkerGlobalScope/setInterval#code) (`code` argument)
171+
172+
#### TrustedScriptURL
173+
174+
- {{domxref("HTMLScriptElement.src")}}
175+
- {{domxref("ServiceWorkerContainer.register()")}}
176+
- {{domxref("SvgAnimatedString.baseVal")}}
177+
- {{domxref("WorkerGlobalScope.importScripts()")}}
178+
- `url` argument to [`Worker()` constructor](/en-US/docs/Web/API/Worker/Worker#url)
179+
- `url` argument to [`SharedWorker()` constructor](/en-US/docs/Web/API/SharedWorker/SharedWorker#url)
180+
137181
### Cross-browser support for trusted types
138182

139183
The Trusted Types API is not yet available in all modern browsers, but it is usable everywhere today thanks to [compatibility aids created by the W3C](https://github.com/w3c/trusted-types/tree/main?tab=readme-ov-file#polyfill).
@@ -201,6 +245,28 @@ Either way, the injection sink gets sanitized data, and because we could enforce
201245
- {{domxref("TrustedTypePolicyFactory")}}
202246
- : Creates policies and verifies that Trusted Type object instances were created via one of the policies.
203247

248+
### Extensions to other interfaces
249+
250+
- {{domxref("Window.trustedTypes")}}
251+
- : Returns the {{domxref("TrustedTypePolicyFactory")}} object associated with the global object in the main thread.
252+
This is the entry point for using the API in the Window thread.
253+
- {{domxref("WorkerGlobalScope.trustedTypes")}}.
254+
- : Returns the {{domxref("TrustedTypePolicyFactory")}} object associated with the global object in a worker.
255+
256+
### Extensions to HTTP
257+
258+
#### `Content-Security-Policy` directives
259+
260+
- {{CSP("require-trusted-types-for")}}
261+
- : 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).
262+
- {{CSP("trusted-types")}}
263+
- : Used to specify an allowlist of [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) policy names.
264+
265+
#### `Content-Security-Policy` keywords
266+
267+
- [`trusted-types-eval`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#trusted-types-eval)
268+
- : 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 enforced.
269+
204270
## Examples
205271

206272
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)