Truly independent web browser
LibWebView: Group sandboxed documents by origin at every level
Andreas Kling391c1e3024b2
diff --git a/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp b/Libraries/LibWebView/CanonicalBrowsingContextGroup.cppindex 5b60176931f..949c3e81862 100644--- a/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp+++ b/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp@@ -174,18 +174,18 @@ RefPtr<CanonicalSimilarOriginWindowAgent> CanonicalBrowsingContextGroup::similarreturn agent_cluster->similar_origin_window_agent;}-RefPtr<WebContentClient> CanonicalBrowsingContextGroup::process_hosting_sandboxed_documents_of_site(URL::Site const& site) const+RefPtr<WebContentClient> CanonicalBrowsingContextGroup::process_hosting_sandboxed_documents_of_origin(String const& origin) const{- auto process = m_processes_hosting_sandboxed_documents.get(site.serialize());+ auto process = m_processes_hosting_sandboxed_documents.get(origin);if (!process.has_value())return nullptr;return process_able_to_host(*process);}-void CanonicalBrowsingContextGroup::set_process_hosting_sandboxed_documents_of_site(URL::Site const& site, WebContentClient& process)+void CanonicalBrowsingContextGroup::set_process_hosting_sandboxed_documents_of_origin(String const& origin, WebContentClient& process){m_processes_hosting_sandboxed_documents.remove_all_matching([](auto const&, auto const& hosting_process) { return hosting_process.is_null(); });- m_processes_hosting_sandboxed_documents.set(site.serialize(), process.make_weak_ptr<WebContentClient>());+ m_processes_hosting_sandboxed_documents.set(origin, process.make_weak_ptr<WebContentClient>());}}diff --git a/Libraries/LibWebView/CanonicalBrowsingContextGroup.h b/Libraries/LibWebView/CanonicalBrowsingContextGroup.hindex d26dbc8a0ed..5d475847808 100644--- a/Libraries/LibWebView/CanonicalBrowsingContextGroup.h+++ b/Libraries/LibWebView/CanonicalBrowsingContextGroup.h@@ -66,10 +66,10 @@ public:NonnullRefPtr<CanonicalSimilarOriginWindowAgent> obtain_similar_origin_window_agent(URL::Origin const&, bool requests_oac);RefPtr<CanonicalSimilarOriginWindowAgent> similar_origin_window_agent_for(URL::Origin const&) const;- // The process hosting this group's sandboxed documents of a site. It hosts no other documents of the site, since- // a sandbox exists to keep its content away from them.- RefPtr<WebContentClient> process_hosting_sandboxed_documents_of_site(URL::Site const&) const;- void set_process_hosting_sandboxed_documents_of_site(URL::Site const&, WebContentClient&);+ // The process hosting this group's sandboxed documents of an origin, named by the serialized origin. It hosts no+ // other documents of the origin, since a sandbox exists to keep its content away from them.+ RefPtr<WebContentClient> process_hosting_sandboxed_documents_of_origin(String const&) const;+ void set_process_hosting_sandboxed_documents_of_origin(String const&, WebContentClient&);private:CanonicalBrowsingContextGroup() = default;diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex 35cce90ee21..76457bcf59a 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -860,26 +860,35 @@ void CanonicalNavigable::hand_pending_webdriver_commands_to(WebContentPage& new_view->move_pending_webdriver_commands_to_new_host({}, id(), *old_host, new_host);}-// The site whose sandboxed documents a sandboxed document is hosted with: that of its URL when it was fetched over-// HTTP(S), or that of the nearest ancestor fetched so when it holds what its embedder gave it, as a srcdoc or data: URL-// iframe does. Any other document stays with its initiator, as an about:blank document can, since nothing else can-// script it.-static Optional<URL::Site> site_of_sandboxed_document(CanonicalNavigable const& navigable, CanonicalDocument const& document)+static auto const serialized_file_origin = "file://"sv;++// The serialized origin of a URL a document can be fetched from, or nothing for a URL that holds its own content.+static Optional<String> serialized_origin_of_fetched_url(URL::URL const& url)+{+ if (url.scheme().is_one_of("http"sv, "https"sv))+ return url.origin().serialize();+ if (url.scheme() == "file"sv)+ return String::from_utf8_without_validation(serialized_file_origin.bytes());+ return {};+}++// The origin whose sandboxed documents a sandboxed document is hosted with: that of its URL when it was fetched, or+// that of the nearest ancestor fetched so when it holds what its embedder gave it, as a srcdoc or data: URL iframe+// does. Any other document stays with its initiator, as an about:blank document can, since nothing else can script it.+static Optional<String> origin_of_sandboxed_document(CanonicalNavigable const& navigable, CanonicalDocument const& document){if (!document.origin().is_opaque() || !has_flag(document.active_sandboxing_flag_set(), Web::HTML::SandboxingFlagSet::SandboxedOrigin))return {};- auto is_http_or_https = [](URL::URL const& url) { return url.scheme().is_one_of("http"sv, "https"sv); };auto const& url = document.creation_url();- if (is_http_or_https(url))- return URL::Site::obtain(url.origin());+ if (auto origin = serialized_origin_of_fetched_url(url); origin.has_value())+ return origin;if (!Web::HTML::url_matches_about_srcdoc(url) && url.scheme() != "data"sv)return {};for (auto const* ancestor = navigable.parent(); ancestor; ancestor = ancestor->parent()) {- auto const& ancestor_url = ancestor->active_document().creation_url();- if (is_http_or_https(ancestor_url))- return URL::Site::obtain(ancestor_url.origin());+ if (auto origin = serialized_origin_of_fetched_url(ancestor->active_document().creation_url()); origin.has_value())+ return origin;}return {};}@@ -898,16 +907,21 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(if (mode == SiteIsolationMode::Disabled || (mode == SiteIsolationMode::TopLevel && parent()))return process_holding_navigable;- // A sandboxed iframe's content comes from its URL's site, so it stays out of its embedder's process, and every- // sandboxed iframe of that site shares one process rather than having a process per agent cluster.- auto sandboxed_site = site_of_sandboxed_document(*this, document);- if (sandboxed_site.has_value() && parent())- return document.browsing_context().top_level_browsing_context().group()->process_hosting_sandboxed_documents_of_site(*sandboxed_site);+ // A sandboxed document stays out of its initiator's process, and the sandboxed documents of an origin share one+ // process rather than having a process per agent cluster. A traversable's first document can still take the process+ // its initial about:blank came with, below.+ auto sandboxed_origin = origin_of_sandboxed_document(*this, document);+ if (sandboxed_origin.has_value()) {+ if (auto process = document.browsing_context().top_level_browsing_context().group()->process_hosting_sandboxed_documents_of_origin(*sandboxed_origin))+ return process;+ if (parent())+ return nullptr;+ }// Any other opaque origin keys an agent cluster of its own, which nothing but the documents it was created from can// address: it goes with the initiator's agent. A traversable's document of an opaque origin that no such agent// hosts, as a file's opened from the browser's UI is, keeps the process of a document of an opaque origin.- if (document.origin().is_opaque() && !sandboxed_site.has_value()) {+ if (document.origin().is_opaque() && !sandboxed_origin.has_value()) {if (initiator_origin.has_value()) {if (auto initiator_agent = document.browsing_context().top_level_browsing_context().group()->similar_origin_window_agent_for(*initiator_origin)) {if (auto process = initiator_agent->hosting_process())@@ -971,9 +985,13 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host(CanonicalDocument cErrorOr<NonnullRefPtr<WebContentPage>> CanonicalNavigable::obtain_page_to_host(CanonicalDocument const& document, Optional<URL::Origin> const& initiator_origin){auto page = TRY(obtain_page_to_host_in(process_to_host(document, initiator_origin)));- if (site_isolation_mode() == SiteIsolationMode::IFrame && parent()) {- if (auto site = site_of_sandboxed_document(*this, document); site.has_value())- document.browsing_context().top_level_browsing_context().group()->set_process_hosting_sandboxed_documents_of_site(*site, page->client());+ if (site_isolation_mode() == SiteIsolationMode::IFrame) {+ if (auto origin = origin_of_sandboxed_document(*this, document); origin.has_value()) {+ document.browsing_context().top_level_browsing_context().group()->set_process_hosting_sandboxed_documents_of_origin(*origin, page->client());+ // NB: A sandboxed srcdoc or data: URL iframe of local file content loads files as that content can.+ if (*origin == serialized_file_origin)+ page->client().set_has_hosted_local_file_content();+ }}if (document.is_local_file_content())page->client().set_has_hosted_local_file_content();diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.txtnew file mode 100644index 00000000000..c0599bce446--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.txt@@ -0,0 +1,5 @@+WebContent#0+ iframe#0: remote WebContent#1+ iframe#1: remote WebContent#1+ iframe#2: remote WebContent#1+ iframe#3: localdiff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.txtsimilarity index 84%rename from Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.txtrename to Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.txtindex 4b70865c5f4..a18b53c5b4e 100644--- a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.txt+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.txt@@ -4,3 +4,4 @@ WebContent#0iframe#2: remote WebContent#1iframe#3: remote WebContent#2iframe#4: remote WebContent#3+ iframe#5: remote WebContent#4diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.txtnew file mode 100644index 00000000000..630cc2dd130--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.txt@@ -0,0 +1 @@+{"popupIsSandboxed":true,"popupIsInProcessOfSandboxedOpener":true}diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.htmlnew file mode 100644index 00000000000..1d0aaedacb3--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-leave-the-process-of-local-file-content.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<script>+ // A process holding local file content may read local files, so script in a sandboxed iframe must not run there,+ // whatever URL the iframe has. The iframes share a process of their own, and an iframe that keeps its origin with+ // allow-same-origin stays with the page.+ promiseTest(async () => {+ const loadIframe = async (setup, sandbox) => {+ const iframe = document.createElement("iframe");+ iframe.sandbox = sandbox;+ setup(iframe);+ const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));+ document.body.appendChild(iframe);+ await loaded;+ };++ const fileUrl = "../../../data/iframe-test-content-1.html";+ await loadIframe(iframe => (iframe.src = fileUrl), "allow-scripts");+ await loadIframe(iframe => (iframe.srcdoc = "<p>srcdoc</p>"), "allow-scripts");+ await loadIframe(iframe => (iframe.src = "data:text/html,<p>data</p>"), "allow-scripts");+ await loadIframe(iframe => (iframe.src = fileUrl), "allow-same-origin");++ println(internals.dumpSiteIsolationProcessTree().trimEnd());+ });+</script>diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.htmlsimilarity index 86%rename from Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.htmlrename to Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.htmlindex e271a3e31ad..4c81958c857 100644--- a/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-a-site-share-a-process.html+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-iframes-of-an-origin-share-a-process.html@@ -1,8 +1,8 @@<!DOCTYPE html><script src="../../include.js"></script><script>- // Sandboxed documents loaded over HTTP(S) share one process per site of their URLs, whichever way a redirect took- // them there. That process hosts only sandboxed documents, so it is not the process of the site's other documents.+ // Sandboxed documents loaded over HTTP(S) share one process per origin of their URLs, whichever way a redirect took+ // them there. That process hosts only sandboxed documents, so it is not the process of the origin's other documents.promiseTest(async () => {const httpServer = httpTestServer();@@ -30,7 +30,7 @@const crossSiteRedirect = new URL(await httpServer.createEcho("GET", "/sandboxed-cross-site-redirect", {status: 302,- headers: { Location: urlOnHost(childUrl, `c.${site}`).href },+ headers: { Location: urlOnHost(childUrl, `a.${site}`).href },}));@@ -44,8 +44,9 @@};await loadIframe(urlOnHost(childUrl, `a.${site}`), "");- await loadIframe(urlOnHost(sameSiteRedirect, `b.${site}`), "");+ await loadIframe(urlOnHost(sameSiteRedirect, `a.${site}`), "");await loadIframe(urlOnHost(crossSiteRedirect, uniqueLocalhostHostname("redirecting-site")), "");+ await loadIframe(urlOnHost(childUrl, `b.${site}`), "");await loadIframe(urlOnHost(childUrl, uniqueLocalhostHostname("other-sandboxed-site")), "");await loadIframe(urlOnHost(childUrl, `a.${site}`), null);diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.htmlnew file mode 100644index 00000000000..21c04f58218--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/sandboxed-popup-shares-the-process-of-its-origins-sandboxed-iframes.html@@ -0,0 +1,62 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<style>+ iframe {+ display: block;+ width: 200px;+ height: 100px;+ border: 0;+ }+</style>+<script>+ // A popup that inherits the sandbox of the iframe opening it is a sandboxed document of the iframe's origin, so it+ // is hosted with the origin's other sandboxed documents.+ promiseTest(async () => {+ const httpServer = httpTestServer();++ const popupUrl = new URL(+ await httpServer.createEcho("GET", "/sandboxed-popup", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ opener.postMessage({ origin: self.origin, processId: internals.webContentProcessId() }, "*");+ <\/script>`,+ })+ );++ const iframeUrl = new URL(+ await httpServer.createEcho("GET", "/sandboxed-popup-opener", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ addEventListener("mousedown", () => {+ window.popup = window.open(${JSON.stringify(popupUrl.pathname)}, "sandboxed-popup");+ });+ addEventListener("message", event => {+ parent.postMessage({+ popupIsSandboxed: event.data.origin === "null",+ popupIsInProcessOfSandboxedOpener: event.data.processId === internals.webContentProcessId(),+ }, "*");+ window.popup.close();+ });+ parent.postMessage("ready", "*");+ <\/script>`,+ })+ );+ iframeUrl.hostname = uniqueLocalhostHostname("sandboxed-popup-opener");++ const nextMessage = () => new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));++ const ready = nextMessage();+ const iframe = document.createElement("iframe");+ iframe.sandbox = "allow-scripts allow-popups";+ iframe.src = iframeUrl;+ document.body.appendChild(iframe);+ await ready;++ const report = nextMessage();+ const rect = iframe.getBoundingClientRect();+ internals.clickThroughUIProcess(rect.left + 30, rect.top + 20);+ println(JSON.stringify(await report));+ });+</script>