Truly independent web browser
LibWeb+LibWebView: Give popups their creator's top-level creation URL
Zaggy1024f0b9b2bd2bbd
diff --git a/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Libraries/LibWeb/HTML/BrowsingContext.cppindex f78cf86e3de..e690e1659c1 100644--- a/Libraries/LibWeb/HTML/BrowsingContext.cpp+++ b/Libraries/LibWeb/HTML/BrowsingContext.cpp@@ -139,8 +139,19 @@ BrowsingContext::BrowsingContextAndDocument BrowsingContext::create_a_new_browsiauto& realm = *realm_execution_context->realm;- // 11. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.- auto top_level_creation_url = !embedder ? URL::about_blank() : relevant_settings_object(*embedder).top_level_creation_url.value();+ // 11. Let topLevelCreationURL be the return value of running these steps:+ auto top_level_creation_url = [&] {+ // 1. If embedder is non-null, then return embedder's relevant settings object's top-level creation URL.+ if (embedder)+ return relevant_settings_object(*embedder).top_level_creation_url.value();++ // 2. If creator is non-null, then return creator's relevant settings object's top-level creation URL.+ if (creator)+ return creator->relevant_settings_object().top_level_creation_url.value();++ // 3. Return about:blank.+ return URL::about_blank();+ }();// 12. Let topLevelOrigin be origin if embedder is null; otherwise embedder's relevant settings object's top-level origin.auto top_level_origin = !embedder ? origin : relevant_settings_object(*embedder).top_level_origin.value();diff --git a/Libraries/LibWebView/CanonicalBrowsingContext.cpp b/Libraries/LibWebView/CanonicalBrowsingContext.cppindex ec0493b47b2..2e6980e0ee3 100644--- a/Libraries/LibWebView/CanonicalBrowsingContext.cpp+++ b/Libraries/LibWebView/CanonicalBrowsingContext.cpp@@ -9,6 +9,7 @@#include <LibWebView/CanonicalBrowsingContext.h>#include <LibWebView/CanonicalBrowsingContextGroup.h>#include <LibWebView/CanonicalDocument.h>+#include <LibWebView/CanonicalEnvironmentSettingsObject.h>#include <LibWebView/CanonicalTraversable.h>#include <LibWebView/CanonicalWindow.h>#include <LibWebView/WebContentClient.h>@@ -77,9 +78,16 @@ CanonicalBrowsingContext::BrowsingContextAndDocument CanonicalBrowsingContext::c// NB: The realm is in the process creating the document, which hosts agent once it holds the document.auto window = CanonicalWindow::create(agent);+ // 11. Let topLevelCreationURL be the return value of running these steps:+ // 1. If embedder is non-null, then return embedder's relevant settings object's top-level creation URL.+ // 2. If creator is non-null, then return creator's relevant settings object's top-level creation URL.+ // 3. Return about:blank.+ // NB: An embedder's relevant settings object is that of its node document, which is creator.+ auto top_level_creation_url = creator ? creator->relevant_global_object().relevant_settings_object().top_level_creation_url().value() : about_blank;+// 13. Set up a window environment settings object with about:blank, realm execution context, null,// topLevelCreationURL, and topLevelOrigin.- window->set_up_a_window_environment_settings_object(move(environment_id));+ window->set_up_a_window_environment_settings_object(move(environment_id), move(top_level_creation_url));// 15. Let document be a new Document, with:// origin: origindiff --git a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cppindex cf2600e2387..360afe3d5e7 100644--- a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp+++ b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp@@ -16,9 +16,10 @@ bool CanonicalEnvironmentSettingsObject::may_use_cookies_of(URL::URL const& url)return !origin().is_opaque() && url.origin().is_same_origin(origin());}-CanonicalWindowEnvironmentSettingsObject::CanonicalWindowEnvironmentSettingsObject(CanonicalWindow& window, Web::HTML::EnvironmentId id)+CanonicalWindowEnvironmentSettingsObject::CanonicalWindowEnvironmentSettingsObject(CanonicalWindow& window, Web::HTML::EnvironmentId id, URL::URL top_level_creation_url): CanonicalEnvironmentSettingsObject(move(id)), m_window(window)+ , m_top_level_creation_url(move(top_level_creation_url)){}diff --git a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.h b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.hindex ae762da1ed3..415edf599af 100644--- a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.h+++ b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.h@@ -28,6 +28,9 @@ public:// https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-originvirtual URL::Origin const& origin() const = 0;+ // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url+ virtual Optional<URL::URL> top_level_creation_url() const = 0;+// https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-originvirtual Optional<URL::Origin> top_level_origin() const = 0;@@ -46,13 +49,15 @@ private:// https://html.spec.whatwg.org/multipage/nav-history-apis.html#script-settings-for-window-objectsclass WEBVIEW_API CanonicalWindowEnvironmentSettingsObject final : public CanonicalEnvironmentSettingsObject {public:- CanonicalWindowEnvironmentSettingsObject(CanonicalWindow&, Web::HTML::EnvironmentId id);+ CanonicalWindowEnvironmentSettingsObject(CanonicalWindow&, Web::HTML::EnvironmentId id, URL::URL top_level_creation_url);virtual URL::Origin const& origin() const override;+ virtual Optional<URL::URL> top_level_creation_url() const override { return m_top_level_creation_url; }virtual Optional<URL::Origin> top_level_origin() const override;private:CanonicalWindow& m_window;+ URL::URL m_top_level_creation_url;};// https://html.spec.whatwg.org/multipage/workers.html#script-settings-for-workers@@ -61,6 +66,7 @@ public:CanonicalWorkerEnvironmentSettingsObject(URL::Origin, Optional<URL::Origin> top_level_origin, bool has_cross_site_ancestor, Web::HTML::EnvironmentId id);virtual URL::Origin const& origin() const override { return m_origin; }+ virtual Optional<URL::URL> top_level_creation_url() const override { return {}; }virtual Optional<URL::Origin> top_level_origin() const override { return m_top_level_origin; }// https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-has-cross-site-ancestordiff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex b8699a88ca5..0f96e374809 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -19,6 +19,7 @@#include <LibWebView/CanonicalBrowsingContext.h>#include <LibWebView/CanonicalBrowsingContextGroup.h>#include <LibWebView/CanonicalDocument.h>+#include <LibWebView/CanonicalEnvironmentSettingsObject.h>#include <LibWebView/CanonicalTraversable.h>#include <LibWebView/CanonicalWindow.h>#include <LibWebView/PictureInPictureManager.h>@@ -419,12 +420,29 @@ NonnullRefPtr<CanonicalDocument> CanonicalNavigable::create_and_initialize_a_doc// - For the global object, create a new Window object.// - For the global this binding, use browsingContext's WindowProxy object.// 6. Set window to the global object of realmExecutionContext's Realm component.- // NB: The realm is in the process hosting agent, which runs steps 7 to 10.+ // NB: The realm is in the process hosting agent.window = CanonicalWindow::create(agent);+ // 7. Let topLevelCreationURL be creationURL.+ auto top_level_creation_url = creation_url;++ // 8. Let topLevelOrigin be navigationParams's origin.+ // NB: The UI process derives the top-level origin from the window's associated document.++ // 9. If navigable's container is not null, then:+ if (m_parent) {+ // 1. Let parentEnvironment be navigable's container's relevant settings object.+ auto const& parent_environment = m_parent->active_document().relevant_global_object().relevant_settings_object();++ // 2. Set topLevelCreationURL to parentEnvironment's top-level creation URL.+ top_level_creation_url = parent_environment.top_level_creation_url().value();++ // 3. Set topLevelOrigin to parentEnvironment's top-level origin.+ }+// 10. Set up a window environment settings object with creationURL, realmExecutionContext, navigationParams's// reserved environment, topLevelCreationURL, and topLevelOrigin.- window->set_up_a_window_environment_settings_object(navigation_params.environment_id);+ window->set_up_a_window_environment_settings_object(navigation_params.environment_id, move(top_level_creation_url));}// 9. Let document be a new Document, with@@ -1122,7 +1140,7 @@ Optional<Web::HTML::ReplicatedNavigableState> CanonicalNavigable::replicated_sta.active_document_url = active_document_url(),.active_document_origin = active_document().origin(),.active_document_is_fully_active = hosted_state.active_document_is_fully_active,- .top_level_creation_url = traversable.active_document().creation_url(),+ .top_level_creation_url = active_document().relevant_global_object().relevant_settings_object().top_level_creation_url().value(),.top_level_origin = traversable.active_document().origin(),.top_level_is_created_by_web_content = traversable.is_created_by_web_content(),.top_level_session_history_entry_count = traversable.session_history().size(),diff --git a/Libraries/LibWebView/CanonicalWindow.cpp b/Libraries/LibWebView/CanonicalWindow.cppindex 9eafa4412d2..c09f840f656 100644--- a/Libraries/LibWebView/CanonicalWindow.cpp+++ b/Libraries/LibWebView/CanonicalWindow.cpp@@ -30,7 +30,7 @@ CanonicalEnvironmentSettingsObject const& CanonicalWindow::relevant_settings_obj}// https://html.spec.whatwg.org/multipage/nav-history-apis.html#set-up-a-window-environment-settings-object-void CanonicalWindow::set_up_a_window_environment_settings_object(Optional<Web::HTML::EnvironmentId> id)+void CanonicalWindow::set_up_a_window_environment_settings_object(Optional<Web::HTML::EnvironmentId> id, URL::URL top_level_creation_url){VERIFY(!m_relevant_settings_object);@@ -46,8 +46,11 @@ void CanonicalWindow::set_up_a_window_environment_settings_object(Optional<Web::if (!id.has_value())id = Web::HTML::EnvironmentId::generate();+ // 6. Set settings object's creation URL to creationURL, settings object's top-level creation URL to+ // topLevelCreationURL, and settings object's top-level origin to topLevelOrigin.+ // NB: The UI process derives the creation URL and top-level origin from the window's associated document.// 7. Set realm's [[HostDefined]] field to settings object.- m_relevant_settings_object = make<CanonicalWindowEnvironmentSettingsObject>(*this, id.release_value());+ m_relevant_settings_object = make<CanonicalWindowEnvironmentSettingsObject>(*this, id.release_value(), move(top_level_creation_url));}CanonicalDocument const& CanonicalWindow::associated_document() constdiff --git a/Libraries/LibWebView/CanonicalWindow.h b/Libraries/LibWebView/CanonicalWindow.hindex c27bf4339cb..3a4f6c9a7ae 100644--- a/Libraries/LibWebView/CanonicalWindow.h+++ b/Libraries/LibWebView/CanonicalWindow.h@@ -32,7 +32,7 @@ public:CanonicalSimilarOriginWindowAgent& agent() const { return m_agent; }CanonicalEnvironmentSettingsObject const& relevant_settings_object() const;- void set_up_a_window_environment_settings_object(Optional<Web::HTML::EnvironmentId> id);+ void set_up_a_window_environment_settings_object(Optional<Web::HTML::EnvironmentId> id, URL::URL top_level_creation_url);bool has_associated_document() const { return m_associated_document; }CanonicalDocument const& associated_document() const;diff --git a/Libraries/LibWebView/NavigationLoader.cpp b/Libraries/LibWebView/NavigationLoader.cppindex 3f08087d720..f67f536433b 100644--- a/Libraries/LibWebView/NavigationLoader.cpp+++ b/Libraries/LibWebView/NavigationLoader.cpp@@ -199,9 +199,10 @@ void NavigationLoader::obtain_the_response_policies(CanonicalNavigable const& na// 3. If navigable is not a top-level traversable:// 1. Let parentEnvironment be navigable's parent's active document's relevant settings object.// 2. Set topLevelCreationURL to parentEnvironment's top-level creation URL.- // NB: The UI process runs these steps for the final response only. The parent environment's top-level creation URL- // is the creation URL of the top-level traversable's active document.- auto const& top_level_creation_url = navigable.is_top_level_traversable() ? response_url : navigable.top_level_traversable().active_document().creation_url();+ // NB: The UI process runs these steps for the final response only.+ auto top_level_creation_url = response_url;+ if (!navigable.is_top_level_traversable())+ top_level_creation_url = navigable.parent()->active_document().relevant_global_object().relevant_settings_object().top_level_creation_url().value();// https://html.spec.whatwg.org/multipage/webappapis.html#secure-context// 2. If environment's top-level creation URL's scheme is "data", then return false.diff --git a/Tests/LibWeb/Text/expected/secure-context-popup-follows-creator.txt b/Tests/LibWeb/Text/expected/secure-context-popup-follows-creator.txtnew file mode 100644index 00000000000..de98dd492d7--- /dev/null+++ b/Tests/LibWeb/Text/expected/secure-context-popup-follows-creator.txt@@ -0,0 +1,3 @@+opener: false+popup: false+frame in popup: falsediff --git a/Tests/LibWeb/Text/input/secure-context-popup-follows-creator.html b/Tests/LibWeb/Text/input/secure-context-popup-follows-creator.htmlnew file mode 100644index 00000000000..315343024a0--- /dev/null+++ b/Tests/LibWeb/Text/input/secure-context-popup-follows-creator.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ test(() => {+ internals.setEnvironmentsTopLevelURL("http://ladybird.org/");+ const popup = window.open();+ println(`opener: ${isSecureContext}`);+ println(`popup: ${popup.isSecureContext}`);++ const frame = popup.document.createElement("iframe");+ popup.document.body.appendChild(frame);+ println(`frame in popup: ${frame.contentWindow.isSecureContext}`);+ popup.close();+ });+</script>