Skip to content

Truly independent web browser

LibWeb: Mute an iframe's load event after it reopens its document

Shannon Boothc4e359f85c02
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 79886e3ec36..d8a1a2269f7 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1430,7 +1430,10 @@ WebIDL::ExceptionOr<Document*> Document::open(Optional<Utf16String> const&, Opti
// 13. Set document's is initial about:blank to false.
set_is_initial_about_blank(false);
- // FIXME: 14. If document's iframe load in progress flag is set, then set document's mute iframe load flag.
+ // 14. If document's iframe load in progress flag is set, then set document's mute iframe load flag.
+ // AD-HOC: Other engines only skip the load of a document opened during its iframe load, so opening it again
+ // later unmutes it. See https://github.com/whatwg/html/issues/4292
+ set_mute_iframe_load(iframe_load_in_progress());
// 15. Set document to no-quirks mode.
set_quirks_mode(QuirksMode::No);
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 4361397f9ad..05212ac70f2 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1010,6 +1010,12 @@ public:
bool is_initial_about_blank() const { return m_is_initial_about_blank; }
void set_is_initial_about_blank(bool b) { m_is_initial_about_blank = b; }
+ bool iframe_load_in_progress() const { return m_iframe_load_in_progress; }
+ void set_iframe_load_in_progress(bool value) { m_iframe_load_in_progress = value; }
+
+ bool mute_iframe_load() const { return m_mute_iframe_load; }
+ void set_mute_iframe_load(bool value) { m_mute_iframe_load = value; }
+
HTML::CrossProcessId id() const { return m_id.value(); }
void set_id(HTML::CrossProcessId);
@@ -2005,6 +2011,13 @@ private:
// https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
bool m_is_initial_about_blank { false };
+
+ // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-in-progress
+ bool m_iframe_load_in_progress { false };
+
+ // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#mute-iframe-load
+ bool m_mute_iframe_load { false };
+
Optional<HTML::CrossProcessId> m_id;
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-about-base-url
diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
index e1ad235e529..8c2663ae42c 100644
--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
@@ -241,8 +241,15 @@ void run_iframe_load_event_steps(HTMLIFrameElement& element)
}
}
- // FIXME: 2. Let childDocument be element's content navigable's active document.
- // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
+ // 2. Let childDocument be element's content navigable's active document.
+ // NB: A document hosted by another process can't be opened from this one, so it is never muted.
+ GC::Ptr<DOM::Document> child_document;
+ if (auto* local_navigable = as_if<LocalNavigable>(*element.content_navigable()))
+ child_document = local_navigable->active_document();
+
+ // 3. If childDocument has its mute iframe load flag set, then return.
+ if (child_document && child_document->mute_iframe_load())
+ return;
// 4. If element's pending resource-timing start time is not null:
if (element.pending_resource_start_time().has_value()) {
@@ -278,12 +285,18 @@ void run_iframe_load_event_steps(HTMLIFrameElement& element)
element.set_pending_resource_timing_url({});
}
- // 5. Fire an event named load at element.
+ // 5. Set childDocument's iframe load in progress flag.
+ if (child_document)
+ child_document->set_iframe_load_in_progress(true);
+
+ // 6. Fire an event named load at element.
element.dispatch_event(DOM::Event::create(
HTML::EventNames::load,
HighResolutionTime::current_high_resolution_time(relevant_global_object(element))));
- // FIXME: 7. Unset childDocument's iframe load in progress flag.
+ // 7. Unset childDocument's iframe load in progress flag.
+ if (child_document)
+ child_document->set_iframe_load_in_progress(false);
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
diff --git a/Tests/LibWeb/Text/expected/HTML/document-open-during-iframe-load-mutes-only-that-load.txt b/Tests/LibWeb/Text/expected/HTML/document-open-during-iframe-load-mutes-only-that-load.txt
new file mode 100644
index 00000000000..25ab738b0fd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/document-open-during-iframe-load-mutes-only-that-load.txt
@@ -0,0 +1 @@
+Load 2: Reopened after the load event
diff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/inheriting-csp-for-local-schemes.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/inheriting-csp-for-local-schemes.txt
new file mode 100644
index 00000000000..55159baffa7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/inheriting-csp-for-local-schemes.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 10 tests
+
+10 Pass
+Pass require-trusted-types-for directive should be inherited in local srcdoc frames
+Pass trusted-types directive should be inherited in local srcdoc frames
+Pass require-trusted-types-for directive should be inherited in local data frames
+Pass trusted-types directive should be inherited in local data frames
+Pass require-trusted-types-for directive should be inherited in local blob frames
+Pass trusted-types directive should be inherited in local blob frames
+Pass require-trusted-types-for directive should be inherited in local about:blank frames
+Pass trusted-types directive should be inherited in local about:blank frames
+Pass require-trusted-types-for directive should not be inherited in local blank.html frames
+Pass trusted-types directive should not be inherited in local blank.html frames
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/document-open-during-iframe-load-mutes-only-that-load.html b/Tests/LibWeb/Text/input/HTML/document-open-during-iframe-load-mutes-only-that-load.html
new file mode 100644
index 00000000000..8aaeb68c785
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/document-open-during-iframe-load-mutes-only-that-load.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const iframe = document.createElement("iframe");
+ let loadCount = 0;
+ iframe.addEventListener("load", () => {
+ ++loadCount;
+ const childDocument = iframe.contentDocument;
+ if (loadCount > 1) {
+ println(`Load ${loadCount}: ${childDocument.body.textContent}`);
+ iframe.remove();
+ done();
+ return;
+ }
+
+ childDocument.open();
+ childDocument.addEventListener("readystatechange", () => {
+ if (childDocument.readyState !== "complete")
+ return;
+ setTimeout(() => {
+ childDocument.open();
+ childDocument.write("<body>Reopened after the load event</body>");
+ childDocument.close();
+ });
+ });
+ childDocument.write("<body>Reopened during the load event</body>");
+ childDocument.close();
+ });
+ iframe.srcdoc = "Initial document";
+ document.body.appendChild(iframe);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-delays-parent-load.html b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-delays-parent-load.html
index 127cb5b1f08..4799b28627b 100644
--- a/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-delays-parent-load.html
+++ b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-delays-parent-load.html
@@ -13,11 +13,11 @@
const parentDocument = parentIframe.contentDocument;
parentDocument.open().write('<iframe id="child" srcdoc="Initial child document"></iframe>');
const childIframe = parentDocument.querySelector("#child");
- const initialChildLoad = new Promise(resolve => childIframe.addEventListener("load", resolve, { once: true }));
+ const initialChildLoad = new Promise(resolve => childIframe.addEventListener("load", () => setTimeout(resolve), { once: true }));
await initialChildLoad;
const childDocument = childIframe.contentDocument;
- const firstChildLoad = new Promise(resolve => childIframe.addEventListener("load", resolve, { once: true }));
+ const firstChildLoad = new Promise(resolve => childIframe.addEventListener("load", () => setTimeout(resolve), { once: true }));
childDocument.open().write("<body>First child document</body>");
childDocument.close();
await firstChildLoad;
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html b/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html
new file mode 100644
index 00000000000..5b4380da907
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html
@@ -0,0 +1,127 @@
+<!DOCTYPE html>
+<meta charset="UTF-8">
+<link rel="help" href="https://w3c.github.io/webappsec-csp/#security-inherit-csp">
+<!--
+ The following policies from inheriting-csp-for-local-schemes.html.headers
+ should be inherited by the subframes using local schemes:
+ - Content-Security-Policy: trusted-types allowedPolicy passThrough
+ - Content-Security-Policy: require-trusted-types-for 'script'
+ -->
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<div id="log"></div>
+<script>
+ const passThroughPolicy = trustedTypes.createPolicy("passThrough", {
+ createHTML: s => s
+ });
+
+ function postMessageAndWaitReplyFromIframe(iframe, data) {
+ return new Promise(resolve => {
+ window.addEventListener("message", e => resolve(e.data), {once: true});
+ iframe.contentWindow.postMessage(data, '*');
+ });
+ }
+ const subframeHTML = `<!DOCTYPE html>
+<script>onmessage = (event) => {
+ try {
+ switch(event.data.fn) {
+ case 'setInnerHTML':
+ document.createElement("div").innerHTML = event.data.arg;
+ break;
+ case 'createPolicy':
+ trustedTypes.createPolicy(event.data.arg);
+ break;
+ }
+ parent.postMessage(null, '*');
+ } catch(e) {
+ parent.postMessage(e, '*');
+ }
+}\u003C/script>`
+
+ let iframes = [];
+
+ let iframe1 = document.createElement("iframe");
+ iframe1.dataset.urlType = "srcdoc";
+ iframe1.dataset.inherits = true;
+ iframe1.srcdoc = passThroughPolicy.createHTML(`<p>srcdoc</p>${subframeHTML}`);
+ iframes.push(iframe1);
+
+ let iframe2 = document.createElement("iframe");
+ iframe2.dataset.urlType = "data";
+ iframe2.dataset.inherits = true;
+ iframe2.src = `data:text/html,<p>data</p>${subframeHTML}`;
+ iframes.push(iframe2);
+
+ let iframe3 = document.createElement("iframe");
+ iframe3.dataset.urlType = "blob";
+ iframe3.dataset.inherits = true;
+ iframe3.src = URL.createObjectURL(new Blob([`<p>blob</p>${subframeHTML}`],
+ {type: 'text/html'}));
+ iframes.push(iframe3);
+
+ let iframe4 = document.createElement("iframe");
+ iframe4.dataset.urlType = "about:blank";
+ iframe4.dataset.inherits = true;
+ iframe4.src = "about:blank";
+ iframe4.onload = _ => {
+ let iframeDocument = iframe4.contentWindow.document;
+ iframeDocument.open();
+ iframeDocument.write(passThroughPolicy.createHTML(
+ `<p>overwritten about:blank</p>${subframeHTML}`));
+ iframeDocument.close();
+ }
+ iframes.push(iframe4);
+
+ let iframe5 = document.createElement("iframe");
+ iframe5.dataset.urlType = "blank.html";
+ iframe5.dataset.inherits = false;
+ iframe5.src = "../common/blank.html";
+ iframe5.onload = _ => {
+ let iframeDocument = iframe5.contentWindow.document;
+ iframeDocument.open();
+ iframeDocument.write(passThroughPolicy.createHTML(
+ `<p>overwritten blank.html</p>${subframeHTML}`));
+ iframeDocument.close();
+ }
+ iframes.push(iframe5);
+
+ document.addEventListener("DOMContentLoaded", _ =>
+ iframes.forEach(iframe => document.body.appendChild(iframe))
+ );
+
+ promise_setup(_ => new Promise(resolve =>
+ window.addEventListener("load", resolve)));
+
+ iframes.forEach(iframe => {
+ const inherits = iframe.dataset.inherits === "true";
+ const whetherItIsInherited = `${inherits ? "should" : "should not"} be inherited`;
+
+ promise_test(async _ => {
+ let exception = await postMessageAndWaitReplyFromIframe(iframe, {
+ fn: "setInnerHTML", arg: "unsafe"});
+ if (inherits) {
+ assert_not_equals(exception, null);
+ assert_true(exception instanceof TypeError);
+ } else {
+ assert_equals(exception, null);
+ }
+ }, `require-trusted-types-for directive ${whetherItIsInherited} in local ${iframe.dataset.urlType} frames`);
+
+ promise_test(async _ => {
+ let exception1 = await postMessageAndWaitReplyFromIframe(iframe, {
+ fn: "createPolicy", arg: "allowedPolicy",
+ });
+ assert_equals(exception1, null);
+
+ let exception2 = await postMessageAndWaitReplyFromIframe(iframe, {
+ fn: "createPolicy", arg: "forbiddenPolicy",
+ });
+ if (inherits) {
+ assert_not_equals(exception2, null);
+ assert_true(exception2 instanceof TypeError);
+ } else {
+ assert_equals(exception2, null);
+ }
+ }, `trusted-types directive ${whetherItIsInherited} in local ${iframe.dataset.urlType} frames`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html.headers b/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html.headers
new file mode 100644
index 00000000000..a4e416b2bca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/inheriting-csp-for-local-schemes.html.headers
@@ -0,0 +1,2 @@
+Content-Security-Policy: trusted-types allowedPolicy passThrough
+Content-Security-Policy: require-trusted-types-for 'script'