Skip to content

Truly independent web browser

LibWeb: Read a Window's import map when resolving module integrity

Shannon Booth8ebf31628d2a
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index d56e7da3b27..f3655e1dffb 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -635,11 +635,14 @@ ScriptFetchOptions get_descendant_script_fetch_options(ScriptFetchOptions const&
Utf16String resolve_a_module_integrity_metadata(URL::URL const& url, EnvironmentSettingsObject& settings_object)
{
// 1. Let map be settingsObject's global object's import map.
- auto map = relevant_window_or_worker_global_scope(settings_object.global_object()).import_map();
+ // NB: Only a Window's import map is ever modified from the initial empty one.
+ auto const* window = window_from_global_object(settings_object.global_object());
+ if (!window)
+ return {};
// 2. If map's integrity[url] does not exist, then return the empty string.
// 3. Return map's integrity[url].
- return map.integrity().get(url).value_or(""_utf16);
+ return window->import_map().integrity().get(url).value_or(""_utf16);
}
// https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 5b1a09d1cbe..71124081773 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -563,6 +563,7 @@ Text/input/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-scroll-to-
Text/input/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-security.sub.html
Text/input/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment.html
Text/input/wpt-import/scroll-to-text-fragment/text-directive-mime-type-parameter.html
+Text/input/wpt-import/worklets/audio-worklet-import.https.html
[Skipped]
; Needs vertical-rl inline geometry fidelity beyond the binary transpose
diff --git a/Tests/LibWeb/Text/expected/wpt-import/worklets/audio-worklet-import.https.txt b/Tests/LibWeb/Text/expected/wpt-import/worklets/audio-worklet-import.https.txt
new file mode 100644
index 00000000000..287d2b2f983
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/worklets/audio-worklet-import.https.txt
@@ -0,0 +1,24 @@
+Harness status: OK
+
+Found 18 tests
+
+13 Pass
+5 Fail
+Pass Importing a script resolves the given promise.
+Pass Importing scripts resolves all the given promises.
+Pass Importing nested scripts resolves the given promise
+Pass Importing cyclic scripts resolves the given promise
+Fail Importing a script which throws should still resolve the given promise.
+Pass Importing a non-existent script rejects the given promise with an AbortError.
+Pass Importing an invalid URL should reject the given promise with a SyntaxError.
+Pass Importing a blob URL should resolve the given promise.
+Pass Importing a file:// URL should reject the given promise.
+Pass Importing a data URL should resolve the given promise.
+Pass Importing about:blank should reject the given promise.
+Fail Importing a cross origin resource with the Access-Control-Allow-Origin header should resolve the given promise
+Pass Importing a cross origin resource without the Access-Control-Allow-Origin header should reject the given promise
+Fail Importing a cross-origin-redirected resource with the Access-Control-Allow-Origin header should resolve the given promise
+Pass Importing a cross-origin-redirected resource without the Access-Control-Allow-Origin header should reject the given promise
+Fail Importing a script that has a syntax error should reject the given promise.
+Fail Importing a nested script that has a syntax error should reject the given promise.
+Pass Importing a script that imports an invalid identifier should reject the given promise.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/audio-worklet-import.https.html b/Tests/LibWeb/Text/input/wpt-import/worklets/audio-worklet-import.https.html
new file mode 100644
index 00000000000..6decf85013d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/audio-worklet-import.https.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <script src="../common/get-host-info.sub.js"></script>
+ <script src="../resources/testharness.js"></script>
+ <script src="../resources/testharnessreport.js"></script>
+ <script src="resources/worklet-test-utils.js"></script>
+ <script src="resources/import-tests.js"></script>
+</head>
+<body>
+<script>
+ runImportTests("audio");
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/empty-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/empty-worklet-script.js
new file mode 100644
index 00000000000..49ceb2648a9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/empty-worklet-script.js
@@ -0,0 +1 @@
+// Do nothing.
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-cyclic-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-cyclic-worklet-script.js
new file mode 100644
index 00000000000..2dc7e0bd3c4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-cyclic-worklet-script.js
@@ -0,0 +1 @@
+import './import-cyclic-worklet-script.js';
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-internal-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-internal-worklet-script.js
new file mode 100644
index 00000000000..5fe8d91100f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-internal-worklet-script.js
@@ -0,0 +1 @@
+import './empty-worklet-script.js';
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-worklet-script.js
new file mode 100644
index 00000000000..b63685f6421
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-nested-worklet-script.js
@@ -0,0 +1 @@
+import './import-nested-internal-worklet-script.js';
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-syntax-error-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-syntax-error-worklet-script.js
new file mode 100644
index 00000000000..1539ad004ed
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-syntax-error-worklet-script.js
@@ -0,0 +1 @@
+import './syntax-error-worklet-script.js';
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-tests.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-tests.js
new file mode 100644
index 00000000000..0c631a8a9f1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/import-tests.js
@@ -0,0 +1,162 @@
+// Runs a series of tests related to importing scripts on a worklet.
+//
+// Usage:
+// runImportTests("paint");
+function runImportTests(worklet_type) {
+ const worklet = get_worklet(worklet_type);
+
+ promise_test(() => {
+ const kScriptURL = 'resources/empty-worklet-script.js';
+ return worklet.addModule(kScriptURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined,
+ 'Promise should resolve with no arguments.');
+ });
+ }, 'Importing a script resolves the given promise.');
+
+ promise_test(() => {
+ const kScriptURL = 'resources/empty-worklet-script.js';
+ return Promise.all([
+ worklet.addModule(kScriptURL + '?1'),
+ worklet.addModule(kScriptURL + '?2'),
+ worklet.addModule(kScriptURL + '?3')
+ ]).then(undefined_args => {
+ assert_array_equals(undefined_args,
+ [undefined, undefined, undefined],
+ 'Promise should resolve with no arguments.');
+ });
+ }, 'Importing scripts resolves all the given promises.');
+
+ promise_test(() => {
+ const kScriptURL = 'resources/import-nested-worklet-script.js';
+ return worklet.addModule(kScriptURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined,
+ 'Promise should resolve with no arguments.');
+ });
+ }, 'Importing nested scripts resolves the given promise');
+
+ promise_test(() => {
+ const kScriptURL = 'resources/import-cyclic-worklet-script.js';
+ return worklet.addModule(kScriptURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined,
+ 'Promise should resolve with no arguments.');
+ });
+ }, 'Importing cyclic scripts resolves the given promise');
+
+ promise_test(() => {
+ const kScriptURL = 'resources/throwing-worklet-script.js';
+ return worklet.addModule(kScriptURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined,
+ 'Promise should resolve with no arguments.');
+ });
+ }, 'Importing a script which throws should still resolve the given ' +
+ 'promise.');
+
+ promise_test(t => {
+ const kScriptURL = 'non-existent-worklet-script.js';
+ return promise_rejects_dom(t, 'AbortError',
+ worklet.addModule(kScriptURL));
+ }, 'Importing a non-existent script rejects the given promise with an ' +
+ 'AbortError.');
+
+ promise_test(t => {
+ const kInvalidScriptURL = 'http://invalid:123$'
+ return promise_rejects_dom(t, 'SyntaxError',
+ worklet.addModule(kInvalidScriptURL));
+ }, 'Importing an invalid URL should reject the given promise with a ' +
+ 'SyntaxError.');
+
+ promise_test(() => {
+ const kBlob = new Blob([""], {type: 'text/javascript'});
+ const kBlobURL = URL.createObjectURL(kBlob);
+ return worklet.addModule(kBlobURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined);
+ });
+ }, 'Importing a blob URL should resolve the given promise.');
+
+ promise_test(t => {
+ const kFileURL = 'file:///empty-worklet-script.js';
+ return promise_rejects_dom(t, 'AbortError',
+ worklet.addModule(kFileURL));
+ }, 'Importing a file:// URL should reject the given promise.');
+
+ promise_test(() => {
+ const kDataURL = 'data:text/javascript, // Do nothing.';
+ return worklet.addModule(kDataURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined);
+ });
+ }, 'Importing a data URL should resolve the given promise.');
+
+ promise_test(t => {
+ const kScriptURL = 'about:blank';
+ return promise_rejects_dom(t, 'AbortError',
+ worklet.addModule(kScriptURL));
+ }, 'Importing about:blank should reject the given promise.');
+
+ promise_test(() => {
+ // Specify the Access-Control-Allow-Origin header to enable cross origin
+ // access.
+ const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
+ '/worklets/resources/empty-worklet-script.js' +
+ '?pipe=header(Access-Control-Allow-Origin, *)';
+ return worklet.addModule(kScriptURL).then(undefined_arg => {
+ assert_equals(undefined_arg, undefined);
+ });
+ }, 'Importing a cross origin resource with the ' +
+ 'Access-Control-Allow-Origin header should resolve the given promise');
+
+ promise_test(t => {
+ // Don't specify the Access-Control-Allow-Origin header. addModule()
+ // should be rejected because of disallowed cross origin access.
+ const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
+ '/worklets/resources/empty-worklet-script.js';
+ return promise_rejects_dom(t, 'AbortError',
+ worklet.addModule(kScriptURL));
+ }, 'Importing a cross origin resource without the ' +
+ 'Access-Control-Allow-Origin header should reject the given promise');
+
+ promise_test(() => {
+ const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
+ '/worklets/resources/empty-worklet-script.js' +
+ '?pipe=header(Access-Control-Allow-Origin, ' +
+ location.origin + ')';
+ return worklet.addModule('/common/redirect.py?location=' +
+ encodeURIComponent(kScriptURL))
+ .then(undefined_arg => {
+ assert_equals(undefined_arg, undefined);
+ });
+ }, 'Importing a cross-origin-redirected resource with the ' +
+ 'Access-Control-Allow-Origin header should resolve the given promise');
+
+ promise_test(t => {
+ const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
+ '/worklets/resources/empty-worklet-script.js';
+ return promise_rejects_dom(t, 'AbortError',
+ worklet.addModule(
+ '/common/redirect.py?location=' +
+ encodeURIComponent(kScriptURL)));
+ }, 'Importing a cross-origin-redirected resource without the ' +
+ 'Access-Control-Allow-Origin header should reject the given promise');
+
+ promise_test(t => {
+ const kScriptURL = 'resources/syntax-error-worklet-script.js';
+ return promise_rejects_js(t, SyntaxError,
+ worklet.addModule(kScriptURL));
+ }, 'Importing a script that has a syntax error should reject the given ' +
+ 'promise.');
+
+ promise_test(t => {
+ const kScriptURL = 'resources/import-syntax-error-worklet-script.js';
+ return promise_rejects_js(t, SyntaxError,
+ worklet.addModule(kScriptURL));
+ }, 'Importing a nested script that has a syntax error should reject the ' +
+ 'given promise.');
+
+ promise_test(t => {
+ const kBlob = new Blob(["import 'invalid-specifier.js';"],
+ {type: 'text/javascript'});
+ const kBlobURL = URL.createObjectURL(kBlob);
+ return promise_rejects_js(t, TypeError,
+ worklet.addModule(kBlobURL));
+ }, 'Importing a script that imports an invalid identifier should reject ' +
+ 'the given promise.');
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/syntax-error-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/syntax-error-worklet-script.js
new file mode 100644
index 00000000000..471697a43bf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/syntax-error-worklet-script.js
@@ -0,0 +1 @@
+;-)
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/throwing-worklet-script.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/throwing-worklet-script.js
new file mode 100644
index 00000000000..6417d6c5f7f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/throwing-worklet-script.js
@@ -0,0 +1 @@
+throw Error();
diff --git a/Tests/LibWeb/Text/input/wpt-import/worklets/resources/worklet-test-utils.js b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/worklet-test-utils.js
new file mode 100644
index 00000000000..797edc9e563
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/worklets/resources/worklet-test-utils.js
@@ -0,0 +1,12 @@
+// Returns a reference to a worklet object corresponding to a given type.
+function get_worklet(type) {
+ if (type == 'animation')
+ return CSS.animationWorklet;
+ if (type == 'layout')
+ return CSS.layoutWorklet;
+ if (type == 'paint')
+ return CSS.paintWorklet;
+ if (type == 'audio')
+ return new OfflineAudioContext(2,44100*40,44100).audioWorklet;
+ return undefined;
+}