Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / navigation_params.lucb

#==============================================================================================
#
#   navigation_params - HTML::NavigationParams and NonFetchSchemeNavigationParams: what a
#   navigation hands to the creation of a Document
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/NavigationParams.cpp (lines 1-111) with the constructors
#       of NavigationParams.h, and NavigationType.h's idl_enum_to_string (lines 19-35).
#
#       Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from NavigationParams.h:76 Web::HTML::NavigationParams::NavigationParams.
pub func html_navigation_params_construct(this: HtmlNavigationParams*, id: ak.String?, navigable: HtmlNavigable*?, request: FetchRequest*?, response: FetchResponse*?, fetch_controller: FetchController*?, commit_early_hints: gc.Function1[DomDocument*, unit]*?, coop_enforcement_result: HtmlOpenerPolicyEnforcementResult, reserved_environment: HtmlEnvironment*?, origin: web_url.Origin, policy_container: HtmlPolicyContainer*?, final_sandboxing_flag_set: HtmlSandboxingFlagSet, opener_policy: HtmlOpenerPolicy, about_base_url: web_url.Url?, user_involvement: HtmlUserNavigationInvolvement):
    this.id = id
    this.navigable = navigable
    this.request = request
    this.response = response
    this.fetch_controller = fetch_controller
    this.commit_early_hints = commit_early_hints
    this.coop_enforcement_result = coop_enforcement_result
    this.reserved_environment = reserved_environment
    this.origin = origin
    this.policy_container = policy_container
    this.final_sandboxing_flag_set = final_sandboxing_flag_set
    this.opener_policy = opener_policy
    this.about_base_url = about_base_url
    this.user_involvement = user_involvement

## Ported from NavigationParams.cpp:20 Web::HTML::NavigationParams::visit_edges (override).
pub func html_navigation_params_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlNavigationParams*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.navigable)
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.request)  # opaque in phase 1
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.response)  # opaque in phase 1
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.fetch_controller)  # opaque in phase 1
    gc.cell_visitor_visit_t(visitor, this.commit_early_hints)
    gc.cell_visitor_visit_t(visitor, this.reserved_environment)
    gc.cell_visitor_visit_t(visitor, this.policy_container)

## Ported from NavigationParams.h:138 Web::HTML::NonFetchSchemeNavigationParams::NonFetchSchemeNavigationParams.
pub func html_non_fetch_scheme_navigation_params_construct(this: HtmlNonFetchSchemeNavigationParams*, id: ak.String?, navigable: HtmlNavigable*?, url: web_url.Url, target_snapshot_sandboxing_flags: HtmlSandboxingFlagSet, source_snapshot_has_transient_activation: bool, initiator_origin: web_url.Origin, user_involvement: HtmlUserNavigationInvolvement):
    this.id = id
    this.navigable = navigable
    this.url = url
    this.target_snapshot_sandboxing_flags = target_snapshot_sandboxing_flags
    this.source_snapshot_has_transient_activation = source_snapshot_has_transient_activation
    this.initiator_origin = initiator_origin
    this.user_involvement = user_involvement

## Ported from NavigationParams.cpp:32 Web::HTML::NonFetchSchemeNavigationParams::visit_edges (override).
pub func html_non_fetch_scheme_navigation_params_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlNonFetchSchemeNavigationParams*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.navigable)

## Ported from NavigationParams.cpp:39 Web::HTML::check_a_navigation_responses_adherence_to_x_frame_options.
# https://html.spec.whatwg.org/multipage/document-lifecycle.html#check-a-navigation-response's-adherence-to-x-frame-options
pub func html_check_a_navigation_responses_adherence_to_x_frame_options(response: FetchResponse*?, navigable: HtmlNavigable*?, csp_list: ContentSecurityPolicyPolicyList*, destination_origin: web_url.Origin) -> bool:
    let the_navigable = navigable else trap("check_a_navigation_responses_adherence_to_x_frame_options: no navigable")

    # 1. If navigable is not a child navigable, then return true.
    if html_navigable_parent(the_navigable) == none:
        return true

    # 2. For each policy of cspList:
    var policies = content_security_policy_policy_list_policies(csp_list)
    for policy in ak.vector_span(&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 frame-ancestors directive, then return true.
        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_frame_ancestors):
                return true

    # 3. Let rawXFrameOptions be the result of getting, decoding, and splitting `X-Frame-Options` from response's header list.
    let the_response = response else trap("check_a_navigation_responses_adherence_to_x_frame_options: no response")
    let raw_x_frame_options = http_header_list_get_decode_and_split(fetch_response_header_list(the_response), ak.sv("X-Frame-Options"))

    # 4. Let xFrameOptions be a new set.
    var x_frame_options = ak.OrderedHashTable[ak.String, ak.StringTraits]()

    # 5. For each value of rawXFrameOptions, append value, converted to ASCII lowercase, to xFrameOptions.
    if let raw_values = raw_x_frame_options:
        for value in ak.vector_span_const(&raw_values):
            ak.ordered_hash_table_set(&x_frame_options, ak.string_to_ascii_lowercase(&value))

    # 6. If xFrameOptions's size is greater than 1, and xFrameOptions contains any of "deny", "allowall", or "sameorigin", then return false.
    if ak.ordered_hash_table_size(&x_frame_options) > 1 and (ak.ordered_hash_table_contains_t(&x_frame_options, ak.string_from_str("deny")) or ak.ordered_hash_table_contains_t(&x_frame_options, ak.string_from_str("allowall")) or ak.ordered_hash_table_contains_t(&x_frame_options, ak.string_from_str("sameorigin"))):
        return false

    # 7. If xFrameOptions's size is greater than 1, then return true.
    if ak.ordered_hash_table_size(&x_frame_options) > 1:
        return true

    # 8. If xFrameOptions[0] is "deny", then return false.
    var first_x_frame_option = ak.ordered_hash_table_begin(&x_frame_options)
    let first_value = first_x_frame_option.next()
    if not ak.ordered_hash_table_is_empty(&x_frame_options) and ak.string_eq_string_view(first_value else trap("unreachable: not empty"), ak.sv("deny")):
        return false

    # 9. If xFrameOptions[0] is "sameorigin", then:
    if not ak.ordered_hash_table_is_empty(&x_frame_options) and ak.string_eq_string_view(first_value else trap("unreachable: not empty"), ak.sv("sameorigin")):
        # 1. Let containerDocument be navigable's container document.
        var container_document = html_navigable_container_document(the_navigable)

        # 2. While containerDocument is not null:
        while let document = container_document:
            # 1. If containerDocument's origin is not same origin with destinationOrigin, then return false.
            let document_origin = dom_document_origin(document)
            if not web_url.origin_is_same_origin(&document_origin, destination_origin):
                return false

            # 2. Set containerDocument to containerDocument's container document.
            container_document = dom_document_container_document(document)

    # 10. Return true.
    return true

## Ported from NavigationType.h:21 Web::Bindings::idl_enum_to_string.
pub func bind_idl_enum_to_string(value: BindNavigationType) -> ak.String:
    match value:
        .push:
            return ak.string_from_str("push")
        .replace:
            return ak.string_from_str("replace")
        .reload:
            return ak.string_from_str("reload")
        .traverse:
            return ak.string_from_str("traverse")
        _:
            return ak.string_from_str("<unknown>")