Skip to content

Truly independent web browser

LibWeb+LibWebCommon: Obtain an embedder policy from a fetch response

Shannon Booth0c00db34d578
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 634302831f8..596da553473 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -2384,7 +2384,7 @@ static void perform_navigation_params_fetch(JS::Realm& realm, GC::Ref<Navigation
}
// 9. Set responsePolicyContainer to the result of creating a policy container from a fetch response given response and request's reserved client.
- state_holder->response_policy_container = create_a_policy_container_from_a_fetch_response(*state_holder->response, nullptr);
+ state_holder->response_policy_container = create_a_policy_container_from_a_fetch_response(*state_holder->response, state_holder->request->reserved_client());
// 10. Set finalSandboxFlags to the union of targetSnapshotParams's sandboxing flags and responsePolicyContainer's CSP list's CSP-derived sandboxing flags.
state_holder->final_sandbox_flags = state_holder->target_snapshot_params.sandboxing_flags | state_holder->response_policy_container->csp_list->csp_derived_sandboxing_flags();
diff --git a/Libraries/LibWeb/HTML/PolicyContainers.cpp b/Libraries/LibWeb/HTML/PolicyContainers.cpp
index b1005534973..80142ac5706 100644
--- a/Libraries/LibWeb/HTML/PolicyContainers.cpp
+++ b/Libraries/LibWeb/HTML/PolicyContainers.cpp
@@ -11,6 +11,7 @@
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/PolicyContainers.h>
+#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/HTML/SerializedPolicyContainer.h>
@@ -37,7 +38,7 @@ bool url_requires_storing_the_policy_container_in_history(URL::URL const& url)
}
// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-policy-container-from-a-fetch-response
-GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ptr<Environment>)
+GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ptr<Environment> environment)
{
auto& heap = GC::Heap::the();
@@ -50,8 +51,10 @@ GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref
// 3. Set result's CSP list to the result of parsing a response's Content Security Policies given response.
result->csp_list = ContentSecurityPolicy::Policy::parse_a_responses_content_security_policies(heap, response);
- // FIXME: 4. If environment is non-null, then set result's embedder policy to the result of obtaining an embedder
- // policy given response and environment. Otherwise, set it to "unsafe-none".
+ // 4. If environment is non-null, then set result's embedder policy to the result of obtaining an embedder policy
+ // given response and environment. Otherwise, set it to "unsafe-none".
+ if (environment)
+ result->embedder_policy = obtain_an_embedder_policy(*response->header_list(), is_non_secure_context(*environment));
// 5. Set result's referrer policy to the result of parsing the `Referrer-Policy` header given response.
// [REFERRERPOLICY]
diff --git a/Libraries/LibWebCommon/HTML/EmbedderPolicy.cpp b/Libraries/LibWebCommon/HTML/EmbedderPolicy.cpp
index 36acea03502..04a79f83b8d 100644
--- a/Libraries/LibWebCommon/HTML/EmbedderPolicy.cpp
+++ b/Libraries/LibWebCommon/HTML/EmbedderPolicy.cpp
@@ -5,12 +5,84 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibHTTP/HeaderList.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibWebCommon/HTML/EmbedderPolicy.h>
namespace Web::HTML {
+// https://html.spec.whatwg.org/multipage/browsers.html#compatible-with-cross-origin-isolation
+bool is_compatible_with_cross_origin_isolation(EmbedderPolicyValue value)
+{
+ // An embedder policy value is compatible with cross-origin isolation if it is "credentialless" or "require-corp".
+ return value == EmbedderPolicyValue::Credentialless || value == EmbedderPolicyValue::RequireCorp;
+}
+
+static Optional<EmbedderPolicyValue> embedder_policy_value_of_token(Optional<HTTP::StructuredFields::Item> const& parsed_item)
+{
+ if (!parsed_item.has_value())
+ return {};
+ auto const* token = parsed_item->bare_item.get_pointer<HTTP::StructuredFields::Token>();
+ if (!token)
+ return {};
+ if (token->value == "unsafe-none"sv)
+ return EmbedderPolicyValue::UnsafeNone;
+ if (token->value == "require-corp"sv)
+ return EmbedderPolicyValue::RequireCorp;
+ if (token->value == "credentialless"sv)
+ return EmbedderPolicyValue::Credentialless;
+ return {};
+}
+
+// https://html.spec.whatwg.org/multipage/browsers.html#obtain-an-embedder-policy
+// NB: response is given as its header list, and environment as whether it is a non-secure context, which is all these
+// steps read of them.
+EmbedderPolicy obtain_an_embedder_policy(HTTP::HeaderList const& header_list, bool environment_is_a_non_secure_context)
+{
+ // 1. Let policy be a new embedder policy.
+ EmbedderPolicy policy;
+
+ // 2. If environment is a non-secure context, then return policy.
+ if (environment_is_a_non_secure_context)
+ return policy;
+
+ // 3. Let parsedItem be the result of getting a structured field value with `Cross-Origin-Embedder-Policy` and
+ // "item" from response's header list.
+ auto parsed_item = header_list.get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Embedder-Policy"sv);
+
+ // 4. If parsedItem is non-null, parsedItem[0] is a token, and parsedItem[0] is compatible with cross-origin
+ // isolation:
+ if (auto value = embedder_policy_value_of_token(parsed_item); value.has_value() && is_compatible_with_cross_origin_isolation(*value)) {
+ // 1. Set policy's value to parsedItem[0].
+ policy.value = *value;
+
+ // 2. 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 with
+ // `Cross-Origin-Embedder-Policy-Report-Only` and "item" from response's header list.
+ parsed_item = header_list.get_structured_field_value<HTTP::StructuredFields::Item>("Cross-Origin-Embedder-Policy-Report-Only"sv);
+
+ // 6. If parsedItem is non-null, parsedItem[0] is a token, and parsedItem[0] is compatible with cross-origin
+ // isolation:
+ if (auto value = embedder_policy_value_of_token(parsed_item); value.has_value() && is_compatible_with_cross_origin_isolation(*value)) {
+ // 1. Set policy's report-only value to parsedItem[0].
+ policy.report_only_value = *value;
+
+ // 2. 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;
+}
+
}
namespace IPC {
diff --git a/Libraries/LibWebCommon/HTML/EmbedderPolicy.h b/Libraries/LibWebCommon/HTML/EmbedderPolicy.h
index 449543777b0..392de55e3fc 100644
--- a/Libraries/LibWebCommon/HTML/EmbedderPolicy.h
+++ b/Libraries/LibWebCommon/HTML/EmbedderPolicy.h
@@ -10,6 +10,7 @@
#include <AK/Optional.h>
#include <AK/Utf16String.h>
#include <AK/Utf16View.h>
+#include <LibHTTP/Forward.h>
#include <LibIPC/Forward.h>
#include <LibWebCommon/Export.h>
@@ -22,6 +23,8 @@ enum class EmbedderPolicyValue : u8 {
Credentialless,
};
+WEBCOMMON_API bool is_compatible_with_cross_origin_isolation(EmbedderPolicyValue);
+
// https://html.spec.whatwg.org/multipage/browsers.html#embedder-policy
struct EmbedderPolicy {
// https://html.spec.whatwg.org/multipage/browsers.html#embedder-policy-value-2
@@ -41,6 +44,8 @@ struct EmbedderPolicy {
Utf16String report_only_reporting_endpoint;
};
+WEBCOMMON_API EmbedderPolicy obtain_an_embedder_policy(HTTP::HeaderList const&, bool environment_is_a_non_secure_context);
+
}
namespace IPC {