Skip to content

Truly independent web browser

LibWeb: Replace CSP directive subclasses with directive records

Luke Wilde8636f9b6b0d5
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 5767d46fff1..8f151f8ec6d 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -39,36 +39,15 @@ set(SOURCES
Compositor/NavigablePresenter.cpp
Compositor/RenderClock.cpp
ContentSecurityPolicy/BlockingAlgorithms.cpp
- ContentSecurityPolicy/Directives/BaseUriDirective.cpp
- ContentSecurityPolicy/Directives/ChildSourceDirective.cpp
- ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp
- ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp
ContentSecurityPolicy/Directives/Directive.cpp
- ContentSecurityPolicy/Directives/DirectiveFactory.cpp
ContentSecurityPolicy/Directives/DirectiveOperations.cpp
- ContentSecurityPolicy/Directives/FontSourceDirective.cpp
- ContentSecurityPolicy/Directives/FormActionDirective.cpp
- ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
- ContentSecurityPolicy/Directives/FrameSourceDirective.cpp
- ContentSecurityPolicy/Directives/ImageSourceDirective.cpp
+ ContentSecurityPolicy/Directives/FetchIntegration.cpp
+ ContentSecurityPolicy/Directives/HTMLIntegration.cpp
ContentSecurityPolicy/Directives/KeywordSources.cpp
ContentSecurityPolicy/Directives/KeywordTrustedTypes.cpp
- ContentSecurityPolicy/Directives/ManifestSourceDirective.cpp
- ContentSecurityPolicy/Directives/MediaSourceDirective.cpp
ContentSecurityPolicy/Directives/Names.cpp
- ContentSecurityPolicy/Directives/ObjectSourceDirective.cpp
- ContentSecurityPolicy/Directives/ReportToDirective.cpp
- ContentSecurityPolicy/Directives/ReportUriDirective.cpp
- ContentSecurityPolicy/Directives/SandboxDirective.cpp
- ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.cpp
- ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp
- ContentSecurityPolicy/Directives/ScriptSourceElementDirective.cpp
ContentSecurityPolicy/Directives/SourceExpression.cpp
- ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.cpp
- ContentSecurityPolicy/Directives/StyleSourceDirective.cpp
- ContentSecurityPolicy/Directives/StyleSourceElementDirective.cpp
- ContentSecurityPolicy/Directives/WebRTCDirective.cpp
- ContentSecurityPolicy/Directives/WorkerSourceDirective.cpp
+ ContentSecurityPolicy/Directives/WebRTCIntegration.cpp
ContentSecurityPolicy/Policy.cpp
ContentSecurityPolicy/PolicyList.cpp
ContentSecurityPolicy/SecurityPolicyViolationEvent.cpp
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
index 632943726f2..cd0a7199364 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
@@ -5,9 +5,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
#include <LibGC/Heap.h>
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
#include <LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
@@ -16,6 +19,7 @@
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
+#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
@@ -30,51 +34,51 @@
namespace Web::ContentSecurityPolicy {
// https://w3c.github.io/webappsec-csp/#does-resource-hint-violate-policy
-[[nodiscard]] static GC::Ptr<Directives::Directive> does_resource_hint_request_violate_policy(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy)
+[[nodiscard]] static Directives::Directive const* does_resource_hint_request_violate_policy(Fetch::Infrastructure::Request const& request, Policy const& policy)
{
// 1. Let defaultDirective be policy’s first directive whose name is "default-src".
- auto default_directive_iterator = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::DefaultSrc;
+ auto default_directive_iterator = policy.directives().find_if([](auto const& directive) {
+ return directive.kind() == Directives::Directive::Kind::DefaultSrc;
});
// 2. If defaultDirective does not exist, return "Does Not Violate".
if (default_directive_iterator.is_end())
- return {};
+ return nullptr;
// 3. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy.directives()) {
// 1. Let result be the result of executing directive’s pre-request check on request and policy.
- auto result = directive->pre_request_check(heap, request, policy);
+ auto result = Directives::pre_request_check(directive, request, policy);
// 2. If result is "Allowed", then return "Does Not Violate".
if (result == Directives::Directive::Result::Allowed) {
- return {};
+ return nullptr;
}
}
// 4. Return defaultDirective.
- return *default_directive_iterator;
+ return &*default_directive_iterator;
}
// https://w3c.github.io/webappsec-csp/#does-request-violate-policy
-[[nodiscard]] static GC::Ptr<Directives::Directive> does_request_violate_policy(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy)
+[[nodiscard]] static Directives::Directive const* does_request_violate_policy(Fetch::Infrastructure::Request const& request, Policy const& policy)
{
// 1. If request’s initiator is "prefetch", then return the result of executing § 6.7.2.2 Does resource hint
// request violate policy? on request and policy.
- if (request->initiator() == Fetch::Infrastructure::Request::Initiator::Prefetch)
- return does_resource_hint_request_violate_policy(heap, request, policy);
+ if (request.initiator() == Fetch::Infrastructure::Request::Initiator::Prefetch)
+ return does_resource_hint_request_violate_policy(request, policy);
// 2. Let violates be "Does Not Violate".
- GC::Ptr<Directives::Directive> violates;
+ Directives::Directive const* violates = nullptr;
// 3. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy.directives()) {
// 1. Let result be the result of executing directive’s pre-request check on request and policy.
- auto result = directive->pre_request_check(heap, request, policy);
+ auto result = Directives::pre_request_check(directive, request, policy);
// 2. If result is "Blocked", then let violates be directive.
if (result == Directives::Directive::Result::Blocked) {
- violates = directive;
+ violates = &directive;
}
}
@@ -95,7 +99,7 @@ void report_content_security_policy_violations_for_request(JS::Realm& realm, GC:
continue;
// 2. Let violates be the result of executing § 6.7.2.1 Does request violate policy? on request and policy.
- auto violates = does_request_violate_policy(GC::Heap::the(), request, policy);
+ auto violates = does_request_violate_policy(request, policy);
// 3. If violates is not "Does Not Violate", then execute § 5.5 Report a violation on the result of executing
// § 2.4.2 Create a violation object for request, and policy. on request, and policy.
@@ -122,7 +126,7 @@ Directives::Directive::Result should_request_be_blocked_by_content_security_poli
continue;
// 2. Let violates be the result of executing § 6.7.2.1 Does request violate policy? on request and policy.
- auto violates = does_request_violate_policy(GC::Heap::the(), request, policy);
+ auto violates = does_request_violate_policy(request, policy);
// 3. If violates is not "Does Not Violate", then:
if (violates) {
@@ -214,9 +218,9 @@ Directives::Directive::Result should_response_to_request_be_blocked_by_content_s
// Worker hasn't substituted a file which would violate the page’s CSP.
for (auto policy : csp_list->policies()) {
// 1. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. If the result of executing directive’s post-request check is "Blocked", then:
- if (directive->post_request_check(GC::Heap::the(), request, response, policy) == Directives::Directive::Result::Blocked) {
+ if (Directives::post_request_check(directive, request, response, policy) == Directives::Directive::Result::Blocked) {
// 1. Execute § 5.5 Report a violation on the result of executing § 2.4.2 Create a violation object for
// request, and policy. on request, and policy.
auto violation = Violation::create_a_violation_object_for_request_and_policy(request, policy);
@@ -244,16 +248,16 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
auto policy_container = navigation_request->policy_container().get<GC::Ref<HTML::PolicyContainer>>();
for (auto policy : policy_container->csp_list->policies()) {
// 1. For each directive of policy:
- for (auto directive : policy->directives()) {
+ 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.
- auto directive_result = directive->pre_navigation_check(navigation_request, navigation_type, policy);
+ auto directive_result = Directives::pre_navigation_check(directive, navigation_request, navigation_type, policy);
if (directive_result == Directives::Directive::Result::Allowed)
continue;
// 2. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on navigation request’s
// client’s global object, policy, and directive’s name.
auto& realm = navigation_request->client()->realm();
- auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(navigation_request->client()->global_object(), policy, directive->name().view().to_utf8_but_should_be_ported_to_utf16());
+ auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(navigation_request->client()->global_object(), policy, directive.name().view().to_utf8_but_should_be_ported_to_utf16());
// 3. Set violation’s resource to navigation request’s URL.
violation->set_resource(navigation_request->url());
@@ -275,7 +279,7 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
for (auto policy : csp_list->policies()) {
// 1. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. Let directive-name be the result of executing § 6.8.2 Get the effective directive for inline
// checks on type.
// FIXME: File spec issue that the type should probably always be "navigation", as NavigationType would
@@ -293,7 +297,7 @@ Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_co
auto& realm = navigation_request->client()->realm();
auto serialized_url = navigation_request->current_url().to_string();
auto serialized_url_utf16 = Utf16String::from_utf8(serialized_url);
- if (directive->inline_check(GC::Heap::the(), nullptr, Directives::Directive::InlineType::Navigation, policy, serialized_url_utf16.utf16_view()) == Directives::Directive::Result::Allowed)
+ if (Directives::inline_check(directive, nullptr, Directives::Directive::InlineType::Navigation, policy, serialized_url_utf16.utf16_view()) == Directives::Directive::Result::Allowed)
continue;
// 3. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global,
@@ -332,13 +336,27 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
if (!navigation_request)
return result;
+ // 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) {
+ return policy->contains_directive_with_name(Directives::Names::FrameAncestors);
+ });
+ Vector<URL::Origin> target_container_document_origins;
+ if (has_frame_ancestors) {
+ for (auto ancestor = target->parent(); ancestor; ancestor = ancestor->parent()) {
+ // AD-HOC: An ancestor whose document was destroyed has no active document, so its origin counts as opaque.
+ auto origin = ancestor->active_document_origin();
+ target_container_document_origins.append(origin.has_value() ? origin.release_value() : URL::Origin::create_opaque());
+ }
+ }
+
// 2. For each policy of response CSP list:
for (auto 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 directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. If directive’s navigation response check returns "Allowed" when executed upon navigation request, type, navigation response, target, "response", and policy skip to the next directive.
- auto directive_result = directive->navigation_response_check(*navigation_request, navigation_type, navigation_response, target, Directives::Directive::CheckType::Response, policy);
+ auto directive_result = Directives::navigation_response_check(directive, *navigation_request, navigation_type, navigation_response, target_container_document_origins, Directives::Directive::CheckType::Response, policy);
if (directive_result == Directives::Directive::Result::Allowed)
continue;
@@ -346,7 +364,7 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
// Spec Note: We use null for the global object, as no global exists: we haven’t processed the navigation to create a Document yet.
// FIXME: What should the realm be here?
auto& realm = navigation_request->client()->realm();
- auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(nullptr, policy, directive->name().view().to_utf8_but_should_be_ported_to_utf16());
+ auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(nullptr, policy, directive.name().view().to_utf8_but_should_be_ported_to_utf16());
// 3. Set violation’s resource to navigation response’s URL.
if (navigation_response->url().has_value()) {
@@ -369,15 +387,15 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
for (auto 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 directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. If directive’s navigation response check returns "Allowed" when executed upon navigation request, type, navigation response, target, "source", and policy skip to the next directive.
- auto directive_result = directive->navigation_response_check(*navigation_request, navigation_type, navigation_response, target, Directives::Directive::CheckType::Source, policy);
+ auto directive_result = Directives::navigation_response_check(directive, *navigation_request, navigation_type, navigation_response, target_container_document_origins, Directives::Directive::CheckType::Source, policy);
if (directive_result == Directives::Directive::Result::Allowed)
continue;
// 2. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on navigation request’s client’s global object, policy, and directive’s name.
auto& realm = navigation_request->client()->realm();
- auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(navigation_request->client()->global_object(), policy, directive->name().view().to_utf8_but_should_be_ported_to_utf16());
+ auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(navigation_request->client()->global_object(), policy, directive.name().view().to_utf8_but_should_be_ported_to_utf16());
// 3. Set violation’s resource to navigation request’s URL.
violation->set_resource(navigation_request->url());
@@ -414,10 +432,10 @@ Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by
for (auto const policy : csp_list->policies()) {
// 1. For each directive of policy’s directive set:
- for (auto const directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. If directive’s inline check returns "Allowed" when executed upon element, type, policy and source,
// skip to the next directive.
- if (directive->inline_check(GC::Heap::the(), element, type, policy, source) == Directives::Directive::Result::Allowed)
+ if (Directives::inline_check(directive, element, type, policy, source) == Directives::Directive::Result::Allowed)
continue;
// 2. Let directive-name be the result of executing § 6.8.2 Get the effective directive for inline checks
@@ -439,7 +457,7 @@ Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by
// 6. If directive’s value contains the expression "'report-sample'", then set violation’s sample to the
// substring of source containing its first 40 characters.
// FIXME: Should this be case insensitive?
- auto maybe_report_sample = directive->value().find_if([](auto const& directive_value) {
+ auto maybe_report_sample = directive.value().find_if([](auto const& directive_value) {
return directive_value.equals_ignoring_ascii_case(Directives::KeywordSources::ReportSample.view());
});
@@ -537,20 +555,20 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_string_compilation(JS::Rea
// 2. If policy contains a directive whose name is "script-src", then set source-list to that directive's value.
auto maybe_script_src = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::ScriptSrc;
+ return directive.kind() == Directives::Directive::Kind::ScriptSrc;
});
if (!maybe_script_src.is_end()) {
- maybe_source_list = (*maybe_script_src)->value();
+ maybe_source_list = maybe_script_src->value();
} else {
// Otherwise if policy contains a directive whose name is "default-src", then set source-list to that
// directive’s value.
auto maybe_default_src = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::DefaultSrc;
+ return directive.kind() == Directives::Directive::Kind::DefaultSrc;
});
if (!maybe_default_src.is_end())
- maybe_source_list = (*maybe_default_src)->value();
+ maybe_source_list = maybe_default_src->value();
}
// 3. If source-list is not null, and does not contain a source expression which is an ASCII case-insensitive
@@ -618,20 +636,20 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_wasm_byte_compilation(JS::
// 2. If policy contains a directive whose name is "script-src", then set source-list to that directive's value.
auto maybe_script_src = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::ScriptSrc;
+ return directive.kind() == Directives::Directive::Kind::ScriptSrc;
});
if (!maybe_script_src.is_end()) {
- maybe_source_list = (*maybe_script_src)->value();
+ maybe_source_list = maybe_script_src->value();
} else {
// Otherwise if policy contains a directive whose name is "default-src", then set source-list to that
// directive’s value.
auto maybe_default_src = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::DefaultSrc;
+ return directive.kind() == Directives::Directive::Kind::DefaultSrc;
});
if (!maybe_default_src.is_end())
- maybe_source_list = (*maybe_default_src)->value();
+ maybe_source_list = maybe_default_src->value();
}
// 3. If source-list is non-null, and does not contain a source expression which is an ASCII case-insensitive
@@ -684,14 +702,14 @@ Directives::Directive::Result is_base_allowed_for_document(URL::URL const& base,
// 2. If a directive whose name is "base-uri" is present in policy’s directive set, set source list to that
// directive’s value.
auto maybe_base_uri = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::BaseUri;
+ return directive.kind() == Directives::Directive::Kind::BaseUri;
});
// 3. If source list is null, skip to the next policy.
if (maybe_base_uri.is_end())
continue;
- auto const& source_list = (*maybe_base_uri)->value();
+ auto const& source_list = maybe_base_uri->value();
// 4. If the result of executing § 6.7.2.7 Does url match source list in origin with redirect count? on base,
// source list, policy’s self-origin, and 0 is "Does Not Match":
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.cpp
deleted file mode 100644
index 41a5bba11f5..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(BaseUriDirective);
-
-BaseUriDirective::BaseUriDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.h
deleted file mode 100644
index f50265ea8be..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-base-uri
-class BaseUriDirective final : public Directive {
- GC_CELL(BaseUriDirective, Directive)
- GC_DECLARE_ALLOCATOR(BaseUriDirective);
-
-public:
- virtual ~BaseUriDirective() = default;
-
-private:
- BaseUriDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.cpp
deleted file mode 100644
index c1df8d4a0cb..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ChildSourceDirective);
-
-ChildSourceDirective::ChildSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#child-src-pre-request
-Directive::Result ChildSourceDirective::pre_request_check(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, child-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ChildSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing the pre-request check for the directive whose name is name on request and
- // policy, using this directive’s value for the comparison.
- auto directive = create_directive(heap, *name, value());
- return directive->pre_request_check(heap, request, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#child-src-post-request
-Directive::Result ChildSourceDirective::post_request_check(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, child-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ChildSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing the post-request check for the directive whose name is name on request,
- // response, and policy, using this directive’s value for the comparison.
- auto directive = create_directive(heap, *name, value());
- return directive->post_request_check(heap, request, response, policy);
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.h
deleted file mode 100644
index 43b010db044..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-child-src
-class ChildSourceDirective final : public Directive {
- GC_CELL(ChildSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ChildSourceDirective);
-
-public:
- virtual ~ChildSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- ChildSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp
deleted file mode 100644
index ca9953519e2..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ConnectSourceDirective);
-
-ConnectSourceDirective::ConnectSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#connect-src-pre-request
-Directive::Result ConnectSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, connect-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ConnectSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#connect-src-post-request
-Directive::Result ConnectSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, connect-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ConnectSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.h
deleted file mode 100644
index 6176c9afda7..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-connect-src
-class ConnectSourceDirective final : public Directive {
- GC_CELL(ConnectSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ConnectSourceDirective);
-
-public:
- virtual ~ConnectSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- ConnectSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp
deleted file mode 100644
index 922b4a2d485..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(DefaultSourceDirective);
-
-DefaultSourceDirective::DefaultSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#default-src-pre-request
-Directive::Result DefaultSourceDirective::pre_request_check(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing the pre-request check for the directive whose name is name on request and
- // policy, using this directive’s value for the comparison.
- auto directive = create_directive(heap, *name, value());
- return directive->pre_request_check(heap, request, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#default-src-post-request
-Directive::Result DefaultSourceDirective::post_request_check(GC::Heap& heap, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing the post-request check for the directive whose name is name on request,
- // response, and policy, using this directive’s value for the comparison.
- auto directive = create_directive(heap, *name, value());
- return directive->post_request_check(heap, request, response, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#default-src-inline
-Directive::Result DefaultSourceDirective::inline_check(GC::Heap& heap, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Otherwise, return the result of executing the inline check for the directive whose name is name on element,
- // type, policy and source, using this directive’s value for the comparison.
- auto directive = create_directive(heap, name, value());
- return directive->inline_check(heap, element, type, policy, source);
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.h
deleted file mode 100644
index 05f0ecd6a1f..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-default-src
-class DefaultSourceDirective final : public Directive {
- GC_CELL(DefaultSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(DefaultSourceDirective);
-
-public:
- virtual ~DefaultSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- DefaultSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.cpp
index fe8242466c2..ba9f0137ab6 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.cpp
@@ -1,26 +1,36 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
#include <LibWebCommon/ContentSecurityPolicy/Directives/SerializedDirective.h>
namespace Web::ContentSecurityPolicy::Directives {
-GC_DEFINE_ALLOCATOR(Directive);
+static Directive::Kind kind_for_name(Utf16FlyString const& name)
+{
+#define __ENUMERATE_DIRECTIVE_NAME(directive_name, value) \
+ if (name == Names::directive_name) \
+ return Directive::Kind::directive_name;
+ ENUMERATE_DIRECTIVE_NAMES
+#undef __ENUMERATE_DIRECTIVE_NAME
+ return Directive::Kind::Unrecognized;
+}
-Directive::Directive(Utf16FlyString name, Vector<Utf16String> value)
- : m_name(move(name))
- , m_value(move(value))
+Directive Directive::create(Utf16FlyString name, Vector<Utf16String> value)
{
+ auto kind = kind_for_name(name);
+ return Directive { kind, move(name), move(value) };
}
-GC::Ref<Directive> Directive::clone(GC::Heap& heap) const
+Directive::Directive(Kind kind, Utf16FlyString name, Vector<Utf16String> value)
+ : m_kind(kind)
+ , m_name(move(name))
+ , m_value(move(value))
{
- return create_directive(heap, m_name, m_value);
}
SerializedDirective Directive::serialize() const
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.h
index 86028a8f8b4..95d1edce265 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/Directive.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -8,10 +9,8 @@
#include <AK/Utf16FlyString.h>
#include <AK/Utf16String.h>
-#include <AK/Utf16View.h>
-#include <LibGC/CellAllocator.h>
-#include <LibGC/Ptr.h>
-#include <LibJS/Heap/Cell.h>
+#include <AK/Vector.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/Forward.h>
#include <LibWebCommon/ContentSecurityPolicy/Directives/NavigationType.h>
@@ -20,13 +19,17 @@ namespace Web::ContentSecurityPolicy::Directives {
// https://w3c.github.io/webappsec-csp/#directives
// Each policy contains an ordered set of directives (its directive set), each of which controls a specific behavior.
// The directives defined in this document are described in detail in § 6 Content Security Policy Directives.
-class Directive : public GC::Cell {
- GC_CELL(Directive, GC::Cell)
- GC_DECLARE_ALLOCATOR(Directive);
-
+class Directive {
public:
using NavigationType = Directives::NavigationType;
+ enum class Kind : u8 {
+#define __ENUMERATE_DIRECTIVE_NAME(name, value) name,
+ ENUMERATE_DIRECTIVE_NAMES
+#undef __ENUMERATE_DIRECTIVE_NAME
+ Unrecognized,
+ };
+
enum class [[nodiscard]] Result {
Blocked,
Allowed,
@@ -45,62 +48,19 @@ public:
StyleAttribute,
};
- virtual ~Directive() = default;
-
- // Directives have a number of associated algorithms:
- // https://w3c.github.io/webappsec-csp/#directive-pre-request-check
- // 1. A pre-request check, which takes a request and a policy as an argument, and is executed during
- // § 4.1.2 Should request be blocked by Content Security Policy?. This algorithm returns "Allowed"
- // unless otherwise specified.
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-post-request-check
- // 2. A post-request check, which takes a request, a response, and a policy as arguments, and is executed during
- // § 4.1.3 Should response to request be blocked by Content Security Policy?. This algorithm returns "Allowed"
- // unless otherwise specified.
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-inline-check
- // 3. An inline check, which takes an Element, a type string, a policy, and a source string as arguments, and is
- // executed during § 4.2.3 Should element’s inline type behavior be blocked by Content Security Policy? and
- // during § 4.2.4 Should navigation request of type be blocked by Content Security Policy? for javascript:
- // requests. This algorithm returns "Allowed" unless otherwise specified.
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-initialization
- // 4. An initialization, which takes a Document or global object and a policy as arguments. This algorithm is
- // executed during § 4.2.1 Run CSP initialization for a Document and § 4.2.6 Run CSP initialization for
- // a global object. Unless otherwise specified, it has no effect and it returns "Allowed".
- virtual Result initialization(Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>>, GC::Ref<Policy const>) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-pre-navigation-check
- // 5. A pre-navigation check, which takes a request, a navigation type string ("form-submission" or "other")
- // and a policy as arguments, and is executed during § 4.2.4 Should navigation request of type be blocked by
- // Content Security Policy?. It returns "Allowed" unless otherwise specified.
- virtual Result pre_navigation_check(GC::Ref<Fetch::Infrastructure::Request>, NavigationType, GC::Ref<Policy const>) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-navigation-response-check
- // 6. A navigation response check, which takes a request, a navigation type string ("form-submission" or "other"),
- // a response, a navigable, a check type string ("source" or "response"), and a policy as arguments, and is
- // executed during § 4.2.5 Should navigation response to navigation request of type in target be blocked by
- // Content Security Policy?. It returns "Allowed" unless otherwise specified.
- virtual Result navigation_response_check(GC::Ref<Fetch::Infrastructure::Request const>, NavigationType, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<HTML::LocalNavigable const>, CheckType, GC::Ref<Policy const>) const { return Result::Allowed; }
-
- // https://w3c.github.io/webappsec-csp/#directive-webrtc-pre-connect-check
- // 7. A webrtc pre-connect check, which takes a policy, and is executed during § 4.3.1 Should RTC connections be
- // blocked for global?. It returns "Allowed" unless otherwise specified.
- virtual Result webrtc_pre_connect_check(GC::Ref<Policy const>) const { return Result::Allowed; }
+ [[nodiscard]] static Directive create(Utf16FlyString name, Vector<Utf16String> value);
+ [[nodiscard]] Kind kind() const { return m_kind; }
[[nodiscard]] Utf16FlyString const& name() const { return m_name; }
[[nodiscard]] Vector<Utf16String> const& value() const { return m_value; }
- [[nodiscard]] GC::Ref<Directive> clone(GC::Heap&) const;
[[nodiscard]] SerializedDirective serialize() const;
-protected:
- Directive(Utf16FlyString name, Vector<Utf16String> value);
-
private:
+ Directive(Kind, Utf16FlyString name, Vector<Utf16String> value);
+
+ Kind m_kind { Kind::Unrecognized };
+
// https://w3c.github.io/webappsec-csp/#directive-name
// https://w3c.github.io/webappsec-csp/#directive-value
// Each directive is a name / value pair. The name is a non-empty string, and the value is a set of non-empty strings.
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.cpp
deleted file mode 100644
index 2b7e1b6214c..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.cpp
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright (c) 2025, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/BaseUriDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ChildSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.h>
-#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC::Ref<Directive> create_directive(GC::Heap& heap, Utf16FlyString name, Vector<Utf16String> value)
-{
- if (name == Names::BaseUri)
- return heap.allocate<BaseUriDirective>(move(name), move(value));
-
- if (name == Names::ChildSrc)
- return heap.allocate<ChildSourceDirective>(move(name), move(value));
-
- if (name == Names::ConnectSrc)
- return heap.allocate<ConnectSourceDirective>(move(name), move(value));
-
- if (name == Names::DefaultSrc)
- return heap.allocate<DefaultSourceDirective>(move(name), move(value));
-
- if (name == Names::FontSrc)
- return heap.allocate<FontSourceDirective>(move(name), move(value));
-
- if (name == Names::FormAction)
- return heap.allocate<FormActionDirective>(move(name), move(value));
-
- if (name == Names::FrameAncestors)
- return heap.allocate<FrameAncestorsDirective>(move(name), move(value));
-
- if (name == Names::FrameSrc)
- return heap.allocate<FrameSourceDirective>(move(name), move(value));
-
- if (name == Names::ImgSrc)
- return heap.allocate<ImageSourceDirective>(move(name), move(value));
-
- if (name == Names::ManifestSrc)
- return heap.allocate<ManifestSourceDirective>(move(name), move(value));
-
- if (name == Names::MediaSrc)
- return heap.allocate<MediaSourceDirective>(move(name), move(value));
-
- if (name == Names::ObjectSrc)
- return heap.allocate<ObjectSourceDirective>(move(name), move(value));
-
- if (name == Names::ReportTo)
- return heap.allocate<ReportToDirective>(move(name), move(value));
-
- if (name == Names::ReportUri)
- return heap.allocate<ReportUriDirective>(move(name), move(value));
-
- if (name == Names::RequireTrustedTypesFor)
- return heap.allocate<TrustedTypes::RequireTrustedTypesForDirective>(move(name), move(value));
-
- if (name == Names::Sandbox)
- return heap.allocate<SandboxDirective>(move(name), move(value));
-
- if (name == Names::ScriptSrcAttr)
- return heap.allocate<ScriptSourceAttributeDirective>(move(name), move(value));
-
- if (name == Names::ScriptSrc)
- return heap.allocate<ScriptSourceDirective>(move(name), move(value));
-
- if (name == Names::ScriptSrcElem)
- return heap.allocate<ScriptSourceElementDirective>(move(name), move(value));
-
- if (name == Names::StyleSrcAttr)
- return heap.allocate<StyleSourceAttributeDirective>(move(name), move(value));
-
- if (name == Names::StyleSrc)
- return heap.allocate<StyleSourceDirective>(move(name), move(value));
-
- if (name == Names::StyleSrcElem)
- return heap.allocate<StyleSourceElementDirective>(move(name), move(value));
-
- if (name == Names::WebRTC)
- return heap.allocate<WebRTCDirective>(move(name), move(value));
-
- if (name == Names::WorkerSrc)
- return heap.allocate<WorkerSourceDirective>(move(name), move(value));
-
- return heap.allocate<Directive>(move(name), move(value));
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h
deleted file mode 100644
index 7fcfdd6bfa3..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (c) 2025, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Utf16FlyString.h>
-#include <AK/Utf16String.h>
-#include <AK/Vector.h>
-#include <LibGC/Forward.h>
-#include <LibGC/Ptr.h>
-#include <LibWeb/Forward.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-[[nodiscard]] GC::Ref<Directive> create_directive(GC::Heap&, Utf16FlyString name, Vector<Utf16String> value);
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
index f46f271a104..4903f798ae3 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
@@ -801,25 +801,25 @@ 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, GC::Ref<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, GC::Ref<Policy const> policy)
{
// 1. If request’s destination is script-like:
if (request->destination_is_script_like()) {
// 1. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce
// metadata and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), directive->value()) == MatchResult::Matches)
+ if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
return Directive::Result::Allowed;
// 2. If the result of executing § 6.7.2.4 Does integrity metadata match source list? on request’s integrity
// metadata and this directive’s value is "Matches", return "Allowed".
- if (does_integrity_metadata_match_source_list(request->integrity_metadata(), directive->value()) == MatchResult::Matches)
+ if (does_integrity_metadata_match_source_list(request->integrity_metadata(), directive.value()) == MatchResult::Matches)
return Directive::Result::Allowed;
// 3. If directive’s value contains a source expression that is an ASCII case-insensitive match for the
// "'strict-dynamic'" keyword-source:
// Spec Note: "'strict-dynamic'" is explained in more detail in § 8.2 Usage of "'strict-dynamic'".
// https://w3c.github.io/webappsec-csp/#strict-dynamic-usage
- auto maybe_strict_dynamic = directive->value().find_if([](auto const& directive_value) {
+ auto maybe_strict_dynamic = directive.value().find_if([](auto const& directive_value) {
return directive_value.equals_ignoring_ascii_case(KeywordSources::StrictDynamic.view());
});
@@ -834,7 +834,7 @@ Directive::Result script_directives_pre_request_check(GC::Ref<Fetch::Infrastruct
// 4. If the result of executing § 6.7.2.5 Does request match source list? on request, directive’s value, and
// policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, directive->value(), policy) == MatchResult::DoesNotMatch)
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
return Directive::Result::Blocked;
}
@@ -843,23 +843,23 @@ 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, GC::Ref<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)
{
// 1. If request’s destination is script-like:
if (request->destination_is_script_like()) {
// 1. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce
// metadata and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), directive->value()) == MatchResult::Matches)
+ if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
return Directive::Result::Allowed;
// 2. If the result of executing § 6.7.2.4 Does integrity metadata match source list? on request’s integrity
// metadata and this directive’s value is "Matches", return "Allowed".
- if (does_integrity_metadata_match_source_list(request->integrity_metadata(), directive->value()) == MatchResult::Matches)
+ if (does_integrity_metadata_match_source_list(request->integrity_metadata(), directive.value()) == MatchResult::Matches)
return Directive::Result::Allowed;
// 3. If directive’s value contains "'strict-dynamic'":
// FIXME: Should this be case insensitive?
- auto maybe_strict_dynamic = directive->value().find_if([](auto const& directive_value) {
+ auto maybe_strict_dynamic = directive.value().find_if([](auto const& directive_value) {
return directive_value.equals_ignoring_ascii_case(KeywordSources::StrictDynamic.view());
});
@@ -874,7 +874,7 @@ Directive::Result script_directives_post_request_check(GC::Ref<Fetch::Infrastruc
// 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request,
// directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, directive->value(), policy) == MatchResult::DoesNotMatch)
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
return Directive::Result::Blocked;
}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
index d68c0f43dad..68ab818bf3a 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h
@@ -39,8 +39,8 @@ MatchResult does_request_match_source_list(GC::Ref<Fetch::Infrastructure::Reques
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_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, GC::Ref<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, GC::Ref<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, 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);
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/Directives/FetchIntegration.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.cpp
new file mode 100644
index 00000000000..efb87441cfd
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.cpp
@@ -0,0 +1,636 @@
+/*
+ * Copyright (c) 2024-2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#child-src-pre-request
+static Directive::Result child_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, child-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ChildSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing the pre-request check for the directive whose name is name on request and
+ // policy, using this directive’s value for the comparison.
+ return pre_request_check(Directive::create(*name, directive.value()), request, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#connect-src-pre-request
+static Directive::Result connect_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, connect-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ConnectSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#default-src-pre-request
+static Directive::Result default_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing the pre-request check for the directive whose name is name on request and
+ // policy, using this directive’s value for the comparison.
+ return pre_request_check(Directive::create(*name, directive.value()), request, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#font-src-pre-request
+static Directive::Result font_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, font-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::FontSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#frame-src-pre-request
+static Directive::Result frame_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, frame-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::FrameSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#img-src-pre-request
+static Directive::Result img_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, img-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ImgSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#manifest-src-pre-request
+static Directive::Result manifest_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, manifest-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ManifestSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#media-src-pre-request
+static Directive::Result media_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, media-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::MediaSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#object-src-pre-request
+static Directive::Result object_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, object-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ObjectSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-pre-request
+static Directive::Result script_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
+ // and policy.
+ return script_directives_pre_request_check(request, directive, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-elem-pre-request
+static Directive::Result script_src_elem_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
+ // and policy.
+ return script_directives_pre_request_check(request, directive, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-pre-request
+static Directive::Result style_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
+ // and this directive’s value is "Matches", return "Allowed".
+ if (does_nonce_match_source_list(request.cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value, and
+ // policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-elem-pre-request
+static Directive::Result style_src_elem_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
+ // and this directive’s value is "Matches", return "Allowed".
+ if (does_nonce_match_source_list(request.cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value, and
+ // policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#worker-src-pre-request
+static Directive::Result worker_src_pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, worker-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::WorkerSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#child-src-post-request
+static Directive::Result child_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, child-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ChildSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing the post-request check for the directive whose name is name on request,
+ // response, and policy, using this directive’s value for the comparison.
+ return post_request_check(Directive::create(*name, directive.value()), request, response, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#connect-src-post-request
+static Directive::Result connect_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, connect-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ConnectSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#default-src-post-request
+static Directive::Result default_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing the post-request check for the directive whose name is name on request,
+ // response, and policy, using this directive’s value for the comparison.
+ return post_request_check(Directive::create(*name, directive.value()), request, response, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#font-src-post-request
+static Directive::Result font_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, font-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::FontSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#frame-src-post-request
+static Directive::Result frame_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, frame-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::FrameSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#img-src-post-request
+static Directive::Result img_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, img-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ImgSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#manifest-src-post-request
+static Directive::Result manifest_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, manifest-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ManifestSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#media-src-post-request
+static Directive::Result media_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, media-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::MediaSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#object-src-post-request
+static Directive::Result object_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, object-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ObjectSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-post-request
+static Directive::Result script_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
+ // directive, and policy.
+ return script_directives_post_request_check(request, response, directive, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-elem-post-request
+static Directive::Result script_src_elem_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
+ // directive, and policy.
+ return script_directives_post_request_check(request, response, directive, policy);
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-post-request
+static Directive::Result style_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
+ // and this directive’s value is "Matches", return "Allowed".
+ if (does_nonce_match_source_list(request.cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-elem-post-request
+static Directive::Result style_src_elem_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
+ // and this directive’s value is "Matches", return "Allowed".
+ if (does_nonce_match_source_list(request.cryptographic_nonce_metadata(), directive.value()) == MatchResult::Matches)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#worker-src-post-request
+static Directive::Result worker_src_post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
+ auto name = get_the_effective_directive_for_request(request);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, worker-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::WorkerSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
+ // directive’s value, and policy, is "Does Not Match", return "Blocked".
+ if (does_response_match_source_list(response, request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+Directive::Result pre_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Policy const& policy)
+{
+ switch (directive.kind()) {
+ case Directive::Kind::ChildSrc:
+ return child_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ConnectSrc:
+ return connect_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::DefaultSrc:
+ return default_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::FontSrc:
+ return font_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::FrameSrc:
+ return frame_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ImgSrc:
+ return img_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ManifestSrc:
+ return manifest_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::MediaSrc:
+ return media_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ObjectSrc:
+ return object_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ScriptSrc:
+ return script_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::ScriptSrcElem:
+ return script_src_elem_pre_request_check(directive, request, policy);
+ case Directive::Kind::StyleSrc:
+ return style_src_pre_request_check(directive, request, policy);
+ case Directive::Kind::StyleSrcElem:
+ return style_src_elem_pre_request_check(directive, request, policy);
+ case Directive::Kind::WorkerSrc:
+ return worker_src_pre_request_check(directive, request, policy);
+ default:
+ return Directive::Result::Allowed;
+ }
+}
+
+Directive::Result post_request_check(Directive const& directive, Fetch::Infrastructure::Request const& request, Fetch::Infrastructure::Response const& response, Policy const& policy)
+{
+ switch (directive.kind()) {
+ case Directive::Kind::ChildSrc:
+ return child_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ConnectSrc:
+ return connect_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::DefaultSrc:
+ return default_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::FontSrc:
+ return font_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::FrameSrc:
+ return frame_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ImgSrc:
+ return img_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ManifestSrc:
+ return manifest_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::MediaSrc:
+ return media_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ObjectSrc:
+ return object_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ScriptSrc:
+ return script_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::ScriptSrcElem:
+ return script_src_elem_post_request_check(directive, request, response, policy);
+ case Directive::Kind::StyleSrc:
+ return style_src_post_request_check(directive, request, response, policy);
+ case Directive::Kind::StyleSrcElem:
+ return style_src_elem_post_request_check(directive, request, response, policy);
+ case Directive::Kind::WorkerSrc:
+ return worker_src_post_request_check(directive, request, response, policy);
+ default:
+ return Directive::Result::Allowed;
+ }
+}
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.h
new file mode 100644
index 00000000000..f41d0332c5f
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FetchIntegration.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2024-2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#directive-pre-request-check
+// A pre-request check, which takes a request and a policy as an argument, and is executed during § 4.1.2 Should
+// request be blocked by Content Security Policy?. This algorithm returns "Allowed" unless otherwise specified.
+Directive::Result pre_request_check(Directive const&, Fetch::Infrastructure::Request const&, Policy const&);
+
+// https://w3c.github.io/webappsec-csp/#directive-post-request-check
+// A post-request check, which takes a request, a response, and a policy as arguments, and is executed during § 4.1.3
+// Should response to request be blocked by Content Security Policy?. This algorithm returns "Allowed" unless otherwise
+// specified.
+Directive::Result post_request_check(Directive const&, Fetch::Infrastructure::Request const&, Fetch::Infrastructure::Response const&, Policy const&);
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.cpp
deleted file mode 100644
index c9d4ee6e0f7..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(FontSourceDirective);
-
-FontSourceDirective::FontSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#font-src-pre-request
-Directive::Result FontSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, font-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::FontSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#font-src-post-request
-Directive::Result FontSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, font-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::FontSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h
deleted file mode 100644
index ea86104b708..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-font-src
-class FontSourceDirective final : public Directive {
- GC_CELL(FontSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(FontSourceDirective);
-
-public:
- virtual ~FontSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- FontSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.cpp
deleted file mode 100644
index f7e78057cc0..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(FormActionDirective);
-
-FormActionDirective::FormActionDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-Directive::Result FormActionDirective::pre_navigation_check(GC::Ref<Fetch::Infrastructure::Request> request, NavigationType navigation_type, GC::Ref<Policy const> policy) const
-{
- // 1. Assert: policy is unused in this algorithm.
- // FIXME: File spec issue, because this is not the case. The policy is required to resolve 'self'.
-
- // 2. If navigation type is "form-submission":
- if (navigation_type == NavigationType::FormSubmission) {
- // 1. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and a policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
- }
-
- // 3. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h
deleted file mode 100644
index 78396afb1aa..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-form-action
-class FormActionDirective final : public Directive {
- GC_CELL(FormActionDirective, Directive)
- GC_DECLARE_ALLOCATOR(FormActionDirective);
-
-public:
- virtual ~FormActionDirective() = default;
-
- virtual Result pre_navigation_check(GC::Ref<Fetch::Infrastructure::Request>, NavigationType, GC::Ref<Policy const>) const override;
-
-private:
- FormActionDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
deleted file mode 100644
index 9d44c09d9ac..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibURL/Parser.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Policy.h>
-#include <LibWeb/DOM/Document.h>
-#include <LibWeb/DOMURL/DOMURL.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/HTML/LocalNavigable.h>
-#include <LibWebCommon/Fetch/Infrastructure/URL.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(FrameAncestorsDirective);
-
-FrameAncestorsDirective::FrameAncestorsDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#frame-ancestors-navigation-response
-Directive::Result FrameAncestorsDirective::navigation_response_check(GC::Ref<Fetch::Infrastructure::Request const>, NavigationType, GC::Ref<Fetch::Infrastructure::Response const> navigation_response, GC::Ref<HTML::LocalNavigable const> target, CheckType check_type, GC::Ref<Policy const> policy) const
-{
- // 1. If navigation response’s URL is local, return "Allowed".
- VERIFY(navigation_response->url().has_value());
- if (Fetch::Infrastructure::is_local_url(navigation_response->url().value()))
- return Result::Allowed;
-
- // 2. Assert: request, navigation response, and navigation type, are unused from this point forward in this
- // algorithm, as frame-ancestors is concerned only with navigation response’s frame-ancestors directive.
-
- // 3. If check type is "source", return "Allowed".
- // Spec Note: The 'frame-ancestors' directive is relevant only to the target navigable and it has no impact on the
- // request’s context.
- if (check_type == CheckType::Source)
- return Result::Allowed;
-
- // 4. If target is not a child navigable, return "Allowed".
- if (!target->parent())
- return Result::Allowed;
-
- // 5. Let current be target.
- // 6. While current is a child navigable:
- // 1. Let document be current’s container document.
- // 4. Set current to document’s node navigable.
- // NB: Each container document is the next parent navigable's active document, and a parent navigable answers
- // for a document hosted in another process.
- for (auto current = target->parent(); current; current = current->parent()) {
- // 2. Let origin be the result of executing the URL parser on the ASCII serialization of document’s origin.
- auto serialized_origin = current->active_document_origin()->serialize();
- auto origin = DOMURL::parse_from_byte_string(serialized_origin.bytes_as_string_view());
-
- // AD-HOC: If the origin is opaque, serialization produces "null" which fails URL parsing.
- // All major engines block in this case, as an opaque origin can never match any source expression.
- if (!origin.has_value())
- return Result::Blocked;
-
- // 3. If § 6.7.2.7 Does url match source list in origin with redirect count? returns Does Not Match when
- // executed upon origin, this directive’s value, policy’s self-origin, and 0, return "Blocked".
- if (does_url_match_source_list_in_origin_with_redirect_count(origin.value(), value(), policy->self_origin(), 0) == MatchResult::DoesNotMatch)
- return Result::Blocked;
- }
-
- // 7. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h
deleted file mode 100644
index cfa65ac1086..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-frame-ancestors
-class FrameAncestorsDirective final : public Directive {
- GC_CELL(FrameAncestorsDirective, Directive)
- GC_DECLARE_ALLOCATOR(FrameAncestorsDirective);
-
-public:
- virtual ~FrameAncestorsDirective() = default;
-
- virtual Result navigation_response_check(GC::Ref<Fetch::Infrastructure::Request const>, NavigationType, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<HTML::LocalNavigable const>, CheckType, GC::Ref<Policy const>) const override;
-
-private:
- FrameAncestorsDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.cpp
deleted file mode 100644
index a2fc75d240e..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(FrameSourceDirective);
-
-FrameSourceDirective::FrameSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#frame-src-pre-request
-Directive::Result FrameSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, frame-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::FrameSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#frame-src-post-request
-Directive::Result FrameSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, frame-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::FrameSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h
deleted file mode 100644
index 426ddf36222..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-frame-src
-class FrameSourceDirective final : public Directive {
- GC_CELL(FrameSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(FrameSourceDirective);
-
-public:
- virtual ~FrameSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- FrameSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
new file mode 100644
index 00000000000..0fa9e570ed8
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
@@ -0,0 +1,305 @@
+/*
+ * Copyright (c) 2024-2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibURL/Origin.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
+#include <LibWeb/ContentSecurityPolicy/Policy.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOMURL/DOMURL.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
+#include <LibWeb/HTML/WorkerGlobalScope.h>
+#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
+#include <LibWebCommon/HTML/SandboxingFlagSet.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#default-src-inline
+static Directive::Result default_src_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, default-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::DefaultSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. Otherwise, return the result of executing the inline check for the directive whose name is name on element,
+ // type, policy and source, using this directive’s value for the comparison.
+ return inline_check(Directive::create(name, directive.value()), element, type, policy, source);
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-attr-inline
+static Directive::Result script_src_attr_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Assert: element is not null or type is "navigation".
+ VERIFY(element || type == Directive::InlineType::Navigation);
+
+ // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-attr and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrcAttr, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-inline
+static Directive::Result script_src_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Assert: element is not null or type is "navigation".
+ VERIFY(element || type == Directive::InlineType::Navigation);
+
+ // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source, is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#script-src-elem-inline
+static Directive::Result script_src_elem_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Assert: element is not null or type is "navigation".
+ VERIFY(element || type == Directive::InlineType::Navigation);
+
+ // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-attr-inline
+static Directive::Result style_src_attr_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-attr and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrcAttr, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source, is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-inline
+static Directive::Result style_src_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source, is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#style-src-elem-inline
+static Directive::Result style_src_elem_inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
+ auto name = get_the_effective_directive_for_inline_checks(type);
+
+ // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
+ // return "Allowed".
+ if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
+ return Directive::Result::Allowed;
+
+ // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
+ // directive’s value, type, and source, is "Does Not Match", return "Blocked".
+ if (does_element_match_source_list_for_type_and_source(element, directive.value(), type, source) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+
+ // 4. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#sandbox-init
+static Directive::Result sandbox_initialization(Directive const& directive, Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>> context, Policy const& policy)
+{
+ // 1. If policy’s disposition is not "enforce", or context is not a WorkerGlobalScope, then abort this algorithm.
+ // FIXME: File spec issue that this step doesn't specify the return value. It must be allowed, because Document
+ // asserts that the result of this algorithm is Allowed.
+ if (policy.disposition() != Policy::Disposition::Enforce || !context.has<GC::Ref<HTML::WorkerGlobalScope const>>())
+ return Directive::Result::Allowed;
+
+ // 2. Let sandboxing flag set be a new sandboxing flag set.
+ // 3. Parse a sandboxing directive using this directive’s value as the input, and sandboxing flag set as the output.
+ // FIXME: File spec issue that "parse a sandboxing directive" does not accept a set of tokens.
+ auto sandboxing_flag_set = HTML::parse_a_sandboxing_directive(directive.value());
+
+ // 4. If sandboxing flag set contains either the sandboxed scripts browsing context flag or the sandboxed origin
+ // browsing context flag flags, return "Blocked".
+ // Spec Note: This will need to change if we allow Workers to be sandboxed into unique origins, which seems like a
+ // pretty reasonable thing to do.
+ if (has_flag(sandboxing_flag_set, HTML::SandboxingFlagSet::SandboxedScripts) || has_flag(sandboxing_flag_set, HTML::SandboxingFlagSet::SandboxedOrigin))
+ return Directive::Result::Blocked;
+
+ // 5. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#form-action-pre-navigate
+static Directive::Result form_action_pre_navigation_check(Directive const& directive, Fetch::Infrastructure::Request& request, Directive::NavigationType navigation_type, Policy const& policy)
+{
+ // 1. Assert: policy is unused in this algorithm.
+ // FIXME: File spec issue, because this is not the case. The policy is required to resolve 'self'.
+
+ // 2. If navigation type is "form-submission":
+ if (navigation_type == Directive::NavigationType::FormSubmission) {
+ // 1. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
+ // and a policy, is "Does Not Match", return "Blocked".
+ if (does_request_match_source_list(request, directive.value(), policy) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+ }
+
+ // 3. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+// https://w3c.github.io/webappsec-csp/#frame-ancestors-navigation-response
+static Directive::Result frame_ancestors_navigation_response_check(Directive const& directive, Fetch::Infrastructure::Response const& navigation_response, ReadonlySpan<URL::Origin> target_container_document_origins, Directive::CheckType check_type, Policy const& policy)
+{
+ // 1. If navigation response’s URL is local, return "Allowed".
+ VERIFY(navigation_response.url().has_value());
+ if (Fetch::Infrastructure::is_local_url(navigation_response.url().value()))
+ return Directive::Result::Allowed;
+
+ // 2. Assert: request, navigation response, and navigation type, are unused from this point forward in this
+ // algorithm, as frame-ancestors is concerned only with navigation response’s frame-ancestors directive.
+
+ // 3. If check type is "source", return "Allowed".
+ // Spec Note: The 'frame-ancestors' directive is relevant only to the target navigable and it has no impact on the
+ // request’s context.
+ if (check_type == Directive::CheckType::Source)
+ return Directive::Result::Allowed;
+
+ // 4. If target is not a child navigable, return "Allowed".
+ if (target_container_document_origins.is_empty())
+ return Directive::Result::Allowed;
+
+ // 5. Let current be target.
+ // 6. While current is a child navigable:
+ // 1. Let document be current’s container document.
+ // 4. Set current to document’s node navigable.
+ for (auto const& document_origin : target_container_document_origins) {
+ // 2. Let origin be the result of executing the URL parser on the ASCII serialization of document’s origin.
+ auto serialized_origin = document_origin.serialize();
+ auto origin = DOMURL::parse_from_byte_string(serialized_origin.bytes_as_string_view());
+
+ // AD-HOC: If the origin is opaque, serialization produces "null" which fails URL parsing.
+ // All major engines block in this case, as an opaque origin can never match any source expression.
+ if (!origin.has_value())
+ return Directive::Result::Blocked;
+
+ // 3. If § 6.7.2.7 Does url match source list in origin with redirect count? returns Does Not Match when
+ // executed upon origin, this directive’s value, policy’s self-origin, and 0, return "Blocked".
+ if (does_url_match_source_list_in_origin_with_redirect_count(origin.value(), directive.value(), policy.self_origin(), 0) == MatchResult::DoesNotMatch)
+ return Directive::Result::Blocked;
+ }
+
+ // 7. Return "Allowed".
+ return Directive::Result::Allowed;
+}
+
+Directive::Result inline_check(Directive const& directive, GC::Ptr<DOM::Element const> element, Directive::InlineType type, Policy const& policy, Utf16View source)
+{
+ switch (directive.kind()) {
+ case Directive::Kind::DefaultSrc:
+ return default_src_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::ScriptSrc:
+ return script_src_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::ScriptSrcAttr:
+ return script_src_attr_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::ScriptSrcElem:
+ return script_src_elem_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::StyleSrc:
+ return style_src_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::StyleSrcAttr:
+ return style_src_attr_inline_check(directive, element, type, policy, source);
+ case Directive::Kind::StyleSrcElem:
+ return style_src_elem_inline_check(directive, element, type, policy, source);
+ default:
+ return Directive::Result::Allowed;
+ }
+}
+
+Directive::Result initialization(Directive const& directive, Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>> context, Policy const& policy)
+{
+ if (directive.kind() == Directive::Kind::Sandbox)
+ return sandbox_initialization(directive, context, policy);
+ return Directive::Result::Allowed;
+}
+
+Directive::Result pre_navigation_check(Directive const& directive, Fetch::Infrastructure::Request& request, Directive::NavigationType navigation_type, Policy const& policy)
+{
+ switch (directive.kind()) {
+ case Directive::Kind::FormAction:
+ return form_action_pre_navigation_check(directive, request, navigation_type, policy);
+ case Directive::Kind::RequireTrustedTypesFor:
+ return TrustedTypes::require_trusted_types_for_pre_navigation_check(request);
+ default:
+ return Directive::Result::Allowed;
+ }
+}
+
+Directive::Result navigation_response_check(Directive const& directive, Fetch::Infrastructure::Request const&, Directive::NavigationType, Fetch::Infrastructure::Response const& navigation_response, ReadonlySpan<URL::Origin> target_container_document_origins, Directive::CheckType check_type, Policy const& policy)
+{
+ if (directive.kind() == Directive::Kind::FrameAncestors)
+ return frame_ancestors_navigation_response_check(directive, navigation_response, target_container_document_origins, check_type, policy);
+ return Directive::Result::Allowed;
+}
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h
new file mode 100644
index 00000000000..ccc4c085964
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2024-2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Span.h>
+#include <AK/Utf16View.h>
+#include <LibGC/Ptr.h>
+#include <LibURL/Forward.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#directive-inline-check
+// An inline check, which takes an Element, a type string, a policy, and a source string as arguments, and is executed
+// during § 4.2.3 Should element’s inline type behavior be blocked by Content Security Policy? and during § 4.2.4 Should
+// navigation request of type be blocked by Content Security Policy? for javascript: requests. This algorithm returns
+// "Allowed" unless otherwise specified.
+Directive::Result inline_check(Directive const&, GC::Ptr<DOM::Element const>, Directive::InlineType, Policy const&, Utf16View source);
+
+// https://w3c.github.io/webappsec-csp/#directive-initialization
+// An initialization, which takes a Document or global object and a policy as arguments. This algorithm is executed
+// during § 4.2.1 Run CSP initialization for a Document and § 4.2.6 Run CSP initialization for a global object. Unless
+// otherwise specified, it has no effect and it returns "Allowed".
+Directive::Result initialization(Directive const&, Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>>, Policy const&);
+
+// https://w3c.github.io/webappsec-csp/#directive-pre-navigation-check
+// A pre-navigation check, which takes a request, a navigation type string ("form-submission" or "other") and a policy
+// as arguments, and is executed during § 4.2.4 Should navigation request of type be blocked by Content Security
+// Policy?. It returns "Allowed" unless otherwise specified.
+Directive::Result pre_navigation_check(Directive const&, Fetch::Infrastructure::Request&, Directive::NavigationType, Policy const&);
+
+// https://w3c.github.io/webappsec-csp/#directive-navigation-response-check
+// A navigation response check, which takes a request, a navigation type string ("form-submission" or "other"), a
+// response, a navigable, a check type string ("source" or "response"), and a policy as arguments, and is executed
+// during § 4.2.5 Should navigation response to navigation request of type in target be blocked by Content Security
+// Policy?. It returns "Allowed" unless otherwise specified.
+// NB: The navigable is given as the origins of its container documents, nearest first.
+Directive::Result navigation_response_check(Directive const&, Fetch::Infrastructure::Request const&, Directive::NavigationType, Fetch::Infrastructure::Response const&, ReadonlySpan<URL::Origin> target_container_document_origins, Directive::CheckType, Policy const&);
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.cpp
deleted file mode 100644
index cdcf78ba784..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ImageSourceDirective);
-
-ImageSourceDirective::ImageSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#frame-src-pre-request
-Directive::Result ImageSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, img-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ImgSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#frame-src-post-request
-Directive::Result ImageSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, img-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ImgSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h
deleted file mode 100644
index ef2bc6a87da..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-img-src
-class ImageSourceDirective final : public Directive {
- GC_CELL(ImageSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ImageSourceDirective);
-
-public:
- virtual ~ImageSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- ImageSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.cpp
deleted file mode 100644
index 45345b99b6d..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ManifestSourceDirective);
-
-ManifestSourceDirective::ManifestSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#manifest-src-pre-request
-Directive::Result ManifestSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, manifest-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ManifestSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#manifest-src-post-request
-Directive::Result ManifestSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, manifest-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ManifestSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h
deleted file mode 100644
index e8f80711097..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-manifest-src
-class ManifestSourceDirective final : public Directive {
- GC_CELL(ManifestSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ManifestSourceDirective);
-
-public:
- virtual ~ManifestSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- ManifestSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.cpp
deleted file mode 100644
index 4ee41b1c02d..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(MediaSourceDirective);
-
-MediaSourceDirective::MediaSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#media-src-pre-request
-Directive::Result MediaSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, media-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::MediaSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#media-src-post-request
-Directive::Result MediaSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, media-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::MediaSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.h
deleted file mode 100644
index e143c7736a4..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/MediaSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-media-src
-class MediaSourceDirective final : public Directive {
- GC_CELL(MediaSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(MediaSourceDirective);
-
-public:
- virtual ~MediaSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- MediaSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.cpp
deleted file mode 100644
index 668ed644ac9..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ObjectSourceDirective);
-
-ObjectSourceDirective::ObjectSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#object-src-pre-request
-Directive::Result ObjectSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, object-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ObjectSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#object-src-post-request
-Directive::Result ObjectSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, object-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ObjectSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h
deleted file mode 100644
index 3548acdad01..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-object-src
-class ObjectSourceDirective final : public Directive {
- GC_CELL(ObjectSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ObjectSourceDirective);
-
-public:
- virtual ~ObjectSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- ObjectSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.cpp
deleted file mode 100644
index facd693dbd6..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ReportToDirective);
-
-ReportToDirective::ReportToDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.h
deleted file mode 100644
index beab4da890f..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportToDirective.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-report-to
-class ReportToDirective final : public Directive {
- GC_CELL(ReportToDirective, Directive)
- GC_DECLARE_ALLOCATOR(ReportToDirective);
-
-public:
- virtual ~ReportToDirective() = default;
-
-private:
- ReportToDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.cpp
deleted file mode 100644
index 4169c759351..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ReportUriDirective);
-
-ReportUriDirective::ReportUriDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.h
deleted file mode 100644
index dfec3caa6a8..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ReportUriDirective.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-report-uri
-class ReportUriDirective final : public Directive {
- GC_CELL(ReportUriDirective, Directive)
- GC_DECLARE_ALLOCATOR(ReportUriDirective);
-
-public:
- virtual ~ReportUriDirective() = default;
-
-private:
- ReportUriDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.cpp
deleted file mode 100644
index e0afc0f513c..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.cpp
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.h>
-#include <LibWeb/ContentSecurityPolicy/Policy.h>
-#include <LibWebCommon/HTML/SandboxingFlagSet.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(SandboxDirective);
-
-SandboxDirective::SandboxDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#sandbox-init
-Directive::Result SandboxDirective::initialization(Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>> context, GC::Ref<Policy const> policy) const
-{
- // 1. If policy’s disposition is not "enforce", or context is not a WorkerGlobalScope, then abort this algorithm.
- // FIXME: File spec issue that this step doesn't specify the return value. It must be allowed, because Document
- // asserts that the result of this algorithm is Allowed.
- if (policy->disposition() != Policy::Disposition::Enforce || !context.has<GC::Ref<HTML::WorkerGlobalScope const>>())
- return Result::Allowed;
-
- // 2. Let sandboxing flag set be a new sandboxing flag set.
- // 3. Parse a sandboxing directive using this directive’s value as the input, and sandboxing flag set as the output.
- // FIXME: File spec issue that "parse a sandboxing directive" does not accept a set of tokens.
- auto sandboxing_flag_set = HTML::parse_a_sandboxing_directive(value());
-
- // 4. If sandboxing flag set contains either the sandboxed scripts browsing context flag or the sandboxed origin
- // browsing context flag flags, return "Blocked".
- // Spec Note: This will need to change if we allow Workers to be sandboxed into unique origins, which seems like a
- // pretty reasonable thing to do.
- if (has_flag(sandboxing_flag_set, HTML::SandboxingFlagSet::SandboxedScripts) || has_flag(sandboxing_flag_set, HTML::SandboxingFlagSet::SandboxedOrigin))
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.h
deleted file mode 100644
index ebc6bf67e63..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-sandbox
-class SandboxDirective final : public Directive {
- GC_CELL(SandboxDirective, Directive)
- GC_DECLARE_ALLOCATOR(SandboxDirective);
-
-public:
- virtual ~SandboxDirective() = default;
-
- [[nodiscard]] virtual Result initialization(Variant<GC::Ref<DOM::Document const>, GC::Ref<HTML::WorkerGlobalScope const>>, GC::Ref<Policy const>) const override;
-
-private:
- SandboxDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.cpp
deleted file mode 100644
index d21010f876e..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ScriptSourceAttributeDirective);
-
-ScriptSourceAttributeDirective::ScriptSourceAttributeDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-attr-inline
-Directive::Result ScriptSourceAttributeDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Assert: element is not null or type is "navigation".
- VERIFY(element || type == InlineType::Navigation);
-
- // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-attr and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrcAttr, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.h
deleted file mode 100644
index 057de786b94..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceAttributeDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-script-src-attr
-class ScriptSourceAttributeDirective final : public Directive {
- GC_CELL(ScriptSourceAttributeDirective, Directive)
- GC_DECLARE_ALLOCATOR(ScriptSourceAttributeDirective);
-
-public:
- virtual ~ScriptSourceAttributeDirective() = default;
-
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- ScriptSourceAttributeDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp
deleted file mode 100644
index 9f32a5bd99c..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ScriptSourceDirective);
-
-ScriptSourceDirective::ScriptSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-pre-request
-Directive::Result ScriptSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
- // and policy.
- return script_directives_pre_request_check(request, *this, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-post-request
-Directive::Result ScriptSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
- // directive, and policy.
- return script_directives_post_request_check(request, response, *this, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-inline
-Directive::Result ScriptSourceDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Assert: element is not null or type is "navigation".
- VERIFY(element || type == InlineType::Navigation);
-
- // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source, is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h
deleted file mode 100644
index caf22046a5b..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-script-src
-class ScriptSourceDirective final : public Directive {
- GC_CELL(ScriptSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(ScriptSourceDirective);
-
-public:
- virtual ~ScriptSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- ScriptSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.cpp
deleted file mode 100644
index 21b6732eec3..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(ScriptSourceElementDirective);
-
-ScriptSourceElementDirective::ScriptSourceElementDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-elem-pre-request
-Directive::Result ScriptSourceElementDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
- // and policy.
- return script_directives_pre_request_check(request, *this, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-elem-post-request
-Directive::Result ScriptSourceElementDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
- // directive, and policy.
- return script_directives_post_request_check(request, response, *this, policy);
-}
-
-// https://w3c.github.io/webappsec-csp/#script-src-elem-inline
-Directive::Result ScriptSourceElementDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Assert: element is not null or type is "navigation".
- VERIFY(element || type == InlineType::Navigation);
-
- // 2. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h
deleted file mode 100644
index c31fbe6bdd8..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-script-src-elem
-class ScriptSourceElementDirective final : public Directive {
- GC_CELL(ScriptSourceElementDirective, Directive)
- GC_DECLARE_ALLOCATOR(ScriptSourceElementDirective);
-
-public:
- virtual ~ScriptSourceElementDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- ScriptSourceElementDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.cpp
deleted file mode 100644
index 2280291c5ea..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(StyleSourceAttributeDirective);
-
-StyleSourceAttributeDirective::StyleSourceAttributeDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-attr-inline
-Directive::Result StyleSourceAttributeDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-attr and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrcAttr, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source, is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.h
deleted file mode 100644
index fe9bf3f17e5..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceAttributeDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-style-src-attr
-class StyleSourceAttributeDirective final : public Directive {
- GC_CELL(StyleSourceAttributeDirective, Directive)
- GC_DECLARE_ALLOCATOR(StyleSourceAttributeDirective);
-
-public:
- virtual ~StyleSourceAttributeDirective() = default;
-
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- StyleSourceAttributeDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.cpp
deleted file mode 100644
index ea840f318ed..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(StyleSourceDirective);
-
-StyleSourceDirective::StyleSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-pre-request
-Directive::Result StyleSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
- // and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value, and
- // policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-post-request
-Directive::Result StyleSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
- // and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-inline
-Directive::Result StyleSourceDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source, is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h
deleted file mode 100644
index 67ee2a164be..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-style-src
-class StyleSourceDirective final : public Directive {
- GC_CELL(StyleSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(StyleSourceDirective);
-
-public:
- virtual ~StyleSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- StyleSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.cpp
deleted file mode 100644
index 4512b94d169..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(StyleSourceElementDirective);
-
-StyleSourceElementDirective::StyleSourceElementDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-elem-pre-request
-Directive::Result StyleSourceElementDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
- // and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value, and
- // policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-elem-post-request
-Directive::Result StyleSourceElementDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.3 Does nonce match source list? on request’s cryptographic nonce metadata
- // and this directive’s value is "Matches", return "Allowed".
- if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
- return Result::Allowed;
-
- // 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 5. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#style-src-elem-inline
-Directive::Result StyleSourceElementDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, Utf16View source) const
-{
- // 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
- auto name = get_the_effective_directive_for_inline_checks(type);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
- // directive’s value, type, and source, is "Does Not Match", return "Blocked".
- if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h
deleted file mode 100644
index 6b061b8ab5e..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-style-src-elem
-class StyleSourceElementDirective final : public Directive {
- GC_CELL(StyleSourceElementDirective, Directive)
- GC_DECLARE_ALLOCATOR(StyleSourceElementDirective);
-
-public:
- virtual ~StyleSourceElementDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
- virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, Utf16View) const override;
-
-private:
- StyleSourceElementDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.cpp
deleted file mode 100644
index 8ba90772179..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.h>
-#include <LibWebCommon/Infra/Strings.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(WebRTCDirective);
-
-WebRTCDirective::WebRTCDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#webrtc-pre-connect
-Directive::Result WebRTCDirective::webrtc_pre_connect_check(GC::Ref<Policy const>) const
-{
- // 1. If this directive’s value contains a single item which is an ASCII case-insensitive match for the string
- // "'allow'", return "Allowed".
- if (value().size() == 1 && value().first().equals_ignoring_ascii_case("'allow'"sv))
- return Result::Allowed;
-
- // 2. Return "Blocked".
- return Result::Blocked;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.h
deleted file mode 100644
index 5ea7364b5d4..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCDirective.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-webrtc
-class WebRTCDirective final : public Directive {
- GC_CELL(WebRTCDirective, Directive)
- GC_DECLARE_ALLOCATOR(WebRTCDirective);
-
-public:
- virtual ~WebRTCDirective() = default;
-
- [[nodiscard]] virtual Result webrtc_pre_connect_check(GC::Ref<Policy const>) const override;
-
-private:
- WebRTCDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.cpp
new file mode 100644
index 00000000000..347c8d5416f
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.cpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#webrtc-pre-connect
+static Directive::Result webrtc_directive_pre_connect_check(Directive const& directive, Policy const&)
+{
+ // 1. If this directive’s value contains a single item which is an ASCII case-insensitive match for the string
+ // "'allow'", return "Allowed".
+ if (directive.value().size() == 1 && directive.value().first().equals_ignoring_ascii_case("'allow'"sv))
+ return Directive::Result::Allowed;
+
+ // 2. Return "Blocked".
+ return Directive::Result::Blocked;
+}
+
+Directive::Result webrtc_pre_connect_check(Directive const& directive, Policy const& policy)
+{
+ if (directive.kind() == Directive::Kind::WebRTC)
+ return webrtc_directive_pre_connect_check(directive, policy);
+ return Directive::Result::Allowed;
+}
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.h
new file mode 100644
index 00000000000..1c44577f2b7
--- /dev/null
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WebRTCIntegration.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::ContentSecurityPolicy::Directives {
+
+// https://w3c.github.io/webappsec-csp/#directive-webrtc-pre-connect-check
+// A webrtc pre-connect check, which takes a policy, and is executed during § 4.3.1 Should RTC connections be blocked
+// for global?. It returns "Allowed" unless otherwise specified.
+Directive::Result webrtc_pre_connect_check(Directive const&, Policy const&);
+
+}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.cpp
deleted file mode 100644
index 17a03a3b4da..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-GC_DEFINE_ALLOCATOR(WorkerSourceDirective);
-
-WorkerSourceDirective::WorkerSourceDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
-// https://w3c.github.io/webappsec-csp/#worker-src-pre-request
-Directive::Result WorkerSourceDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, worker-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::WorkerSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.5 Does request match source list? on request, this directive’s value,
- // and policy, is "Does Not Match", return "Blocked".
- if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-// https://w3c.github.io/webappsec-csp/#worker-src-post-request
-Directive::Result WorkerSourceDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
-{
- // 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
- auto name = get_the_effective_directive_for_request(request);
-
- // 2. If the result of executing § 6.8.4 Should fetch directive execute on name, worker-src and policy is "No",
- // return "Allowed".
- if (should_fetch_directive_execute(name, Names::WorkerSrc, policy) == ShouldExecute::No)
- return Result::Allowed;
-
- // 3. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
- // directive’s value, and policy, is "Does Not Match", return "Blocked".
- if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
- return Result::Blocked;
-
- // 4. Return "Allowed".
- return Result::Allowed;
-}
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.h
deleted file mode 100644
index 26c06d7de23..00000000000
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/WorkerSourceDirective.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2024, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
-
-namespace Web::ContentSecurityPolicy::Directives {
-
-// https://w3c.github.io/webappsec-csp/#directive-worker-src
-class WorkerSourceDirective final : public Directive {
- GC_CELL(WorkerSourceDirective, Directive)
- GC_DECLARE_ALLOCATOR(WorkerSourceDirective);
-
-public:
- virtual ~WorkerSourceDirective() = default;
-
- virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
- virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
-
-private:
- WorkerSourceDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
-
-}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
index 50c900d9030..45155e315bd 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp
@@ -7,7 +7,6 @@
#include <AK/GenericLexer.h>
#include <AK/String.h>
#include <LibTextCodec/Decoder.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
@@ -78,10 +77,10 @@ GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Variant<ByteStrin
}
// 7. Let directive be a new directive whose name is directive name, and value is directive value.
- auto directive = Directives::create_directive(heap, move(lowercase_directive_name), move(directive_value));
+ auto directive = Directives::Directive::create(move(lowercase_directive_name), move(directive_value));
// 8. Append directive to policy’s directive set.
- policy->m_directives.append(directive);
+ policy->m_directives.append(move(directive));
}
// 4. Return policy.
@@ -148,10 +147,8 @@ GC::Ref<Policy> Policy::create_from_serialized_policy(GC::Heap& heap, Serialized
{
auto policy = heap.allocate<Policy>();
- for (auto const& serialized_directive : serialized_policy.directives) {
- auto directive = Directives::create_directive(heap, serialized_directive.name, serialized_directive.value);
- policy->m_directives.append(directive);
- }
+ for (auto const& serialized_directive : serialized_policy.directives)
+ policy->m_directives.append(Directives::Directive::create(serialized_directive.name, serialized_directive.value));
policy->m_disposition = serialized_policy.disposition;
policy->m_source = serialized_policy.source;
@@ -163,19 +160,19 @@ GC::Ref<Policy> Policy::create_from_serialized_policy(GC::Heap& heap, Serialized
bool Policy::contains_directive_with_name(Utf16View name) const
{
auto maybe_directive = m_directives.find_if([name](auto const& directive) {
- return directive->name() == name;
+ return directive.name() == name;
});
return !maybe_directive.is_end();
}
-GC::Ptr<Directives::Directive> Policy::get_directive_by_name(Utf16View name) const
+Directives::Directive const* Policy::get_directive_by_name(Utf16View name) const
{
auto maybe_directive = m_directives.find_if([name](auto const& directive) {
- return directive->name() == name;
+ return directive.name() == name;
});
if (!maybe_directive.is_end())
- return *maybe_directive;
+ return &*maybe_directive;
return nullptr;
}
@@ -183,12 +180,7 @@ GC::Ptr<Directives::Directive> Policy::get_directive_by_name(Utf16View name) con
GC::Ref<Policy> Policy::clone(GC::Heap& heap) const
{
auto policy = heap.allocate<Policy>();
-
- for (auto directive : m_directives) {
- auto cloned_directive = directive->clone(heap);
- policy->m_directives.append(cloned_directive);
- }
-
+ policy->m_directives = m_directives;
policy->m_disposition = m_disposition;
policy->m_source = m_source;
policy->m_self_origin = m_self_origin;
@@ -200,9 +192,8 @@ SerializedPolicy Policy::serialize() const
{
Vector<Directives::SerializedDirective> serialized_directives;
- for (auto directive : m_directives) {
- serialized_directives.append(directive->serialize());
- }
+ for (auto const& directive : m_directives)
+ serialized_directives.append(directive.serialize());
return SerializedPolicy {
.directives = move(serialized_directives),
@@ -216,7 +207,7 @@ SerializedPolicy Policy::serialize() const
void Policy::remove_directive(Badge<HTML::HTMLMetaElement>, Utf16FlyString const& name)
{
m_directives.remove_all_matching([&name](auto const& directive) {
- return directive->name() == name;
+ return directive.name() == name;
});
}
@@ -225,10 +216,4 @@ void Policy::set_self_origin(Badge<HTML::HTMLMetaElement>, URL::Origin const& or
m_self_origin = origin;
}
-void Policy::visit_edges(Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_directives);
-}
-
}
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Policy.h b/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
index 7cb09d56ded..2d14dda0ec2 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Policy.h
@@ -38,14 +38,14 @@ public:
[[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]] Vector<GC::Ref<Directives::Directive>> const& directives() const { return m_directives; }
+ [[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]] bool contains_directive_with_name(Utf16View name) const;
- [[nodiscard]] GC::Ptr<Directives::Directive> get_directive_by_name(Utf16View) const;
+ [[nodiscard]] Directives::Directive const* get_directive_by_name(Utf16View) const;
[[nodiscard]] GC::Ref<Policy> clone(GC::Heap&) const;
[[nodiscard]] SerializedPolicy serialize() const;
@@ -53,16 +53,13 @@ public:
void remove_directive(Badge<HTML::HTMLMetaElement>, Utf16FlyString const& name);
void set_self_origin(Badge<HTML::HTMLMetaElement>, URL::Origin const& origin);
-protected:
- virtual void visit_edges(Cell::Visitor&) override;
-
private:
Policy() = default;
// https://w3c.github.io/webappsec-csp/#policy-directive-set
// Each policy has an associated directive set, which is an ordered set of directives that define the policy’s
// implications when applied.
- Vector<GC::Ref<Directives::Directive>> m_directives;
+ Vector<Directives::Directive> m_directives;
// https://w3c.github.io/webappsec-csp/#policy-disposition
// Each policy has an associated disposition, which is either "enforce" or "report".
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
index 6e1489eeddc..5fca6420185 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp
@@ -72,7 +72,7 @@ HTML::SandboxingFlagSet PolicyList::csp_derived_sandboxing_flags() const
{
// 1. Let directives be an empty ordered set.
// NOTE: Since the algorithm only uses the last entry, we instead use a pointer to the last entry.
- GC::Ptr<Directives::Directive> sandbox_directive = nullptr;
+ Directives::Directive const* sandbox_directive = nullptr;
// 2. For each policy in cspList:
for (auto const policy : m_policies) {
@@ -83,11 +83,11 @@ HTML::SandboxingFlagSet PolicyList::csp_derived_sandboxing_flags() const
// 2. If policy's directive set contains a directive whose name is "sandbox", then append that directive to
// directives.
auto maybe_sandbox_directive = policy->directives().find_if([](auto const& directive) {
- return directive->name() == Directives::Names::Sandbox;
+ return directive.kind() == Directives::Directive::Kind::Sandbox;
});
if (!maybe_sandbox_directive.is_end())
- sandbox_directive = *maybe_sandbox_directive;
+ sandbox_directive = &*maybe_sandbox_directive;
}
// 3. If directives is empty, then return an empty sandboxing flag set.
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 8e61bcc663d..c624b13b99a 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -90,7 +90,7 @@
#include <LibWeb/CSS/TransitionEvent.h>
#include <LibWeb/CSS/VisualViewport.h>
#include <LibWeb/ComputedValuesRustFFI.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/DOM/AbstractElement.h>
@@ -11466,9 +11466,9 @@ 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()) {
// 1. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. Execute directive’s initialization algorithm on document, and assert: its returned value is "Allowed".
- auto result = directive->initialization(GC::Ref { *this }, policy);
+ auto result = ContentSecurityPolicy::Directives::initialization(directive, GC::Ref { *this }, policy);
VERIFY(result == ContentSecurityPolicy::Directives::Directive::Result::Allowed);
}
}
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index d109579b1c2..3b5454154da 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -167,30 +167,7 @@ class Violation;
namespace Web::ContentSecurityPolicy::Directives {
-class BaseUriDirective;
-class ChildSourceDirective;
-class ConnectSourceDirective;
-class DefaultSourceDirective;
class Directive;
-class FontSourceDirective;
-class FormActionDirective;
-class FrameAncestorsDirective;
-class FrameSourceDirective;
-class ImageSourceDirective;
-class ManifestSourceDirective;
-class MediaSourceDirective;
-class ObjectSourceDirective;
-class ReportToDirective;
-class ReportUriDirective;
-class SandboxDirective;
-class ScriptSourceAttributeDirective;
-class ScriptSourceDirective;
-class ScriptSourceElementDirective;
-class StyleSourceAttributeDirective;
-class StyleSourceDirective;
-class StyleSourceElementDirective;
-class WebRTCDirective;
-class WorkerSourceDirective;
}
diff --git a/Libraries/LibWeb/HTML/NavigationParams.cpp b/Libraries/LibWeb/HTML/NavigationParams.cpp
index a15ddddd162..7a68ba7d052 100644
--- a/Libraries/LibWeb/HTML/NavigationParams.cpp
+++ b/Libraries/LibWeb/HTML/NavigationParams.cpp
@@ -51,7 +51,7 @@ bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::In
// 2. If policy's directive set contains a frame-ancestors directive, then return true.
auto maybe_frame_ancestors = policy->directives().find_if([](auto const& directive) {
- return directive->name() == ContentSecurityPolicy::Directives::Names::FrameAncestors;
+ return directive.kind() == ContentSecurityPolicy::Directives::Directive::Kind::FrameAncestors;
});
if (!maybe_frame_ancestors.is_end())
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
index e06cbf0d111..b7caedc71dc 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
@@ -9,7 +9,7 @@
#include <LibGC/Heap.h>
#include <LibWeb/CSS/FontComputer.h>
#include <LibWeb/CSS/FontFaceSet.h>
-#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
@@ -257,10 +257,10 @@ ContentSecurityPolicy::Directives::Directive::Result WorkerGlobalScope::run_csp_
// 2. For each policy of global’s CSP list:
for (auto policy : policy_container()->csp_list->policies()) {
// 1. For each directive of policy:
- for (auto directive : policy->directives()) {
+ for (auto const& directive : policy->directives()) {
// 1. Execute directive’s initialization algorithm on global. If its returned value is "Blocked", then set
// result to "Blocked".
- auto directive_result = directive->initialization(GC::Ref { *this }, policy);
+ auto directive_result = ContentSecurityPolicy::Directives::initialization(directive, GC::Ref { *this }, policy);
if (directive_result == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {
result = ContentSecurityPolicy::Directives::Directive::Result::Blocked;
}
diff --git a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
index 4f644cd9a9f..127b8774f2d 100644
--- a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
+++ b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
@@ -40,22 +40,17 @@ static bool sink_group_matches(Utf16String const& directive_value, Utf16View sin
return value.equals_ignoring_ascii_case(sink_group);
}
-GC_DEFINE_ALLOCATOR(RequireTrustedTypesForDirective);
-
-RequireTrustedTypesForDirective::RequireTrustedTypesForDirective(Utf16FlyString name, Vector<Utf16String> value)
- : Directive(move(name), move(value))
-{
-}
-
// https://www.w3.org/TR/trusted-types/#require-trusted-types-for-pre-navigation-check
-ContentSecurityPolicy::Directives::Directive::Result RequireTrustedTypesForDirective::pre_navigation_check(GC::Ref<Fetch::Infrastructure::Request> request, NavigationType, GC::Ref<ContentSecurityPolicy::Policy const>) const
+ContentSecurityPolicy::Directives::Directive::Result require_trusted_types_for_pre_navigation_check(Fetch::Infrastructure::Request& request)
{
+ using Result = ContentSecurityPolicy::Directives::Directive::Result;
+
// 1. If request’s url’s scheme is not "javascript", return "Allowed" and abort further steps.
- if (request->url().scheme() != "javascript"sv)
+ if (request.url().scheme() != "javascript"sv)
return Result::Allowed;
// 2. Let urlString be the result of running the URL serializer on request’s url.
- auto url_string = request->url().serialize();
+ auto url_string = request.url().serialize();
// 3. Let encodedScriptSource be the result of removing the leading "javascript:" from urlString.
auto const encoded_script_source = MUST(url_string.substring_from_byte_offset("javascript:"sv.length()));
@@ -71,7 +66,7 @@ ContentSecurityPolicy::Directives::Directive::Result RequireTrustedTypesForDirec
// "Location href":
auto converted_script_source = process_value_with_a_default_policy(
TrustedTypeName::TrustedScript,
- request->client()->global_object(),
+ request.client()->global_object(),
Utf16String::from_utf8(encoded_script_source),
InjectionSink::Location_href);
@@ -93,7 +88,7 @@ ContentSecurityPolicy::Directives::Directive::Result RequireTrustedTypesForDirec
return Result::Blocked;
// 7. Set request’s url to newURL.
- request->set_url(new_url.value());
+ request.set_url(new_url.value());
// 8. Return "Allowed".
return Result::Allowed;
diff --git a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h
index 1117ef11eaf..28b7d1f5f7f 100644
--- a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h
+++ b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h
@@ -25,19 +25,8 @@ enum class IncludeReportOnlyPolicies {
No
};
-// https://www.w3.org/TR/trusted-types/#require-trusted-types-for-csp-directive
-class RequireTrustedTypesForDirective final : public ContentSecurityPolicy::Directives::Directive {
- GC_CELL(RequireTrustedTypesForDirective, ContentSecurityPolicy::Directives::Directive)
- GC_DECLARE_ALLOCATOR(RequireTrustedTypesForDirective);
-
-public:
- ~RequireTrustedTypesForDirective() override = default;
-
- Result pre_navigation_check(GC::Ref<Fetch::Infrastructure::Request>, NavigationType, GC::Ref<ContentSecurityPolicy::Policy const>) const override;
-
-private:
- RequireTrustedTypesForDirective(Utf16FlyString name, Vector<Utf16String> value);
-};
+// 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&);
bool does_sink_require_trusted_types(JS::Object&, Utf16View, IncludeReportOnlyPolicies);
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index d6d49e46764..a87fea663e3 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -18,6 +18,7 @@ Crash/DOM/shared-resource-request-finalize-while-in-flight.html
Text/input/css/linked-stylesheet-redirect-base-url.html
Text/input/HTML/initial-about-blank-window-outlives-abandoned-navigation.html
Crash/HTML/location-href-setter-during-navigation-from-initial-about-blank.html
+Text/input/HTML/csp-frame-ancestors-checks-every-ancestor.html
Text/input/css/font-cache-first-paint.html
Text/input/css/font-display-timeline.html
Text/input/css/font-cascade-stable-display-period.html
diff --git a/Tests/LibWeb/Text/expected/HTML/csp-frame-ancestors-checks-every-ancestor.txt b/Tests/LibWeb/Text/expected/HTML/csp-frame-ancestors-checks-every-ancestor.txt
new file mode 100644
index 00000000000..d6e7881503f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/csp-frame-ancestors-checks-every-ancestor.txt
@@ -0,0 +1,3 @@
+allowed by both ancestors: true
+blocked by the top-level document: true
+blocked by the parent: true
diff --git a/Tests/LibWeb/Text/input/HTML/csp-frame-ancestors-checks-every-ancestor.html b/Tests/LibWeb/Text/input/HTML/csp-frame-ancestors-checks-every-ancestor.html
new file mode 100644
index 00000000000..894c0727342
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/csp-frame-ancestors-checks-every-ancestor.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // frame-ancestors checks every ancestor of the framed document: here its parent, same-origin with it, and the
+ // test page above that, on another site.
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const frameHostname = uniqueLocalhostHostname("csp-frame-ancestors");
+
+ const echoOnFrameHost = async (name, headers, body) => {
+ const url = new URL(await server.createEcho("GET", `/csp-frame-ancestors-${name}-${crypto.randomUUID()}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html", ...headers },
+ body,
+ }));
+ url.hostname = frameHostname;
+ return url.href;
+ };
+
+ // The middle document frames the inner one and reports whether the inner document loaded.
+ const loadsUnder = async policy => {
+ const innerURL = await echoOnFrameHost("inner", { "Content-Security-Policy": `frame-ancestors ${policy}` },
+ "<!DOCTYPE html><span id='inner'>inner</span>");
+ const middleURL = await echoOnFrameHost("middle", {}, `<!DOCTYPE html><body><script>
+ const frame = document.createElement("iframe");
+ frame.onload = () => parent.postMessage(!!frame.contentDocument?.getElementById("inner"), "*");
+ frame.src = ${JSON.stringify(innerURL)};
+ document.body.append(frame);
+ <\/script>`);
+
+ const middle = document.createElement("iframe");
+ const loaded = new Promise(resolve => {
+ window.addEventListener("message", function receive(event) {
+ if (event.source !== middle.contentWindow)
+ return;
+ window.removeEventListener("message", receive);
+ resolve(event.data);
+ });
+ });
+ middle.src = middleURL;
+ document.body.append(middle);
+ try {
+ return await loaded;
+ } finally {
+ middle.remove();
+ }
+ };
+
+ println(`allowed by both ancestors: ${await loadsUnder(`'self' ${location.origin}`)}`);
+ println(`blocked by the top-level document: ${!(await loadsUnder("'self'"))}`);
+ println(`blocked by the parent: ${!(await loadsUnder(location.origin))}`);
+ done();
+ });
+</script>