/* * Copyright (c) 2024, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Web::ContentSecurityPolicy::Directives { GC_DEFINE_ALLOCATOR(ConnectSourceDirective); ConnectSourceDirective::ConnectSourceDirective(String name, Vector 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 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, 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 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, 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; } }