Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / sandboxing_flag_set.lucb

#==============================================================================================
#
#   sandboxing_flag_set - HTML::SandboxingFlagSet: the flag operators and parsing a sandboxing
#   directive
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/SandboxingFlagSet.h (lines 1-41: the
#       AK_ENUM_BITWISE_OPERATORS of SandboxingFlagSet and is_empty) and SandboxingFlagSet.cpp
#       (lines 1-120: parse_a_sandboxing_directive).
#
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: SandboxingFlagSet.h =====================================================================

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator&= (AK_ENUM_BITWISE_OPERATORS).
pub func html_and_assign(lhs: HtmlSandboxingFlagSet*, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet*:
    *lhs = *lhs & rhs
    return lhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator& (AK_ENUM_BITWISE_OPERATORS).
pub func html_bit_and(lhs: HtmlSandboxingFlagSet, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet:
    return lhs & rhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator~ (AK_ENUM_BITWISE_OPERATORS).
pub func html_bit_not(rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet:
    return ~rhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator| (AK_ENUM_BITWISE_OPERATORS).
pub func html_bit_or(lhs: HtmlSandboxingFlagSet, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet:
    return lhs | rhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator^ (AK_ENUM_BITWISE_OPERATORS).
pub func html_bit_xor(lhs: HtmlSandboxingFlagSet, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet:
    return lhs ^ rhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::has_any_flag (AK_ENUM_BITWISE_OPERATORS).
pub func html_has_any_flag(value: HtmlSandboxingFlagSet, mask: HtmlSandboxingFlagSet) -> bool:
    return (i32)(value & mask) != 0

## Ported from SandboxingFlagSet.h:36 Web::HTML::has_flag (AK_ENUM_BITWISE_OPERATORS).
pub func html_has_flag(value: HtmlSandboxingFlagSet, mask: HtmlSandboxingFlagSet) -> bool:
    return (value & mask) == mask

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator|= (AK_ENUM_BITWISE_OPERATORS).
pub func html_or_assign(lhs: HtmlSandboxingFlagSet*, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet*:
    *lhs = *lhs | rhs
    return lhs

## Ported from SandboxingFlagSet.h:36 Web::HTML::operator^= (AK_ENUM_BITWISE_OPERATORS).
pub func html_xor_assign(lhs: HtmlSandboxingFlagSet*, rhs: HtmlSandboxingFlagSet) -> HtmlSandboxingFlagSet*:
    *lhs = *lhs ^ rhs
    return lhs

## Ported from SandboxingFlagSet.h:37 Web::HTML::is_empty.
pub func html_is_empty(s: HtmlSandboxingFlagSet) -> bool:
    return ((i32)s & 0x1FF) == 0

# mark: Parsing a sandboxing directive ==========================================================

## `tokens.contains_slow(keyword)` of parse_a_sandboxing_directive.
func html_sandboxing_tokens_contain(tokens: const ak.Vector[ak.String]*, keyword: str) -> bool:
    return ak.vector_contains_slow[ak.String, ak.StringTraits](tokens, ak.string_from_str(keyword))

## Ported from SandboxingFlagSet.cpp:13 Web::HTML::parse_a_sandboxing_directive.
## The `Variant<String, Vector<String>>` input is `ak.KeyPath` (the generator's name for it).
#  https://html.spec.whatwg.org/multipage/browsers.html#parse-a-sandboxing-directive
pub func html_parse_a_sandboxing_directive(input: ak.KeyPath) -> HtmlSandboxingFlagSet:
    # 1. Split input on ASCII whitespace, to obtain tokens.
    var tokens = ak.Vector[ak.String]()
    match input:
        .string(string):
            let lowercase_input = ak.string_to_ascii_lowercase(&string)
            let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
            let lowercase_view = ak.string_bytes_as_string_view(&lowercase_input)
            var token_views = ak.string_view_split_view_if(&lowercase_view, &is_ascii_whitespace)
            ak.vector_ensure_capacity(&tokens, ak.vector_size(&token_views))
            for token in ak.vector_span(&token_views):
                ak.vector_unchecked_append_u(&tokens, ak.string_from_utf8(token) else trap("MUST: String::from_utf8"))
        .vector_string(pre_parsed_tokens):
            ak.vector_ensure_capacity(&tokens, ak.vector_size(&pre_parsed_tokens))
            for token in ak.vector_span_const(&pre_parsed_tokens):
                ak.vector_unchecked_append_u(&tokens, ak.string_to_ascii_lowercase(&token))

    # 2. Let output be empty.
    var output = (HtmlSandboxingFlagSet)0

    # 3. Add the following flags to output:
    # - The sandboxed navigation browsing context flag.
    output |= HtmlSandboxingFlagSet.sandboxed_navigation

    # - The sandboxed auxiliary navigation browsing context flag, unless tokens contains the allow-popups keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-popups"):
        output |= HtmlSandboxingFlagSet.sandboxed_auxiliary_navigation

    # - The sandboxed top-level navigation without user activation browsing context flag, unless tokens contains the
    #   allow-top-navigation keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-top-navigation"):
        output |= HtmlSandboxingFlagSet.sandboxed_top_level_navigation_without_user_activation

    # - The sandboxed top-level navigation with user activation browsing context flag, unless tokens contains either
    #   the allow-top-navigation-by-user-activation keyword or the allow-top-navigation keyword.
    # Spec Note: This means that if the allow-top-navigation is present, the allow-top-navigation-by-user-activation
    #            keyword will have no effect. For this reason, specifying both is a document conformance error.
    if not html_sandboxing_tokens_contain(&tokens, "allow-top-navigation") and not html_sandboxing_tokens_contain(&tokens, "allow-top-navigation-by-user-activation"):
        output |= HtmlSandboxingFlagSet.sandboxed_top_level_navigation_with_user_activation

    # - The sandboxed origin browsing context flag, unless the tokens contains the allow-same-origin keyword.
    # Spec Note: The allow-same-origin keyword is intended for two cases.
    #
    #            First, it can be used to allow content from the same site to be sandboxed to disable scripting,
    #            while still allowing access to the DOM of the sandboxed content.
    #
    #            Second, it can be used to embed content from a third-party site, sandboxed to prevent that site from
    #            opening popups, etc, without preventing the embedded page from communicating back to its originating
    #            site, using the database APIs to store data, etc.
    if not html_sandboxing_tokens_contain(&tokens, "allow-same-origin"):
        output |= HtmlSandboxingFlagSet.sandboxed_origin

    # - The sandboxed forms browsing context flag, unless tokens contains the allow-forms keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-forms"):
        output |= HtmlSandboxingFlagSet.sandboxed_forms

    # - The sandboxed pointer lock browsing context flag, unless tokens contains the allow-pointer-lock keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-pointer-lock"):
        output |= HtmlSandboxingFlagSet.sandboxed_pointer_lock

    # - The sandboxed scripts browsing context flag, unless tokens contains the allow-scripts keyword.
    # - The sandboxed automatic features browsing context flag, unless tokens contains the allow-scripts keyword
    #   (defined above).
    # Spec Note: This flag is relaxed by the same keyword as scripts, because when scripts are enabled these features
    #            are trivially possible anyway, and it would be unfortunate to force authors to use script to do them
    #            when sandboxed rather than allowing them to use the declarative features.
    if not html_sandboxing_tokens_contain(&tokens, "allow-scripts"):
        output |= HtmlSandboxingFlagSet.sandboxed_scripts
        output |= HtmlSandboxingFlagSet.sandboxed_automatic_features

    # - The sandboxed document.domain browsing context flag.
    output |= HtmlSandboxingFlagSet.sandboxed_document_domain

    # - The sandbox propagates to auxiliary browsing contexts flag, unless tokens contains the
    #   allow-popups-to-escape-sandbox keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-popups-to-escape-sandbox"):
        output |= HtmlSandboxingFlagSet.sandbox_propagates_to_auxiliary_browsing_contexts

    # - The sandboxed modals flag, unless tokens contains the allow-modals keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-modals"):
        output |= HtmlSandboxingFlagSet.sandboxed_modals

    # - The sandboxed orientation lock browsing context flag, unless tokens contains the allow-orientation-lock
    #   keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-orientation-lock"):
        output |= HtmlSandboxingFlagSet.sandboxed_orientation_lock

    # - The sandboxed presentation browsing context flag, unless tokens contains the allow-presentation keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-presentation"):
        output |= HtmlSandboxingFlagSet.sandboxed_presentation

    # - The sandboxed downloads browsing context flag, unless tokens contains the allow-downloads keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-downloads"):
        output |= HtmlSandboxingFlagSet.sandboxed_downloads

    # - The sandboxed custom protocols navigation browsing context flag, unless tokens contains either the
    #   allow-top-navigation-to-custom-protocols keyword, the allow-popups keyword, or the allow-top-navigation
    #   keyword.
    if not html_sandboxing_tokens_contain(&tokens, "allow-top-navigation-to-custom-protocols") and not html_sandboxing_tokens_contain(&tokens, "allow-popups") and not html_sandboxing_tokens_contain(&tokens, "allow-top-navigation"):
        output |= HtmlSandboxingFlagSet.sandboxed_custom_protocols

    return output