Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   sandbox_directive - ContentSecurityPolicy::Directives::SandboxDirective
#   (the sandbox directive): its initialization of worker global scopes
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.cpp
#       (lines 1-45).
#
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/SandboxDirective.cpp:15 Web::ContentSecurityPolicy::Directives::SandboxDirective::SandboxDirective.
func content_security_policy_sandbox_directive_construct(this: ContentSecurityPolicySandboxDirective*, 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/SandboxDirective.cpp:21 Web::ContentSecurityPolicy::Directives::SandboxDirective::initialization (override).
# https://w3c.github.io/webappsec-csp/#sandbox-init
pub func content_security_policy_sandbox_directive_initialization_impl(base: ContentSecurityPolicyDirective*, context: DomDocumentOrHtmlWorkerGlobalScope, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyResult:
    # 1. If policy’s disposition is not "enforce", or context is not a WorkerGlobalScope, then abort this algorithm.
    # FIXME: File spec issue that this step doesn't specify the return value. It must be allowed, because Document
    #        asserts that the result of this algorithm is Allowed.
    let is_worker_global_scope = match context:
        .html_worker_global_scope(_) => true
        _ => false
    if content_security_policy_policy_disposition(policy) != ContentSecurityPolicyPolicyDisposition.enforce or not is_worker_global_scope:
        return ContentSecurityPolicyResult.allowed

    # 2. Let sandboxing flag set be a new sandboxing flag set.
    # 3. Parse a sandboxing directive using this directive’s value as the input, and sandboxing flag set as the output.
    # FIXME: File spec issue that "parse a sandboxing directive" does not accept a set of tokens.
    let sandboxing_flag_set = html_parse_a_sandboxing_directive(ak.KeyPath.vector_string(base.m_value))

    # 4. If sandboxing flag set contains either the sandboxed scripts browsing context flag or the sandboxed origin
    #    browsing context flag flags, return "Blocked".
    # Spec Note: This will need to change if we allow Workers to be sandboxed into unique origins, which seems like a
    #            pretty reasonable thing to do.
    if html_has_flag(sandboxing_flag_set, HtmlSandboxingFlagSet.sandboxed_scripts) or html_has_flag(sandboxing_flag_set, HtmlSandboxingFlagSet.sandboxed_origin):
        return ContentSecurityPolicyResult.blocked

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