Truly independent web browser
LibWeb+LibWebView: Check a posted message's source in the UI process
Shannon Boothe69e7ecfcc21
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 5aea3bf2cc6..1c13a2c98b8 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -3456,7 +3456,7 @@ void LocalNavigable::navigate_to_a_javascript_url_from_ui_process(URL::URL const}// https://html.spec.whatwg.org/multipage/web-messaging.html#window-post-message-steps-void LocalNavigable::deliver_posted_message_from_another_process(PostedMessageDescriptor message)+void LocalNavigable::deliver_posted_message_from_another_process(MessageDescriptor message){auto window = active_window();if (!window)diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex bbf38f2c43d..14c1ad8579f 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -195,7 +195,7 @@ public:void adopt_navigation_started_in_ui_process(Utf16String navigation_id);void navigate_to_a_javascript_url_from_ui_process(URL::URL const&, HistoryHandlingBehavior, URL::Origin const& initiator_origin, NavigationSourceSnapshot const&, UserNavigationInvolvement, ContentSecurityPolicy::Directives::Directive::NavigationType csp_navigation_type, Utf16String navigation_id);void navigate_to_a_fragment(URL::URL const&, HistoryHandlingBehavior, UserNavigationInvolvement, GC::Ptr<DOM::Element> source_element, Optional<StorageSerializationRecord> navigation_api_state, Utf16String navigation_id, GC::Ptr<NavigationAPIMethodTracker> api_method_tracker, Optional<URL::Origin> initiator_origin);- void deliver_posted_message_from_another_process(PostedMessageDescriptor);+ void deliver_posted_message_from_another_process(MessageDescriptor);void run_navigation_unload_check(Utf16String const& navigation_id, UnloadPromptShown, GC::Ref<GC::Function<void(bool)>> completion_steps);void continue_navigation_at_population(NavigationPopulationRequest, NavigationPopulationResult);diff --git a/Libraries/LibWeb/HTML/RemoteWindow.cpp b/Libraries/LibWeb/HTML/RemoteWindow.cppindex 474587463a8..6b3f5b65787 100644--- a/Libraries/LibWeb/HTML/RemoteWindow.cpp+++ b/Libraries/LibWeb/HTML/RemoteWindow.cpp@@ -247,16 +247,17 @@ WebIDL::ExceptionOr<void> RemoteWindow::post_message(JS::Realm& realm, JS::Value// process, which forwards it there. That process represents the source's navigable, which names the source.// A source whose [[Window]] has no navigable is null there.Optional<CrossProcessId> source_navigable_id;+ // AD-HOC: The UI process takes the source only from a navigable this process hosts, so a source whose [[Window]] is+ // in another process is null.// NB: A navigable in a destroyed subtree is discarded, like it is for the source's window.parent.- if (auto remote_window = prepared.source->remote_window()) {- if (auto source_navigable = remote_window->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree())+ if (auto window = prepared.source->window()) {+ if (auto source_navigable = window->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree())source_navigable_id = source_navigable->id();- } else if (auto source_navigable = prepared.source->window()->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree()) {- source_navigable_id = source_navigable->id();}PostedMessageDescriptor posted_message {.serialize_with_transfer_result = move(prepared.serialize_with_transfer_result),.target_origin = move(prepared.target_origin),+ .incumbent_settings_id = move(prepared.incumbent_settings_id),.source_origin = move(prepared.source_origin),.source_navigable_id = source_navigable_id,};diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cppindex aeb3ab295ba..22a88a965d7 100644--- a/Libraries/LibWeb/HTML/Window.cpp+++ b/Libraries/LibWeb/HTML/Window.cpp@@ -1670,6 +1670,7 @@ WebIDL::ExceptionOr<Window::PreparedPostMessage> Window::prepare_post_message(JSreturn PreparedPostMessage {.serialize_with_transfer_result = move(serialize_with_transfer_result),.target_origin = move(target_origin),+ .incumbent_settings_id = incumbent_settings.id,.source_origin = move(source_origin),.source = source,};diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.hindex 420cc7ca934..601c78b2d60 100644--- a/Libraries/LibWeb/HTML/Window.h+++ b/Libraries/LibWeb/HTML/Window.h@@ -232,6 +232,7 @@ public:struct PreparedPostMessage {SerializedTransferRecord serialize_with_transfer_result;Variant<Utf16String, URL::Origin> target_origin;+ EnvironmentId incumbent_settings_id;URL::Origin source_origin;GC::Ref<WindowProxy> source;};diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 2d8ae994121..39cb890415e 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -1105,6 +1105,13 @@ void Internals::spoof_current_url(Utf16String const& url_string)page().client().page_did_spoof_document_origin_for_testing(window.associated_document().relevant_settings_object(), origin);}+void Internals::spoof_current_origin_in_this_process(Utf16String const& url_string)+{+ auto url = DOMURL::parse(url_string.utf16_view());+ VERIFY(url.has_value());+ window().associated_document().set_origin(url->origin());+}+void Internals::load_url(Utf16String const& url_string){auto url = DOMURL::parse(url_string.utf16_view());diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex 0bede9b5234..1c8e42b1dfd 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -144,6 +144,7 @@ public:WebIDL::ExceptionOr<bool> dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event);void spoof_current_url(Utf16String const& url);+ void spoof_current_origin_in_this_process(Utf16String const& url);void set_navigator_compatibility_mode(Bindings::InternalsNavigatorCompatibilityMode);void load_url(Utf16String const& url);diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex d82c7923395..c68f10a6070 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -135,6 +135,8 @@ interface Internals {boolean dispatchUserActivatedEvent(EventTarget target, Event event);undefined spoofCurrentURL(USVString url);+ // Gives the document the URL's origin in this process only, as a compromised process would claim it.+ undefined spoofCurrentOriginInThisProcess(USVString url);// Sets the navigator compatibility mode of this process; the realms created afterwards expose what that mode has.undefined setNavigatorCompatibilityMode(InternalsNavigatorCompatibilityMode mode);diff --git a/Libraries/LibWebCommon/Forward.h b/Libraries/LibWebCommon/Forward.hindex d4ccab3475b..2d25a0f5e4f 100644--- a/Libraries/LibWebCommon/Forward.h+++ b/Libraries/LibWebCommon/Forward.h@@ -75,6 +75,7 @@ struct EmbedderPolicy;class EnvironmentId;struct HistoryNavigationPopulation;enum class HistoryStepResult;+struct MessageDescriptor;struct NavigationPopulationRequest;struct NavigationPopulationResult;struct NavigationStartRequest;diff --git a/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.cpp b/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.cppindex 0cab2d5d9b3..0523d05e59a 100644--- a/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.cpp+++ b/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.cpp@@ -10,11 +10,34 @@namespace IPC {+template<>+ErrorOr<void> encode(Encoder& encoder, Web::HTML::MessageDescriptor const& message)+{+ TRY(encoder.encode(message.serialize_with_transfer_result));+ TRY(encoder.encode(message.target_origin));+ TRY(encoder.encode(message.source_origin));+ TRY(encoder.encode(message.source_navigable_id));+ return {};+}++template<>+ErrorOr<Web::HTML::MessageDescriptor> decode(Decoder& decoder)+{+ using TargetOrigin = Variant<Utf16String, URL::Origin>;+ return Web::HTML::MessageDescriptor {+ .serialize_with_transfer_result = TRY(decoder.decode<Web::HTML::SerializedTransferRecord>()),+ .target_origin = TRY(decoder.decode<TargetOrigin>()),+ .source_origin = TRY(decoder.decode<URL::Origin>()),+ .source_navigable_id = TRY(decoder.decode<Optional<Web::HTML::CrossProcessId>>()),+ };+}+template<>ErrorOr<void> encode(Encoder& encoder, Web::HTML::PostedMessageDescriptor const& message){TRY(encoder.encode(message.serialize_with_transfer_result));TRY(encoder.encode(message.target_origin));+ TRY(encoder.encode(message.incumbent_settings_id));TRY(encoder.encode(message.source_origin));TRY(encoder.encode(message.source_navigable_id));return {};@@ -27,6 +50,7 @@ ErrorOr<Web::HTML::PostedMessageDescriptor> decode(Decoder& decoder)return Web::HTML::PostedMessageDescriptor {.serialize_with_transfer_result = TRY(decoder.decode<Web::HTML::SerializedTransferRecord>()),.target_origin = TRY(decoder.decode<TargetOrigin>()),+ .incumbent_settings_id = TRY(decoder.decode<Web::HTML::EnvironmentId>()),.source_origin = TRY(decoder.decode<URL::Origin>()),.source_navigable_id = TRY(decoder.decode<Optional<Web::HTML::CrossProcessId>>()),};diff --git a/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.h b/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.hindex 06797023554..604640647a0 100644--- a/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.h+++ b/Libraries/LibWebCommon/HTML/PostedMessageDescriptor.h@@ -12,13 +12,22 @@#include <LibURL/Origin.h>#include <LibWebCommon/Export.h>#include <LibWebCommon/HTML/CrossProcessId.h>+#include <LibWebCommon/HTML/Scripting/EnvironmentId.h>#include <LibWebCommon/HTML/SerializationRecords.h>namespace Web::HTML {+struct MessageDescriptor {+ SerializedTransferRecord serialize_with_transfer_result;+ Variant<Utf16String, URL::Origin> target_origin;+ URL::Origin source_origin;+ Optional<CrossProcessId> source_navigable_id;+};+struct PostedMessageDescriptor {SerializedTransferRecord serialize_with_transfer_result;Variant<Utf16String, URL::Origin> target_origin;+ EnvironmentId incumbent_settings_id;URL::Origin source_origin;Optional<CrossProcessId> source_navigable_id;};@@ -27,6 +36,12 @@ struct PostedMessageDescriptor {namespace IPC {+template<>+WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::MessageDescriptor const&);++template<>+WEBCOMMON_API ErrorOr<Web::HTML::MessageDescriptor> decode(Decoder&);+template<>WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::PostedMessageDescriptor const&);diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex ceb1b8454bb..2894a7bac84 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -75,6 +75,19 @@ Optional<CanonicalEnvironmentSettingsObject const&> WebContentClient::hosted_envreturn environment;}+bool WebContentClient::hosts_a_document_of_origin(URL::Origin const& origin)+{+ bool hosts_one = false;+ for_each_page([&](WebContentPage& page) {+ page.for_each_hosted_document([&](CanonicalDocument& document) {+ hosts_one = document.origin().is_same_origin(origin);+ return hosts_one ? IterationDecision::Break : IterationDecision::Continue;+ });+ return hosts_one ? IterationDecision::Break : IterationDecision::Continue;+ });+ return hosts_one;+}+void WebContentClient::did_retain_blob_url_token(Web::HTML::CrossProcessId navigable_id, URL::BlobURLEntry::Token token){if (auto navigable = hosted_navigable(navigable_id); navigable.has_value())diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex 5c4883365c2..181fe16e93f 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -135,6 +135,7 @@ public:void release_unneeded_representing_pages();bool hosts_an_environment_with_storage_key(Web::StorageAPI::StorageKey const&);Optional<CanonicalEnvironmentSettingsObject const&> hosted_environment(Web::HTML::EnvironmentId const& environment_id);+ bool hosts_a_document_of_origin(URL::Origin const&);WebContentPage* page(Web::PageId page_id) const;template<CallableAs<IterationDecision, WebContentPage&> Callback>diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex abf0fa2510b..884a2fc4fd4 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -715,12 +715,43 @@ void WebContentPage::did_request_navigation_of_navigable(Web::HTML::CrossProcesstarget->begin_navigation(move(navigation));}-void WebContentPage::did_post_message_to_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor message)+void WebContentPage::did_post_message_to_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor posted_message){- // The window post message steps queue their task on the target window in the process hosting its document.+ // https://html.spec.whatwg.org/multipage/web-messaging.html#window-post-message-steps+ // 2. Let incumbentSettings be the incumbent settings object.+ // NB: The posting process determined it. Any page of that process can host it, since the request comes through the+ // page holding the target's tab.+ auto incumbent_settings = client().hosted_environment(posted_message.incumbent_settings_id);++ // 8. Queue a global task on the posted message task source given targetWindow to run the following steps:+ // NB: The process hosting targetWindow's document queues it, with the origin and source given here.auto endpoint = endpoint_hosting_navigable_represented_by(navigable_id);if (!endpoint)return;++ // 8.2. Let origin be incumbentSettings's origin.+ // AD-HOC: For a window whose document this process no longer holds, which a same-origin document can still call+ // into, the origin the posting process gives is taken if that process hosts a document of that origin.+ Optional<URL::Origin> origin;+ if (incumbent_settings.has_value())+ origin = incumbent_settings->origin();+ else if (client().hosts_a_document_of_origin(posted_message.source_origin))+ origin = move(posted_message.source_origin);+ if (!origin.has_value())+ return;++ // 8.3. Let source be the WindowProxy object corresponding to incumbentSettings's global object (a Window object).+ // NB: The source must be a navigable the posting process hosts.+ Optional<Web::HTML::CrossProcessId> source_navigable_id;+ if (posted_message.source_navigable_id.has_value() && client().hosted_navigable(*posted_message.source_navigable_id).has_value())+ source_navigable_id = posted_message.source_navigable_id;++ Web::HTML::MessageDescriptor message {+ .serialize_with_transfer_result = move(posted_message.serialize_with_transfer_result),+ .target_origin = move(posted_message.target_origin),+ .source_origin = origin.release_value(),+ .source_navigable_id = source_navigable_id,+ };endpoint->async_deliver_posted_message(navigable_id, move(message));}diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 0784ede91b1..c3a17df4c19 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -174,7 +174,7 @@ private:void maybe_record_history_visit_for_current_load(URL::URL const&, Optional<String> title, StringView reason);virtual void did_request_navigation_of_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::CrossProcessId source_navigable_id, Web::HTML::CrossProcessId source_document_id, Web::HTML::PreparedNavigationDescriptor navigation) override;- virtual void did_post_message_to_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor message) override;+ virtual void did_post_message_to_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor posted_message) override;virtual void did_request_focusing_steps_for_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::FocusTrigger focus_trigger) override;virtual void did_request_window_focus_of_navigable(Web::HTML::CrossProcessId navigable_id) override;virtual void did_request_set_opener_of_navigable(Web::HTML::CrossProcessId navigable_id, Web::HTML::CrossProcessId opener_navigable_id) override;diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex 50aff8bab0b..e223da21fd5 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -300,7 +300,7 @@ void ConnectionFromClient::cancel_navigation_params_creation(Web::PageId page_idpage->cancel_navigation_params_creation(navigable_id, navigation_id);}-void ConnectionFromClient::deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor message)+void ConnectionFromClient::deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::MessageDescriptor message){if (auto page = this->page(page_id); page.has_value())page->deliver_posted_message(navigable_id, move(message));diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex 5cc8b6bd87e..fb0eb6d9ca9 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -130,7 +130,7 @@ private:virtual void run_navigation_unload_check(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Utf16String navigation_id, Web::HTML::UnloadPromptShown) override;virtual void create_navigation_params(Web::PageId page_id, Web::HTML::NavigationPopulationRequest) override;virtual void cancel_navigation_params_creation(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Utf16String navigation_id) override;- virtual void deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor) override;+ virtual void deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::MessageDescriptor) override;virtual void close_traversable_from_script(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) override;virtual void run_focusing_steps_for_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::FocusTrigger) override;virtual void focus_window_of_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 1086505a6e1..f6b531d3c0e 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -301,7 +301,7 @@ void PageClient::request_set_opener_of_remote_navigable(Web::HTML::RemoteNavigabclient().async_did_request_set_opener_of_navigable(m_id, navigable.id(), opener.id());}-void PageClient::deliver_posted_message(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor message)+void PageClient::deliver_posted_message(Web::HTML::CrossProcessId navigable_id, Web::HTML::MessageDescriptor message){if (auto* navigable = as_if<Web::HTML::LocalNavigable>(page().navigable_with_id(navigable_id).ptr()))navigable->deliver_posted_message_from_another_process(move(message));diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 77d4a7e5d96..ec0f0ebc033 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -151,7 +151,7 @@ public:void run_navigation_unload_check(Web::HTML::CrossProcessId navigable_id, Utf16String const& navigation_id, Web::HTML::UnloadPromptShown);void did_receive_unload_check_result(Web::HTML::CrossProcessId check_id, Web::HTML::CheckIfUnloadingIsCanceledResult);void create_navigation_params(Web::HTML::NavigationPopulationRequest);- void deliver_posted_message(Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor);+ void deliver_posted_message(Web::HTML::CrossProcessId navigable_id, Web::HTML::MessageDescriptor);void cancel_navigation_params_creation(Web::HTML::CrossProcessId navigable_id, Utf16String const& navigation_id);void populate_navigation(Web::HTML::NavigationPopulationRequest, Web::HTML::NavigationPopulationResult);void cancel_download(u64 download_id);diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex 6d2a1cceb69..2daae2483dd 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -108,7 +108,7 @@ endpoint WebContentServer routed by Web::PageId page_idrun_navigation_unload_check(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Utf16String navigation_id, Web::HTML::UnloadPromptShown unload_prompt_shown) =|create_navigation_params(Web::PageId page_id, Web::HTML::NavigationPopulationRequest request) =|cancel_navigation_params_creation(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Utf16String navigation_id) =|- deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::PostedMessageDescriptor message) =|+ deliver_posted_message(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::MessageDescriptor message) =|close_traversable_from_script(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) =|run_focusing_steps_for_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Web::HTML::FocusTrigger focus_trigger) =|focus_window_of_navigable(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) =|diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.txtnew file mode 100644index 00000000000..2a75867634f--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.txt@@ -0,0 +1,2 @@+first message: postWithItsOwnOrigin+its origin is this page's: truediff --git a/Tests/LibWeb/Text/expected/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.txt b/Tests/LibWeb/Text/expected/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.txtnew file mode 100644index 00000000000..9dbdfee12fb--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.txt@@ -0,0 +1,2 @@+origin is the page's own: true+origin is the claimed one: falsediff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.htmlnew file mode 100644index 00000000000..bea46a0c6c6--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/removed-iframe-posts-only-with-an-origin-its-process-hosts.html@@ -0,0 +1,52 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<script>+ // A function from a removed iframe posts to an isolated iframe after the UI process has stopped holding the removed+ // iframe's document. The message keeps the origin its process gives only if that process hosts a document of it.+ promiseTest(async () => {+ const remoteUrl = new URL(+ await httpTestServer().createEcho("GET", "/removed-iframe-posts-with-a-hosted-origin", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ addEventListener("message", event => parent.postMessage({ data: event.data, origin: event.origin }, "*"));+ parent.postMessage("ready", "*");+ <\/script>`,+ })+ );+ remoteUrl.hostname = uniqueLocalhostHostname("removed-iframe-posts-with-a-hosted-origin");++ const nextMessage = () => new Promise(resolve => window.addEventListener("message", event => resolve(event.data), { once: true }));++ const ready = nextMessage();+ const remoteIframe = document.createElement("iframe");+ remoteIframe.src = remoteUrl;+ document.body.appendChild(remoteIframe);+ await ready;++ const postFromARemovedIframe = async (name, spoofedOrigin) => {+ const iframe = document.createElement("iframe");+ iframe.srcdoc = `<script>parent.${name} = target => target.postMessage("${name}", "*");<\/script>`;+ await new Promise(resolve => {+ iframe.onload = resolve;+ document.body.appendChild(iframe);+ });+ const post = window[name];+ if (spoofedOrigin)+ iframe.contentWindow.internals.spoofCurrentOriginInThisProcess(spoofedOrigin);+ iframe.remove();+ return post;+ };+ const postWithItsOwnOrigin = await postFromARemovedIframe("postWithItsOwnOrigin");+ const postWithAnotherOrigin = await postFromARemovedIframe("postWithAnotherOrigin", "https://spoofed.example");+ while (internals.dumpSiteIsolationProcessTree().includes(": local"))+ await new Promise(resolve => setTimeout(resolve, 10));++ const reply = nextMessage();+ postWithAnotherOrigin(remoteIframe.contentWindow);+ postWithItsOwnOrigin(remoteIframe.contentWindow);+ const { data, origin } = await reply;+ println(`first message: ${data}`);+ println(`its origin is this page's: ${origin === self.origin}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.html b/Tests/LibWeb/Text/input/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.htmlnew file mode 100644index 00000000000..1b578dc631c--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/popup-receives-the-ui-process-origin-of-a-posted-message.html@@ -0,0 +1,45 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A message posted to a window in another process has the origin the UI process holds for the posting document,+ // not the one the posting process claims.+ promiseTest(async () => {+ const httpServer = httpTestServer();+ const runId = crypto.randomUUID();++ const crossSiteUrl = new URL(+ await httpServer.createEcho("GET", `/posted-message-origin-cross-site-${runId}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ addEventListener("message", event => opener.postMessage(event.origin, "*"));+ opener.postMessage("ready", "*");+ <\/script>`,+ })+ );+ crossSiteUrl.hostname = uniqueLocalhostHostname("posted-message-origin");++ const popupUrl = await httpServer.createEcho("GET", `/posted-message-origin-same-site-${runId}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>location.href = ${JSON.stringify(crossSiteUrl.href)};<\/script>`,+ });++ const nextMessage = () => new Promise(resolve => {+ addEventListener("message", event => resolve(event.data), { once: true });+ });++ const realOrigin = self.origin;+ const ready = nextMessage();+ const popup = window.open(popupUrl);+ await ready;++ const reply = nextMessage();+ internals.spoofCurrentOriginInThisProcess("https://spoofed.example");+ popup.postMessage("hello", "*");+ const origin = await reply;+ println(`origin is the page's own: ${origin === realOrigin}`);+ println(`origin is the claimed one: ${origin === "https://spoofed.example"}`);+ popup.close();+ });+</script>