Skip to content

Truly independent web browser

LibWeb+LibWebCommon+LibWebView: Check CORP on nested navigations

Shannon Booth2aa3d5a52e74
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 788f3f386d6..6464f4e0cef 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -41,6 +41,7 @@
#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/Editing/SpellCheckController.h>
+#include <LibWeb/Fetch/Fetching/Checks.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
@@ -2347,11 +2348,31 @@ static void perform_navigation_params_fetch(JS::Realm& realm, GC::Ref<Navigation
}
}
- // 13. FIXME: If response is not a network error, navigable is a child navigable, and the result of performing a cross-origin resource policy check
- // with navigable's container document's origin, navigable's container document's relevant settings object, request's destination, response,
- // and true is blocked, then set response to a network error and break.
- // NOTE: Here we're running the cross-origin resource policy check against the parent navigable rather than navigable itself
+ // 13. If all of the following are true:
+ // - response is not a network error;
+ // - navigable is a child navigable;
+ // - response's URL's scheme is an HTTP(S) scheme; and
+ // - the result of performing a cross-origin resource policy check with navigable's container document's origin, navigable's container
+ // document's relevant settings object, request's destination, response, and true is blocked,
+ // then set response to a network error and break.
+ // NOTE: Here we're running the cross-origin resource policy check against the parent navigable rather than navigable itself.
// This is because we care about the same-originness of the embedded content against the parent context, not the navigation source.
+ // We only run it for HTTP(S) scheme responses, as in Fetch; other responses are still subject to checking a navigation response's
+ // adherence to its embedder policy.
+ // NB: The container document is the parent navigable's active document, which can be hosted in another process.
+ if (!state_holder->response->is_network_error() && Fetch::Infrastructure::is_http_or_https_scheme(state_holder->response->url()->scheme())) {
+ if (auto parent = state_holder->navigable->parent()) {
+ auto container_document_origin = parent->active_document_origin();
+ if (container_document_origin.has_value()
+ && Fetch::Fetching::cross_origin_resource_policy_check(*container_document_origin, state_holder->navigable->container_document_embedder_policy(), *state_holder->response, Fetch::Fetching::ForNavigation::Yes) == Fetch::Infrastructure::RequestOrResponseBlocking::Blocked) {
+ // AD-HOC: Stop the fetch, as nothing will consume the response this replaces.
+ state_holder->fetch_controller->stop_fetch();
+ state_holder->response = Fetch::Infrastructure::Response::network_error(realm.vm(), "Blocked by Cross-Origin-Resource-Policy"_string);
+ fetch_completion_steps->function()();
+ return;
+ }
+ }
+ }
// 14. Set locationURL to response's location URL given currentURL's fragment.
auto location_url_without_an_inherited_fragment = state_holder->response->location_url({});
diff --git a/Libraries/LibWeb/HTML/Navigable.h b/Libraries/LibWeb/HTML/Navigable.h
index 08b0899a665..0d3d0fb6f5f 100644
--- a/Libraries/LibWeb/HTML/Navigable.h
+++ b/Libraries/LibWeb/HTML/Navigable.h
@@ -89,6 +89,7 @@ public:
bool container_is_in_document_tree() const { return container_state().is_in_document_tree; }
SandboxingFlagSet container_iframe_sandboxing_flag_set() const { return container_state().iframe_sandboxing_flag_set; }
SandboxingFlagSet container_document_active_sandboxing_flag_set() const { return container_state().document_active_sandboxing_flag_set; }
+ EmbedderPolicy container_document_embedder_policy() const { return container_state().document_embedder_policy; }
Optional<Utf16FlyString> container_local_name() const { return container_state().local_name; }
ReferrerPolicy::ReferrerPolicy container_iframe_referrer_policy() const { return container_state().iframe_referrer_policy; }
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cpp
index cfa22054254..b31c83b1234 100644
--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp
+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp
@@ -21,6 +21,7 @@
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/NavigationParams.h>
+#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/RemoteNavigable.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
@@ -604,6 +605,7 @@ ReplicatedContainerState NavigableContainer::replicated_container_state()
if (auto const* iframe = as_if<HTMLIFrameElement>(*this))
state.iframe_sandboxing_flag_set = iframe->iframe_sandboxing_flag_set();
state.document_active_sandboxing_flag_set = document().active_sandboxing_flag_set();
+ state.document_embedder_policy = document().policy_container()->embedder_policy;
state.local_name = local_name();
state.iframe_referrer_policy = determine_iframe_element_referrer_policy(*this);
return state;
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
index c567cd3d008..06a2163b78e 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
@@ -17,6 +17,7 @@ ErrorOr<void> encode(Encoder& encoder, Web::HTML::ReplicatedContainerState const
TRY(encoder.encode(state.is_in_document_tree));
TRY(encoder.encode(state.iframe_sandboxing_flag_set));
TRY(encoder.encode(state.document_active_sandboxing_flag_set));
+ TRY(encoder.encode(state.document_embedder_policy));
TRY(encoder.encode(state.local_name));
TRY(encoder.encode(state.iframe_referrer_policy));
return {};
@@ -29,6 +30,7 @@ ErrorOr<Web::HTML::ReplicatedContainerState> decode(Decoder& decoder)
.is_in_document_tree = TRY(decoder.decode<bool>()),
.iframe_sandboxing_flag_set = TRY(decoder.decode<Web::HTML::SandboxingFlagSet>()),
.document_active_sandboxing_flag_set = TRY(decoder.decode<Web::HTML::SandboxingFlagSet>()),
+ .document_embedder_policy = TRY(decoder.decode<Web::HTML::EmbedderPolicy>()),
.local_name = TRY(decoder.decode<Optional<Utf16FlyString>>()),
.iframe_referrer_policy = TRY(decoder.decode<Web::ReferrerPolicy::ReferrerPolicy>()),
};
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
index 971e398c423..bca96e068ab 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
@@ -15,6 +15,7 @@
#include <LibWebCommon/Export.h>
#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
+#include <LibWebCommon/HTML/EmbedderPolicy.h>
#include <LibWebCommon/HTML/SandboxingFlagSet.h>
#include <LibWebCommon/Page/CompositorContextId.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
@@ -32,6 +33,10 @@ struct ReplicatedContainerState {
// https://html.spec.whatwg.org/multipage/browsers.html#active-sandboxing-flag-set, of the container's node document.
SandboxingFlagSet document_active_sandboxing_flag_set {};
+ // https://html.spec.whatwg.org/multipage/browsers.html#policy-container-embedder-policy, of the container's node
+ // document's policy container.
+ EmbedderPolicy document_embedder_policy {};
+
// The container's local name, which navigate makes its request's destination and initiator type.
Optional<Utf16FlyString> local_name;
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 241bb8a0f4d..23ae99ed986 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -1075,8 +1075,10 @@ void CanonicalNavigable::update_container_state(Web::HTML::ReplicatedContainerSt
{
if (!m_hosted_state.has_value())
return;
- if (m_container_document)
+ if (m_container_document) {
state.document_active_sandboxing_flag_set = m_container_document->active_sandboxing_flag_set();
+ state.document_embedder_policy = m_container_document->embedder_policy();
+ }
m_hosted_state->container = state;
if (has_remote_host())
remote_host().async_update_local_root_container_state(id(), move(state));
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 1e4a2d3cc61..ad623d9294d 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -801,6 +801,7 @@ void WebContentPage::did_create_child_frame(Web::HTML::CrossProcessId parent_fra
}
hosted_state.container.document_active_sandboxing_flag_set = container_document->active_sandboxing_flag_set();
+ hosted_state.container.document_embedder_policy = container_document->embedder_policy();
traversable.adopt_nested_history_for_created_child(*parent_navigable, *container_document, frame_id);
// https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-new-child-navigable
diff --git a/Tests/LibWeb/Text/expected/iframe-cross-origin-embedder-policy.txt b/Tests/LibWeb/Text/expected/iframe-cross-origin-embedder-policy.txt
new file mode 100644
index 00000000000..53cd2e4e466
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/iframe-cross-origin-embedder-policy.txt
@@ -0,0 +1,6 @@
+same-origin-without-coep: blocked
+same-origin-with-coep: loaded
+cross-origin-with-coep-and-corp: loaded
+cross-origin-with-coep-without-corp: blocked
+cross-origin-with-corp-without-coep: blocked
+srcdoc: loaded
diff --git a/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html b/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html
new file mode 100644
index 00000000000..ad80541c282
--- /dev/null
+++ b/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+
+ // A child document that loads holds an iframe of its own, which this page can count across origins.
+ const childSource = "<!DOCTYPE html><iframe></iframe>";
+
+ const report = async (name, iframe) => {
+ await new Promise(resolve => {
+ iframe.onload = resolve;
+ document.body.appendChild(iframe);
+ });
+ println(`${name}: ${iframe.contentWindow.length === 1 ? "loaded" : "blocked"}`);
+ iframe.remove();
+ };
+
+ const frame = async (name, { crossOrigin, headers = {} }) => {
+ const url = new URL(await server.createEcho("GET", `/${name}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html", ...headers },
+ body: childSource,
+ }));
+ if (crossOrigin)
+ url.hostname = uniqueLocalhostHostname(name);
+ const iframe = document.createElement("iframe");
+ iframe.src = url;
+ await report(name, iframe);
+ };
+
+ const coep = { "Cross-Origin-Embedder-Policy": "require-corp" };
+ const corp = { "Cross-Origin-Resource-Policy": "cross-origin" };
+ await frame("same-origin-without-coep", { crossOrigin: false });
+ await frame("same-origin-with-coep", { crossOrigin: false, headers: coep });
+ await frame("cross-origin-with-coep-and-corp", { crossOrigin: true, headers: { ...coep, ...corp } });
+ await frame("cross-origin-with-coep-without-corp", { crossOrigin: true, headers: coep });
+ await frame("cross-origin-with-corp-without-coep", { crossOrigin: true, headers: corp });
+
+ const srcdoc = document.createElement("iframe");
+ srcdoc.srcdoc = childSource;
+ await report("srcdoc", srcdoc);
+
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html.headers b/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html.headers
new file mode 100644
index 00000000000..6604450991a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/iframe-cross-origin-embedder-policy.html.headers
@@ -0,0 +1 @@
+Cross-Origin-Embedder-Policy: require-corp