Truly independent web browser
LibWeb: Limit how deep frames can nest
Andreas Kling04547665ade2
diff --git a/Libraries/LibWeb/HTML/Navigable.cpp b/Libraries/LibWeb/HTML/Navigable.cppindex 00c26492124..ddc63010fc4 100644--- a/Libraries/LibWeb/HTML/Navigable.cpp+++ b/Libraries/LibWeb/HTML/Navigable.cpp@@ -5,6 +5,7 @@*/#include <LibWeb/DOM/Document.h>+#include <LibWeb/HTML/BrowsingContext.h>#include <LibWeb/HTML/LocalNavigable.h>#include <LibWeb/HTML/Navigable.h>#include <LibWeb/HTML/NavigableContainer.h>@@ -177,6 +178,26 @@ GC::Ref<Navigable> Navigable::top_level_traversable()return navigable;}+// INTEROP: The spec only stops a frame from loading the URL of an ancestor, so a page that gives each copy of itself a+// new URL nests without end. Like other engines, we limit how deep frames can load documents. URLs that fetch+// nothing stay allowed at any depth.+bool Navigable::is_too_deeply_nested_to_load(URL::URL const& url, MayNavigateToAFragment may_navigate_to_a_fragment) const+{+ static constexpr size_t maximum_depth = 10;++ if (url_matches_about_blank(url) || url_matches_about_srcdoc(url) || url.scheme() == "javascript"sv)+ return false;++ // A navigation to a fragment of the active document stays in that document.+ if (auto active_url = active_document_url(); may_navigate_to_a_fragment == MayNavigateToAFragment::Yes && url.fragment().has_value() && active_url.has_value() && url.equals(*active_url, URL::ExcludeFragment::Yes))+ return false;++ size_t depth = 1;+ for (auto ancestor = parent(); ancestor; ancestor = ancestor->parent())+ ++depth;+ return depth > maximum_depth;+}+// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigateWebIDL::ExceptionOr<void> Navigable::navigate(NavigateParams params){@@ -226,6 +247,12 @@ WebIDL::ExceptionOr<void> Navigable::navigate(NavigateParams params)}}+ // AD-HOC: See is_too_deeply_nested_to_load(). Only a navigation without a document resource or response can be one+ // to a fragment; a POST to a URL with a fragment still loads a document.+ auto may_navigate_to_a_fragment = params.document_resource.has<Empty>() && !params.response ? MayNavigateToAFragment::Yes : MayNavigateToAFragment::No;+ if (is_too_deeply_nested_to_load(params.url, may_navigate_to_a_fragment))+ return {};+// AD-HOC: Nothing the browser process can see names this URL's blob URL entry until the navigation commits, so// ask it to keep the entry until then.if (auto const& blob_url_entry = params.url.blob_url_entry(); blob_url_entry.has_value() && source_document) {diff --git a/Libraries/LibWeb/HTML/Navigable.h b/Libraries/LibWeb/HTML/Navigable.hindex 139eda52895..d5c5af2bf1e 100644--- a/Libraries/LibWeb/HTML/Navigable.h+++ b/Libraries/LibWeb/HTML/Navigable.h@@ -112,6 +112,12 @@ public:bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&) const;+ enum class MayNavigateToAFragment : bool {+ No,+ Yes,+ };+ bool is_too_deeply_nested_to_load(URL::URL const&, MayNavigateToAFragment = MayNavigateToAFragment::Yes) const;+protected:explicit Navigable(GC::Ref<Page>);void set_id(CrossProcessId id) { m_id = id; }diff --git a/Libraries/LibWeb/HTML/Navigation.cpp b/Libraries/LibWeb/HTML/Navigation.cppindex f59b68e4e64..ff00436c5da 100644--- a/Libraries/LibWeb/HTML/Navigation.cpp+++ b/Libraries/LibWeb/HTML/Navigation.cpp@@ -287,6 +287,10 @@ WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(Utf16String url, Bindif (document.unload_counter() > 0)return early_error_result(WebIDL::InvalidStateError::create("Document already unloaded"_utf16));+ // AD-HOC: navigate() ignores this navigation, which would leave the promises unsettled.+ if (document.navigable()->is_too_deeply_nested_to_load(*url_record))+ return early_error_result(WebIDL::AbortError::create("Frame is nested too deeply"_utf16));+// 10. Let info be options["info"], if it exists; otherwise, undefined.auto info = options.info.value_or(JS::js_undefined());diff --git a/Tests/LibWeb/Text/data/nested-iframe-with-distinct-url.html b/Tests/LibWeb/Text/data/nested-iframe-with-distinct-url.htmlindex 4e33597f3ec..2a01820aa3a 100644--- a/Tests/LibWeb/Text/data/nested-iframe-with-distinct-url.html+++ b/Tests/LibWeb/Text/data/nested-iframe-with-distinct-url.html@@ -15,7 +15,16 @@return;}const blankIframe = document.createElement("iframe");- blankIframe.onload = () => top.postMessage({ depth }, "*");+ blankIframe.onload = () => {+ // An opened document takes our URL, so this is a navigation to a fragment of it.+ blankIframe.contentDocument.open();+ blankIframe.contentDocument.close();+ blankIframe.contentWindow.location.hash = "fragment";+ const fragmentNavigated = blankIframe.contentWindow.location.hash === "#fragment";+ iframe.contentWindow.navigation.navigate(nextURL).committed.then(+ () => top.postMessage({ depth, fragmentNavigated, navigation: "committed" }, "*"),+ error => top.postMessage({ depth, fragmentNavigated, navigation: error.name }, "*"));+ };document.documentElement.appendChild(blankIframe);};</script>diff --git a/Tests/LibWeb/Text/expected/HTML/iframe-nesting-depth-limit.txt b/Tests/LibWeb/Text/expected/HTML/iframe-nesting-depth-limit.txtindex ba34c589ff0..48e8305fa30 100644--- a/Tests/LibWeb/Text/expected/HTML/iframe-nesting-depth-limit.txt+++ b/Tests/LibWeb/Text/expected/HTML/iframe-nesting-depth-limit.txt@@ -1 +1,3 @@-Deepest nested document: 30+Deepest nested document: 9+Fragment navigation of a frame below it: true+Navigation of the frame below it: AbortErrordiff --git a/Tests/LibWeb/Text/input/HTML/iframe-nesting-depth-limit.html b/Tests/LibWeb/Text/input/HTML/iframe-nesting-depth-limit.htmlindex ea08cde46cb..632a5217f1d 100644--- a/Tests/LibWeb/Text/input/HTML/iframe-nesting-depth-limit.html+++ b/Tests/LibWeb/Text/input/HTML/iframe-nesting-depth-limit.html@@ -5,6 +5,8 @@asyncTest(done => {window.onmessage = event => {println(`Deepest nested document: ${event.data.depth}`);+ println(`Fragment navigation of a frame below it: ${event.data.fragmentNavigated}`);+ println(`Navigation of the frame below it: ${event.data.navigation}`);done();};window.addEventListener("load", () => frame.contentWindow.postMessage("probe", "*"));