Skip to content

Truly independent web browser

LibWebView: Host sandboxed documents apart from their initiators

Andreas Kling200c38228601
diff --git a/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp b/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp
index 2f6cd1aa4b8..5b60176931f 100644
--- a/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp
+++ b/Libraries/LibWebView/CanonicalBrowsingContextGroup.cpp
@@ -25,15 +25,20 @@ CanonicalSimilarOriginWindowAgent::CanonicalSimilarOriginWindowAgent(Web::HTML::
{
}
-RefPtr<WebContentClient> CanonicalSimilarOriginWindowAgent::hosting_process() const
+// A process that has exited, or has been asked to, hosts nothing.
+static RefPtr<WebContentClient> process_able_to_host(WeakPtr<WebContentClient> const& weak_process)
{
- // A process that has exited, or has been asked to, hosts nothing.
- auto process = m_hosting_process.strong_ref();
+ auto process = weak_process.strong_ref();
if (!process || !process->is_open() || process->has_requested_close())
return nullptr;
return process;
}
+RefPtr<WebContentClient> CanonicalSimilarOriginWindowAgent::hosting_process() const
+{
+ return process_able_to_host(m_hosting_process);
+}
+
void CanonicalSimilarOriginWindowAgent::set_hosting_process_if_unset(WebContentClient& process)
{
if (hosting_process())
@@ -169,4 +174,18 @@ RefPtr<CanonicalSimilarOriginWindowAgent> CanonicalBrowsingContextGroup::similar
return agent_cluster->similar_origin_window_agent;
}
+RefPtr<WebContentClient> CanonicalBrowsingContextGroup::process_hosting_sandboxed_documents_of_site(URL::Site const& site) const
+{
+ auto process = m_processes_hosting_sandboxed_documents.get(site.serialize());
+ 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)
+{
+ 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>());
+}
+
}
diff --git a/Libraries/LibWebView/CanonicalBrowsingContextGroup.h b/Libraries/LibWebView/CanonicalBrowsingContextGroup.h
index a9924a18b6a..d26dbc8a0ed 100644
--- a/Libraries/LibWebView/CanonicalBrowsingContextGroup.h
+++ b/Libraries/LibWebView/CanonicalBrowsingContextGroup.h
@@ -66,6 +66,11 @@ 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&);
+
private:
CanonicalBrowsingContextGroup() = default;
@@ -101,6 +106,8 @@ private:
// https://html.spec.whatwg.org/multipage/document-sequences.html#historical-agent-cluster-key-map
HashMap<URL::Origin, AgentClusterKey> m_historical_agent_cluster_key_map;
+
+ HashMap<String, WeakPtr<WebContentClient>> m_processes_hosting_sandboxed_documents;
};
}
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index d981f451264..2bd5f455065 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -860,6 +860,18 @@ void CanonicalNavigable::hand_pending_webdriver_commands_to(WebContentPage& new_
view->move_pending_webdriver_commands_to_new_host({}, id(), *old_host, new_host);
}
+// A document fetched over HTTP(S) has an opaque origin only because it is sandboxed. Such a document goes by the site
+// of its URL, which a document made from what its creator holds, such as a data: URL's, does not have.
+static Optional<URL::Site> site_of_sandboxed_document(CanonicalDocument const& document)
+{
+ if (!document.origin().is_opaque())
+ return {};
+ auto const& url = document.creation_url();
+ if (!url.scheme().is_one_of("http"sv, "https"sv))
+ return {};
+ return URL::Site::obtain(url.origin());
+}
+
RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(CanonicalDocument const& document, Optional<URL::Origin> const& initiator_origin) const
{
RefPtr<WebContentPage> page_holding_navigable = parent() ? reporting_page() : top_level_traversable().page_hosting(*this);
@@ -874,10 +886,16 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(
if (mode == SiteIsolationMode::Disabled || (mode == SiteIsolationMode::TopLevel && parent()))
return process_holding_navigable;
- // An opaque origin keys an agent cluster of its own, which nothing but the documents it was created from can
+ // 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(document);
+ if (sandboxed_site.has_value() && parent())
+ return document.browsing_context().top_level_browsing_context().group()->process_hosting_sandboxed_documents_of_site(*sandboxed_site);
+
+ // 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()) {
+ if (document.origin().is_opaque() && !sandboxed_site.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())
@@ -891,7 +909,7 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host_for_site_isolation(
// A traversable's first document takes the process its initial about:blank came with, unless that document
// inherited its creator's origin and shares the creator's process, or a browsing context group switch leaves the
// initial about:blank's group.
- if (!parent() && active_document().is_initial_about_blank() && active_document().origin().is_opaque() && &document.browsing_context() == &active_browsing_context())
+ if (!parent() && active_document().is_initial_about_blank() && active_document().origin().is_opaque() && !active_browsing_context().is_auxiliary() && &document.browsing_context() == &active_browsing_context())
return process_holding_navigable;
return nullptr;
@@ -941,6 +959,10 @@ RefPtr<WebContentClient> CanonicalNavigable::process_to_host(CanonicalDocument c
ErrorOr<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(document); site.has_value())
+ document.browsing_context().top_level_browsing_context().group()->set_process_hosting_sandboxed_documents_of_site(*site, page->client());
+ }
if (document.is_local_file_content())
page->client().set_has_hosted_local_file_content();
if (auto const& web_ui_host = document.web_ui_host(); web_ui_host.has_value()) {
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.txt
new file mode 100644
index 00000000000..1b3f4a7db68
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.txt
@@ -0,0 +1 @@
+{"popupHasOriginOfItsOwn":true,"popupIsInProcessOfSandboxedOpener":false}
diff --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-a-site-share-a-process.txt
index 0f1c2bae72f..4b70865c5f4 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-a-site-share-a-process.txt
@@ -1,6 +1,6 @@
WebContent#0
iframe#0: remote WebContent#1
- iframe#1: remote WebContent#2
- iframe#2: remote WebContent#3
- iframe#3: remote WebContent#4
- iframe#4: remote WebContent#5
+ iframe#1: remote WebContent#1
+ iframe#2: remote WebContent#1
+ iframe#3: remote WebContent#2
+ iframe#4: remote WebContent#3
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.txt
new file mode 100644
index 00000000000..e6ca9f56d8f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.txt
@@ -0,0 +1 @@
+{"popupIsSandboxed":true,"popupIsInProcessOfInitiator":false}
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/opaque-origin-iframe-is-hosted-with-its-initiator.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/opaque-origin-iframe-is-hosted-with-its-initiator.html
index 85b8c5ca1d8..3876b1a630d 100644
--- a/Tests/LibWeb/Text/input/SiteIsolation/iframe/opaque-origin-iframe-is-hosted-with-its-initiator.html
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/opaque-origin-iframe-is-hosted-with-its-initiator.html
@@ -1,20 +1,10 @@
<!DOCTYPE html>
<script src="../../include.js"></script>
<script>
- // A document with an opaque origin is in an agent cluster of its own, hosted with the agent cluster of the
- // navigation's initiator origin: a data: URL iframe and a sandboxed cross-site iframe stay with their parent.
+ // A document with an opaque origin that is made from what its creator holds is in an agent cluster of its own,
+ // hosted with the agent cluster of the navigation's initiator origin: a data: URL iframe and a sandboxed srcdoc
+ // iframe stay with their parent.
promiseTest(async () => {
- const httpServer = httpTestServer();
-
- const sandboxedUrl = new URL(
- await httpServer.createEcho("GET", "/oop-sandboxed-child", {
- status: 200,
- headers: { "Content-Type": "text/html" },
- body: "<!DOCTYPE html>sandboxed",
- })
- );
- sandboxedUrl.hostname = uniqueLocalhostHostname("oop-sandboxed-child");
-
const dataIframe = document.createElement("iframe");
const dataLoaded = new Promise(resolve => dataIframe.addEventListener("load", resolve, { once: true }));
dataIframe.src = "data:text/html,<p>data</p>";
@@ -23,7 +13,7 @@
const sandboxedIframe = document.createElement("iframe");
sandboxedIframe.sandbox = "";
const sandboxedLoaded = new Promise(resolve => sandboxedIframe.addEventListener("load", resolve, { once: true }));
- sandboxedIframe.src = sandboxedUrl;
+ sandboxedIframe.srcdoc = "<!DOCTYPE html>sandboxed";
document.body.appendChild(sandboxedIframe);
await Promise.all([dataLoaded, sandboxedLoaded]);
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.html
new file mode 100644
index 00000000000..7ec3e7f28e2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/popup-escaping-its-sandboxed-opener-leaves-its-process.html
@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<style>
+ iframe {
+ display: block;
+ width: 200px;
+ height: 100px;
+ border: 0;
+ }
+</style>
+<script>
+ // A popup that escapes the sandbox of the iframe opening it holds a document with an origin of its own, so it does
+ // not stay in the process that hosts the sandboxed iframes of a site.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+
+ const popupUrl = new URL(
+ await httpServer.createEcho("GET", "/sandbox-escaping-popup", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><script>
+ opener.postMessage({ origin: self.origin, processId: internals.webContentProcessId() }, "*");
+ <\/script>`,
+ })
+ );
+ popupUrl.hostname = uniqueLocalhostHostname("sandbox-escaping-popup");
+
+ const iframeUrl = new URL(
+ await httpServer.createEcho("GET", "/sandbox-escaping-popup-opener", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><script>
+ addEventListener("mousedown", () => {
+ window.popup = window.open(${JSON.stringify(popupUrl.href)}, "escaping-its-sandboxed-opener");
+ });
+ addEventListener("message", event => {
+ parent.postMessage({
+ popupHasOriginOfItsOwn: event.data.origin !== "null",
+ popupIsInProcessOfSandboxedOpener: event.data.processId === internals.webContentProcessId(),
+ }, "*");
+ window.popup.close();
+ });
+ parent.postMessage("ready", "*");
+ <\/script>`,
+ })
+ );
+ iframeUrl.hostname = uniqueLocalhostHostname("sandbox-escaping-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 allow-popups-to-escape-sandbox";
+ 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>
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.html
new file mode 100644
index 00000000000..c9a7d1305fa
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/top-level-sandboxed-document-leaves-the-process-of-its-initiator.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 top-level document that a response sandboxes holds another site's content, so it does not stay in the process
+ // of the document that navigated to it.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+
+ const popupUrl = new URL(
+ await httpServer.createEcho("GET", "/csp-sandboxed-popup", {
+ status: 200,
+ headers: { "Content-Type": "text/html", "Content-Security-Policy": "sandbox allow-scripts" },
+ body: `<!DOCTYPE html><script>
+ opener.postMessage({ origin: self.origin, processId: internals.webContentProcessId() }, "*");
+ <\/script>`,
+ })
+ );
+ popupUrl.hostname = uniqueLocalhostHostname("csp-sandboxed-popup");
+
+ const iframeUrl = new URL(
+ await httpServer.createEcho("GET", "/csp-sandboxed-popup-opener", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><script>
+ addEventListener("mousedown", () => {
+ window.popup = window.open(${JSON.stringify(popupUrl.href)}, "csp-sandboxed-popup");
+ });
+ addEventListener("message", event => {
+ parent.postMessage({
+ popupIsSandboxed: event.data.origin === "null",
+ popupIsInProcessOfInitiator: event.data.processId === internals.webContentProcessId(),
+ }, "*");
+ window.popup.close();
+ });
+ parent.postMessage("ready", "*");
+ <\/script>`,
+ })
+ );
+ iframeUrl.hostname = uniqueLocalhostHostname("csp-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.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>