Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / content_security_policy / directives / default_source_directive.lucb

#==============================================================================================
#
#   default_source_directive - ContentSecurityPolicy::Directives::DefaultSourceDirective
#   (the default-src directive): it runs the checks of the request's effective directive with its own value
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp
#       (lines 1-73).
#
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp:17 Web::ContentSecurityPolicy::Directives::DefaultSourceDirective::DefaultSourceDirective.
func content_security_policy_default_source_directive_construct(this: ContentSecurityPolicyDefaultSourceDirective*, 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/DefaultSourceDirective.cpp:23 Web::ContentSecurityPolicy::Directives::DefaultSourceDirective::pre_request_check (override).
# https://w3c.github.io/webappsec-csp/#default-src-pre-request
pub func content_security_policy_default_source_directive_pre_request_check_impl(base: ContentSecurityPolicyDirective*, heap: 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, default-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_default_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.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.
    let name_value = name else trap("VERIFY: Optional::value of the effective directive")
    let directive = content_security_policy_directives_create_directive(heap, ak.fly_string_to_string_const(&name_value), base.m_value)
    return content_security_policy_directive_pre_request_check(directive, heap, request, policy)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp:40 Web::ContentSecurityPolicy::Directives::DefaultSourceDirective::post_request_check (override).
# https://w3c.github.io/webappsec-csp/#default-src-post-request
pub func content_security_policy_default_source_directive_post_request_check_impl(base: ContentSecurityPolicyDirective*, heap: 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, default-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_default_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.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.
    let name_value = name else trap("VERIFY: Optional::value of the effective directive")
    let directive = content_security_policy_directives_create_directive(heap, ak.fly_string_to_string_const(&name_value), base.m_value)
    return content_security_policy_directive_post_request_check(directive, heap, request, response, policy)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/DefaultSourceDirective.cpp:57 Web::ContentSecurityPolicy::Directives::DefaultSourceDirective::inline_check (override).
# https://w3c.github.io/webappsec-csp/#default-src-inline
pub func content_security_policy_default_source_directive_inline_check_impl(base: ContentSecurityPolicyDirective*, heap: gc.Heap*, element: DomElement*?, type_: ContentSecurityPolicyInlineType, policy: ContentSecurityPolicyPolicy*, source: ak.String) -> ContentSecurityPolicyResult:
    # 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
    let name = content_security_policy_directives_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 content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_default_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.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.
    let directive = content_security_policy_directives_create_directive(heap, ak.fly_string_to_string_const(&name), base.m_value)
    return content_security_policy_directive_inline_check(directive, heap, element, type_, policy, source)