Skip to content

Truly independent web browser

LibWeb+LibWebView: Check familiarity and activation of remote close()

Shannon Booth0203ab20ca96
diff --git a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
index 9af02a0ea39..23ee290ef7f 100644
--- a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
@@ -234,7 +234,7 @@ void LocalTraversableNavigable::close_top_level_traversable_from_script()
// - thisTraversable is script-closable;
// - the incumbent global object's browsing context is familiar with browsingContext; and
// - the incumbent global object's navigable is allowed by sandboxing to navigate thisTraversable, given sourceSnapshotParams,
- // NB: The requesting process found these true, and the UI process checked again the ones it holds the state for.
+ // NB: The requesting process found these true, and the UI process checked them again.
// then:
// 1. Set thisTraversable's is closing to true.
this_traversable.set_closing(true);
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 6946a5379ce..4fce57939ba 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -518,6 +518,41 @@ bool CanonicalNavigable::is_ancestor_of(CanonicalNavigable const& potential_desc
return false;
}
+// https://html.spec.whatwg.org/multipage/document-sequences.html#familiar-with
+// NB: Stated on the navigables whose active browsing contexts are compared, as in the renderer.
+bool CanonicalNavigable::is_familiar_with(CanonicalNavigable const& other) const
+{
+ // A browsing context A is familiar with a second browsing context B if the following algorithm returns true:
+ auto const& A = *this;
+ auto const& B = other;
+
+ // 1. If A's active document's origin is same origin with B's active document's origin, then return true.
+ if (A.active_document().origin().is_same_origin(B.active_document().origin()))
+ return true;
+
+ // 2. If A's top-level browsing context is B, then return true.
+ if (&A.top_level_traversable() == &B)
+ return true;
+
+ // 3. If B is an auxiliary browsing context and A is familiar with B's opener browsing context, then return true.
+ if (B.active_browsing_context().is_auxiliary()) {
+ if (auto opener = B.active_browsing_context().opener_browsing_context()) {
+ if (auto const* opener_navigable = CanonicalTraversable::navigable_with_active_browsing_context(*opener); opener_navigable && A.is_familiar_with(*opener_navigable))
+ return true;
+ }
+ }
+
+ // 4. If there exists an ancestor browsing context of B whose active document has the same origin as the active document of A, then return true.
+ // NOTE: This includes the case where A is an ancestor browsing context of B.
+ for (auto const* ancestor = B.parent(); ancestor; ancestor = ancestor->parent()) {
+ if (ancestor->active_document().origin().is_same_origin(A.active_document().origin()))
+ return true;
+ }
+
+ // 5. Return false.
+ return false;
+}
+
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#allowed-to-navigate
bool CanonicalNavigable::allowed_by_sandboxing_to_navigate(CanonicalNavigable const& target, Web::InitiatorSourceSnapshot const& source_snapshot_params) const
{
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index b554422f909..ea9e1232b0b 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -126,6 +126,7 @@ public:
NonnullOwnPtr<CanonicalNavigable> remove_child(CanonicalNavigable&);
bool is_ancestor_of(CanonicalNavigable const&) const;
bool allowed_by_sandboxing_to_navigate(CanonicalNavigable const& target, Web::InitiatorSourceSnapshot const& source_snapshot_params) const;
+ bool is_familiar_with(CanonicalNavigable const&) const;
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#snapshotting-target-snapshot-params
Web::HTML::TargetSnapshotParams snapshot_target_snapshot_params() const;
IterationDecision for_each_in_inclusive_subtree(Function<IterationDecision(CanonicalNavigable&)> const&);
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index d037ffaaef5..f7c317c1857 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1794,21 +1794,31 @@ void WebContentPage::did_request_close_of_traversable(Web::HTML::CrossProcessId
// The process closing it must host the navigable it closes from. The request comes through the page holding the
// traversable's tab, which is not the source's when the two are different tabs.
- auto source = client().hosted_navigable(source_navigable_id);
+ Optional<CanonicalNavigable&> source;
+ RefPtr<WebContentPage> source_page;
+ client().for_each_page([&](WebContentPage& page) {
+ source = page.hosted_navigable(source_navigable_id);
+ if (!source.has_value())
+ return IterationDecision::Continue;
+ source_page = page;
+ return IterationDecision::Break;
+ });
if (!source.has_value())
return;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-close
// 6. If all the following are true:
// - thisTraversable is script-closable;
+ // - the incumbent global object's browsing context is familiar with browsingContext; and
// - the incumbent global object's navigable is allowed by sandboxing to navigate thisTraversable, given sourceSnapshotParams,
// NB: The requesting process found these true. The UI process checks them again with the state it holds, including
- // the sandboxing flags of the incumbent global object's associated Document.
+ // the sandboxing flags of the incumbent global object's associated Document and the activation of the input it
+ // delivered to the page hosting it.
Web::InitiatorSourceSnapshot source_snapshot_params {
.sandboxing_flags = source_snapshot.sandboxing_flags | source->active_document().active_sandboxing_flag_set(),
- .has_transient_activation = source_snapshot.has_transient_activation,
+ .has_transient_activation = source_snapshot.has_transient_activation && source_page->has_transient_user_activation(),
};
- if (!traversable().is_script_closable() || !source->allowed_by_sandboxing_to_navigate(traversable(), source_snapshot_params))
+ if (!traversable().is_script_closable() || !source->is_familiar_with(traversable()) || !source->allowed_by_sandboxing_to_navigate(traversable(), source_snapshot_params))
return;
auto endpoint = endpoint_hosting_navigable_represented_by(navigable_id);