Skip to content

Truly independent web browser

LibWeb: Reject CSS modules where CSSStyleSheet is not exposed

Shannon Boothd100fc1c1678
diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
index aef4a04144c..c58906729f8 100644
--- a/Libraries/LibWeb/HTML/Scripting/Environments.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
@@ -10,6 +10,7 @@
#include <LibGC/Heap.h>
#include <LibURL/InternalURLs.h>
#include <LibWeb/Bindings/HostDefined.h>
+#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/DOM/Document.h>
@@ -347,13 +348,15 @@ bool is_scripting_disabled(EnvironmentSettingsObject const& settings)
}
// https://html.spec.whatwg.org/multipage/webappapis.html#module-type-allowed
-bool module_type_allowed(EnvironmentSettingsObject const&, Utf16View module_type)
+bool module_type_allowed(EnvironmentSettingsObject const& settings, Utf16View module_type)
{
// 1. If moduleType is not "javascript-or-wasm", "css", "json", or "text", then return false.
if (module_type != "javascript-or-wasm"sv && module_type != "css"sv && module_type != "json"sv && module_type != "text"sv)
return false;
- // FIXME: 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in settings's realm, then return false.
+ // 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in settings's realm, then return false.
+ if (module_type == "css"sv && !Bindings::is_exposed(Bindings::InterfaceName::CSSStyleSheet, settings.realm()))
+ return false;
// 3. Return true.
return true;
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 1fe44216cda..27275f92e97 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -256,6 +256,7 @@ Text/input/wpt-import/html/semantics/forms/the-input-element/cloning-steps.html
Text/input/wpt-import/html/semantics/forms/the-input-element/show-picker-disabled-readonly.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/import-css-module-dynamic.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/module.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.any.worker-module.html
Text/input/wpt-import/wasm/webapi/esm-integration/wasm-import.tentative.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.txt
new file mode 100644
index 00000000000..1959940d6fe
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass A static import CSS Module within a web worker should not load and should not attempt to fetch the module.
+Pass A dynamic import CSS Module within a web worker should not load and should not attempt to fetch the module.
+Pass An attempt to load a CSS module as a worker should fail.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html
new file mode 100644
index 00000000000..d9663236b35
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html
@@ -0,0 +1,54 @@
+<!doctype html>
+
+<head>
+ <script src="../../../../../resources/testharness.js"></script>
+ <script src="../../../../../resources/testharnessreport.js"></script>
+ <script src="../../../../../common/utils.js"></script>
+</head>
+
+<body>
+ <script>
+ setup({allow_uncaught_exception: true});
+ promise_test(function (test) {
+ const uuid = token();
+ const worker = new Worker(`./resources/worker.sub.js?key=${uuid}`, {
+ type: "module"
+ });
+ return new Promise((resolve, reject) => {
+ worker.addEventListener("error", resolve);
+ worker.addEventListener("message", reject);
+ }).then(async () => {
+ const fetchResponse = await fetch(`./resources/record-fetch.py?key=${uuid}&action=getCount`);
+ const fetchData = await fetchResponse.json();
+ assert_equals(fetchData.count, 0, "Shouldn't have tried fetching CSS module in worker");
+ });
+ }, "A static import CSS Module within a web worker should not load and should not attempt to fetch the module.");
+
+ promise_test(function (test) {
+ const uuid = token();
+ const worker = new Worker(`./resources/worker-dynamic-import.sub.js?key=${uuid}`, {
+ type: "module"
+ });
+
+ return new Promise(resolve => {
+ worker.addEventListener("message", resolve);
+ }).then(async (event) => {
+ assert_equals(event.data, "NOT LOADED");
+ const fetchResponse = await fetch(`./resources/record-fetch.py?key=${uuid}&action=getCount`);
+ const fetchData = await fetchResponse.json();
+ assert_equals(fetchData.count, 0, "Shouldn't have tried fetching CSS module in worker");
+ });
+ }, "A dynamic import CSS Module within a web worker should not load and should not attempt to fetch the module.");
+
+ promise_test(function (test) {
+ const worker = new Worker("./resources/basic.css", {
+ type: "module"
+ });
+ return new Promise(resolve => {
+ worker.onerror = resolve;
+ });
+ }, "An attempt to load a CSS module as a worker should fail.");
+
+ </script>
+
+</body>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/record-fetch.py b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/record-fetch.py
new file mode 100644
index 00000000000..4928cb4acb9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/record-fetch.py
@@ -0,0 +1,20 @@
+def main(request, response):
+ try:
+ stash_key = request.GET.first(b"key")
+ action = request.GET.first(b"action")
+
+ run_count = request.server.stash.take(stash_key)
+ if not run_count:
+ run_count = 0
+
+ if action == b"incCount":
+ request.server.stash.put(stash_key, run_count + 1)
+ response.headers.set(b"Content-Type", b"text/css")
+ response.content = b'#test { background-color: #FF0000; }'
+ elif action == b"getCount":
+ response.headers.set(b"Content-Type", b"text/json")
+ response.content = b'{"count": %d }' % run_count
+ else:
+ response.set_error(400, u"Invalid action")
+ except:
+ response.set_error(400, u"Not enough parameters")
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker-dynamic-import.sub.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker-dynamic-import.sub.js
new file mode 100644
index 00000000000..30dc8cde857
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker-dynamic-import.sub.js
@@ -0,0 +1,3 @@
+import("./record-fetch.py?key={{GET[key]}}&action=incCount", { with: { type: "css" } })
+ .then(() => postMessage("LOADED"))
+ .catch(e => postMessage("NOT LOADED"));
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker.sub.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker.sub.js
new file mode 100644
index 00000000000..dc85c13ed91
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/worker.sub.js
@@ -0,0 +1,2 @@
+import "./record-fetch.py?key={{GET[key]}}&action=incCount" with { type: "css" };
+postMessage("Unexpectedly loaded");
\ No newline at end of file