Skip to content

Truly independent web browser

Compositor: Leave a context parentless if its parent context is gone

Andreas Kling9c5fa0906865
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index 7fde6a058a4..df1e213e90c 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -169,7 +169,13 @@ void CompositorState::set_parent_context(Web::CompositorContextId context_id, Op
if (parent_context_id.has_value()) {
VERIFY(!context->presents_to_client());
VERIFY(*parent_context_id != context_id);
- VERIFY(context_if_present(*parent_context_id));
+ // The parent's context belongs to the process hosting the parent, which can destroy it before this context's
+ // process hears that the parent is gone. The context is left without a parent then, as destroying the parent
+ // would have left it.
+ // NB: The UI process creates a context here before it hands the context's ID out, so a parent this doesn't know
+ // has been destroyed.
+ if (!context_if_present(*parent_context_id))
+ parent_context_id = {};
}
auto current_parent_context_id = context->parent_context_id();
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.txt
new file mode 100644
index 00000000000..3da67ab4891
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.txt
@@ -0,0 +1 @@
+all iframes removed
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html
new file mode 100644
index 00000000000..3676c73969b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html
@@ -0,0 +1,60 @@
+<!doctype html>
+<script src="../../include.js"></script>
+<script>
+ // Each iframe's child navigates to a site another process hosts, and the iframe is removed while the child's
+ // process attaches its compositor context to the iframe's. The iframe's context can be gone by then.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+ const FRAME_COUNT = 6;
+
+ const grandchildBody = "<!DOCTYPE html>grandchild";
+ const sameSiteGrandchildUrl = await httpServer.createEcho("GET", "/stale-parent-context-grandchild", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: grandchildBody,
+ });
+ const crossSiteGrandchildUrl = new URL(sameSiteGrandchildUrl);
+ crossSiteGrandchildUrl.hostname = uniqueLocalhostHostname("stale-parent-context-grandchild");
+
+ const middleUrl = await httpServer.createEcho("GET", "/stale-parent-context-middle", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><body><script>
+ const id = +location.hash.slice(1);
+ const child = document.createElement("iframe");
+ child.src = ${JSON.stringify(sameSiteGrandchildUrl)};
+ child.onload = () => {
+ child.onload = null;
+ child.src = ${JSON.stringify(crossSiteGrandchildUrl.href)};
+ setTimeout(() => parent.postMessage(id, "*"), id);
+ };
+ document.body.appendChild(child);
+ <\/script>`,
+ });
+
+ const frames = [];
+ let removedCount = 0;
+ const allRemoved = new Promise(resolve => {
+ addEventListener("message", event => {
+ frames[event.data].remove();
+ if (++removedCount === FRAME_COUNT)
+ resolve();
+ });
+ });
+ for (let i = 0; i < FRAME_COUNT; ++i) {
+ const frame = document.createElement("iframe");
+ frame.src = `${middleUrl}#${i}`;
+ frames.push(frame);
+ document.body.appendChild(frame);
+ }
+ await allRemoved;
+
+ if (!(await waitForCondition(() => remoteFrameCount() === 0))) {
+ println("frame tree still has remote frames");
+ return;
+ }
+ await animationFrame();
+ await animationFrame();
+ println("all iframes removed");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html.headers b/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/iframe-removed-while-its-child-moves-to-another-process.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html