Skip to content

Truly independent web browser

LibWeb: Do not enter a cross-origin realm to call a callback object

Tim Ledbettera32a59bd01f8
diff --git a/Libraries/LibWeb/DOM/EventTarget.cpp b/Libraries/LibWeb/DOM/EventTarget.cpp
index b698e07efdb..1965aac12c1 100644
--- a/Libraries/LibWeb/DOM/EventTarget.cpp
+++ b/Libraries/LibWeb/DOM/EventTarget.cpp
@@ -140,12 +140,12 @@ JS::Realm* callback_realm(WebIDL::CallbackType* callback)
{
if (!callback)
return nullptr;
- return &callback->callback->shape().realm();
+ return &callback->associated_realm();
}
HTML::Window* window_from_callback(WebIDL::CallbackType& callback)
{
- auto& realm = callback.callback->shape().realm();
+ auto& realm = callback.associated_realm();
return HTML::window_from_global_object(realm.global_object());
}
@@ -158,7 +158,7 @@ DOM::EventTarget* current_target_global(WebIDL::CallbackType& callback, DOM::Eve
return window;
// Any other target's relevant realm is its wrapper's: the realm it was created in.
- auto& callback_realm = callback.callback->shape().realm();
+ auto& callback_realm = callback.associated_realm();
auto wrapper = current_target_wrapper(callback_realm, event);
if (!wrapper)
return nullptr;
@@ -167,13 +167,13 @@ DOM::EventTarget* current_target_global(WebIDL::CallbackType& callback, DOM::Eve
void report_exception_for_callback(WebIDL::CallbackType& callback, JS::Value exception)
{
- auto& realm = callback.callback->shape().realm();
+ auto& realm = callback.associated_realm();
HTML::report_an_exception(realm.global_object(), exception);
}
JS::Completion invoke_event_listener(WebIDL::CallbackType& callback, DOM::Event& event)
{
- auto& callback_realm = callback.callback->shape().realm();
+ auto& callback_realm = callback.associated_realm();
auto this_value = current_target_wrapper(callback_realm, event);
auto& event_realm = this_value ? this_value_realm(callback_realm, this_value) : callback_realm;
auto wrapped_event = Bindings::event(event_realm, GC::Ref { event });
@@ -195,11 +195,11 @@ JS::Completion invoke_event_handler(WebIDL::CallbackType& callback, DOM::Event&
auto wrapped_lineno = JS::Value(error_event.lineno());
auto wrapped_colno = JS::Value(error_event.colno());
- auto this_value = current_target_wrapper(callback.callback->shape().realm(), error_event);
+ auto this_value = current_target_wrapper(callback.associated_realm(), error_event);
return WebIDL::invoke_callback(callback, this_value.ptr(), { { wrapped_message, wrapped_filename, wrapped_lineno, wrapped_colno, error_event.error() } });
}
- auto& callback_realm = callback.callback->shape().realm();
+ auto& callback_realm = callback.associated_realm();
auto this_value = current_target_wrapper(callback_realm, event);
auto& event_realm = this_value ? this_value_realm(callback_realm, this_value) : callback_realm;
auto wrapped_event = Bindings::event(event_realm, GC::Ref { event });
diff --git a/Libraries/LibWeb/DOM/NodeFilter.cpp b/Libraries/LibWeb/DOM/NodeFilter.cpp
index af0aa087879..86c4a67ce7a 100644
--- a/Libraries/LibWeb/DOM/NodeFilter.cpp
+++ b/Libraries/LibWeb/DOM/NodeFilter.cpp
@@ -29,7 +29,7 @@ static JS::Value filter_or_null(GC::Ptr<DOM::NodeFilter> filter)
JS::ThrowCompletionOr<DOM::NodeFilter::Result> accept_node(JS::Realm& realm, DOM::NodeFilter& filter, DOM::Node& node)
{
auto& callback = filter.callback();
- auto& callback_realm = callback.callback->shape().realm();
+ auto& callback_realm = callback.associated_realm();
auto wrapped_node = Bindings::wrap(Bindings::host_defined_wrapper_world(callback_realm), callback_realm, GC::Ref { node });
auto result = WebIDL::call_user_object_operation(callback, "acceptNode"_utf16_fly_string, {}, { { wrapped_node } });
if (result.is_abrupt())
diff --git a/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp b/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp
index b4644e88280..73d8dcef298 100644
--- a/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp
+++ b/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp
@@ -62,14 +62,6 @@ static Optional<Variant<GC::Ref<Location const>, GC::Ref<Window const>, GC::Ref<
return {};
}
-bool is_cross_origin_platform_object(JS::Object const& object)
-{
- auto platform_object = cross_origin_exposed_platform_object(object);
- if (!platform_object.has_value())
- return false;
- return !platform_object->visit([](auto object) { return is_platform_object_same_origin(*object); });
-}
-
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#integration-with-idl
JS::ThrowCompletionOr<void> perform_a_security_check(JS::VM& vm, JS::Value js_value, Utf16View const& identifier, SecurityCheckType type)
{
diff --git a/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h b/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h
index da10265fe89..2c550f2ce5d 100644
--- a/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h
+++ b/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h
@@ -42,7 +42,6 @@ decltype(auto) invoke_on_window(Window* window, RemoteWindow* remote_window, Cal
Vector<CrossOriginProperty> cross_origin_properties(Variant<HTML::Location const*, HTML::Window const*, HTML::RemoteWindow const*> const&);
JS::ThrowCompletionOr<JS::PropertyDescriptor> cross_origin_property_fallback(JS::VM&, JS::PropertyKey const&);
-bool is_cross_origin_platform_object(JS::Object const&);
bool is_platform_object_same_origin(JS::Object const&);
bool is_platform_object_same_origin(Location const&);
bool is_platform_object_same_origin(Window const&);
diff --git a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
index b773f20050e..1ca75c6038f 100644
--- a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
+++ b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
@@ -19,7 +19,6 @@
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Export.h>
-#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
@@ -196,7 +195,7 @@ JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString
auto& object = callback.callback;
// 4. Let realm be O’s associated realm.
- auto& realm = object->shape().realm();
+ auto& realm = callback.associated_realm();
// 5. Let relevant settings be realm’s settings object.
auto& relevant_settings = HTML::principal_realm_settings_object(realm);
@@ -204,13 +203,6 @@ JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString
// 6. Let stored settings be value’s callback context.
auto& stored_settings = callback.callback_context;
- // AD-HOC: Reject a cross-origin callback object before entering its realm; otherwise the operation lookup below
- // throws in that protected realm, exposing it to the caller. See https://github.com/whatwg/webidl/issues/1640.
- if (HTML::is_cross_origin_platform_object(object)) {
- auto& caller_realm = *object->vm().current_realm();
- return throw_completion(caller_realm, WebIDL::SecurityError::create(caller_realm, "Cannot invoke a callback on a cross-origin object"_utf16));
- }
-
// AD-HOC: See is_settings_object_of_inactive_window().
if (refuse_from_inactive_window == RefuseCallbackFromInactiveWindow::Yes && is_settings_object_of_inactive_window(relevant_settings))
return refuse_callback_from_inactive_window(realm);
diff --git a/Libraries/LibWeb/WebIDL/CallbackType.cpp b/Libraries/LibWeb/WebIDL/CallbackType.cpp
index 5b9058dfd3c..ffedff98bf3 100644
--- a/Libraries/LibWeb/WebIDL/CallbackType.cpp
+++ b/Libraries/LibWeb/WebIDL/CallbackType.cpp
@@ -26,6 +26,24 @@ CallbackType::CallbackType(JS::Object& callback, JS::Realm& callback_context, Op
{
}
+// https://webidl.spec.whatwg.org/#dfn-associated-realm
+JS::Realm& CallbackType::associated_realm() const
+{
+ auto& realm = callback->shape().realm();
+ if (callback->is_function())
+ return realm;
+
+ // NB: The specification leaves the realm of an object that is neither a platform object nor a function
+ // underspecified. Ours is the realm of its shape, which an object created with a cross-origin prototype takes
+ // from that prototype. So, matching other engines, it is kept only if it is same origin-domain with the
+ // callback context.
+ // AD-HOC: This also applies to a cross-origin WindowProxy or Location itself, whose relevant realm the
+ // specification would enter. See https://github.com/whatwg/webidl/issues/1640.
+ if (HTML::principal_realm_settings_object(realm).origin().is_same_origin_domain(callback_context->origin()))
+ return realm;
+ return callback_context->realm();
+}
+
void CallbackType::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/WebIDL/CallbackType.h b/Libraries/LibWeb/WebIDL/CallbackType.h
index 11ff38a4fd5..cfe5a548993 100644
--- a/Libraries/LibWeb/WebIDL/CallbackType.h
+++ b/Libraries/LibWeb/WebIDL/CallbackType.h
@@ -27,6 +27,8 @@ public:
CallbackType(JS::Object& callback, HTML::EnvironmentSettingsObject& callback_context, OperationReturnsPromise = OperationReturnsPromise::No);
CallbackType(JS::Object& callback, JS::Realm& callback_context, OperationReturnsPromise = OperationReturnsPromise::No);
+ JS::Realm& associated_realm() const;
+
GC::Ref<JS::Object> callback;
// https://webidl.spec.whatwg.org/#dfn-callback-context
diff --git a/Tests/LibWeb/Text/expected/HTML/cross-origin-WindowProxy-prototype-callback-objects.txt b/Tests/LibWeb/Text/expected/HTML/cross-origin-WindowProxy-prototype-callback-objects.txt
new file mode 100644
index 00000000000..443fdc598c3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/cross-origin-WindowProxy-prototype-callback-objects.txt
@@ -0,0 +1,3 @@
+EventListener: reported SecurityError, current realm: true
+NodeFilter: SecurityError, current realm: true
+Cross-origin functions called: none
diff --git a/Tests/LibWeb/Text/input/HTML/cross-origin-WindowProxy-prototype-callback-objects.html b/Tests/LibWeb/Text/input/HTML/cross-origin-WindowProxy-prototype-callback-objects.html
new file mode 100644
index 00000000000..5f70f4f9acf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/cross-origin-WindowProxy-prototype-callback-objects.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const frame = document.createElement("iframe");
+ frame.sandbox = "allow-scripts";
+ frame.srcdoc = `<!DOCTYPE html><script>
+ window.handleEvent = () => parent.postMessage("handleEvent was called", "*");
+ window.acceptNode = () => {
+ parent.postMessage("acceptNode was called", "*");
+ return NodeFilter.FILTER_ACCEPT;
+ };
+ window.onmessage = () => parent.postMessage("done", "*");
+ <\/script>`;
+ await new Promise(resolve => {
+ frame.onload = resolve;
+ document.body.appendChild(frame);
+ });
+ const crossOriginWindow = frame.contentWindow;
+
+ const messages = [];
+ const framePostedEverything = new Promise(resolve => {
+ window.addEventListener("message", event => {
+ if (event.data === "done")
+ resolve();
+ else
+ messages.push(event.data);
+ });
+ });
+
+ removeTestErrorHandler();
+ let reportedError;
+ window.addEventListener("error", event => {
+ reportedError = event.error;
+ event.preventDefault();
+ });
+ const target = document.createElement("div");
+ target.addEventListener("test", Object.create(crossOriginWindow));
+ target.dispatchEvent(new Event("test"));
+ println(`EventListener: reported ${reportedError?.name ?? "nothing"}, current realm: ${reportedError instanceof DOMException}`);
+
+ try {
+ document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, Object.create(crossOriginWindow)).nextNode();
+ println("NodeFilter: no exception");
+ } catch (e) {
+ println(`NodeFilter: ${e.name}, current realm: ${e instanceof DOMException}`);
+ }
+
+ crossOriginWindow.postMessage("ping", "*");
+ await framePostedEverything;
+ println(`Cross-origin functions called: ${messages.length ? messages.join(", ") : "none"}`);
+ });
+</script>