Skip to content

Truly independent web browser

LibWeb: Handle parents without a document in cross-site ancestor checks

Andreas Klinga8663921dd7c
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index ee7a74874fb..e7e03e2a8b1 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -1522,7 +1522,10 @@ Optional<URL::Origin> LocalNavigable::active_document_top_level_origin() const
// initial about:blank populates its next document, and once this navigable is destroyed.
bool LocalNavigable::active_document_has_cross_site_ancestor() const
{
- VERIFY(m_active_document);
+ // NB: A navigable whose document was destroyed has no active document, while the navigables of iframes removed as
+ // that document unloaded still have theirs. Conservatively treat it as a third-party context.
+ if (!m_active_document)
+ return true;
// 1. If window's navigable's parent is null, then return false.
auto parent = this->parent();
diff --git a/Tests/LibWeb/Text/expected/Fetch/fetch-from-iframe-removed-while-its-popup-closes.txt b/Tests/LibWeb/Text/expected/Fetch/fetch-from-iframe-removed-while-its-popup-closes.txt
new file mode 100644
index 00000000000..7ef22e9a431
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/fetch-from-iframe-removed-while-its-popup-closes.txt
@@ -0,0 +1 @@
+PASS
diff --git a/Tests/LibWeb/Text/input/Fetch/fetch-from-iframe-removed-while-its-popup-closes.html b/Tests/LibWeb/Text/input/Fetch/fetch-from-iframe-removed-while-its-popup-closes.html
new file mode 100644
index 00000000000..24b53b4f617
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/fetch-from-iframe-removed-while-its-popup-closes.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const popup = window.open("about:blank");
+ const popupDocument = popup.document;
+
+ // An iframe removed while the popup's document unloads is still waiting for its own document to be unloaded
+ // when the popup's document is destroyed, which leaves its navigable with a parent that has no active document.
+ let iframeWindow;
+ popup.addEventListener("unload", () => {
+ const iframe = popupDocument.createElement("iframe");
+ popupDocument.body.appendChild(iframe);
+ iframeWindow = iframe.contentWindow;
+ iframe.remove();
+ });
+ popup.close();
+
+ while (popupDocument.defaultView)
+ await new Promise(resolve => setTimeout(resolve, 0));
+
+ // Fetching snapshots the iframe window's settings, including its cross-site ancestor status, which is derived
+ // from the active document of its navigable's parent.
+ // NB: The fetch is not awaited, since the iframe's document is no longer fully active.
+ iframeWindow.fetch("data:text/plain,").catch(() => {});
+ println("PASS");
+ done();
+ });
+</script>