Skip to content

Truly independent web browser

LibWeb+LibWebCommon: Obtain an opener policy in LibWebCommon

Shannon Boothedbd2e5f0180
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 596da553473..fe70be2a391 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -2013,77 +2013,6 @@ static GC::Ref<PolicyContainer> determine_navigation_params_policy_container(URL
return heap.allocate<PolicyContainer>(heap);
}
-// https://html.spec.whatwg.org/multipage/browsers.html#obtain-coop
-static OpenerPolicy obtain_an_opener_policy(GC::Ref<Fetch::Infrastructure::Response> response, Fetch::Infrastructure::Request::ReservedClientType const& reserved_client)
-{
- // 1. Let policy be a new opener policy.
- OpenerPolicy policy = {};
-
- // AD-HOC: We don't yet setup environments in all cases
- if (!reserved_client)
- return policy;
-
- auto& reserved_environment = *reserved_client;
-
- // 2. If reservedEnvironment is a non-secure context, then return policy.
- if (is_non_secure_context(reserved_environment))
- return policy;
-
- // 3. Let parsedItem be the result of getting a structured field value given `Cross-Origin-Opener-Policy` and "item" from response's header list.
- auto parsed_item = response->header_list()->get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Opener-Policy"sv);
-
- // 4. If parsedItem is not null and parsedItem[0] is a token:
- if (parsed_item.has_value() && parsed_item->bare_item.has<HTTP::StructuredFields::Token>()) {
- auto const& token = parsed_item->bare_item.get<HTTP::StructuredFields::Token>().value;
-
- // 1. If parsedItem[0] is "same-origin":
- if (token == "same-origin"sv) {
- // 1. Let coep be the result of obtaining a cross-origin embedder policy from response and reservedEnvironment.
- // AD-HOC: Rather than running the full "obtain an embedder policy" algorithm, we read the
- // Cross-Origin-Embedder-Policy header's value directly.
- auto coep = response->header_list()->get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Embedder-Policy"sv);
-
- // 2. If coep's value is compatible with cross-origin isolation, then set policy's value to "same-origin-plus-COEP".
- // NB: An embedder policy value is compatible with cross-origin isolation when it's "require-corp" or "credentialless".
- if (coep.has_value()
- && (coep->bare_item == HTTP::StructuredFields::Token { "require-corp"sv } || coep->bare_item == HTTP::StructuredFields::Token { "credentialless"sv })) {
- policy.value = OpenerPolicyValue::SameOriginPlusCOEP;
- }
- // 3. Otherwise, set policy's value to "same-origin".
- else {
- policy.value = OpenerPolicyValue::SameOrigin;
- }
- }
- // 2. If parsedItem[0] is "same-origin-allow-popups", then set policy's value to "same-origin-allow-popups".
- else if (token == "same-origin-allow-popups"sv) {
- policy.value = OpenerPolicyValue::SameOriginAllowPopups;
- }
- // 3. If parsedItem[0] is "noopener-allow-popups", then set policy's value to "noopener-allow-popups".
- else if (token == "noopener-allow-popups"sv) {
- policy.value = OpenerPolicyValue::NoopenerAllowPopups;
- }
-
- // 4. If parsedItem[1]["report-to"] exists and it is a string, then set policy's reporting endpoint to
- // parsedItem[1]["report-to"].
- if (auto report_to = parsed_item->parameters.get("report-to"sv); report_to.has_value() && report_to->has<ByteString>())
- policy.reporting_endpoint = Utf16String::from_utf8(report_to->get<ByteString>());
- }
-
- // FIXME: Steps 5-6 obtain the report-only opener policy from Cross-Origin-Opener-Policy-Report-Only. We don't
- // yet implement report-only.
- // 5. Set parsedItem to the result of getting a structured field value given `Cross-Origin-Opener-Policy-Report-Only` and "item" from response's header list.
- // 6. If parsedItem is not null and parsedItem[0] is a token:
- // 1. If parsedItem[0] is "same-origin":
- // 1. Let coep be the result of obtaining a cross-origin embedder policy from response and reservedEnvironment.
- // 2. If coep's value is compatible with cross-origin isolation or coep's report-only value is compatible with cross-origin isolation, then set policy's report-only value to "same-origin-plus-COEP".
- // 3. Otherwise, set policy's report-only value to "same-origin".
- // 2. If parsedItem[0] is "same-origin-allow-popups", then set policy's report-only value to "same-origin-allow-popups".
- // 3. If parsedItem[1]["report-to"] exists and it is a string, then set policy's report-only reporting endpoint to parsedItem[1]["report-to"].
-
- // 7. Return policy.
- return policy;
-}
-
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#hand-off-to-external-software
// FIXME: `resource` can also be a response: https://fetch.spec.whatwg.org/#concept-response
static void hand_off_to_external_software(URL::URL const& resource, GC::Ref<LocalNavigable> navigable, SandboxingFlagSet sandboxing_flags, bool has_transient_activation, URL::Origin const& initiator_origin)
@@ -2392,15 +2321,17 @@ static void perform_navigation_params_fetch(JS::Realm& realm, GC::Ref<Navigation
// 11. Set responseOrigin to the result of determining the origin given response's URL, finalSandboxFlags, and entry's document state's initiator origin.
state_holder->response_origin = determine_the_origin(state_holder->response->url(), state_holder->final_sandbox_flags, state_holder->initiator_origin);
- // 12. If navigable is a top-level traversable, then:
+ // 12. If navigable is a top-level traversable:
// AD-HOC: Skip a network error, which may have no URL to enforce an opener policy with. This algorithm returns
// null for it, so the enforcement result would go unused.
if (!state_holder->response->is_network_error() && state_holder->navigable->is_top_level_traversable()) {
// 1. Set responseCOOP to the result of obtaining an opener policy given response and request's reserved client.
- state_holder->response_coop = obtain_an_opener_policy(*state_holder->response, state_holder->request->reserved_client());
+ // AD-HOC: We don't yet set up environments in all cases. Without one, responseCOOP stays a new opener policy.
+ if (auto const& reserved_client = state_holder->request->reserved_client())
+ state_holder->response_coop = obtain_an_opener_policy(*state_holder->response->header_list(), is_non_secure_context(*reserved_client));
// 2. Set coopEnforcementResult to the result of enforcing the response's opener policy given navigable's active browsing context,
- // response's URL, responseOrigin, responseCOOP, coopEnforcementResult and request's referrer.
+ // response's URL, responseOrigin, responseCOOP, coopEnforcementResult, and request's referrer.
auto const& active_document = *state_holder->navigable->active_document();
state_holder->coop_enforcement_result = enforce_a_responses_opener_policy(active_document.is_initial_about_blank(), state_holder->response->url().value(), *state_holder->response_origin, state_holder->response_coop, state_holder->coop_enforcement_result);
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index efaa4607c9f..0ae6197d402 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -15,6 +15,7 @@ set(SOURCES
HTML/AutoplayPolicy.cpp
HTML/BroadcastChannelMessage.cpp
HTML/BrowsingContext.cpp
+ HTML/CrossOrigin/OpenerPolicy.cpp
HTML/CrossOrigin/OpenerPolicyEnforcement.cpp
HTML/CrossProcessId.cpp
HTML/EmbedderPolicy.cpp
diff --git a/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.cpp b/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.cpp
new file mode 100644
index 00000000000..1ed39000bef
--- /dev/null
+++ b/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibHTTP/HeaderList.h>
+#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h>
+#include <LibWebCommon/HTML/EmbedderPolicy.h>
+
+namespace Web::HTML {
+
+// https://html.spec.whatwg.org/multipage/browsers.html#obtain-coop
+// NB: response is given as its header list, and reservedEnvironment as whether it is a non-secure context, which is all
+// these steps read of them.
+OpenerPolicy obtain_an_opener_policy(HTTP::HeaderList const& header_list, bool reserved_environment_is_a_non_secure_context)
+{
+ // 1. Let policy be a new opener policy.
+ OpenerPolicy policy;
+
+ // 2. If reservedEnvironment is a non-secure context, then return policy.
+ if (reserved_environment_is_a_non_secure_context)
+ return policy;
+
+ // 3. Let parsedItem be the result of getting a structured field value given `Cross-Origin-Opener-Policy` and "item"
+ // from response's header list.
+ auto parsed_item = header_list.get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Opener-Policy"sv);
+
+ // 4. If parsedItem is not null and parsedItem[0] is a token:
+ if (parsed_item.has_value() && parsed_item->bare_item.has<HTTP::StructuredFields::Token>()) {
+ auto const& token = parsed_item->bare_item.get<HTTP::StructuredFields::Token>().value;
+
+ // 1. If parsedItem[0] is "same-origin":
+ if (token == "same-origin"sv) {
+ // 1. Let coep be the result of obtaining a cross-origin embedder policy from response and
+ // reservedEnvironment.
+ auto coep = obtain_an_embedder_policy(header_list, reserved_environment_is_a_non_secure_context);
+
+ // 2. If coep's value is compatible with cross-origin isolation, then set policy's value to
+ // "same-origin-plus-COEP".
+ if (is_compatible_with_cross_origin_isolation(coep.value)) {
+ policy.value = OpenerPolicyValue::SameOriginPlusCOEP;
+ }
+ // 3. Otherwise, set policy's value to "same-origin".
+ else {
+ policy.value = OpenerPolicyValue::SameOrigin;
+ }
+ }
+ // 2. If parsedItem[0] is "same-origin-allow-popups", then set policy's value to "same-origin-allow-popups".
+ else if (token == "same-origin-allow-popups"sv) {
+ policy.value = OpenerPolicyValue::SameOriginAllowPopups;
+ }
+ // 3. If parsedItem[0] is "noopener-allow-popups", then set policy's value to "noopener-allow-popups".
+ else if (token == "noopener-allow-popups"sv) {
+ policy.value = OpenerPolicyValue::NoopenerAllowPopups;
+ }
+
+ // 4. If parsedItem[1]["report-to"] exists and it is a string, then set policy's reporting endpoint to
+ // parsedItem[1]["report-to"].
+ if (auto report_to = parsed_item->parameters.get("report-to"sv); report_to.has_value() && report_to->has<ByteString>())
+ policy.reporting_endpoint = Utf16String::from_utf8(report_to->get<ByteString>());
+ }
+
+ // 5. Set parsedItem to the result of getting a structured field value given `Cross-Origin-Opener-Policy-Report-Only`
+ // and "item" from response's header list.
+ parsed_item = header_list.get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Opener-Policy-Report-Only"sv);
+
+ // 6. If parsedItem is not null and parsedItem[0] is a token:
+ if (parsed_item.has_value() && parsed_item->bare_item.has<HTTP::StructuredFields::Token>()) {
+ auto const& token = parsed_item->bare_item.get<HTTP::StructuredFields::Token>().value;
+
+ // 1. If parsedItem[0] is "same-origin":
+ if (token == "same-origin"sv) {
+ // 1. Let coep be the result of obtaining a cross-origin embedder policy from response and
+ // reservedEnvironment.
+ auto coep = obtain_an_embedder_policy(header_list, reserved_environment_is_a_non_secure_context);
+
+ // 2. If coep's value is compatible with cross-origin isolation or coep's report-only value is compatible
+ // with cross-origin isolation, then set policy's report-only value to "same-origin-plus-COEP".
+ if (is_compatible_with_cross_origin_isolation(coep.value) || is_compatible_with_cross_origin_isolation(coep.report_only_value)) {
+ policy.report_only_value = OpenerPolicyValue::SameOriginPlusCOEP;
+ }
+ // 3. Otherwise, set policy's report-only value to "same-origin".
+ else {
+ policy.report_only_value = OpenerPolicyValue::SameOrigin;
+ }
+ }
+ // 2. If parsedItem[0] is "same-origin-allow-popups", then set policy's report-only value to
+ // "same-origin-allow-popups".
+ else if (token == "same-origin-allow-popups"sv) {
+ policy.report_only_value = OpenerPolicyValue::SameOriginAllowPopups;
+ }
+
+ // 3. If parsedItem[1]["report-to"] exists and it is a string, then set policy's report-only reporting endpoint
+ // to parsedItem[1]["report-to"].
+ if (auto report_to = parsed_item->parameters.get("report-to"sv); report_to.has_value() && report_to->has<ByteString>())
+ policy.report_only_reporting_endpoint = Utf16String::from_utf8(report_to->get<ByteString>());
+ }
+
+ // 7. Return policy.
+ return policy;
+}
+
+}
diff --git a/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h b/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h
index ffaf5b86564..63fc9358085 100644
--- a/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h
+++ b/Libraries/LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h
@@ -7,6 +7,8 @@
#pragma once
#include <AK/Utf16String.h>
+#include <LibHTTP/Forward.h>
+#include <LibWebCommon/Export.h>
namespace Web::HTML {
@@ -34,4 +36,6 @@ struct OpenerPolicy {
Optional<Utf16String> report_only_reporting_endpoint;
};
+WEBCOMMON_API OpenerPolicy obtain_an_opener_policy(HTTP::HeaderList const&, bool reserved_environment_is_a_non_secure_context);
+
}
diff --git a/Tests/LibWeb/Text/expected/cross-origin-isolated-with-report-only-coep.txt b/Tests/LibWeb/Text/expected/cross-origin-isolated-with-report-only-coep.txt
new file mode 100644
index 00000000000..10b1e677ee4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/cross-origin-isolated-with-report-only-coep.txt
@@ -0,0 +1 @@
+crossOriginIsolated: false
diff --git a/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html b/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html
new file mode 100644
index 00000000000..885f1da6fdd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ // A report-only embedder policy does not make the opener policy "same-origin-plus-COEP".
+ println("crossOriginIsolated: " + self.crossOriginIsolated);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html.headers b/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html.headers
new file mode 100644
index 00000000000..764d40409b3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/cross-origin-isolated-with-report-only-coep.html.headers
@@ -0,0 +1,2 @@
+Cross-Origin-Opener-Policy: same-origin
+Cross-Origin-Embedder-Policy-Report-Only: require-corp