Skip to content

TrustedTypes: WorkerGlobalScope: importScripts()#42396

Merged
wbamberg merged 7 commits intomdn:mainfrom
hamishwillee:tt_importscripts
Jan 21, 2026
Merged

TrustedTypes: WorkerGlobalScope: importScripts()#42396
wbamberg merged 7 commits intomdn:mainfrom
hamishwillee:tt_importscripts

Conversation

@hamishwillee
Copy link
Collaborator

WorkerGlobalScope.importScripts() takes any number of trusted script URL. parameters. This updates the docs to reflect that the scriptURL can be a trusted type.
(it mirrors #42362)

Still working on it - cursed imports.

Related docs work can be tracked in:

@github-actions github-actions bot added the Content:WebAPI Web API docs label Dec 15, 2025
@github-actions github-actions bot added the size/s [PR only] 6-50 LoC changed label Dec 15, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 15, 2025

Preview URLs

(comment last updated: 2026-01-21 20:22:49)

@github-actions github-actions bot added size/m [PR only] 51-500 LoC changed and removed size/s [PR only] 6-50 LoC changed labels Dec 16, 2025
@hamishwillee hamishwillee marked this pull request as ready for review December 16, 2025 03:48
@hamishwillee hamishwillee requested a review from a team as a code owner December 16, 2025 03:48

{{APIRef("Web Workers API")}}{{AvailableInWorkers("worker")}}

> [!WARNING]
Copy link
Collaborator Author

@hamishwillee hamishwillee Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wbamberg This follows same pattern as the others. The method is used to import (one or more) classic scripts into a classic module.

I have done quite a lot of testing.

  1. FF will silently fail to load a worker or import a script with UTF-16 encoding. The web prefers UTF8 but the fact this fails silently is irritating, because I assumed my files would default to UTF8.
    • Does a BCD note on the worker itself makes sense? Its difficult because UTF8 is strongly recommended so there is no expectation that anyone would set UTF-16.
  2. Both the initial script and subsequent importScript operation are fetched in no-cors mode.
    • This means both same and cross origin requests are not blocked by CORs.
    • Cross origin import requests just work unless blocked by CSP or CORP
  3. The initial script MUST be same-origin with the worker, or a blob or data type.
  4. You can block the load by having a CORP on the cross-origin file to be loaded.
  5. However if you add a CSP for the worker-src it seem to be ignored for the nested importScript (though it obeys it for the top level import in that you can set none.
    • However CSP of script-src is respected.

Essentially it looks like for this method you can import what you like from wherever you like unless blocked by the server resource policy you are importing from.

FYI, my testing is here https://github.com/hamishwillee/various_webapi_tests/tree/main/classic_worker_import - this uses a caddyfile - caddy allows you to set up a server with headers, https and certificate with no real effort..

Copy link
Collaborator Author

@hamishwillee hamishwillee Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT. This is ready for review.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
If the input is provided by a user, this is a possible vector for [cross-site scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks.

It is extremely risky to accept and execute arbitrary URLs from untrusted origins.
A website should control what scripts that are allowed to run using a [Content Security Policy (CSP)](/en-US/docs/Web/HTTP/Guides/CSP) with the [`script-src`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/worker-src) directive (or a fallback defined in [`default-src`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/default-src)).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, wierd as it is, script-src, not worker-src for classic workers.

Co-authored-by: wbamberg <will@bootbonnet.ca>
@hamishwillee hamishwillee requested a review from wbamberg January 9, 2026 01:29
@hamishwillee
Copy link
Collaborator Author

Thanks @wbamberg - I have accepted your changes - much appreciated. Note, this depends on a link created by #42073 - so if you are happy with this, approve it, but we might not get to merge it yet.

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you Hamish!

@wbamberg wbamberg merged commit 25f4f22 into mdn:main Jan 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants