Skip to content

Commit 848dbd0

Browse files
committed
Add example
1 parent 206974b commit 848dbd0

File tree

1 file changed

+64
-10
lines changed
  • files/en-us/web/api/workerglobalscope/importscripts

1 file changed

+64
-10
lines changed

files/en-us/web/api/workerglobalscope/importscripts/index.md

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ browser-compat: api.WorkerGlobalScope.importScripts
99
{{APIRef("Web Workers API")}}{{AvailableInWorkers("worker")}}
1010

1111
> [!WARNING]
12-
> 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.
1313
> 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.
1414
>
1515
> 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
2020
## Syntax
2121

2222
```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)
2626
```
2727

2828
### Parameters
2929

30-
- `pathN`
30+
- `urlN`
3131
- : A {{domxref("TrustedScriptURL")}} instance or a string representing the URL of the script to be imported.
3232
The URL may be absolute or relative.
3333
If the URL is relative, it is relative to the worker entry script's URL.
@@ -46,6 +46,10 @@ None ({{jsxref("undefined")}}).
4646

4747
## Description
4848

49+
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+
4953
### Security considerations
5054

5155
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
6064

6165
## Examples
6266

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:
6470

6571
```js
6672
importScripts("foo.js");
6773
```
6874

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-
7375
`importScripts()` and `self.importScripts()` are effectively equivalent — both represent `importScripts()` being called from inside the worker's inner scope.
7476

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:
87+
88+
```js
89+
if (typeof trustedTypes === "undefined")
90+
trustedTypes = { createPolicy: (n, rules) => rules };
91+
```
92+
93+
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.
96+
97+
```js
98+
const scriptAllowList = [
99+
// Some list of allowed URLs
100+
];
101+
const policy = trustedTypes.createPolicy("script-url-policy", {
102+
createScriptURL(input) {
103+
if (scriptAllowList.includes(input)) {
104+
return input; // allow the script
105+
}
106+
console.log(`Script not in scriptAllowList: ${input}`);
107+
return ""; // Block the script
108+
},
109+
});
110+
```
111+
112+
Then we use the `policy` object to create a `trustedScript` object from a potentially unsafe input string:
113+
114+
```js
115+
// The potentially malicious string
116+
// We won't be including untrustedScript in our scriptAllowList array
117+
const untrustedScript = "https://evil.example.com/import_worker.js";
118+
119+
// Create a TrustedScriptURL instance using the policy
120+
const trustedScriptURL = policy.createScriptURL(untrustedScript);
121+
```
122+
123+
The `trustedScriptURL` property can now be used when importing the script in a classic worker:
124+
125+
```js
126+
importScripts(trustedScriptURL);
127+
```
128+
75129
## Specifications
76130

77131
{{Specifications}}

0 commit comments

Comments
 (0)