Skip to content

Truly independent web browser

LibWeb+WebWorker: Apply the embedder policy when a worker starts

Shannon Boothe7cd6ae35513
diff --git a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
index b8ca70a4d61..81299200405 100644
--- a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
@@ -16,6 +16,7 @@ EnvironmentSettingsSnapshot::EnvironmentSettingsSnapshot(NonnullOwnPtr<JS::Execu
, m_origin(serialized_settings.origin)
, m_has_cross_site_ancestor(serialized_settings.has_cross_site_ancestor)
, m_policy_container(create_a_policy_container_from_serialized_policy_container(serialized_settings.policy_container))
+ , m_cross_origin_isolated_capability(serialized_settings.cross_origin_isolated_capability)
, m_agent_cluster_id(serialized_settings.agent_cluster_id)
, m_agent_cluster_cross_origin_isolation_mode(serialized_settings.agent_cluster_cross_origin_isolation_mode)
, m_time_origin(serialized_settings.time_origin)
diff --git a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
index 8cc407c61de..0dbe0afd11f 100644
--- a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
+++ b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
@@ -28,7 +28,7 @@ public:
virtual URL::Origin origin() const override { return m_origin; }
virtual bool has_cross_site_ancestor() const override { return m_has_cross_site_ancestor; }
virtual GC::Ref<PolicyContainer> policy_container() const override { return m_policy_container; }
- virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override { return CanUseCrossOriginIsolatedAPIs::No; }
+ virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override { return m_cross_origin_isolated_capability; }
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
virtual double time_origin() const override { return m_time_origin; }
@@ -45,6 +45,7 @@ private:
URL::Origin m_origin;
bool m_has_cross_site_ancestor;
GC::Ref<PolicyContainer> m_policy_container;
+ CanUseCrossOriginIsolatedAPIs m_cross_origin_isolated_capability { CanUseCrossOriginIsolatedAPIs::No };
Optional<u64> m_agent_cluster_id;
CrossOriginIsolationMode m_agent_cluster_cross_origin_isolation_mode { CrossOriginIsolationMode::None };
double m_time_origin { 0 };
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
index 2463e25d445..9c899ceb4a0 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
@@ -30,12 +30,6 @@ GC::Ref<WorkerEnvironmentSettingsObject> WorkerEnvironmentSettingsObject::setup(
auto* worker = Bindings::worker_global_scope_from_global_object(realm->global_object());
VERIFY(worker);
- // AD-HOC: The worker global scope's cross-origin isolated capability is inherited from the outside/creating
- // settings object. In our multi-process model, it arrives via the serialized outside settings; the spec
- // sets it while running the worker (before this algorithm), from the worker agent's agent cluster's
- // cross-origin isolation mode.
- worker->set_cross_origin_isolated_capability(outside_settings.cross_origin_isolated_capability == CanUseCrossOriginIsolatedAPIs::Yes);
-
// 3. Let origin be a unique opaque origin if worker global scope's url's scheme is "data"; otherwise outside settings's origin.
// NB: The browser process determines it, as it holds the worker's environment too.
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
index a3ba3071af7..754257092c1 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
+++ b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
@@ -41,6 +41,7 @@ public:
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override;
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
+ void set_agent_cluster_cross_origin_isolation_mode(CrossOriginIsolationMode mode) { m_agent_cluster_cross_origin_isolation_mode = mode; }
virtual double time_origin() const override;
private:
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
index 5b983398c0c..8bfae2b8ffa 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
@@ -12,6 +12,7 @@
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
+#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/MessageEvent.h>
@@ -215,6 +216,38 @@ void WorkerGlobalScope::initialize_policy_container(GC::Ref<Fetch::Infrastructur
m_policy_container = create_a_policy_container_from_a_fetch_response(response, environment);
}
+// https://html.spec.whatwg.org/multipage/browsers.html#check-a-global-object's-embedder-policy
+// NB: response is only used for violation reports, which are not queued.
+bool WorkerGlobalScope::check_embedder_policy(EnvironmentSettingsObject const& owner) const
+{
+ // 1. If workerGlobalScope is not a DedicatedWorkerGlobalScope object, then return true.
+ if (!is<DedicatedWorkerGlobalScope>(*this))
+ return true;
+
+ // 2. Let policy be workerGlobalScope's embedder policy.
+ // AD-HOC: Nothing sets workerGlobalScope's embedder policy. This reads its policy container's embedder policy
+ // instead. See https://github.com/whatwg/html/issues/11316.
+ auto const& policy = policy_container()->embedder_policy;
+
+ // 3. Let ownerPolicy be owner's policy container's embedder policy.
+ auto const& owner_policy = owner.policy_container()->embedder_policy;
+
+ // FIXME: 4. If ownerPolicy's report-only value is compatible with cross-origin isolation and policy's value is
+ // not, then queue a cross-origin embedder policy inheritance violation with response, "worker
+ // initialization", ownerPolicy's report-only reporting endpoint, "reporting", and owner.
+
+ // 5. If ownerPolicy's value is not compatible with cross-origin isolation or policy's value is compatible with
+ // cross-origin isolation, then return true.
+ if (!is_compatible_with_cross_origin_isolation(owner_policy.value) || is_compatible_with_cross_origin_isolation(policy.value))
+ return true;
+
+ // FIXME: 6. Queue a cross-origin embedder policy inheritance violation with response, "worker initialization",
+ // ownerPolicy's reporting endpoint, "enforce", and owner.
+
+ // 7. Return false.
+ return false;
+}
+
// https://w3c.github.io/webappsec-csp/#run-global-object-csp-initialization
ContentSecurityPolicy::Directives::Directive::Result WorkerGlobalScope::run_csp_initialization() const
{
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.h b/Libraries/LibWeb/HTML/WorkerGlobalScope.h
index 73a6d6a00c5..0cacd27ad39 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.h
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.h
@@ -136,6 +136,7 @@ public:
bool is_closing() const { return m_closing; }
void initialize_policy_container(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<EnvironmentSettingsObject> environment);
+ [[nodiscard]] bool check_embedder_policy(EnvironmentSettingsObject const& owner) const;
[[nodiscard]] ContentSecurityPolicy::Directives::Directive::Result run_csp_initialization() const;
auto& owner_set() { return m_owner_set; }
diff --git a/Services/WebWorker/WorkerHost.cpp b/Services/WebWorker/WorkerHost.cpp
index 584ccb4a9ba..6b5d575fc3b 100644
--- a/Services/WebWorker/WorkerHost.cpp
+++ b/Services/WebWorker/WorkerHost.cpp
@@ -18,6 +18,7 @@
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/MessagePort.h>
+#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -153,7 +154,7 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes
: Web::Fetch::Infrastructure::Request::Destination::Worker;
// In both cases, let performFetch be the following perform the fetch hook given request, isTopLevel, and processCustomFetchResponse:
- auto perform_fetch_function = [inside_settings, worker_global_scope, is_shared](GC::Ref<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {
+ auto perform_fetch_function = [inside_settings, outside_settings, worker_global_scope, is_shared](GC::Ref<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {
auto& realm = inside_settings->realm();
Web::Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
@@ -175,11 +176,11 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes
// 3. Fetch request with processResponseConsumeBody set to the following steps given response response and
// null, failure, or a byte sequence bodyBytes:
- fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings, is_shared](auto response, auto body_bytes) {
- // 1. Set worker global scope's url to response's url.
+ fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings, outside_settings, is_shared](auto response, auto body_bytes) {
+ // 1. Set worker global scope's url to response's URL.
worker_global_scope->set_url(response->url().value_or({}));
- // 2. Set inside settings's creation URL to response's url.
+ // 2. Set inside settings's creation URL to response's URL.
inside_settings->creation_url = worker_global_scope->url();
// 3. Initialize worker global scope's policy container given worker global scope, response, and inside
@@ -192,20 +193,35 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes
response = Web::Fetch::Infrastructure::Response::network_error("Blocked by Content Security Policy"_string);
}
- // FIXME: Use worker global scope's policy container's embedder policy
- // FIXME: 5. If worker global scope's embedder policy's value is compatible with cross-origin isolation and is shared is true,
- // then set agent's agent cluster's cross-origin isolation mode to "logical" or "concrete".
- // The one chosen is implementation-defined.
- // FIXME: 6. If the result of checking a global object's embedder policy with worker global scope, outside settings,
+ // 5. If worker global scope's embedder policy's value is compatible with cross-origin isolation and is
+ // shared is true, then set agent's agent cluster's cross-origin isolation mode to "logical" or
+ // "concrete". The one chosen is implementation-defined.
+ // AD-HOC: Nothing sets worker global scope's embedder policy. This reads its policy container's embedder
+ // policy instead. See https://github.com/whatwg/html/issues/11316.
+ // NB: This chooses "concrete", as the UI process does for a browsing context group.
+ if (Web::HTML::is_compatible_with_cross_origin_isolation(worker_global_scope->policy_container()->embedder_policy.value) && is_shared)
+ inside_settings->set_agent_cluster_cross_origin_isolation_mode(Web::HTML::CrossOriginIsolationMode::Concrete);
+
+ // 6. If the result of checking a global object's embedder policy with worker global scope, outside settings,
// and response is false, then set response to a network error.
- // FIXME: 7. Set worker global scope's cross-origin isolated capability to true if agent's agent cluster's cross-origin
- // isolation mode is "concrete".
+ if (!worker_global_scope->check_embedder_policy(*outside_settings))
+ response = Web::Fetch::Infrastructure::Response::network_error("Blocked by Cross-Origin-Embedder-Policy"_string);
+
+ // 7. Set worker global scope's cross-origin isolated capability to true if agent's agent cluster's
+ // cross-origin isolation mode is "concrete".
+ worker_global_scope->set_cross_origin_isolated_capability(inside_settings->agent_cluster_cross_origin_isolation_mode() == Web::HTML::CrossOriginIsolationMode::Concrete);
if (!is_shared) {
- // FIXME: 8. If is shared is false and owner's cross-origin isolated capability is false, then set worker
- // global scope's cross-origin isolated capability to false.
- // FIXME: 9. If is shared is false and response's url's scheme is "data", then set worker global scope's
- // cross-origin isolated capability to false.
+ // 8. If is shared is false and owner's cross-origin isolated capability is false, then set worker
+ // global scope's cross-origin isolated capability to false.
+ // NB: owner's relevant settings object is outside settings.
+ if (outside_settings->cross_origin_isolated_capability() == Web::HTML::CanUseCrossOriginIsolatedAPIs::No)
+ worker_global_scope->set_cross_origin_isolated_capability(false);
+
+ // 9. If is shared is false and response's URL's scheme is "data", then set worker global scope's
+ // cross-origin isolated capability to false.
+ if (auto const& response_url = response->url(); response_url.has_value() && response_url->scheme() == "data"sv)
+ worker_global_scope->set_cross_origin_isolated_capability(false);
}
// 10. Run processCustomFetchResponse with response and bodyBytes.
diff --git a/Tests/LibWeb/Text/expected/worker-cross-origin-embedder-policy.txt b/Tests/LibWeb/Text/expected/worker-cross-origin-embedder-policy.txt
new file mode 100644
index 00000000000..dbe93a86aec
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/worker-cross-origin-embedder-policy.txt
@@ -0,0 +1,6 @@
+dedicated worker without COEP: blocked
+dedicated worker with COEP: loaded, crossOriginIsolated: true
+blob: dedicated worker: loaded, crossOriginIsolated: true
+data: dedicated worker: loaded, crossOriginIsolated: false
+shared worker without COEP: crossOriginIsolated: false
+shared worker with COEP: crossOriginIsolated: true
diff --git a/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html b/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html
new file mode 100644
index 00000000000..fba66d7d72a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const reportIsolation = "postMessage(self.crossOriginIsolated);";
+
+ const dedicatedWorker = (name, url) => new Promise(resolve => {
+ const worker = new Worker(url);
+ worker.onmessage = e => {
+ println(`${name}: loaded, crossOriginIsolated: ${e.data}`);
+ worker.terminate();
+ resolve();
+ };
+ worker.onerror = () => {
+ println(`${name}: blocked`);
+ resolve();
+ };
+ });
+
+ const sharedWorker = (name, url) => new Promise(resolve => {
+ const worker = new SharedWorker(url);
+ worker.port.onmessage = e => {
+ println(`${name}: crossOriginIsolated: ${e.data}`);
+ resolve();
+ };
+ });
+
+ const script = (path, headers, body) => server.createEcho("GET", path, {
+ status: 200,
+ headers: { "Content-Type": "text/javascript", ...headers },
+ body,
+ });
+
+ const coep = { "Cross-Origin-Embedder-Policy": "require-corp" };
+ await dedicatedWorker("dedicated worker without COEP", await script("/without-coep.js", {}, reportIsolation));
+ await dedicatedWorker("dedicated worker with COEP", await script("/with-coep.js", coep, reportIsolation));
+ await dedicatedWorker("blob: dedicated worker", URL.createObjectURL(new Blob([reportIsolation], { type: "text/javascript" })));
+ await dedicatedWorker("data: dedicated worker", `data:text/javascript,${reportIsolation}`);
+
+ const reportIsolationOnConnect = "onconnect = e => e.ports[0].postMessage(self.crossOriginIsolated);";
+ await sharedWorker("shared worker without COEP", await script("/shared-without-coep.js", {}, reportIsolationOnConnect));
+ await sharedWorker("shared worker with COEP", await script("/shared-with-coep.js", coep, reportIsolationOnConnect));
+
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html.headers b/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html.headers
new file mode 100644
index 00000000000..63b60e490f4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/worker-cross-origin-embedder-policy.html.headers
@@ -0,0 +1,2 @@
+Cross-Origin-Opener-Policy: same-origin
+Cross-Origin-Embedder-Policy: require-corp