dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / content_security_policy / directives / connect_source_directive.lucb
#==============================================================================================
#
# connect_source_directive - ContentSecurityPolicy::Directives::ConnectSourceDirective
# (the connect-src directive): its pre- and post-request checks match the request and the response to its source list
#
# DESCRIPTION:
# Port of Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp
# (lines 1-61).
#
# Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp:16 Web::ContentSecurityPolicy::Directives::ConnectSourceDirective::ConnectSourceDirective.
func content_security_policy_connect_source_directive_construct(this: ContentSecurityPolicyConnectSourceDirective*, name: ak.String, value: ak.Vector[ak.String]):
content_security_policy_directive_construct(this.content_security_policy_directive(), name, value)
## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp:22 Web::ContentSecurityPolicy::Directives::ConnectSourceDirective::pre_request_check (override).
# https://w3c.github.io/webappsec-csp/#connect-src-pre-request
pub func content_security_policy_connect_source_directive_pre_request_check_impl(base: ContentSecurityPolicyDirective*, arg0: gc.Heap*, request: FetchRequest*, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyResult:
# 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
let name = content_security_policy_directives_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 content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_connect_src, policy) == ContentSecurityPolicyShouldExecute.no:
return ContentSecurityPolicyResult.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 content_security_policy_directives_does_request_match_source_list(request, &base.m_value, policy) == ContentSecurityPolicyDirectivesMatchResult.does_not_match:
return ContentSecurityPolicyResult.blocked
# 4. Return "Allowed".
return ContentSecurityPolicyResult.allowed
## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ConnectSourceDirective.cpp:42 Web::ContentSecurityPolicy::Directives::ConnectSourceDirective::post_request_check (override).
# https://w3c.github.io/webappsec-csp/#connect-src-post-request
pub func content_security_policy_connect_source_directive_post_request_check_impl(base: ContentSecurityPolicyDirective*, arg0: gc.Heap*, request: FetchRequest*, response: FetchResponse*, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyResult:
# 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
let name = content_security_policy_directives_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 content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_connect_src, policy) == ContentSecurityPolicyShouldExecute.no:
return ContentSecurityPolicyResult.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 content_security_policy_directives_does_response_match_source_list(response, request, &base.m_value, policy) == ContentSecurityPolicyDirectivesMatchResult.does_not_match:
return ContentSecurityPolicyResult.blocked
# 4. Return "Allowed".
return ContentSecurityPolicyResult.allowed