Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   script_source_directive - ContentSecurityPolicy::Directives::ScriptSourceDirective
#   (the script-src directive): the script directives' request checks and the inline check of its source list
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp
#       (lines 1-76).
#
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp:16 Web::ContentSecurityPolicy::Directives::ScriptSourceDirective::ScriptSourceDirective.
func content_security_policy_script_source_directive_construct(this: ContentSecurityPolicyScriptSourceDirective*, 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/ScriptSourceDirective.cpp:22 Web::ContentSecurityPolicy::Directives::ScriptSourceDirective::pre_request_check (override).
# https://w3c.github.io/webappsec-csp/#script-src-pre-request
pub func content_security_policy_script_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, script-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_script_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.allowed

    # 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
    #    and policy.
    return content_security_policy_directives_script_directives_pre_request_check(request, base, policy)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp:38 Web::ContentSecurityPolicy::Directives::ScriptSourceDirective::post_request_check (override).
# https://w3c.github.io/webappsec-csp/#script-src-post-request
pub func content_security_policy_script_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, script-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_script_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.allowed

    # 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
    #    directive, and policy.
    return content_security_policy_directives_script_directives_post_request_check(request, response, base, policy)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp:54 Web::ContentSecurityPolicy::Directives::ScriptSourceDirective::inline_check (override).
# https://w3c.github.io/webappsec-csp/#script-src-inline
pub func content_security_policy_script_source_directive_inline_check_impl(base: ContentSecurityPolicyDirective*, arg0: gc.Heap*, element: DomElement*?, type_: ContentSecurityPolicyInlineType, policy: ContentSecurityPolicyPolicy*, source: ak.String) -> ContentSecurityPolicyResult:
    # 1. Assert: element is not null or type is "navigation".
    assert(element != none or type_ == ContentSecurityPolicyInlineType.navigation)

    # 2. 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_)

    # 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src and policy is "No",
    #    return "Allowed".
    if content_security_policy_directives_should_fetch_directive_execute(name, content_security_policy_directives_names_script_src, policy) == ContentSecurityPolicyShouldExecute.no:
        return ContentSecurityPolicyResult.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 content_security_policy_directives_does_element_match_source_list_for_type_and_source(element, &base.m_value, type_, source) == ContentSecurityPolicyDirectivesMatchResult.does_not_match:
        return ContentSecurityPolicyResult.blocked

    # 5. Return "Allowed".
    return ContentSecurityPolicyResult.allowed