Skip to content

Truly independent web browser

LibWeb: Make CSP policies reference-counted instead of GC-allocated

Luke Wilde395a311e96c9
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
index cd0a7199364..84690520710 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
@@ -90,10 +90,10 @@ namespace Web::ContentSecurityPolicy {
void report_content_security_policy_violations_for_request(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Request> request)
{
// 1. Let CSP list be request’s policy container's CSP list.
- auto csp_list = request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->csp_list;
+ auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;
// 2. For each policy of CSP list:
- for (auto policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. If policy’s disposition is "enforce", then skip to the next policy.
if (policy->disposition() == Policy::Disposition::Enforce)
continue;
@@ -114,13 +114,13 @@ void report_content_security_policy_violations_for_request(JS::Realm& realm, GC:
Directives::Directive::Result should_request_be_blocked_by_content_security_policy(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Request> request)
{
// 1. Let CSP list be request’s policy container's CSP list.
- auto csp_list = request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->csp_list;
+ auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;
// 2. Let result be "Allowed".
auto result = Directives::Directive::Result::Allowed;
// 3. For each policy of CSP list:
- for (auto policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. If policy’s disposition is "report", then skip to the next policy.
if (policy->disposition() == Policy::Disposition::Report)
continue;
@@ -147,10 +147,10 @@ Directives::Directive::Result should_request_be_blocked_by_content_security_poli
// https://w3c.github.io/webappsec-subresource-integrity/#should-request-be-blocked-by-integrity-policy
Directives::Directive::Result should_request_be_blocked_by_integrity_policy(GC::Ref<Fetch::Infrastructure::Request> request)
{
- VERIFY(request->policy_container().has<GC::Ref<HTML::PolicyContainer>>());
+ VERIFY(request->policy_container().has<NonnullRefPtr<HTML::PolicyContainer const>>());
// 1. Let policyContainer be request’s policy container.
- auto const& policy_container = request->policy_container().get<GC::Ref<HTML::PolicyContainer>>();
+ auto const& policy_container = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>();
// 2. Let parsedMetadata be the result of calling parse metadata with request’s integrity metadata.
auto parsed_metadata = MUST(SRI::parse_metadata(request->integrity_metadata().utf16_view()));
@@ -208,7 +208,7 @@ Directives::Directive::Result should_request_be_blocked_by_integrity_policy(GC::
Directives::Directive::Result should_response_to_request_be_blocked_by_content_security_policy(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Response> response, GC::Ref<Fetch::Infrastructure::Request> request)
{
// 1. Let CSP list be request’s policy container's CSP list.
- auto csp_list = request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->csp_list;
+ auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;
// 2. Let result be "Allowed".
auto result = Directives::Directive::Result::Allowed;
@@ -216,7 +216,7 @@ Directives::Directive::Result should_response_to_request_be_blocked_by_content_s
// 3. For each policy of CSP list:
// Spec Note: This portion of the check verifies that the page can load the response. That is, that a Service
// Worker hasn't substituted a file which would violate the page’s CSP.
- for (auto policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
// 1. If the result of executing directive’s post-request check is "Blocked", then:
@@ -245,8 +245,8 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
auto result = Directives::Directive::Result::Allowed;
// 2. For each policy of navigation request’s policy container’s CSP list:
- auto policy_container = navigation_request->policy_container().get<GC::Ref<HTML::PolicyContainer>>();
- for (auto policy : policy_container->csp_list->policies()) {
+ auto policy_container = navigation_request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>();
+ for (auto const& policy : policy_container->csp_list.policies()) {
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
// 1. If directive’s pre-navigation check returns "Allowed" when executed upon navigation request, type, and policy skip to the next directive.
@@ -274,10 +274,10 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
// 3. If result is "Allowed", and if navigation request’s current URL’s scheme is javascript:
if (result == Directives::Directive::Result::Allowed && navigation_request->current_url().scheme() == "javascript"sv) {
// 1. For each policy of navigation request’s policy container’s CSP list:
- VERIFY(navigation_request->policy_container().has<GC::Ref<HTML::PolicyContainer>>());
- auto csp_list = navigation_request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->csp_list;
+ VERIFY(navigation_request->policy_container().has<NonnullRefPtr<HTML::PolicyContainer const>>());
+ auto const& csp_list = navigation_request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;
- for (auto policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
// 1. Let directive-name be the result of executing § 6.8.2 Get the effective directive for inline
@@ -325,7 +325,7 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
Directives::Directive::Result should_navigation_response_to_navigation_request_of_type_in_target_be_blocked_by_content_security_policy(
GC::Ptr<Fetch::Infrastructure::Request> navigation_request,
GC::Ref<Fetch::Infrastructure::Response> navigation_response,
- GC::Ref<PolicyList> response_csp_list,
+ PolicyList const& response_csp_list,
Directives::Directive::NavigationType navigation_type,
GC::Ref<HTML::LocalNavigable> target)
{
@@ -338,7 +338,7 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
// NB: Only frame-ancestors reads the target's container documents. Each is the next parent navigable's active
// document, and a parent navigable answers for a document hosted in another process.
- auto has_frame_ancestors = any_of(response_csp_list->policies(), [](auto const& policy) {
+ auto has_frame_ancestors = any_of(response_csp_list.policies(), [](auto const& policy) {
return policy->contains_directive_with_name(Directives::Names::FrameAncestors);
});
Vector<URL::Origin> target_container_document_origins;
@@ -351,7 +351,7 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
}
// 2. For each policy of response CSP list:
- for (auto policy : response_csp_list->policies()) {
+ for (auto const& policy : response_csp_list.policies()) {
// Spec Note: Some directives (like frame-ancestors) allow a response’s Content Security Policy to act on the navigation.
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
@@ -383,8 +383,8 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
}
// 3. For each policy of navigation request’s policy container’s CSP list:
- auto request_policy_container = navigation_request->policy_container().get<GC::Ref<HTML::PolicyContainer>>();
- for (auto policy : request_policy_container->csp_list->policies()) {
+ auto request_policy_container = navigation_request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>();
+ for (auto const& policy : request_policy_container->csp_list.policies()) {
// Spec Note: NOTE: Some directives in the navigation request’s context (like frame-ancestors) need the response before acting on the navigation.
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
@@ -428,9 +428,9 @@ Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by
// 3. For each policy of element’s Document's global object’s CSP list:
auto& settings = element->document().relevant_settings_object();
auto& global_object = settings.global_object();
- auto csp_list = settings.policy_container()->csp_list;
+ auto const& csp_list = settings.policy_container()->csp_list;
- for (auto const policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. For each directive of policy’s directive set:
for (auto const& directive : policy->directives()) {
// 1. If directive’s inline check returns "Allowed" when executed upon element, type, policy and source,
@@ -548,8 +548,8 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_string_compilation(JS::Rea
auto& global = realm.global_object();
// 5. For each policy of global’s CSP list:
- auto csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
- for (auto const policy : csp_list->policies()) {
+ auto const& csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
+ for (auto const& policy : csp_list.policies()) {
// 1. Let source-list be null.
Optional<Vector<Utf16String>> maybe_source_list;
@@ -629,8 +629,8 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_wasm_byte_compilation(JS::
auto result = Directives::Directive::Result::Allowed;
// 3. For each policy of global’s CSP list:
- auto csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
- for (auto const policy : csp_list->policies()) {
+ auto const& csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
+ for (auto const& policy : csp_list.policies()) {
// 1. Let source-list be null.
Optional<Vector<Utf16String>> maybe_source_list;
@@ -694,8 +694,8 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_wasm_byte_compilation(JS::
Directives::Directive::Result is_base_allowed_for_document(URL::URL const& base, GC::Ref<DOM::Document const> document)
{
// 1. For each policy of document’s global object’s csp list:
- auto csp_list = document->relevant_settings_object().policy_container()->csp_list;
- for (auto const policy : csp_list->policies()) {
+ auto const& csp_list = document->relevant_settings_object().policy_container()->csp_list;
+ for (auto const& policy : csp_list.policies()) {
// 1. Let source list be null.
// NOTE: Not necessary.
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h
index f3421983eb6..d0eb377d9f9 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h
@@ -22,7 +22,7 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
Directives::Directive::Result should_navigation_response_to_navigation_request_of_type_in_target_be_blocked_by_content_security_policy(
GC::Ptr<Fetch::Infrastructure::Request> navigation_request,
GC::Ref<Fetch::Infrastructure::Response> navigation_response,
- GC::Ref<PolicyList> response_csp_list,
+ ContentSecurityPolicy::PolicyList const& response_csp_list,
Directives::Directive::NavigationType navigation_type,
GC::Ref<HTML::LocalNavigable> target);
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
index 4903f798ae3..2ea1287bf52 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
@@ -262,7 +262,7 @@ Vector<Utf16FlyString> get_fetch_directive_fallback_list(Optional<Utf16FlyString
}
// https://w3c.github.io/webappsec-csp/#should-directive-execute
-ShouldExecute should_fetch_directive_execute(Optional<Utf16FlyString> effective_directive_name, Utf16FlyString const& directive_name, GC::Ref<Policy const> policy)
+ShouldExecute should_fetch_directive_execute(Optional<Utf16FlyString> effective_directive_name, Utf16FlyString const& directive_name, Policy const& policy)
{
// 1. Let directive fallback list be the result of executing § 6.8.3 Get fetch directive fallback list on effective
// directive name.
@@ -275,7 +275,7 @@ ShouldExecute should_fetch_directive_execute(Optional<Utf16FlyString> effective_
return ShouldExecute::Yes;
// 2. If policy contains a directive whose name is fallback directive, Return "No".
- if (policy->contains_directive_with_name(fallback_directive))
+ if (policy.contains_directive_with_name(fallback_directive))
return ShouldExecute::No;
}
@@ -698,18 +698,18 @@ MatchResult does_url_match_source_list_in_origin_with_redirect_count(URL::URL co
}
// https://w3c.github.io/webappsec-csp/#match-request-to-source-list
-MatchResult does_request_match_source_list(GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, GC::Ref<Policy const> policy)
+MatchResult does_request_match_source_list(GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, Policy const& policy)
{
// Given a request request, a source list source list, and a policy policy, this algorithm returns the result of
// executing § 6.7.2.7 Does url match source list in origin with redirect count? on request’s current url, source
// list, policy’s self-origin, and request’s redirect count.
// Spec Note: This is generally used in directives' pre-request check algorithms to verify that a given request is
// reasonable.
- return does_url_match_source_list_in_origin_with_redirect_count(request->current_url(), source_list, policy->self_origin(), request->redirect_count());
+ return does_url_match_source_list_in_origin_with_redirect_count(request->current_url(), source_list, policy.self_origin(), request->redirect_count());
}
// https://w3c.github.io/webappsec-csp/#match-response-to-source-list
-MatchResult does_response_match_source_list(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, GC::Ref<Policy const> policy)
+MatchResult does_response_match_source_list(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, Policy const& policy)
{
// Given a request request, and a source list source list, and a policy policy, this algorithm returns the result
// of executing § 6.7.2.7 Does url match source list in origin with redirect count? on response’s url, source list,
@@ -718,7 +718,7 @@ MatchResult does_response_match_source_list(GC::Ref<Fetch::Infrastructure::Respo
// is reasonable.
// FIXME: File spec issue that it does specify to pass in response here.
VERIFY(response->url().has_value());
- return does_url_match_source_list_in_origin_with_redirect_count(response->url().value(), source_list, policy->self_origin(), request->redirect_count());
+ return does_url_match_source_list_in_origin_with_redirect_count(response->url().value(), source_list, policy.self_origin(), request->redirect_count());
}
// https://w3c.github.io/webappsec-csp/#match-nonce-to-source-list
@@ -801,7 +801,7 @@ static MatchResult does_integrity_metadata_match_source_list(Utf16View integrity
}
// https://w3c.github.io/webappsec-csp/#script-pre-request
-Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, Directive const& directive, GC::Ref<Policy const> policy)
+Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, Directive const& directive, Policy const& policy)
{
// 1. If request’s destination is script-like:
if (request->destination_is_script_like()) {
@@ -843,7 +843,7 @@ Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastruct
}
// https://w3c.github.io/webappsec-csp/#script-post-request
-Directive::Result script_directives_post_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, Directive const& directive, GC::Ref<Policy const> policy)
+Directive::Result script_directives_post_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, Directive const& directive, Policy const& policy)
{
// 1. If request’s destination is script-like:
if (request->destination_is_script_like()) {
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
index 68ab818bf3a..be6c9ef1ca0 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
@@ -28,19 +28,19 @@ enum class [[nodiscard]] MatchResult {
[[nodiscard]] Optional<Utf16FlyString> get_the_effective_directive_for_request(GC::Ref<Fetch::Infrastructure::Request const> request);
[[nodiscard]] Vector<Utf16FlyString> get_fetch_directive_fallback_list(Optional<Utf16FlyString> directive_name);
-ShouldExecute should_fetch_directive_execute(Optional<Utf16FlyString> effective_directive_name, Utf16FlyString const& directive_name, GC::Ref<Policy const> policy);
+ShouldExecute should_fetch_directive_execute(Optional<Utf16FlyString> effective_directive_name, Utf16FlyString const& directive_name, Policy const& policy);
[[nodiscard]] Utf16FlyString get_the_effective_directive_for_inline_checks(Directive::InlineType type);
MatchResult does_url_match_expression_in_origin_with_redirect_count(URL::URL const& url, Utf16View expression, URL::Origin const& origin, u8 redirect_count);
MatchResult does_url_match_source_list_in_origin_with_redirect_count(URL::URL const& url, Vector<Utf16String> const& source_list, URL::Origin const& origin, u8 redirect_count);
-MatchResult does_request_match_source_list(GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, GC::Ref<Policy const> policy);
-MatchResult does_response_match_source_list(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, GC::Ref<Policy const> policy);
+MatchResult does_request_match_source_list(GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, Policy const& policy);
+MatchResult does_response_match_source_list(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Fetch::Infrastructure::Request const> request, Vector<Utf16String> const& source_list, Policy const& policy);
MatchResult does_nonce_match_source_list(Utf16View nonce, Vector<Utf16String> const& source_list);
-Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, Directive const& directive, GC::Ref<Policy const> policy);
-Directive::Result script_directives_post_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, Directive const& directive, GC::Ref<Policy const> policy);
+Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, Directive const& directive, Policy const& policy);
+Directive::Result script_directives_post_request_check(GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, Directive const& directive, Policy const& policy);
MatchResult does_element_match_source_list_for_type_and_source(GC::Ptr<DOM::Element const> element, Vector<Utf16String> const& source_list, Directive::InlineType type, Utf16View source);
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
index 45155e315bd..3c81e384439 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
@@ -16,10 +16,8 @@
namespace Web::ContentSecurityPolicy {
-GC_DEFINE_ALLOCATOR(Policy);
-
// https://w3c.github.io/webappsec-csp/#abstract-opdef-parse-a-serialized-csp
-GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Variant<ByteString, String> serialized, Source source, Disposition disposition)
+NonnullRefPtr<Policy> Policy::parse_a_serialized_csp(Variant<ByteString, String> serialized, Source source, Disposition disposition)
{
// To parse a serialized CSP, given a byte sequence or string serialized, a source source, and a disposition disposition,
// execute the following steps.
@@ -32,7 +30,7 @@ GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Variant<ByteStrin
: TextCodec::isomorphic_decode(serialized.get<ByteString>());
// 2. Let policy be a new policy with an empty directive set, a source of source, and a disposition of disposition.
- auto policy = heap.allocate<Policy>();
+ auto policy = adopt_ref(*new Policy);
policy->m_pre_parsed_policy_string = serialized_string;
policy->m_source = source;
policy->m_disposition = disposition;
@@ -87,30 +85,30 @@ GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Variant<ByteStrin
return policy;
}
-GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Utf16View serialized, Source source, Disposition disposition)
+NonnullRefPtr<Policy> Policy::parse_a_serialized_csp(Utf16View serialized, Source source, Disposition disposition)
{
auto serialized_utf8 = MUST(serialized.to_utf8());
- return parse_a_serialized_csp(heap, serialized_utf8, source, disposition);
+ return parse_a_serialized_csp(serialized_utf8, source, disposition);
}
// https://w3c.github.io/webappsec-csp/#abstract-opdef-parse-a-responses-content-security-policies
-GC::Ref<PolicyList> Policy::parse_a_responses_content_security_policies(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Response const> response)
+PolicyList Policy::parse_a_responses_content_security_policies(Fetch::Infrastructure::Response const& response)
{
// To parse a response’s Content Security Policies given a response response, execute the following steps.
// This algorithm returns a list of Content Security Policy objects. If the policies cannot be parsed,
// the returned list will be empty.
// 1. Let policies be an empty list.
- GC::RootVector<GC::Ref<Policy>> policies;
+ Vector<NonnullRefPtr<Policy>> policies;
// 2. For each token returned by extracting header list values given Content-Security-Policy and response’s header
// list:
- auto enforce_policy_tokens_or_failure = response->header_list()->extract_header_list_values("Content-Security-Policy"sv);
+ auto enforce_policy_tokens_or_failure = response.header_list()->extract_header_list_values("Content-Security-Policy"sv);
if (auto const* enforce_policy_tokens = enforce_policy_tokens_or_failure.get_pointer<Vector<ByteString>>()) {
for (auto const& enforce_policy_token : *enforce_policy_tokens) {
// 1. Let policy be the result of parsing token, with a source of "header", and a disposition of "enforce".
- auto policy = parse_a_serialized_csp(heap, enforce_policy_token, Policy::Source::Header, Policy::Disposition::Enforce);
+ auto policy = parse_a_serialized_csp(enforce_policy_token, Policy::Source::Header, Policy::Disposition::Enforce);
// 2. If policy’s directive set is not empty, append policy to policies.
if (!policy->m_directives.is_empty())
@@ -120,12 +118,12 @@ GC::Ref<PolicyList> Policy::parse_a_responses_content_security_policies(GC::Heap
// 3. For each token returned by extracting header list values given Content-Security-Policy-Report-Only and
// response’s header list:
- auto report_policy_tokens_or_failure = response->header_list()->extract_header_list_values("Content-Security-Policy-Report-Only"sv);
+ auto report_policy_tokens_or_failure = response.header_list()->extract_header_list_values("Content-Security-Policy-Report-Only"sv);
if (auto const* report_policy_tokens = report_policy_tokens_or_failure.get_pointer<Vector<ByteString>>()) {
for (auto const& report_policy_token : *report_policy_tokens) {
// 1. Let policy be the result of parsing token, with a source of "header", and a disposition of "report".
- auto policy = parse_a_serialized_csp(heap, report_policy_token, Policy::Source::Header, Policy::Disposition::Report);
+ auto policy = parse_a_serialized_csp(report_policy_token, Policy::Source::Header, Policy::Disposition::Report);
// 2. If policy’s directive set is not empty, append policy to policies.
if (!policy->m_directives.is_empty())
@@ -136,16 +134,19 @@ GC::Ref<PolicyList> Policy::parse_a_responses_content_security_policies(GC::Heap
// 4. For each policy of policies:
for (auto& policy : policies) {
// 1. Set policy’s self-origin to response’s url's origin.
- policy->m_self_origin = response->url()->origin();
+ policy->m_self_origin = response.url()->origin();
}
// 5. Return policies.
- return PolicyList::create(heap, policies);
+ PolicyList policy_list;
+ for (auto& policy : policies)
+ policy_list.enforce_policy(move(policy));
+ return policy_list;
}
-GC::Ref<Policy> Policy::create_from_serialized_policy(GC::Heap& heap, SerializedPolicy const& serialized_policy)
+NonnullRefPtr<Policy> Policy::create_from_serialized_policy(SerializedPolicy const& serialized_policy)
{
- auto policy = heap.allocate<Policy>();
+ auto policy = adopt_ref(*new Policy);
for (auto const& serialized_directive : serialized_policy.directives)
policy->m_directives.append(Directives::Directive::create(serialized_directive.name, serialized_directive.value));
@@ -177,17 +178,6 @@ Directives::Directive const* Policy::get_directive_by_name(Utf16View name) const
return nullptr;
}
-GC::Ref<Policy> Policy::clone(GC::Heap& heap) const
-{
- auto policy = heap.allocate<Policy>();
- policy->m_directives = m_directives;
- policy->m_disposition = m_disposition;
- policy->m_source = m_source;
- policy->m_self_origin = m_self_origin;
- policy->m_pre_parsed_policy_string = m_pre_parsed_policy_string;
- return policy;
-}
-
SerializedPolicy Policy::serialize() const
{
Vector<Directives::SerializedDirective> serialized_directives;
@@ -204,14 +194,14 @@ SerializedPolicy Policy::serialize() const
};
}
-void Policy::remove_directive(Badge<HTML::HTMLMetaElement>, Utf16FlyString const& name)
+void Policy::remove_directive(Utf16FlyString const& name)
{
m_directives.remove_all_matching([&name](auto const& directive) {
return directive.name() == name;
});
}
-void Policy::set_self_origin(Badge<HTML::HTMLMetaElement>, URL::Origin const& origin)
+void Policy::set_self_origin(URL::Origin const& origin)
{
m_self_origin = origin;
}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Policy.h b/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
index 2d14dda0ec2..9bb7e1b8905 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
@@ -1,16 +1,18 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
-#include <LibGC/Ptr.h>
+#include <AK/AtomicRefCounted.h>
+#include <AK/NonnullRefPtr.h>
#include <LibURL/Origin.h>
-#include <LibWeb/Bindings/SecurityPolicyViolationEvent.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
#include <LibWeb/Forward.h>
+#include <LibWebCommon/Bindings/SecurityPolicyViolationEvent.h>
#include <LibWebCommon/ContentSecurityPolicy/PolicySource.h>
namespace Web::ContentSecurityPolicy {
@@ -22,36 +24,30 @@ namespace Web::ContentSecurityPolicy {
// https://w3c.github.io/webappsec-csp/#content-security-policy-object
// A policy defines allowed and restricted behaviors, and may be applied to a Document, WorkerGlobalScope,
// or WorkletGlobalScope.
-class Policy final : public GC::Cell {
- GC_CELL(Policy, GC::Cell);
- GC_DECLARE_ALLOCATOR(Policy);
-
+class Policy final : public AtomicRefCounted<Policy> {
public:
using Disposition = Bindings::SecurityPolicyViolationEventDisposition;
using Source = PolicySource;
- ~Policy() = default;
-
- [[nodiscard]] static GC::Ref<Policy> parse_a_serialized_csp(GC::Heap&, Variant<ByteString, String> serialized, Source source, Disposition disposition);
- [[nodiscard]] static GC::Ref<Policy> parse_a_serialized_csp(GC::Heap&, Utf16View serialized, Source source, Disposition disposition);
- [[nodiscard]] static GC::Ref<PolicyList> parse_a_responses_content_security_policies(GC::Heap&, GC::Ref<Fetch::Infrastructure::Response const> response);
- [[nodiscard]] static GC::Ref<Policy> create_from_serialized_policy(GC::Heap&, SerializedPolicy const&);
+ [[nodiscard]] static NonnullRefPtr<Policy> parse_a_serialized_csp(Variant<ByteString, String> serialized, Source source, Disposition disposition);
+ [[nodiscard]] static NonnullRefPtr<Policy> parse_a_serialized_csp(Utf16View serialized, Source source, Disposition disposition);
+ [[nodiscard]] static PolicyList parse_a_responses_content_security_policies(Fetch::Infrastructure::Response const&);
+ [[nodiscard]] static NonnullRefPtr<Policy> create_from_serialized_policy(SerializedPolicy const&);
[[nodiscard]] Vector<Directives::Directive> const& directives() const { return m_directives; }
[[nodiscard]] Disposition disposition() const { return m_disposition; }
[[nodiscard]] Source source() const { return m_source; }
[[nodiscard]] URL::Origin const& self_origin() const { return m_self_origin.value(); }
- [[nodiscard]] String const& pre_parsed_policy_string(Badge<Violation>) const { return m_pre_parsed_policy_string; }
+ [[nodiscard]] String const& pre_parsed_policy_string() const { return m_pre_parsed_policy_string; }
[[nodiscard]] bool contains_directive_with_name(Utf16View name) const;
[[nodiscard]] Directives::Directive const* get_directive_by_name(Utf16View) const;
- [[nodiscard]] GC::Ref<Policy> clone(GC::Heap&) const;
[[nodiscard]] SerializedPolicy serialize() const;
- void remove_directive(Badge<HTML::HTMLMetaElement>, Utf16FlyString const& name);
- void set_self_origin(Badge<HTML::HTMLMetaElement>, URL::Origin const& origin);
+ void remove_directive(Utf16FlyString const& name);
+ void set_self_origin(URL::Origin const& origin);
private:
Policy() = default;
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
index 5fca6420185..8cc6d0d68a7 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
@@ -1,59 +1,20 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGC/Heap.h>
-#include <LibGC/RootVector.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
-#include <LibWeb/DOM/Document.h>
-#include <LibWeb/HTML/PolicyContainers.h>
-#include <LibWeb/HTML/Scripting/Environments.h>
-#include <LibWeb/HTML/Window.h>
-#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWebCommon/ContentSecurityPolicy/SerializedPolicy.h>
#include <LibWebCommon/HTML/SandboxingFlagSet.h>
namespace Web::ContentSecurityPolicy {
-GC_DEFINE_ALLOCATOR(PolicyList);
-
-GC::Ref<PolicyList> PolicyList::create(GC::Heap& heap, GC::RootVector<GC::Ref<Policy>> const& policies)
-{
- auto policy_list = heap.allocate<PolicyList>();
- for (auto policy : policies)
- policy_list->m_policies.append(policy);
- return policy_list;
-}
-
-GC::Ref<PolicyList> PolicyList::create(GC::Heap& heap, Vector<SerializedPolicy> const& serialized_policies)
-{
- auto policy_list = heap.allocate<PolicyList>();
- for (auto const& serialized_policy : serialized_policies) {
- auto policy = Policy::create_from_serialized_policy(heap, serialized_policy);
- policy_list->m_policies.append(policy);
- }
- return policy_list;
-}
-
-GC::Ptr<PolicyList> PolicyList::from_object(JS::Object& object)
-{
- if (auto* window = Bindings::window_from_global_object(object))
- return window->associated_document().policy_container()->csp_list;
-
- if (auto* worker = Bindings::worker_global_scope_from_global_object(object))
- return worker->policy_container()->csp_list;
-
- return nullptr;
-}
-
-void PolicyList::visit_edges(Cell::Visitor& visitor)
+PolicyList::PolicyList(Vector<SerializedPolicy> const& serialized_policies)
{
- Base::visit_edges(visitor);
- visitor.visit(m_policies);
+ for (auto const& serialized_policy : serialized_policies)
+ m_policies.append(Policy::create_from_serialized_policy(serialized_policy));
}
// https://w3c.github.io/webappsec-csp/#contains-a-header-delivered-content-security-policy
@@ -75,7 +36,7 @@ HTML::SandboxingFlagSet PolicyList::csp_derived_sandboxing_flags() const
Directives::Directive const* sandbox_directive = nullptr;
// 2. For each policy in cspList:
- for (auto const policy : m_policies) {
+ for (auto const& policy : m_policies) {
// 1. If policy's disposition is not "enforce", then continue.
if (policy->disposition() != Policy::Disposition::Enforce)
continue;
@@ -102,29 +63,18 @@ HTML::SandboxingFlagSet PolicyList::csp_derived_sandboxing_flags() const
}
// https://w3c.github.io/webappsec-csp/#enforced
-void PolicyList::enforce_policy(GC::Ref<Policy> policy)
+void PolicyList::enforce_policy(NonnullRefPtr<Policy const> policy)
{
// A policy is enforced or monitored for a global object by inserting it into the global object’s CSP list.
- m_policies.append(policy);
-}
-
-GC::Ref<PolicyList> PolicyList::clone(GC::Heap& heap) const
-{
- auto policy_list = heap.allocate<PolicyList>();
- for (auto policy : m_policies) {
- auto cloned_policy = policy->clone(heap);
- policy_list->m_policies.append(cloned_policy);
- }
- return policy_list;
+ m_policies.append(move(policy));
}
Vector<SerializedPolicy> PolicyList::serialize() const
{
Vector<SerializedPolicy> serialized_policies;
- for (auto policy : m_policies) {
+ for (auto const& policy : m_policies)
serialized_policies.append(policy->serialize());
- }
return serialized_policies;
}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.h b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.h
index b50ac027da4..7e45a953d51 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.h
@@ -1,47 +1,35 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
-#include <LibGC/CellAllocator.h>
-#include <LibJS/Heap/Cell.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/Vector.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
namespace Web::ContentSecurityPolicy {
-class PolicyList final : public GC::Cell {
- GC_CELL(PolicyList, GC::Cell);
- GC_DECLARE_ALLOCATOR(PolicyList);
-
+// https://w3c.github.io/webappsec-csp/#csp-list
+class PolicyList {
public:
- [[nodiscard]] static GC::Ref<PolicyList> create(GC::Heap&, GC::RootVector<GC::Ref<Policy>> const&);
- [[nodiscard]] static GC::Ref<PolicyList> create(GC::Heap&, Vector<SerializedPolicy> const&);
- [[nodiscard]] static GC::Ptr<PolicyList> from_object(JS::Object&);
-
- virtual ~PolicyList() = default;
+ PolicyList() = default;
+ explicit PolicyList(Vector<SerializedPolicy> const&);
- [[nodiscard]] Vector<GC::Ref<Policy>> const& policies() const { return m_policies; }
+ [[nodiscard]] Vector<NonnullRefPtr<Policy const>> const& policies() const { return m_policies; }
[[nodiscard]] bool contains_header_delivered_policy() const;
-
[[nodiscard]] HTML::SandboxingFlagSet csp_derived_sandboxing_flags() const;
- [[nodiscard]] GC::Ref<PolicyList> clone(GC::Heap&) const;
-
- void enforce_policy(GC::Ref<Policy>);
+ void enforce_policy(NonnullRefPtr<Policy const>);
[[nodiscard]] Vector<SerializedPolicy> serialize() const;
-protected:
- virtual void visit_edges(Cell::Visitor&) override;
-
private:
- PolicyList() = default;
-
- Vector<GC::Ref<Policy>> m_policies;
+ Vector<NonnullRefPtr<Policy const>> m_policies;
};
}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h
index 375fdd1f68b..c1061793b63 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h
@@ -35,7 +35,7 @@ public:
Utf16String const& original_policy() const { return m_original_policy; }
Utf16String const& source_file() const { return m_source_file; }
Utf16String const& sample() const { return m_sample; }
- Policy::Disposition disposition() const { return m_disposition; }
+ Bindings::SecurityPolicyViolationEventDisposition disposition() const { return m_disposition; }
u16 status_code() const { return m_status_code; }
u32 line_number() const { return m_line_number; }
u32 column_number() const { return m_column_number; }
@@ -51,7 +51,7 @@ private:
Utf16String m_original_policy;
Utf16String m_source_file;
Utf16String m_sample;
- Policy::Disposition m_disposition { Policy::Disposition::Enforce };
+ Bindings::SecurityPolicyViolationEventDisposition m_disposition { Bindings::SecurityPolicyViolationEventDisposition::Enforce };
u16 m_status_code { 0 };
u32 m_line_number { 0 };
u32 m_column_number { 0 };
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
index c7dc5195c9c..353544e46ce 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
@@ -32,7 +32,7 @@ namespace Web::ContentSecurityPolicy {
GC_DEFINE_ALLOCATOR(Violation);
-Violation::Violation(GC::Ptr<DOM::EventTarget> global_scope, GC::Ref<Policy const> policy, String directive)
+Violation::Violation(GC::Ptr<DOM::EventTarget> global_scope, NonnullRefPtr<Policy const> policy, String directive)
: m_global_scope(global_scope)
, m_policy(policy)
, m_effective_directive(Utf16String::from_utf8(directive))
@@ -51,7 +51,7 @@ static GC::Ptr<DOM::EventTarget> global_scope_from_global_object(GC::Ptr<JS::Obj
}
// https://w3c.github.io/webappsec-csp/#create-violation-for-global
-GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_directive(GC::Ptr<JS::Object> global_object, GC::Ref<Policy const> policy, String directive)
+GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_directive(GC::Ptr<JS::Object> global_object, NonnullRefPtr<Policy const> policy, String directive)
{
// 1. Let violation be a new violation whose global object is global, policy is policy, effective directive is
// directive, and resource is null.
@@ -102,7 +102,7 @@ GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_di
}
// https://w3c.github.io/webappsec-csp/#create-violation-for-request
-GC::Ref<Violation> Violation::create_a_violation_object_for_request_and_policy(GC::Ref<Fetch::Infrastructure::Request> request, GC::Ref<Policy const> policy)
+GC::Ref<Violation> Violation::create_a_violation_object_for_request_and_policy(GC::Ref<Fetch::Infrastructure::Request> request, NonnullRefPtr<Policy const> policy)
{
// 1. Let directive be the result of executing § 6.8.1 Get the effective directive for request on request.
auto directive = Directives::get_the_effective_directive_for_request(request);
@@ -125,7 +125,7 @@ GC::Ref<Violation> Violation::create_a_violation_object_for_request_and_policy(G
// in that process, so the violation is reported there.
if (auto const* snapshot = as_if<HTML::EnvironmentSettingsSnapshot>(*request->client()); snapshot && snapshot->report_violation()) {
// NB: Request's policy container is a clone of the snapshot's, which lists the policies in the same order.
- auto const& policies = request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->csp_list->policies();
+ auto const& policies = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list.policies();
for (size_t policy_index = 0; policy_index < policies.size(); ++policy_index) {
if (policies[policy_index].ptr() != policy.ptr())
continue;
@@ -144,7 +144,6 @@ void Violation::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_global_scope);
- visitor.visit(m_policy);
visitor.visit(m_element);
visitor.visit(m_report_in_other_process);
}
@@ -262,7 +261,7 @@ ByteBuffer Violation::obtain_the_deprecated_serialization(JS::Realm& realm) cons
// "original-policy"
// The serialization of violation's policy
- body.value.set("original-policy"_string, Infra::JSONValue { m_policy->pre_parsed_policy_string({}) });
+ body.value.set("original-policy"_string, Infra::JSONValue { m_policy->pre_parsed_policy_string() });
// "disposition"
// The disposition of violation's policy
@@ -404,7 +403,7 @@ void Violation::report_a_violation(JS::Realm& realm)
// originalPolicy
// The serialization of violation's policy
- event_init.original_policy = Utf16String::from_utf8(m_policy->pre_parsed_policy_string({}));
+ event_init.original_policy = Utf16String::from_utf8(m_policy->pre_parsed_policy_string());
// disposition
// violation's disposition
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.h b/Libraries/LibWeb/ContentSecurityPolicy/Violation.h
index 11f7ec06f89..1713eac1c54 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.h
@@ -41,8 +41,8 @@ public:
virtual ~Violation() = default;
- [[nodiscard]] static GC::Ref<Violation> create_a_violation_object_for_global_policy_and_directive(GC::Ptr<JS::Object> global_object, GC::Ref<Policy const> policy, String directive);
- [[nodiscard]] static GC::Ref<Violation> create_a_violation_object_for_request_and_policy(GC::Ref<Fetch::Infrastructure::Request> request, GC::Ref<Policy const>);
+ [[nodiscard]] static GC::Ref<Violation> create_a_violation_object_for_global_policy_and_directive(GC::Ptr<JS::Object> global_object, NonnullRefPtr<Policy const> policy, String directive);
+ [[nodiscard]] static GC::Ref<Violation> create_a_violation_object_for_request_and_policy(GC::Ref<Fetch::Infrastructure::Request> request, NonnullRefPtr<Policy const>);
// https://w3c.github.io/webappsec-csp/#violation-url
[[nodiscard]] URL::URL url() const;
@@ -83,7 +83,7 @@ protected:
virtual void visit_edges(Cell::Visitor&) override;
private:
- Violation(GC::Ptr<DOM::EventTarget> global_scope, GC::Ref<Policy const> policy, String directive);
+ Violation(GC::Ptr<DOM::EventTarget> global_scope, NonnullRefPtr<Policy const> policy, String directive);
[[nodiscard]] String obtain_the_blocked_uri_of_resource() const;
[[nodiscard]] ByteBuffer obtain_the_deprecated_serialization(JS::Realm&) const;
@@ -112,7 +112,7 @@ private:
// https://w3c.github.io/webappsec-csp/#violation-policy
// Each violation has a policy, which is the policy that has been violated.
- GC::Ref<Policy const> m_policy;
+ NonnullRefPtr<Policy const> m_policy;
// https://w3c.github.io/webappsec-csp/#violation-effective-directive
// Each violation has an effective directive which is a non-empty string representing the directive whose enforcement
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index c624b13b99a..676284c9e0d 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1084,7 +1084,6 @@ void Document::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_local_storage_holder);
visitor.visit(m_session_storage_holder);
visitor.visit(m_render_blocking_elements);
- visitor.visit(m_policy_container);
visitor.visit(m_deferred_parser_start);
visitor.visit(m_custom_element_registry);
visitor.visit(m_ancestor_origins_list);
@@ -6263,15 +6262,15 @@ void Document::set_active_sandboxing_flag_set(HTML::SandboxingFlagSet sandboxing
m_active_sandboxing_flag_set = sandboxing_flag_set;
}
-GC::Ref<HTML::PolicyContainer> Document::policy_container() const
+NonnullRefPtr<HTML::PolicyContainer> Document::policy_container() const
{
if (!m_policy_container) {
- m_policy_container = GC::Heap::the().allocate<HTML::PolicyContainer>(GC::Heap::the());
+ m_policy_container = HTML::PolicyContainer::create();
}
return *m_policy_container;
}
-void Document::set_policy_container(GC::Ref<HTML::PolicyContainer> policy_container)
+void Document::set_policy_container(NonnullRefPtr<HTML::PolicyContainer> policy_container)
{
m_policy_container = policy_container;
}
@@ -11464,7 +11463,7 @@ Document::StepsToFireBeforeunloadResult Document::steps_to_fire_beforeunload(boo
void Document::run_csp_initialization() const
{
// 1. For each policy of document’s policy container's CSP list:
- for (auto policy : policy_container()->csp_list->policies()) {
+ for (auto const& policy : policy_container()->csp_list.policies()) {
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
// 1. Execute directive’s initialization algorithm on document, and assert: its returned value is "Allowed".
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index fa87dcf781b..afaea8dbf41 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1081,8 +1081,8 @@ public:
void set_permissions_policy(PermissionsPolicy::PermissionsPolicy policy) { m_permissions_policy = move(policy); }
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
- GC::Ref<HTML::PolicyContainer> policy_container() const;
- void set_policy_container(GC::Ref<HTML::PolicyContainer>);
+ NonnullRefPtr<HTML::PolicyContainer> policy_container() const;
+ void set_policy_container(NonnullRefPtr<HTML::PolicyContainer>);
Vector<GC::Root<HTML::Navigable>> descendant_navigables();
Vector<GC::Root<HTML::Navigable>> const descendant_navigables() const;
@@ -2126,7 +2126,7 @@ private:
PermissionsPolicy::PermissionsPolicy m_permissions_policy { PermissionsPolicy::PermissionsPolicy::empty_permissions_policy() };
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
- mutable GC::Ptr<HTML::PolicyContainer> m_policy_container;
+ mutable RefPtr<HTML::PolicyContainer> m_policy_container;
// https://html.spec.whatwg.org/multipage/interaction.html#visibility-state
HTML::VisibilityState m_visibility_state { HTML::VisibilityState::Hidden };
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.h b/Libraries/LibWeb/DOM/DocumentLoading.h
index e75e31027ef..acec7f8c607 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.h
+++ b/Libraries/LibWeb/DOM/DocumentLoading.h
@@ -70,7 +70,7 @@ GC::Ref<DOM::Document> create_document_for_inline_content(GC::Ptr<HTML::LocalNav
move(coop_enforcement_result),
nullptr,
move(origin),
- heap.allocate<HTML::PolicyContainer>(heap),
+ HTML::PolicyContainer::create(),
HTML::SandboxingFlagSet {},
ReferrerPolicy::ReferrerPolicy::EmptyString,
move(coop),
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 5059b8d6f7e..10cbf003c2e 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -393,8 +393,6 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
// https://fetch.spec.whatwg.org/#populate-request-from-client
void populate_request_from_client(Infrastructure::Request& request)
{
- auto& heap = GC::Heap::the();
-
// 1. If request’s traversable for user prompts is "client":
auto const* traversable_for_user_prompts = request.traversable_for_user_prompts().get_pointer<Infrastructure::Request::TraversableForUserPrompts>();
if (traversable_for_user_prompts && *traversable_for_user_prompts == Infrastructure::Request::TraversableForUserPrompts::Client) {
@@ -431,10 +429,10 @@ void populate_request_from_client(Infrastructure::Request& request)
// 1. If request’s client is non-null, then set request’s policy container to a clone of request’s client’s
// policy container.
if (request.client())
- request.set_policy_container(request.client()->policy_container()->clone(heap));
+ request.set_policy_container(request.client()->policy_container()->clone());
// 2. Otherwise, set request’s policy container to a new policy container.
else
- request.set_policy_container(heap.allocate<HTML::PolicyContainer>(heap));
+ request.set_policy_container(HTML::PolicyContainer::create());
}
}
@@ -476,8 +474,8 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam
// 8. If request’s referrer policy is the empty string, then set request’s referrer policy to request’s policy
// container’s referrer policy.
if (request->referrer_policy() == ReferrerPolicy::ReferrerPolicy::EmptyString) {
- VERIFY(request->policy_container().has<GC::Ref<HTML::PolicyContainer>>());
- request->set_referrer_policy(request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()->referrer_policy);
+ VERIFY(request->policy_container().has<NonnullRefPtr<HTML::PolicyContainer const>>());
+ request->set_referrer_policy(request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->referrer_policy);
}
// 9. If request’s referrer is not "no-referrer", then set request’s referrer to the result of invoking determine
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp
index 434357292b2..7c0fc58a4ea 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp
@@ -17,6 +17,7 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
+#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
namespace Web::Fetch::Infrastructure {
@@ -47,7 +48,6 @@ void Request::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_reserved_client);
visitor.visit(m_traversable_for_user_prompts);
visitor.visit(m_pending_responses);
- visitor.visit(m_policy_container);
}
// https://fetch.spec.whatwg.org/#concept-request-url
@@ -89,6 +89,16 @@ void Request::set_url(URL::URL url)
m_url_list.append(move(url));
}
+void Request::set_policy_container(PolicyContainerType policy_container)
+{
+ m_policy_container = move(policy_container);
+}
+
+void Request::set_policy_container(NonnullRefPtr<HTML::PolicyContainer const> policy_container)
+{
+ m_policy_container = move(policy_container);
+}
+
// https://fetch.spec.whatwg.org/#request-destination-script-like
bool Request::destination_is_script_like() const
{
@@ -382,7 +392,7 @@ bool Request::cross_origin_embedder_policy_allows_credentials() const
return true;
// 4. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
- if (m_policy_container.has<GC::Ref<HTML::PolicyContainer>>() && m_policy_container.get<GC::Ref<HTML::PolicyContainer>>()->embedder_policy.value != HTML::EmbedderPolicyValue::Credentialless)
+ if (m_policy_container.has<NonnullRefPtr<HTML::PolicyContainer const>>() && m_policy_container.get<NonnullRefPtr<HTML::PolicyContainer const>>()->embedder_policy.value != HTML::EmbedderPolicyValue::Credentialless)
return true;
// 5. If request’s origin is same origin with request’s current URL’s origin and request’s redirect-taint is not
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h
index 96e9d133ca1..49839fd5229 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h
@@ -160,7 +160,7 @@ public:
using BodyType = Variant<Empty, ByteBuffer, GC::Ref<Body>>;
using OriginType = Variant<Origin, URL::Origin>;
- using PolicyContainerType = Variant<PolicyContainer, GC::Ref<HTML::PolicyContainer>>;
+ using PolicyContainerType = Variant<PolicyContainer, NonnullRefPtr<HTML::PolicyContainer const>>;
using ReferrerType = RequestReferrerType;
using ReservedClientType = GC::Ptr<HTML::Environment>;
using TraversableForUserPromptsType = Variant<TraversableForUserPrompts, GC::Ptr<HTML::EnvironmentSettingsObject>, GC::Ptr<HTML::Navigable>>;
@@ -223,7 +223,8 @@ public:
void set_top_level_navigation_initiator_origin(Optional<URL::Origin> top_level_navigation_initiator_origin) { m_top_level_navigation_initiator_origin = move(top_level_navigation_initiator_origin); }
[[nodiscard]] PolicyContainerType const& policy_container() const { return m_policy_container; }
- void set_policy_container(PolicyContainerType policy_container) { m_policy_container = move(policy_container); }
+ void set_policy_container(PolicyContainerType);
+ void set_policy_container(NonnullRefPtr<HTML::PolicyContainer const>);
[[nodiscard]] Mode mode() const { return m_mode; }
void set_mode(Mode mode) { m_mode = mode; }
diff --git a/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Libraries/LibWeb/HTML/BrowsingContext.cpp
index 69239537b0c..b846807af08 100644
--- a/Libraries/LibWeb/HTML/BrowsingContext.cpp
+++ b/Libraries/LibWeb/HTML/BrowsingContext.cpp
@@ -249,7 +249,7 @@ BrowsingContext::BrowsingContextAndDocument BrowsingContext::create_a_new_browsi
document->set_referrer(utf16_string_from_url_ascii(creator->url().serialize()));
// 2. Set document's policy container to a clone of creator's policy container.
- document->set_policy_container(creator->policy_container()->clone(document->heap()));
+ document->set_policy_container(creator->policy_container()->clone());
// 3. If creator's origin is same origin with creator's relevant settings object's top-level origin,
if (creator->origin().is_same_origin(creator->relevant_settings_object().top_level_origin.value())) {
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
index 87469f0a183..21bdaaa7e33 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
@@ -455,7 +455,7 @@ GC::Ptr<Fetch::Infrastructure::Request> HTMLLinkElement::create_link_request(HTM
auto request = create_potential_CORS_request(*url, options.destination, options.crossorigin);
// 6. Set request's policy container to options's policy container.
- request->set_policy_container(GC::Ref { *options.policy_container });
+ request->set_policy_container(options.policy_container);
// 7. Set request's integrity metadata to options's integrity.
request->set_integrity_metadata(options.integrity);
@@ -1713,7 +1713,7 @@ HTMLLinkElement::LinkProcessingOptions::LinkProcessingOptions(
URL::URL base_url,
URL::Origin origin,
GC::Ref<HTML::EnvironmentSettingsObject> environment,
- GC::Ref<HTML::PolicyContainer> policy_container,
+ NonnullRefPtr<HTML::PolicyContainer> policy_container,
GC::Ptr<Web::DOM::Document> document,
Utf16String cryptographic_nonce_metadata,
Fetch::Infrastructure::Request::Priority fetch_priority)
@@ -1734,7 +1734,6 @@ void HTMLLinkElement::LinkProcessingOptions::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(environment);
- visitor.visit(policy_container);
visitor.visit(document);
visitor.visit(on_document_ready);
}
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.h
index 81f973fcd40..1dc2e564db9 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h
@@ -90,7 +90,7 @@ private:
URL::URL base_url,
URL::Origin origin,
GC::Ref<HTML::EnvironmentSettingsObject> environment,
- GC::Ref<HTML::PolicyContainer> policy_container,
+ NonnullRefPtr<HTML::PolicyContainer> policy_container,
GC::Ptr<Web::DOM::Document> document,
Utf16String cryptographic_nonce_metadata,
Fetch::Infrastructure::Request::Priority fetch_priority);
@@ -143,7 +143,7 @@ private:
// policy container
// A policy container
- GC::Ref<HTML::PolicyContainer> policy_container;
+ NonnullRefPtr<HTML::PolicyContainer> policy_container;
// document (default null)
// Null or a Document
diff --git a/Libraries/LibWeb/HTML/HTMLMetaElement.cpp b/Libraries/LibWeb/HTML/HTMLMetaElement.cpp
index 771ab7b167b..42294056e7a 100644
--- a/Libraries/LibWeb/HTML/HTMLMetaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMetaElement.cpp
@@ -208,18 +208,18 @@ void HTMLMetaElement::inserted()
// 3. Let policy be the result of executing Content Security Policy's parse a serialized Content Security
// Policy algorithm on the meta element's content attribute's value, with a source of "meta", and a
// disposition of "enforce".
- auto policy = ContentSecurityPolicy::Policy::parse_a_serialized_csp(GC::Heap::the(), input, ContentSecurityPolicy::Policy::Source::Meta, ContentSecurityPolicy::Policy::Disposition::Enforce);
+ auto policy = ContentSecurityPolicy::Policy::parse_a_serialized_csp(input, ContentSecurityPolicy::Policy::Source::Meta, ContentSecurityPolicy::Policy::Disposition::Enforce);
// 4. Remove all occurrences of the report-uri, frame-ancestors, and sandbox directives from policy.
- policy->remove_directive({}, ContentSecurityPolicy::Directives::Names::ReportUri);
- policy->remove_directive({}, ContentSecurityPolicy::Directives::Names::FrameAncestors);
- policy->remove_directive({}, ContentSecurityPolicy::Directives::Names::Sandbox);
+ policy->remove_directive(ContentSecurityPolicy::Directives::Names::ReportUri);
+ policy->remove_directive(ContentSecurityPolicy::Directives::Names::FrameAncestors);
+ policy->remove_directive(ContentSecurityPolicy::Directives::Names::Sandbox);
// FIXME: File spec issue stating the policy's self origin isn't set here.
- policy->set_self_origin({}, document().origin());
+ policy->set_self_origin(document().origin());
// 5. Enforce the policy policy.
- document().relevant_settings_object().policy_container()->csp_list->enforce_policy(policy);
+ document().relevant_settings_object().policy_container()->csp_list.enforce_policy(policy);
break;
}
default:
diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp b/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
index 18bdacf1bec..4c5d4ad069d 100644
--- a/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
@@ -158,11 +158,11 @@ void HTMLOrSVGOrMathMLElement<ElementBase>::inserted()
// 1. Let CSP list be element's shadow-including root's policy container's CSP list.
// NB: A browsing-context connected element's shadow-including root is its node document.
- auto csp_list = element.document().policy_container()->csp_list;
+ auto const& csp_list = element.document().policy_container()->csp_list;
// 2. If CSP list contains a header-delivered Content Security Policy, and element has a
// nonce content attribute whose value is not the empty string, then:
- if (csp_list->contains_header_delivered_policy() && element.has_attribute(HTML::AttributeNames::nonce)) {
+ if (csp_list.contains_header_delivered_policy() && element.has_attribute(HTML::AttributeNames::nonce)) {
// 2.1. Let nonce be element's [[CryptographicNonce]].
auto nonce = this->nonce();
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index fafa6ac2733..e15c31b771d 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -157,7 +157,7 @@ struct NavigationParamsFetchStateHolder : public JS::Cell {
GC::Ptr<Fetch::Infrastructure::FetchController> fetch_controller;
OpenerPolicyEnforcementResult coop_enforcement_result;
SandboxingFlagSet final_sandbox_flags {};
- GC::Ptr<PolicyContainer> response_policy_container;
+ RefPtr<PolicyContainer> response_policy_container;
OpenerPolicy response_coop {};
ErrorOr<Optional<URL::URL>> location_url { OptionalNone {} };
URL::URL current_url;
@@ -204,7 +204,6 @@ struct NavigationParamsFetchStateHolder : public JS::Cell {
Base::visit_edges(visitor);
visitor.visit(response);
visitor.visit(fetch_controller);
- visitor.visit(response_policy_container);
visitor.visit(commit_early_hints);
visitor.visit(request);
visitor.visit(navigable);
@@ -2001,19 +2000,18 @@ bool LocalNavigable::is_familiar_with(Navigable& other)
}
// https://html.spec.whatwg.org/multipage/browsers.html#determining-navigation-params-policy-container
-static GC::Ref<PolicyContainer> determine_navigation_params_policy_container(URL::URL const& response_url,
- GC::Heap& heap,
- GC::Ptr<PolicyContainer> history_policy_container,
- GC::Ptr<PolicyContainer> initiator_policy_container,
- GC::Ptr<PolicyContainer> parent_policy_container,
- GC::Ptr<PolicyContainer> response_policy_container)
+static NonnullRefPtr<PolicyContainer> determine_navigation_params_policy_container(URL::URL const& response_url,
+ RefPtr<PolicyContainer> history_policy_container,
+ RefPtr<PolicyContainer> initiator_policy_container,
+ RefPtr<PolicyContainer> parent_policy_container,
+ RefPtr<PolicyContainer> response_policy_container)
{
// 1. If historyPolicyContainer is not null, then:
if (history_policy_container) {
// FIXME: 1. Assert: responseURL requires storing the policy container in history.
// 2. Return a clone of historyPolicyContainer.
- return history_policy_container->clone(heap);
+ return history_policy_container->clone();
}
// 2. If responseURL is about:srcdoc, then:
@@ -2022,20 +2020,20 @@ static GC::Ref<PolicyContainer> determine_navigation_params_policy_container(URL
VERIFY(parent_policy_container);
// 2. Return a clone of parentPolicyContainer.
- return parent_policy_container->clone(heap);
+ return parent_policy_container->clone();
}
// 3. If responseURL is local and initiatorPolicyContainer is not null, then return a clone of initiatorPolicyContainer.
if (Fetch::Infrastructure::is_local_url(response_url) && initiator_policy_container)
- return initiator_policy_container->clone(heap);
+ return initiator_policy_container->clone();
// 4. If responsePolicyContainer is not null, then return responsePolicyContainer.
// FIXME: File a spec issue to say "a clone of" here for consistency
if (response_policy_container)
- return response_policy_container->clone(heap);
+ return response_policy_container->clone();
// 5. Return a new policy container.
- return heap.allocate<PolicyContainer>(heap);
+ return PolicyContainer::create();
}
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#hand-off-to-external-software
@@ -2139,22 +2137,22 @@ static GC::Ref<NavigationParams> create_navigation_params_from_a_srcdoc_resource
// 6. Let policyContainer be the result of determining navigation params policy container given response's URL,
// entry's document state's history policy container, null, navigable's container document's policy container, and null.
- GC::Ptr<PolicyContainer> history_policy_container = history_policy_container_variant.visit(
- [&](SerializedPolicyContainer const& s) -> GC::Ptr<PolicyContainer> { return create_a_policy_container_from_serialized_policy_container(s); },
- [](DocumentState::Client) -> GC::Ptr<PolicyContainer> { return {}; });
- GC::Ptr<PolicyContainer> policy_container;
+ RefPtr<PolicyContainer> history_policy_container = history_policy_container_variant.visit(
+ [&](SerializedPolicyContainer const& s) -> RefPtr<PolicyContainer> { return create_a_policy_container_from_serialized_policy_container(s); },
+ [](DocumentState::Client) -> RefPtr<PolicyContainer> { return {}; });
+ RefPtr<PolicyContainer> policy_container;
if (navigable->container()) {
// NOTE: Specification assumes that only navigables corresponding to iframes can be navigated to about:srcdoc.
// We also use srcdoc to implement load_html() for top level navigables so we need to null check container
// because it might be null.
- policy_container = determine_navigation_params_policy_container(*response->url(), realm.heap(), history_policy_container, {}, navigable->container_document()->policy_container(), {});
+ policy_container = determine_navigation_params_policy_container(*response->url(), history_policy_container, {}, navigable->container_document()->policy_container(), {});
} else if (navigable->parent()) {
// NB: The container document is in another process. Only its iframe element's srcdoc attribute navigates the
// navigable to about:srcdoc, so the container document is the source document, whose policy container the
// source snapshot params hold.
- policy_container = determine_navigation_params_policy_container(*response->url(), realm.heap(), history_policy_container, {}, source_snapshot_params.source_policy_container, {});
+ policy_container = determine_navigation_params_policy_container(*response->url(), history_policy_container, {}, source_snapshot_params.source_policy_container, {});
} else {
- policy_container = realm.heap().allocate<PolicyContainer>(realm.heap());
+ policy_container = PolicyContainer::create();
}
// 7. Return a new navigation params, with
@@ -2343,7 +2341,7 @@ static void perform_navigation_params_fetch(JS::Realm& realm, GC::Ref<Navigation
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();
+ state_holder->final_sandbox_flags = state_holder->target_snapshot_params.sandboxing_flags | state_holder->response_policy_container->csp_list.csp_derived_sandboxing_flags();
// 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);
@@ -2762,10 +2760,10 @@ static void create_navigation_params_by_fetching(
// 25. Let resultPolicyContainer be the result of determining navigation params policy container given response's URL,
// entry's document state's history policy container, sourceSnapshotParams's source policy container, null, and responsePolicyContainer.
- GC::Ptr<PolicyContainer> history_policy_container = state_holder->history_policy_container.visit(
- [&](SerializedPolicyContainer const& s) -> GC::Ptr<PolicyContainer> { return create_a_policy_container_from_serialized_policy_container(s); },
- [](DocumentState::Client) -> GC::Ptr<PolicyContainer> { return {}; });
- auto result_policy_container = determine_navigation_params_policy_container(*state_holder->response->url(), realm.heap(), history_policy_container, state_holder->source_snapshot_params->source_policy_container, {}, state_holder->response_policy_container);
+ RefPtr<PolicyContainer> history_policy_container = state_holder->history_policy_container.visit(
+ [&](SerializedPolicyContainer const& s) -> RefPtr<PolicyContainer> { return create_a_policy_container_from_serialized_policy_container(s); },
+ [](DocumentState::Client) -> RefPtr<PolicyContainer> { return {}; });
+ auto result_policy_container = determine_navigation_params_policy_container(*state_holder->response->url(), history_policy_container, state_holder->source_snapshot_params->source_policy_container, {}, state_holder->response_policy_container);
// 26. If navigable's container is an iframe, and response's timing allow passed flag is set,
// then set navigable's container's pending resource-timing start time to null.
@@ -3282,14 +3280,14 @@ void LocalNavigable::continue_navigation_after_population_dispatch(PreparedNavig
// 1. Let sourcePolicyContainer be a clone of the sourceDocument's policy container, if
// sourceDocument is not null; otherwise null.
// NB: sourceDocument is null exactly for a "browser UI" user involvement, by steps 5 and 6.
- GC::Ptr<PolicyContainer> source_policy_container;
+ RefPtr<PolicyContainer> source_policy_container;
if (navigation.user_involvement != UserNavigationInvolvement::BrowserUI)
source_policy_container = source_snapshot_params->source_policy_container;
// 2. Let policyContainer be the result of determining navigation params policy container given
// response's URL, null, sourcePolicyContainer, navigable's container document's policy container,
// and null.
- GC::Ptr<PolicyContainer> parent_policy_container;
+ RefPtr<PolicyContainer> parent_policy_container;
if (auto container_document = this->container_document())
parent_policy_container = container_document->policy_container();
else if (*response_url == URL::about_srcdoc() && parent() && source_policy_container) {
@@ -3300,13 +3298,13 @@ void LocalNavigable::continue_navigation_after_population_dispatch(PreparedNavig
// NOTE: Specification assumes that only navigables corresponding to iframes can be navigated to about:srcdoc.
// We also use srcdoc to implement load_html() for top level navigables so we need a policy container
// because the navigable might not have a container.
- parent_policy_container = heap().allocate<PolicyContainer>(heap());
+ parent_policy_container = PolicyContainer::create();
}
- auto policy_container = determine_navigation_params_policy_container(*response_url, heap(), {}, source_policy_container, parent_policy_container, {});
+ auto policy_container = determine_navigation_params_policy_container(*response_url, {}, source_policy_container, parent_policy_container, {});
// 3. Let finalSandboxFlags be the union of targetSnapshotParams's sandboxing flags and
// policyContainer's CSP list's CSP-derived sandboxing flags.
- auto final_sandbox_flags = population_request.target_snapshot_params.sandboxing_flags | policy_container->csp_list->csp_derived_sandboxing_flags();
+ auto final_sandbox_flags = population_request.target_snapshot_params.sandboxing_flags | policy_container->csp_list.csp_derived_sandboxing_flags();
// 4. Let responseOrigin be the result of determining the origin given response's URL,
// finalSandboxFlags, and documentState's initiator origin.
@@ -3994,7 +3992,7 @@ GC::Ptr<DOM::Document> LocalNavigable::evaluate_javascript_url(URL::URL const& u
auto const& policy_container = active_document()->policy_container();
// 13. Let finalSandboxFlags be policyContainer's CSP list's CSP-derived sandboxing flags.
- auto final_sandbox_flags = policy_container->csp_list->csp_derived_sandboxing_flags();
+ auto final_sandbox_flags = policy_container->csp_list.csp_derived_sandboxing_flags();
// 14. Let coop be targetNavigable's active document's opener policy.
auto const& coop = active_document()->opener_policy();
diff --git a/Libraries/LibWeb/HTML/NavigationParams.cpp b/Libraries/LibWeb/HTML/NavigationParams.cpp
index 7a68ba7d052..9b3d3834dc8 100644
--- a/Libraries/LibWeb/HTML/NavigationParams.cpp
+++ b/Libraries/LibWeb/HTML/NavigationParams.cpp
@@ -26,7 +26,6 @@ void NavigationParams::visit_edges(Visitor& visitor)
visitor.visit(fetch_controller);
visitor.visit(commit_early_hints);
visitor.visit(reserved_environment);
- visitor.visit(policy_container);
}
void NonFetchSchemeNavigationParams::visit_edges(Visitor& visitor)
@@ -36,7 +35,7 @@ void NonFetchSchemeNavigationParams::visit_edges(Visitor& visitor)
}
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#check-a-navigation-response's-adherence-to-x-frame-options
-bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::Infrastructure::Response> response, LocalNavigable* navigable, GC::Ref<ContentSecurityPolicy::PolicyList const> csp_list, URL::Origin destination_origin)
+bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::Infrastructure::Response> response, LocalNavigable* navigable, ContentSecurityPolicy::PolicyList const& csp_list, URL::Origin destination_origin)
{
// 1. If navigable is not a child navigable, then return true.
if (!navigable->parent()) {
@@ -44,7 +43,7 @@ bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::In
}
// 2. For each policy of cspList:
- for (auto const policy : csp_list->policies()) {
+ for (auto const& policy : csp_list.policies()) {
// 1. If policy's disposition is not "enforce", then continue.
if (policy->disposition() != ContentSecurityPolicy::Policy::Disposition::Enforce)
continue;
diff --git a/Libraries/LibWeb/HTML/NavigationParams.h b/Libraries/LibWeb/HTML/NavigationParams.h
index a8312b0fd2e..165790c6513 100644
--- a/Libraries/LibWeb/HTML/NavigationParams.h
+++ b/Libraries/LibWeb/HTML/NavigationParams.h
@@ -81,7 +81,7 @@ struct NavigationParams : GC::Cell {
URL::Origin origin;
// a policy container to use for the new Document
- GC::Ptr<PolicyContainer> policy_container;
+ RefPtr<PolicyContainer> policy_container;
// a sandboxing flag set to impose on the new Document
SandboxingFlagSet final_sandboxing_flag_set = {};
@@ -114,7 +114,7 @@ protected:
OpenerPolicyEnforcementResult coop_enforcement_result,
Fetch::Infrastructure::Request::ReservedClientType reserved_environment,
URL::Origin origin,
- GC::Ptr<PolicyContainer> policy_container,
+ RefPtr<PolicyContainer> policy_container,
SandboxingFlagSet final_sandboxing_flag_set,
ReferrerPolicy::ReferrerPolicy iframe_element_referrer_policy,
OpenerPolicy opener_policy,
@@ -196,6 +196,6 @@ protected:
using NavigationParamsVariant = Variant<NavigationParamsNullOrError, GC::Ref<NavigationParams>, GC::Ref<NonFetchSchemeNavigationParams>>;
-bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::Infrastructure::Response> response, LocalNavigable* navigable, GC::Ref<ContentSecurityPolicy::PolicyList const> csp_list, URL::Origin destination_origin);
+bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::Infrastructure::Response> response, LocalNavigable* navigable, ContentSecurityPolicy::PolicyList const& csp_list, URL::Origin destination_origin);
}
diff --git a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
index 02b5bb76106..ebc32c99c49 100644
--- a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
+++ b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
@@ -36,7 +36,7 @@ static SerializedPolicyContainer serialize_request_policy_container(Fetch::Infra
VERIFY(request.client());
return request.client()->policy_container()->serialize();
},
- [](GC::Ref<PolicyContainer> policy_container) {
+ [](NonnullRefPtr<PolicyContainer const> const& policy_container) {
return policy_container->serialize();
});
}
diff --git a/Libraries/LibWeb/HTML/PolicyContainers.cpp b/Libraries/LibWeb/HTML/PolicyContainers.cpp
index 7ba4dddd60a..4ca7266d112 100644
--- a/Libraries/LibWeb/HTML/PolicyContainers.cpp
+++ b/Libraries/LibWeb/HTML/PolicyContainers.cpp
@@ -20,11 +20,9 @@
namespace Web::HTML {
-GC_DEFINE_ALLOCATOR(PolicyContainer);
-
-PolicyContainer::PolicyContainer(GC::Heap& heap)
- : csp_list(heap.allocate<ContentSecurityPolicy::PolicyList>())
+NonnullRefPtr<PolicyContainer> PolicyContainer::create()
{
+ return adopt_ref(*new PolicyContainer);
}
// https://html.spec.whatwg.org/multipage/browsers.html#requires-storing-the-policy-container-in-history
@@ -40,17 +38,15 @@ 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> environment)
+NonnullRefPtr<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();
-
// 1. If response's URL's scheme is "blob", then return a clone of response's URL's blob URL entry's environment's
// policy container.
// NB: The entry is in this process's store if this process added it, and otherwise in the user agent's, which has
// the environment's policy container serialized. An entry that is gone has no policy container to clone.
if (auto const& response_url = response->url(); response_url.has_value() && response_url->scheme() == "blob"sv) {
if (auto entry = FileAPI::local_blob_url_entry(*response_url); entry.has_value())
- return entry->environment->policy_container()->clone(heap);
+ return entry->environment->policy_container()->clone();
if (auto* settings = as_if<EnvironmentSettingsObject>(environment.ptr())) {
if (auto entry = FileAPI::blob_url_entry_in_the_user_agent_store(Bindings::principal_host_defined_page(settings->realm()), *response_url); entry.has_value())
return create_a_policy_container_from_serialized_policy_container(entry->environment_policy_container);
@@ -58,10 +54,10 @@ GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref
}
// 2. Let result be a new policy container.
- GC::Ref<PolicyContainer> result = heap.allocate<PolicyContainer>(heap);
+ auto result = PolicyContainer::create();
// 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);
+ result->csp_list = ContentSecurityPolicy::Policy::parse_a_responses_content_security_policies(response);
// 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".
@@ -92,12 +88,10 @@ GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref
return result;
}
-GC::Ref<PolicyContainer> create_a_policy_container_from_serialized_policy_container(SerializedPolicyContainer const& serialized_policy_container)
+NonnullRefPtr<PolicyContainer> create_a_policy_container_from_serialized_policy_container(SerializedPolicyContainer const& serialized_policy_container)
{
- auto& heap = GC::Heap::the();
-
- GC::Ref<PolicyContainer> result = heap.allocate<PolicyContainer>(heap);
- result->csp_list = ContentSecurityPolicy::PolicyList::create(heap, serialized_policy_container.csp_list);
+ auto result = PolicyContainer::create();
+ result->csp_list = ContentSecurityPolicy::PolicyList { serialized_policy_container.csp_list };
result->embedder_policy = serialized_policy_container.embedder_policy;
result->referrer_policy = serialized_policy_container.referrer_policy;
result->force_load_at_top = serialized_policy_container.force_load_at_top;
@@ -105,13 +99,14 @@ GC::Ref<PolicyContainer> create_a_policy_container_from_serialized_policy_contai
}
// https://html.spec.whatwg.org/multipage/browsers.html#clone-a-policy-container
-GC::Ref<PolicyContainer> PolicyContainer::clone(GC::Heap& heap) const
+NonnullRefPtr<PolicyContainer> PolicyContainer::clone() const
{
// 1. Let clone be a new policy container.
- auto clone = heap.allocate<PolicyContainer>(heap);
+ auto clone = create();
// 2. For each policy in policyContainer's CSP list, append a copy of policy into clone's CSP list.
- clone->csp_list = csp_list->clone(heap);
+ // NB: A policy in a CSP list is immutable and shared by its clones.
+ clone->csp_list = csp_list;
// 3. Set clone's embedder policy to a copy of policyContainer's embedder policy.
// NOTE: This is a C++ copy.
@@ -135,17 +130,11 @@ GC::Ref<PolicyContainer> PolicyContainer::clone(GC::Heap& heap) const
SerializedPolicyContainer PolicyContainer::serialize() const
{
return SerializedPolicyContainer {
- .csp_list = csp_list->serialize(),
+ .csp_list = csp_list.serialize(),
.embedder_policy = embedder_policy,
.referrer_policy = referrer_policy,
.force_load_at_top = force_load_at_top,
};
}
-void PolicyContainer::visit_edges(Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(csp_list);
-}
-
}
diff --git a/Libraries/LibWeb/HTML/PolicyContainers.h b/Libraries/LibWeb/HTML/PolicyContainers.h
index 6a4bca83b2b..58aeec77bf3 100644
--- a/Libraries/LibWeb/HTML/PolicyContainers.h
+++ b/Libraries/LibWeb/HTML/PolicyContainers.h
@@ -7,10 +7,12 @@
#pragma once
+#include <AK/AtomicRefCounted.h>
+#include <AK/NonnullRefPtr.h>
#include <AK/Utf16String.h>
-#include <LibGC/CellAllocator.h>
-#include <LibJS/Heap/Cell.h>
+#include <LibGC/Ptr.h>
#include <LibURL/Forward.h>
+#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Forward.h>
#include <LibWebCommon/HTML/EmbedderPolicy.h>
@@ -29,16 +31,12 @@ struct IntegrityPolicy {
// https://html.spec.whatwg.org/multipage/origin.html#policy-container
// A policy container is a struct containing policies that apply to a Document, a WorkerGlobalScope, or a WorkletGlobalScope. It has the following items:
-struct PolicyContainer : public GC::Cell {
- GC_CELL(PolicyContainer, GC::Cell)
- GC_DECLARE_ALLOCATOR(PolicyContainer);
-
-public:
- virtual ~PolicyContainer() = default;
+struct PolicyContainer final : public AtomicRefCounted<PolicyContainer> {
+ [[nodiscard]] static NonnullRefPtr<PolicyContainer> create();
// https://html.spec.whatwg.org/multipage/origin.html#policy-container-csp-list
// A CSP list, which is a CSP list. It is initially empty.
- GC::Ref<ContentSecurityPolicy::PolicyList> csp_list;
+ ContentSecurityPolicy::PolicyList csp_list;
// https://html.spec.whatwg.org/multipage/origin.html#policy-container-embedder-policy
// An embedder policy, which is an embedder policy. It is initially a new embedder policy.
@@ -61,22 +59,19 @@ public:
// "force-load-at-top". Its type is boolean with default value false.
bool force_load_at_top { false };
- [[nodiscard]] GC::Ref<PolicyContainer> clone(GC::Heap&) const;
+ [[nodiscard]] NonnullRefPtr<PolicyContainer> clone() const;
[[nodiscard]] SerializedPolicyContainer serialize() const;
-protected:
- virtual void visit_edges(Cell::Visitor&) override;
-
private:
- PolicyContainer(GC::Heap&);
+ PolicyContainer() = default;
};
// https://html.spec.whatwg.org/multipage/browsers.html#requires-storing-the-policy-container-in-history
[[nodiscard]] 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
-[[nodiscard]] GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ptr<Environment> environment);
+[[nodiscard]] NonnullRefPtr<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ptr<Environment> environment);
-[[nodiscard]] GC::Ref<PolicyContainer> create_a_policy_container_from_serialized_policy_container(SerializedPolicyContainer const&);
+[[nodiscard]] NonnullRefPtr<PolicyContainer> create_a_policy_container_from_serialized_policy_container(SerializedPolicyContainer const&);
}
diff --git a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
index 395d67e54b4..7e88cfd5eaf 100644
--- a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.cpp
@@ -37,7 +37,6 @@ EnvironmentSettingsSnapshot::~EnvironmentSettingsSnapshot() = default;
void EnvironmentSettingsSnapshot::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
- visitor.visit(m_policy_container);
visitor.visit(m_report_violation);
}
diff --git a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
index d7bcc7d0115..1cf13425a89 100644
--- a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
+++ b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h
@@ -28,7 +28,7 @@ public:
virtual URL::URL api_base_url() const override { return m_url; }
virtual URL::Origin origin() const override { return m_origin; }
virtual bool has_cross_site_ancestor() const override { return m_has_cross_site_ancestor; }
- virtual GC::Ref<PolicyContainer> policy_container() const override { return m_policy_container; }
+ virtual NonnullRefPtr<PolicyContainer> policy_container() const override { return m_policy_container; }
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override { return m_cross_origin_isolated_capability; }
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
@@ -52,7 +52,7 @@ private:
URL::URL m_url;
URL::Origin m_origin;
bool m_has_cross_site_ancestor;
- GC::Ref<PolicyContainer> m_policy_container;
+ NonnullRefPtr<PolicyContainer> m_policy_container;
CanUseCrossOriginIsolatedAPIs m_cross_origin_isolated_capability { CanUseCrossOriginIsolatedAPIs::No };
Optional<u64> m_agent_cluster_id;
CrossOriginIsolationMode m_agent_cluster_cross_origin_isolation_mode { CrossOriginIsolationMode::None };
diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
index 6db11270aca..81a9aeddbbd 100644
--- a/Libraries/LibWeb/HTML/Scripting/Environments.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
@@ -761,7 +761,7 @@ SerializedEnvironmentSettingsObject EnvironmentSettingsObject::serialize()
template<typename Callback>
static void for_each_enforced_policy_with_upgrade_insecure_requests(EnvironmentSettingsObject const& settings, Callback callback)
{
- for (auto const& policy : settings.policy_container()->csp_list->policies()) {
+ for (auto const& policy : settings.policy_container()->csp_list.policies()) {
if (policy->disposition() != ContentSecurityPolicy::Policy::Disposition::Enforce)
continue;
if (!policy->contains_directive_with_name(ContentSecurityPolicy::Directives::Names::UpgradeInsecureRequests))
diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.h b/Libraries/LibWeb/HTML/Scripting/Environments.h
index 6af2a627bea..489a37edc84 100644
--- a/Libraries/LibWeb/HTML/Scripting/Environments.h
+++ b/Libraries/LibWeb/HTML/Scripting/Environments.h
@@ -104,7 +104,7 @@ public:
virtual bool has_cross_site_ancestor() const = 0;
// A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
- virtual GC::Ref<PolicyContainer> policy_container() const = 0;
+ virtual NonnullRefPtr<PolicyContainer> policy_container() const = 0;
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const = 0;
diff --git a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
index 5d9186bffa1..b776a890407 100644
--- a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
@@ -139,7 +139,7 @@ bool WindowEnvironmentSettingsObject::has_cross_site_ancestor() const
}
// https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-policy-container
-GC::Ref<PolicyContainer> WindowEnvironmentSettingsObject::policy_container() const
+NonnullRefPtr<PolicyContainer> WindowEnvironmentSettingsObject::policy_container() const
{
// Return the policy container of window's associated Document.
return m_window->associated_document().policy_container();
diff --git a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h
index 3110bdada57..d165893f675 100644
--- a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h
+++ b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h
@@ -24,7 +24,7 @@ public:
virtual URL::URL api_base_url() const override;
virtual URL::Origin origin() const override;
virtual bool has_cross_site_ancestor() const override;
- virtual GC::Ref<PolicyContainer> policy_container() const override;
+ virtual NonnullRefPtr<PolicyContainer> policy_container() const override;
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override;
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
index 6f0cd2cc2d7..a527f112980 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
@@ -11,6 +11,7 @@
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
+#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
@@ -115,7 +116,7 @@ bool WorkerEnvironmentSettingsObject::has_cross_site_ancestor() const
}
// https://html.spec.whatwg.org/multipage/workers.html#script-settings-for-workers:concept-settings-object-policy-container
-GC::Ref<PolicyContainer> WorkerEnvironmentSettingsObject::policy_container() const
+NonnullRefPtr<PolicyContainer> WorkerEnvironmentSettingsObject::policy_container() const
{
// Return worker global scope's policy container.
return m_global_scope->policy_container();
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
index 754257092c1..ab4daeddf59 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
+++ b/Libraries/LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h
@@ -37,7 +37,7 @@ public:
virtual URL::URL api_base_url() const override;
virtual URL::Origin origin() const override;
virtual bool has_cross_site_ancestor() const override;
- virtual GC::Ref<PolicyContainer> policy_container() const override;
+ virtual NonnullRefPtr<PolicyContainer> policy_container() const override;
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override;
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.cpp
index 094ddc585df..f075d649deb 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.cpp
@@ -23,7 +23,7 @@ GC::Ref<WorkletEnvironmentSettingsObject> WorkletEnvironmentSettingsObject::setu
auto inherited_api_base_url = outside_settings.api_base_url();
// 2. Let inheritedPolicyContainer be a clone of outsideSettings's policy container.
- auto inherited_policy_container = outside_settings.policy_container()->clone(GC::Heap::the());
+ auto inherited_policy_container = outside_settings.policy_container()->clone();
// 3. Let realm be the value of realmExecutionContext's Realm component.
auto realm = execution_context->realm;
@@ -69,7 +69,7 @@ GC::Ref<WorkletEnvironmentSettingsObject> WorkletEnvironmentSettingsObject::setu
return settings_object;
}
-WorkletEnvironmentSettingsObject::WorkletEnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext> execution_context, GC::Ref<WorkletGlobalScope> global_scope, URL::URL api_base_url, URL::Origin origin, bool has_cross_site_ancestor, GC::Ref<PolicyContainer> policy_container, CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability, Optional<u64> agent_cluster_id, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, double time_origin)
+WorkletEnvironmentSettingsObject::WorkletEnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext> execution_context, GC::Ref<WorkletGlobalScope> global_scope, URL::URL api_base_url, URL::Origin origin, bool has_cross_site_ancestor, NonnullRefPtr<PolicyContainer> policy_container, CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability, Optional<u64> agent_cluster_id, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, double time_origin)
: EnvironmentSettingsObject(move(execution_context))
, m_global_scope(global_scope)
, m_api_base_url(move(api_base_url))
@@ -83,7 +83,7 @@ WorkletEnvironmentSettingsObject::WorkletEnvironmentSettingsObject(NonnullOwnPtr
{
}
-GC::Ref<PolicyContainer> WorkletEnvironmentSettingsObject::policy_container() const
+NonnullRefPtr<PolicyContainer> WorkletEnvironmentSettingsObject::policy_container() const
{
return m_policy_container;
}
@@ -92,7 +92,6 @@ void WorkletEnvironmentSettingsObject::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_global_scope);
- visitor.visit(m_policy_container);
}
}
diff --git a/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.h b/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.h
index ca63533baae..3db7cae4080 100644
--- a/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.h
+++ b/Libraries/LibWeb/HTML/Scripting/WorkletEnvironmentSettingsObject.h
@@ -28,14 +28,14 @@ public:
virtual URL::URL api_base_url() const override { return m_api_base_url; }
virtual URL::Origin origin() const override { return m_origin; }
virtual bool has_cross_site_ancestor() const override { return m_has_cross_site_ancestor; }
- virtual GC::Ref<PolicyContainer> policy_container() const override;
+ virtual NonnullRefPtr<PolicyContainer> policy_container() const override;
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const override { return m_cross_origin_isolated_capability; }
virtual Optional<u64> agent_cluster_id() const override { return m_agent_cluster_id; }
virtual CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode() const override { return m_agent_cluster_cross_origin_isolation_mode; }
virtual double time_origin() const override { return m_time_origin; }
private:
- WorkletEnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>, GC::Ref<WorkletGlobalScope>, URL::URL api_base_url, URL::Origin, bool has_cross_site_ancestor, GC::Ref<PolicyContainer>, CanUseCrossOriginIsolatedAPIs, Optional<u64> agent_cluster_id, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, double time_origin);
+ WorkletEnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>, GC::Ref<WorkletGlobalScope>, URL::URL api_base_url, URL::Origin, bool has_cross_site_ancestor, NonnullRefPtr<PolicyContainer>, CanUseCrossOriginIsolatedAPIs, Optional<u64> agent_cluster_id, CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, double time_origin);
virtual void visit_edges(JS::Cell::Visitor&) override;
@@ -44,7 +44,7 @@ private:
URL::URL m_api_base_url;
URL::Origin m_origin;
bool m_has_cross_site_ancestor { false };
- GC::Ref<PolicyContainer> m_policy_container;
+ NonnullRefPtr<PolicyContainer> m_policy_container;
CanUseCrossOriginIsolatedAPIs m_cross_origin_isolated_capability { CanUseCrossOriginIsolatedAPIs::No };
Optional<u64> m_agent_cluster_id;
CrossOriginIsolationMode m_agent_cluster_cross_origin_isolation_mode { CrossOriginIsolationMode::None };
diff --git a/Libraries/LibWeb/HTML/SourceSnapshotParams.cpp b/Libraries/LibWeb/HTML/SourceSnapshotParams.cpp
index f2b7f3b46a6..9b2b6846f56 100644
--- a/Libraries/LibWeb/HTML/SourceSnapshotParams.cpp
+++ b/Libraries/LibWeb/HTML/SourceSnapshotParams.cpp
@@ -36,7 +36,7 @@ GC::Ref<SourceSnapshotParams> snapshot_source_snapshot_params(GC::Ptr<DOM::Docum
nullptr,
// source policy container
// a new policy container
- heap.allocate<PolicyContainer>(heap));
+ PolicyContainer::create());
}
// 2. Return a new source snapshot params with
@@ -60,7 +60,7 @@ GC::Ref<SourceSnapshotParams> snapshot_source_snapshot_params(GC::Ptr<DOM::Docum
// source policy container
// a clone of sourceDocument's policy container
- source_document->policy_container()->clone(source_document->heap()),
+ source_document->policy_container()->clone(),
// https://wicg.github.io/scroll-to-text-fragment/#restricting-the-text-fragment
// Transfer the one-shot activation and browsing-context-group facts with the source snapshot.
@@ -73,7 +73,6 @@ void SourceSnapshotParams::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(fetch_client);
- visitor.visit(source_policy_container);
}
}
diff --git a/Libraries/LibWeb/HTML/SourceSnapshotParams.h b/Libraries/LibWeb/HTML/SourceSnapshotParams.h
index 37894ff5d69..03cfbfacca8 100644
--- a/Libraries/LibWeb/HTML/SourceSnapshotParams.h
+++ b/Libraries/LibWeb/HTML/SourceSnapshotParams.h
@@ -19,7 +19,7 @@ struct SourceSnapshotParams : public GC::Cell {
GC_DECLARE_ALLOCATOR(SourceSnapshotParams);
public:
- SourceSnapshotParams(bool has_transient_activation, SandboxingFlagSet sandboxing_flags, bool allows_downloading, GC::Ptr<EnvironmentSettingsObject> fetch_client, GC::Ref<PolicyContainer> source_policy_container, bool text_directive_user_activation = false, bool browsing_context_group_has_multiple_contexts = false, bool browsing_context_group_has_remote_contexts = false)
+ SourceSnapshotParams(bool has_transient_activation, SandboxingFlagSet sandboxing_flags, bool allows_downloading, GC::Ptr<EnvironmentSettingsObject> fetch_client, NonnullRefPtr<PolicyContainer> source_policy_container, bool text_directive_user_activation = false, bool browsing_context_group_has_multiple_contexts = false, bool browsing_context_group_has_remote_contexts = false)
: has_transient_activation(has_transient_activation)
, sandboxing_flags(sandboxing_flags)
, allows_downloading(allows_downloading)
@@ -46,7 +46,7 @@ public:
GC::Ptr<EnvironmentSettingsObject> fetch_client;
// a policy container
- GC::Ref<PolicyContainer> source_policy_container;
+ NonnullRefPtr<PolicyContainer> source_policy_container;
bool text_directive_user_activation { false };
bool browsing_context_group_has_multiple_contexts { false };
bool browsing_context_group_has_remote_contexts { false };
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
index 47c7fcdd6c2..30ca74c2b25 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
@@ -240,7 +240,7 @@ void WorkerAgentParent::report_content_security_policy_violation(u64 policy_inde
// only grows, so policy_index names the policy that was violated. The worker's process is less trusted than
// this one, so we only report violations of policies in outside settings' CSP list, and of worker-src, which
// is the effective directive of every worker script request.
- auto const& policies = m_outside_settings->policy_container()->csp_list->policies();
+ auto const& policies = m_outside_settings->policy_container()->csp_list.policies();
if (policy_index >= policies.size() || ContentSecurityPolicy::Directives::Names::WorkerSrc != effective_directive.bytes_as_string_view())
return;
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
index b7caedc71dc..c0fdf76bae8 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
@@ -63,7 +63,6 @@ void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_page);
visitor.visit(m_fonts);
visitor.visit(m_font_computer);
- visitor.visit(m_policy_container);
}
void WorkerGlobalScope::finalize()
@@ -186,10 +185,10 @@ CSS::FontComputer& WorkerGlobalScope::font_computer()
return *m_font_computer;
}
-GC::Ref<PolicyContainer> WorkerGlobalScope::policy_container() const
+NonnullRefPtr<PolicyContainer> WorkerGlobalScope::policy_container() const
{
if (!m_policy_container) {
- m_policy_container = GC::Heap::the().allocate<PolicyContainer>(GC::Heap::the());
+ m_policy_container = PolicyContainer::create();
}
return *m_policy_container;
}
@@ -255,7 +254,7 @@ ContentSecurityPolicy::Directives::Directive::Result WorkerGlobalScope::run_csp_
auto result = ContentSecurityPolicy::Directives::Directive::Result::Allowed;
// 2. For each policy of global’s CSP list:
- for (auto policy : policy_container()->csp_list->policies()) {
+ for (auto const& policy : policy_container()->csp_list.policies()) {
// 1. For each directive of policy:
for (auto const& directive : policy->directives()) {
// 1. Execute directive’s initialization algorithm on global. If its returned value is "Blocked", then set
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.h b/Libraries/LibWeb/HTML/WorkerGlobalScope.h
index 0cacd27ad39..0889a427ee2 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.h
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.h
@@ -131,7 +131,7 @@ public:
Web::Page* page() { return m_page.ptr(); }
- GC::Ref<PolicyContainer> policy_container() const;
+ NonnullRefPtr<PolicyContainer> policy_container() const;
bool is_closing() const { return m_closing; }
@@ -183,7 +183,7 @@ private:
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-policy-container
// A WorkerGlobalScope object has an associated policy container (a policy container). It is initially a new policy container.
- mutable GC::Ptr<PolicyContainer> m_policy_container;
+ mutable RefPtr<PolicyContainer> m_policy_container;
// https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-embedder-policy
// A WorkerGlobalScope object has an associated embedder policy (an embedder policy).
diff --git a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
index 127b8774f2d..e3fea7c416a 100644
--- a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
+++ b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
@@ -10,10 +10,13 @@
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/ContentSecurityPolicy/Violation.h>
+#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
@@ -40,6 +43,17 @@ static bool sink_group_matches(Utf16String const& directive_value, Utf16View sin
return value.equals_ignoring_ascii_case(sink_group);
}
+static ContentSecurityPolicy::PolicyList const* csp_list_of_global(JS::Object& global)
+{
+ if (auto* window = Bindings::window_from_global_object(global))
+ return &window->associated_document().policy_container()->csp_list;
+
+ if (auto* worker = Bindings::worker_global_scope_from_global_object(global))
+ return &worker->policy_container()->csp_list;
+
+ return nullptr;
+}
+
// https://www.w3.org/TR/trusted-types/#require-trusted-types-for-pre-navigation-check
ContentSecurityPolicy::Directives::Directive::Result require_trusted_types_for_pre_navigation_check(Fetch::Infrastructure::Request& request)
{
@@ -98,10 +112,10 @@ ContentSecurityPolicy::Directives::Directive::Result require_trusted_types_for_p
bool does_sink_require_trusted_types(JS::Object& global, Utf16View sink_group, IncludeReportOnlyPolicies include_report_only_policies)
{
// 1. For each policy in global’s CSP list:
- auto csp_list = ContentSecurityPolicy::PolicyList::from_object(global);
+ auto const* csp_list = csp_list_of_global(global);
if (!csp_list)
return false;
- for (auto const policy : csp_list->policies()) {
+ for (auto const& policy : csp_list->policies()) {
// 1. If policy’s directive set does not contain a directive whose name is "require-trusted-types-for", skip to the next policy.
if (!policy->contains_directive_with_name(ContentSecurityPolicy::Directives::Names::RequireTrustedTypesFor))
continue;
@@ -167,10 +181,10 @@ ContentSecurityPolicy::Directives::Directive::Result should_sink_type_mismatch_v
}
// 4. For each policy in global’s CSP list:
- auto csp_list = ContentSecurityPolicy::PolicyList::from_object(global);
+ auto const* csp_list = csp_list_of_global(global);
if (!csp_list)
return result;
- for (auto const policy : csp_list->policies()) {
+ for (auto const& policy : csp_list->policies()) {
// 1. If policy’s directive set does not contain a directive whose name is "require-trusted-types-for", skip to the next policy.
if (!policy->contains_directive_with_name(ContentSecurityPolicy::Directives::Names::RequireTrustedTypesFor))
continue;
diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp
index a563450b053..8c0415b0d79 100644
--- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp
+++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp
@@ -242,8 +242,8 @@ ContentSecurityPolicy::Directives::Directive::Result TrustedTypePolicyFactory::s
auto result = ContentSecurityPolicy::Directives::Directive::Result::Allowed;
// 2. For each policy in global’s CSP list:
- auto csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
- for (auto const policy : csp_list->policies()) {
+ auto const& csp_list = HTML::relevant_settings_object(global).policy_container()->csp_list;
+ for (auto const& policy : csp_list.policies()) {
// 1. Let createViolation be false.
bool create_violation = false;
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index f0f63e12451..4d4418828b4 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -195,7 +195,7 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
// FIXME: Fetch request as per the remaining steps of https://websockets.spec.whatwg.org/#concept-websocket-establish
// AD-HOC: Until the opening handshake goes through fetch, run the blocking checks of main fetch here.
- request->set_policy_container(client.policy_container()->clone(client.heap()));
+ request->set_policy_container(client.policy_container()->clone());
auto& realm = client.realm();
ContentSecurityPolicy::report_content_security_policy_violations_for_request(realm, request);
UpgradeInsecureRequests::upgrade_request_to_a_potentially_trustworthy_url_if_appropriate(request);
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.txt
new file mode 100644
index 00000000000..ce211c1cd35
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.txt
@@ -0,0 +1 @@
+violation: worker-src, worker-src 'none', report
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-violation-names-its-policy.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-violation-names-its-policy.txt
new file mode 100644
index 00000000000..ac673f828d9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/worker-src-violation-names-its-policy.txt
@@ -0,0 +1 @@
+violation: worker-src, worker-src 'none', enforce
diff --git a/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html
new file mode 100644
index 00000000000..e2b21b1560f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<!-- The violated policy is not the first in the CSP list. -->
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ document.addEventListener("securitypolicyviolation", event => {
+ println(`violation: ${event.effectiveDirective}, ${event.originalPolicy}, ${event.disposition}`);
+ done();
+ });
+
+ new Worker("data:text/javascript,");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html.headers b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html.headers
new file mode 100644
index 00000000000..ca144a10864
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-report-only-violation-names-its-policy.html.headers
@@ -0,0 +1,2 @@
+Content-Security-Policy: img-src 'none'
+Content-Security-Policy-Report-Only: worker-src 'none'
diff --git a/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-violation-names-its-policy.html b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-violation-names-its-policy.html
new file mode 100644
index 00000000000..a0040354151
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ContentSecurityPolicy/worker-src-violation-names-its-policy.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<!-- The violated policy is not the first in the CSP list. -->
+<meta http-equiv="Content-Security-Policy" content="img-src 'none'">
+<meta http-equiv="Content-Security-Policy" content="worker-src 'none'">
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ document.addEventListener("securitypolicyviolation", event => {
+ println(`violation: ${event.effectiveDirective}, ${event.originalPolicy}, ${event.disposition}`);
+ done();
+ });
+
+ new Worker("data:text/javascript,");
+ });
+</script>