Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / content_security_policy / directives / frame_source_directive.lucb

#==============================================================================================
#
#   frame_source_directive - ContentSecurityPolicy::Directives::FrameSourceDirective
#   (the frame-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/FrameSourceDirective.cpp
#       (lines 1-61).
#
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.cpp:16 Web::ContentSecurityPolicy::Directives::FrameSourceDirective::FrameSourceDirective.
func content_security_policy_frame_source_directive_construct(this: ContentSecurityPolicyFrameSourceDirective*, 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/FrameSourceDirective.cpp:22 Web::ContentSecurityPolicy::Directives::FrameSourceDirective::pre_request_check (override).
# https://w3c.github.io/webappsec-csp/#frame-src-pre-request
pub func content_security_policy_frame_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, frame-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_frame_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/FrameSourceDirective.cpp:42 Web::ContentSecurityPolicy::Directives::FrameSourceDirective::post_request_check (override).
# https://w3c.github.io/webappsec-csp/#frame-src-post-request
pub func content_security_policy_frame_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, frame-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_frame_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