Skip to content

Truly independent web browser

LibWeb: Remove SharedArrayBuffer from realms that are not isolated

Shannon Booth36b1888b9970
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
index 312862d6a33..207fab9648e 100644
--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp
+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
@@ -730,7 +730,8 @@ JS::VM& main_thread_vm()
}
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-new-javascript-realm
-NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value)
+// NB: agent is given as its agent cluster's cross-origin isolation mode, which is all these steps read of it.
+NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, HTML::CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value)
{
// 1. Perform InitializeHostDefinedRealm() with the provided customizations for creating the global object and the global this binding.
// 2. Let realm execution context be the running JavaScript execution context.
@@ -740,15 +741,22 @@ NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Fu
auto* popped_execution_context = vm.pop_execution_context();
VERIFY(popped_execution_context == realm_execution_context.ptr());
- // NO-OP: 4. Let realm be realm execution context's Realm component.
- // NO-OP: 5. Set realm's agent to agent.
+ // 4. Let realm be realm execution context's Realm component.
+ auto& realm = *realm_execution_context->realm;
- // FIXME: 6. If agent's agent cluster's cross-origin isolation mode is "none", then:
- // 1. Let global be realm's global object.
- // 2. Let status be ! global.[[Delete]]("SharedArrayBuffer").
- // 3. Assert: status is true.
+ // 5. If agent's agent cluster's cross-origin isolation mode is "none":
+ if (agent_cluster_cross_origin_isolation_mode == HTML::CrossOriginIsolationMode::None) {
+ // 1. Let global be realm's global object.
+ auto& global = realm.global_object();
- // 7. Return realm execution context.
+ // 2. Let status be ! global.[[Delete]]("SharedArrayBuffer").
+ auto status = MUST(global.internal_delete(vm.names.SharedArrayBuffer));
+
+ // 3. Assert: status is true.
+ VERIFY(status);
+ }
+
+ // 6. Return realm execution context.
return realm_execution_context;
}
@@ -780,6 +788,7 @@ GC::Ref<JS::Realm> create_a_simple_javascript_realm()
GC::Ptr<JS::HostObject> global_this;
auto execution_context = create_a_new_javascript_realm(
vm,
+ HTML::CrossOriginIsolationMode::None,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
window = HTML::Window::create();
global_this = create_global_object_wrapper(realm, GC::Ref { *window });
@@ -869,6 +878,7 @@ GC::Ref<JS::Realm> create_a_principal_javascript_realm()
GC::Ptr<JS::HostObject> global_this;
auto execution_context = create_a_new_javascript_realm(
main_thread_vm(),
+ HTML::CrossOriginIsolationMode::None,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
window = HTML::Window::create();
global_this = create_global_object_wrapper(realm, GC::Ref { *window });
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.h b/Libraries/LibWeb/Bindings/MainThreadVM.h
index 92cd48f2f58..ae096b9e027 100644
--- a/Libraries/LibWeb/Bindings/MainThreadVM.h
+++ b/Libraries/LibWeb/Bindings/MainThreadVM.h
@@ -15,6 +15,7 @@
#include <LibWeb/Export.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Scripting/Agent.h>
+#include <LibWebCommon/HTML/CrossOriginIsolationMode.h>
#include <LibWebCommon/HTML/WorkerTypes.h>
namespace Web::Bindings {
@@ -44,7 +45,7 @@ HTML::Script* active_script();
WEB_API void initialize_main_thread_vm(HTML::AgentType);
WEB_API JS::VM& main_thread_vm();
-WEB_API NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value);
+WEB_API NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, HTML::CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value);
// Creates a bare Window-backed realm for tests and tools that do not need a full Document.
WEB_API GC::Ref<JS::Realm> create_a_simple_javascript_realm();
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index e160c8f5ea6..c7174334ba8 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -398,8 +398,8 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type type
auto agent_cluster_id = navigation_params.agent_cluster_id;
auto agent_cluster_cross_origin_isolation_mode = navigation_params.agent_cluster_cross_origin_isolation_mode;
- // 5. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
- auto realm_execution_context = HTML::create_window_realm(window, *browsing_context);
+ // 5. Let realmExecutionContext be the result of creating a new realm given agent and the following customizations:
+ auto realm_execution_context = HTML::create_window_realm(window, *browsing_context, agent_cluster_cross_origin_isolation_mode);
// 6. Set window to the global object of realmExecutionContext's Realm component.
window = HTML::window_from_global_object(realm_execution_context->realm->global_object());
diff --git a/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Libraries/LibWeb/HTML/BrowsingContext.cpp
index ef84dff20ef..f78cf86e3de 100644
--- a/Libraries/LibWeb/HTML/BrowsingContext.cpp
+++ b/Libraries/LibWeb/HTML/BrowsingContext.cpp
@@ -118,9 +118,10 @@ BrowsingContext::BrowsingContextAndDocument BrowsingContext::create_a_new_browsi
GC::Ptr<Window> window;
- // 10. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
+ // 10. Let realm execution context be the result of creating a new realm given agent and the following customizations:
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
Bindings::main_thread_vm(),
+ agent_cluster_cross_origin_isolation_mode,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
// NB: A container whose content navigable's document comes back from another process keeps the WindowProxy
// scripts hold for it.
diff --git a/Libraries/LibWeb/HTML/Scripting/WindowRealm.cpp b/Libraries/LibWeb/HTML/Scripting/WindowRealm.cpp
index b33c56f7384..5ae774d5a2b 100644
--- a/Libraries/LibWeb/HTML/Scripting/WindowRealm.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WindowRealm.cpp
@@ -13,10 +13,11 @@
namespace Web::HTML {
-NonnullOwnPtr<JS::ExecutionContext> create_window_realm(GC::Ptr<Window>& window, BrowsingContext& browsing_context)
+NonnullOwnPtr<JS::ExecutionContext> create_window_realm(GC::Ptr<Window>& window, BrowsingContext& browsing_context, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode)
{
return Bindings::create_a_new_javascript_realm(
Bindings::main_thread_vm(),
+ agent_cluster_cross_origin_isolation_mode,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
// For the global object, create a new Window object.
window = Window::create();
diff --git a/Libraries/LibWeb/HTML/Scripting/WindowRealm.h b/Libraries/LibWeb/HTML/Scripting/WindowRealm.h
index bb84a5c1c45..1b52613e881 100644
--- a/Libraries/LibWeb/HTML/Scripting/WindowRealm.h
+++ b/Libraries/LibWeb/HTML/Scripting/WindowRealm.h
@@ -10,12 +10,13 @@
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibWeb/Export.h>
+#include <LibWebCommon/HTML/CrossOriginIsolationMode.h>
namespace Web::HTML {
class BrowsingContext;
class Window;
-WEB_API NonnullOwnPtr<JS::ExecutionContext> create_window_realm(GC::Ptr<Window>&, BrowsingContext&);
+WEB_API NonnullOwnPtr<JS::ExecutionContext> create_window_realm(GC::Ptr<Window>&, BrowsingContext&, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode);
}
diff --git a/Libraries/LibWeb/WebAudio/AudioWorklet.cpp b/Libraries/LibWeb/WebAudio/AudioWorklet.cpp
index 79f8ed1c14a..548c42b3b8f 100644
--- a/Libraries/LibWeb/WebAudio/AudioWorklet.cpp
+++ b/Libraries/LibWeb/WebAudio/AudioWorklet.cpp
@@ -58,6 +58,7 @@ GC::Ref<HTML::WorkletGlobalScope> AudioWorklet::create_global_scope()
GC::Ptr<AudioWorkletGlobalScope> scope;
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
Bindings::main_thread_vm(),
+ outside_settings.agent_cluster_cross_origin_isolation_mode(),
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
scope = AudioWorkletGlobalScope::create(*this, m_context->sample_rate());
return Bindings::create_global_object_wrapper(realm, GC::Ref { *scope });
diff --git a/Services/WebWorker/WorkerHost.cpp b/Services/WebWorker/WorkerHost.cpp
index c23d0bc8f4d..584ccb4a9ba 100644
--- a/Services/WebWorker/WorkerHost.cpp
+++ b/Services/WebWorker/WorkerHost.cpp
@@ -67,8 +67,12 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes
auto unsafe_worker_creation_time = Web::HighResolutionTime::unsafe_shared_current_time();
// 5. Let realm execution context be the result of creating a new realm given agent and the following customizations:
+ // NB: A dedicated worker's agent is in its owner's agent cluster. A shared worker's is in a new one, whose
+ // cross-origin isolation mode is "none" until the worker's response sets it.
+ auto agent_cluster_cross_origin_isolation_mode = is_shared ? Web::HTML::CrossOriginIsolationMode::None : outside_settings_snapshot.agent_cluster_cross_origin_isolation_mode;
auto realm_execution_context = Web::Bindings::create_a_new_javascript_realm(
Web::Bindings::main_thread_vm(),
+ agent_cluster_cross_origin_isolation_mode,
[page, is_shared](JS::Realm& realm) -> GC::Ref<JS::Object> {
// For the global object, if is shared is true, create a new SharedWorkerGlobalScope object.
if (is_shared) {
diff --git a/Tests/LibWeb/TestWindowProxyWorld.cpp b/Tests/LibWeb/TestWindowProxyWorld.cpp
index 1313e1aef1e..662ce06219d 100644
--- a/Tests/LibWeb/TestWindowProxyWorld.cpp
+++ b/Tests/LibWeb/TestWindowProxyWorld.cpp
@@ -108,6 +108,7 @@ TEST_CASE(per_world_windowproxy_and_window_wrapper)
auto extension_world = vm.heap().allocate<Web::Bindings::WrapperWorld>(Web::Bindings::WrapperWorld::Type::Extension);
auto extension_execution_context = Web::Bindings::create_a_new_javascript_realm(
vm,
+ Web::HTML::CrossOriginIsolationMode::None,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
return Web::Bindings::create_global_object_wrapper(realm, GC::Ref { *window });
},
diff --git a/Tests/LibWeb/TestWorldCallback.cpp b/Tests/LibWeb/TestWorldCallback.cpp
index b6ced6fe113..6ed88c8a052 100644
--- a/Tests/LibWeb/TestWorldCallback.cpp
+++ b/Tests/LibWeb/TestWorldCallback.cpp
@@ -31,7 +31,7 @@ TEST_CASE(extension_world_callback_uses_principal_settings_object)
{
auto principal_realm = Web::Bindings::create_a_principal_javascript_realm();
auto& vm = Web::Bindings::main_thread_vm();
- auto extension_execution_context = Web::Bindings::create_a_new_javascript_realm(vm, nullptr, nullptr);
+ auto extension_execution_context = Web::Bindings::create_a_new_javascript_realm(vm, Web::HTML::CrossOriginIsolationMode::None, nullptr, nullptr);
auto& extension_realm = *extension_execution_context->realm;
install_test_host_defined(extension_realm, Web::Bindings::WrapperWorld::Type::Extension, *principal_realm);
vm.push_execution_context(*extension_execution_context);
@@ -54,7 +54,7 @@ TEST_CASE(extension_world_event_listener_wraps_arguments_in_listener_realm)
{
auto principal_realm = Web::Bindings::create_a_principal_javascript_realm();
auto& vm = Web::Bindings::main_thread_vm();
- auto extension_execution_context = Web::Bindings::create_a_new_javascript_realm(vm, nullptr, nullptr);
+ auto extension_execution_context = Web::Bindings::create_a_new_javascript_realm(vm, Web::HTML::CrossOriginIsolationMode::None, nullptr, nullptr);
auto& extension_realm = *extension_execution_context->realm;
install_test_host_defined(extension_realm, Web::Bindings::WrapperWorld::Type::Extension, *principal_realm);
vm.push_execution_context(*extension_execution_context);
diff --git a/Tests/LibWeb/TestWrapperWorld.cpp b/Tests/LibWeb/TestWrapperWorld.cpp
index 0498a1c6f74..05293bef74c 100644
--- a/Tests/LibWeb/TestWrapperWorld.cpp
+++ b/Tests/LibWeb/TestWrapperWorld.cpp
@@ -268,6 +268,7 @@ GC::Ref<JS::Realm> create_test_principal_realm(JS::VM& vm)
GC::Ptr<JS::HostObject> global_this;
auto execution_context = Web::Bindings::create_a_new_javascript_realm(
vm,
+ Web::HTML::CrossOriginIsolationMode::None,
[&](JS::Realm& realm) -> GC::Ref<JS::Object> {
window = Web::HTML::Window::create();
global_this = Web::Bindings::create_global_object_wrapper(realm, GC::Ref { *window });
diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt
index c03cec46353..39d4d47f6bf 100644
--- a/Tests/LibWeb/Text/expected/all-window-properties.txt
+++ b/Tests/LibWeb/Text/expected/all-window-properties.txt
@@ -493,7 +493,6 @@ ServiceWorkerContainer
ServiceWorkerRegistration
Set
ShadowRoot
-SharedArrayBuffer
SharedWorker
SourceBuffer
SourceBufferList
diff --git a/Tests/LibWeb/Text/expected/cross-origin-isolated-without-coop-coep.txt b/Tests/LibWeb/Text/expected/cross-origin-isolated-without-coop-coep.txt
index 5c378be3086..8bc2345e7ef 100644
--- a/Tests/LibWeb/Text/expected/cross-origin-isolated-without-coop-coep.txt
+++ b/Tests/LibWeb/Text/expected/cross-origin-isolated-without-coop-coep.txt
@@ -1,2 +1,3 @@
crossOriginIsolated: false
+typeof SharedArrayBuffer: undefined
SharedArrayBuffer serialization threw: DataCloneError
diff --git a/Tests/LibWeb/Text/input/Streams/enqueue-shared-array-buffer-chunk.html.headers b/Tests/LibWeb/Text/input/Streams/enqueue-shared-array-buffer-chunk.html.headers
new file mode 100644
index 00000000000..63b60e490f4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Streams/enqueue-shared-array-buffer-chunk.html.headers
@@ -0,0 +1,2 @@
+Cross-Origin-Opener-Policy: same-origin
+Cross-Origin-Embedder-Policy: require-corp
diff --git a/Tests/LibWeb/Text/input/canvas/image-data-in-rust.html b/Tests/LibWeb/Text/input/canvas/image-data-in-rust.html
index ea966bf879e..d3ea629140d 100644
--- a/Tests/LibWeb/Text/input/canvas/image-data-in-rust.html
+++ b/Tests/LibWeb/Text/input/canvas/image-data-in-rust.html
@@ -62,7 +62,10 @@
detached.buffer.transfer();
println(`from detached data: ${outcome(() => new ImageData(detached, 1))}`);
println(`from a Uint8Array: ${outcome(() => new ImageData(new Uint8Array(4), 1))}`);
- println(`from shared data: ${outcome(() => new ImageData(new Uint8ClampedArray(new SharedArrayBuffer(4)), 1))}`);
+ // This document is not cross-origin isolated, so it has no SharedArrayBuffer global, but a shared WebAssembly
+ // memory still gives it a SharedArrayBuffer.
+ const sharedBuffer = new WebAssembly.Memory({ initial: 1, maximum: 1, shared: true }).buffer;
+ println(`from shared data: ${outcome(() => new ImageData(new Uint8ClampedArray(sharedBuffer, 0, 4), 1))}`);
// An ImageData wider than a bitmap can be.
const wide = new ImageData(70000, 1);
diff --git a/Tests/LibWeb/Text/input/cross-origin-isolated-without-coop-coep.html b/Tests/LibWeb/Text/input/cross-origin-isolated-without-coop-coep.html
index d1193a155c6..12665a00d7e 100644
--- a/Tests/LibWeb/Text/input/cross-origin-isolated-without-coop-coep.html
+++ b/Tests/LibWeb/Text/input/cross-origin-isolated-without-coop-coep.html
@@ -2,12 +2,14 @@
<script src="include.js"></script>
<script>
test(() => {
- // Without COOP + COEP a document is not cross-origin isolated.
+ // Without COOP + COEP a document is not cross-origin isolated, and its global has no SharedArrayBuffer.
println("crossOriginIsolated: " + self.crossOriginIsolated);
+ println("typeof SharedArrayBuffer: " + typeof SharedArrayBuffer);
- // Serializing a SharedArrayBuffer throws when not cross-origin isolated. Construct it outside the try — so a
- // constructor failure can't masquerade as the serialization failure we're checking for.
- const sab = new SharedArrayBuffer(8);
+ // Serializing a SharedArrayBuffer throws when not cross-origin isolated. A shared WebAssembly memory still
+ // creates one. Construct it outside the try — so a constructor failure can't masquerade as the serialization
+ // failure we're checking for.
+ const sab = new WebAssembly.Memory({ initial: 1, maximum: 1, shared: true }).buffer;
let errorName = "(did not throw)";
try {
new MessageChannel().port1.postMessage(sab);
diff --git a/Tests/LibWeb/Text/input/shared-array-buffer-basic.html.headers b/Tests/LibWeb/Text/input/shared-array-buffer-basic.html.headers
new file mode 100644
index 00000000000..63b60e490f4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/shared-array-buffer-basic.html.headers
@@ -0,0 +1,2 @@
+Cross-Origin-Opener-Policy: same-origin
+Cross-Origin-Embedder-Policy: require-corp