/* * Copyright (c) 2024, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Web::ContentSecurityPolicy::Directives { GC_DEFINE_ALLOCATOR(ScriptSourceDirective); ScriptSourceDirective::ScriptSourceDirective(String name, Vector 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 request, GC::Ref 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 request, GC::Ref response, GC::Ref 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 element, InlineType type, GC::Ref policy, String const& 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; } }