Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / content_security_policy / policy_list.lucb

#==============================================================================================
#
#   policy_list - ContentSecurityPolicy::PolicyList: a CSP list
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp (lines 1-137) and the
#       inline functions of PolicyList.h. Region r28 ported it for phase 1 (every policy
#       container makes a CSP list); region p2e completed the Policy and Directive operations
#       it calls.
#
#       Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:21 Web::ContentSecurityPolicy::PolicyList::create
## (from a GC::RootVector of policies).
pub func content_security_policy_policy_list_create(heap: gc.Heap*, policies: const ak.Vector[ContentSecurityPolicyPolicy*]*) -> ContentSecurityPolicyPolicyList*:
    let policy_list = heap_allocate_content_security_policy_policy_list(heap)
    for policy in ak.vector_span_const(policies):
        ak.vector_append_t(&policy_list.m_policies, policy)
    return policy_list

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:29 Web::ContentSecurityPolicy::PolicyList::create
## (from serialized policies).
pub func content_security_policy_policy_list_create_serialized(heap: gc.Heap*, serialized_policies: const ak.Vector[ContentSecurityPolicySerializedPolicy]*) -> ContentSecurityPolicyPolicyList*:
    let policy_list = heap_allocate_content_security_policy_policy_list(heap)
    let serialized_policies_span = ak.vector_span_const(serialized_policies)
    for serialized_policy in &serialized_policies_span:
        let policy = content_security_policy_policy_create_from_serialized_policy(heap, serialized_policy)
        ak.vector_append_t(&policy_list.m_policies, policy)
    return policy_list

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:40 Web::ContentSecurityPolicy::PolicyList::from_object.
# https://w3c.github.io/webappsec-csp/#get-csp-of-object
pub func content_security_policy_policy_list_from_object(object: JsObject*) -> ContentSecurityPolicyPolicyList*?:
    let cell = (gc.Cell*)object

    # 1. If object is a Document return object’s policy container's CSP list.
    if let document = as_if_dom_document(cell):
        return dom_document_policy_container(document).csp_list

    # 2. If object is a Window or a WorkerGlobalScope or a WorkletGlobalScope, return environment settings object’s
    #    policy container's CSP list.
    if is_html_window(cell) or is_html_worker_global_scope(cell):
        let settings = html_relevant_settings_object_js_object(object)
        return html_environment_settings_object_policy_container(settings).csp_list

    # 3. Return null.
    return none

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:60 Web::ContentSecurityPolicy::PolicyList::visit_edges (override).
pub func content_security_policy_policy_list_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (ContentSecurityPolicyPolicyList*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_vector(visitor, &this.m_policies)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.h:26 Web::ContentSecurityPolicy::PolicyList::policies.
pub func content_security_policy_policy_list_policies(this: ContentSecurityPolicyPolicyList*) -> ak.Vector[ContentSecurityPolicyPolicy*]:
    return this.m_policies

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:67 Web::ContentSecurityPolicy::PolicyList::contains_header_delivered_policy.
# https://w3c.github.io/webappsec-csp/#contains-a-header-delivered-content-security-policy
pub func content_security_policy_policy_list_contains_header_delivered_policy(this: ContentSecurityPolicyPolicyList*) -> bool:
    # A CSP list contains a header-delivered Content Security Policy if it contains a policy whose source is "header".
    for policy in ak.vector_span(&this.m_policies):
        if content_security_policy_policy_source(policy) == ContentSecurityPolicyPolicySource.header:
            return true
    return false

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:78 Web::ContentSecurityPolicy::PolicyList::csp_derived_sandboxing_flags.
# https://html.spec.whatwg.org/multipage/browsers.html#csp-derived-sandboxing-flags
pub func content_security_policy_policy_list_csp_derived_sandboxing_flags(this: ContentSecurityPolicyPolicyList*) -> HtmlSandboxingFlagSet:
    # 1. Let directives be an empty ordered set.
    # NOTE: Since the algorithm only uses the last entry, we instead use a pointer to the last entry.
    var sandbox_directive: ContentSecurityPolicyDirective*? = none

    # 2. For each policy in cspList:
    for policy in ak.vector_span(&this.m_policies):
        # 1. If policy's disposition is not "enforce", then continue.
        if content_security_policy_policy_disposition(policy) != ContentSecurityPolicyPolicyDisposition.enforce:
            continue

        # 2. If policy's directive set contains a directive whose name is "sandbox", then append that directive to
        #   directives.
        var directives = content_security_policy_policy_directives(policy)
        for directive in ak.vector_span(&directives):
            let name = content_security_policy_directive_name(directive)
            if ak.string_eq_fly_string(&name, &content_security_policy_directives_names_sandbox):
                sandbox_directive = directive
                break

    # 3. If directives is empty, then return an empty sandboxing flag set.
    let directive = sandbox_directive else return (HtmlSandboxingFlagSet)0

    # 4. Let directive be directives[directives's size − 1].
    # NOTE: Already done.

    # 5. Return the result of parsing the sandboxing directive directive.
    return html_parse_a_sandboxing_directive(ak.KeyPath.vector_string(content_security_policy_directive_value(directive)))

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:111 Web::ContentSecurityPolicy::PolicyList::enforce_policy.
# https://w3c.github.io/webappsec-csp/#enforced
pub func content_security_policy_policy_list_enforce_policy(this: ContentSecurityPolicyPolicyList*, policy: ContentSecurityPolicyPolicy*):
    # A policy is enforced or monitored for a global object by inserting it into the global object’s CSP list.
    ak.vector_append_t(&this.m_policies, policy)

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:117 Web::ContentSecurityPolicy::PolicyList::clone.
pub func content_security_policy_policy_list_clone(this: ContentSecurityPolicyPolicyList*, heap: gc.Heap*) -> ContentSecurityPolicyPolicyList*:
    let policy_list = heap_allocate_content_security_policy_policy_list(heap)
    for policy in ak.vector_span(&this.m_policies):
        let cloned_policy = content_security_policy_policy_clone(policy, heap)
        ak.vector_append_t(&policy_list.m_policies, cloned_policy)
    return policy_list

## Ported from Libraries/LibWeb/ContentSecurityPolicy/PolicyList.cpp:127 Web::ContentSecurityPolicy::PolicyList::serialize.
pub func content_security_policy_policy_list_serialize(this: ContentSecurityPolicyPolicyList*) -> ak.Vector[ContentSecurityPolicySerializedPolicy]:
    var serialized_policies = ak.Vector[ContentSecurityPolicySerializedPolicy]()

    for policy in ak.vector_span(&this.m_policies):
        ak.vector_append_t(&serialized_policies, content_security_policy_policy_serialize(policy))

    return serialized_policies