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
> The parameters passed to this method represent the URLs of external scripts loaded into a worker.
12
+
> The parameters passed to this method represent the URLs of classic scripts to be imported into a worker.
13
13
> APIs like this are known as [injection sinks](/en-US/docs/Web/API/Trusted_Types_API#concepts_and_usage), and are potentially a vector for [cross-site scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks.
14
14
>
15
15
> You can mitigate this risk by having a [Content Security Policy (CSP)](/en-US/docs/Web/HTTP/Guides/CSP) that restricts the locations from which scripts can be loaded, and by always assigning {{domxref("TrustedScriptURL")}} objects instead of strings and [enforcing trusted types](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types).
@@ -20,14 +20,14 @@ The **`importScripts()`** method of the {{domxref("WorkerGlobalScope")}} interfa
20
20
## Syntax
21
21
22
22
```js-nolint
23
-
importScripts(path0)
24
-
importScripts(path0, path1)
25
-
importScripts(path0, path1, /* …, */ pathN)
23
+
importScripts(url0)
24
+
importScripts(url0, url1)
25
+
importScripts(url0, url1, /* …, */ urlN)
26
26
```
27
27
28
28
### Parameters
29
29
30
-
-`pathN`
30
+
-`urlN`
31
31
- : A {{domxref("TrustedScriptURL")}} instance or a string representing the URL of the script to be imported.
32
32
The URL may be absolute or relative.
33
33
If the URL is relative, it is relative to the worker entry script's URL.
The **`importScripts()`** method synchronously imports one or more scripts into the worker's scope.
50
+
51
+
Unlike the initial classic module script, which must be same-origin with its document, this method can import scripts that are cross-origin unless blocked by a resource {{httpheader("Cross-Origin-Resource-Policy")}} or some other security mechanism.
52
+
49
53
### Security considerations
50
54
51
55
The parameters specify scripts to be imported into the scope of a classic worker.
@@ -60,18 +64,68 @@ This ensures that the input is passed through a transformation function, which h
60
64
61
65
## Examples
62
66
63
-
If you had some functionality written in a separate script called `foo.js` that you wanted to use inside `worker.js`, you could import it using the following line:
67
+
### Basic usage
68
+
69
+
If you had some functionality written in a separate script called `foo.js` in the same directory as `worker.js`, you could import it into the worker using the following line:
64
70
65
71
```js
66
72
importScripts("foo.js");
67
73
```
68
74
69
-
`foo.js` should be in the same URL subtree as the worker's entry point.
70
-
For example, if this worker was created with `new Worker("worker.js")`, then `worker.js` is the entry point.
71
-
If `worker.js` is at `https://example.com/scripts/worker.js`, then `foo.js` should be at `https://example.com/scripts/foo.js`.
72
-
73
75
`importScripts()` and `self.importScripts()` are effectively equivalent — both represent `importScripts()` being called from inside the worker's inner scope.
74
76
77
+
Note that in the next section we show you how to pass a `TrustedScriptURL` instead of a string.
78
+
This was omitted in this example for brevity, but is recommended in production code.
79
+
80
+
### Using TrustedScriptURL
81
+
82
+
To mitigate the risk of XSS, we should always assign `TrustedScriptURL` instances to each of the parameters.
83
+
We also need to do this if we're enforcing trusted types for other reasons and we want to allow some script sources that have been permitted (by `CSP: worker-src`).
84
+
85
+
Trusted types are not yet supported on all browsers, so first we define the [trusted types tinyfill](/en-US/docs/Web/API/Trusted_Types_API#trusted_types_tinyfill).
86
+
This acts as a transparent replacement for the trusted types JavaScript API:
Next we create a {{domxref("TrustedTypePolicy")}} that defines a {{domxref("TrustedTypePolicy/createScriptURL", "createScriptURL()")}} method for transforming input strings into {{domxref("TrustedScriptURL")}} instances.
94
+
95
+
For the purpose of this example we'll assume that we want to allow a predefined set of URLs in the `scriptAllowList` array and log any other scripts.
0 commit comments