Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / mixed_content / abstract_operations.lucb

#==============================================================================================
#
#   abstract_operations - Mixed Content: upgrading a request, and whether a request or a
#   response is blocked as mixed content
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MixedContent/AbstractOperations.cpp (lines 1-121), ported by
#       region p2c with Fetch (MixedContent is in no region; main fetch calls these).
#
#       Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/MixedContent/AbstractOperations.cpp:18 Web::MixedContent::upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate.
# https://w3c.github.io/webappsec-mixed-content/#upgrade-algorithm
pub func mixed_content_upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(request: FetchRequest*):
    let url = fetch_request_url(request)
    let destination = fetch_request_destination(request)

    # 1. If one or more of the following conditions is met, return without modifying request:
    if (secure_contexts_is_url_potentially_trustworthy(url) == SecureContextsTrustworthiness.potentially_trustworthy or mixed_content_host_is_ip_address(web_url.url_host(&url)) or mixed_content_does_settings_prohibit_mixed_security_contexts(fetch_request_client(request)) == MixedContentProhibitsMixedSecurityContexts.does_not_restrict_mixed_security_contexts or (destination != FetchRequestDestination.image and destination != FetchRequestDestination.audio and destination != FetchRequestDestination.video) or (destination == FetchRequestDestination.image and fetch_request_initiator(request) == FetchRequestInitiator.image_set)):
        # 1. request’s URL is a potentially trustworthy URL.

        # 2. request’s URL’s host is an IP address.

        # 3. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contents" when applied to request’s client.

        # 4. request’s destination is not "image", "audio", or "video".

        # 5. request’s destination is "image" and request’s initiator is "imageset".
        return

    # 2. If request’s URL’s scheme is http, set request’s URL’s scheme to https, and return.
    let scheme = web_url.url_scheme(&url)
    if ak.string_eq_string_view(&scheme, ak.sv("http")):
        web_url.url_set_scheme(ak.vector_first(fetch_request_url_list(request)), ak.string_from_str("https"))

## `url.host().has_value() && (url.host()->has<IPv4Address>() || url.host()->has<IPv6Address>())`.
func mixed_content_host_is_ip_address(host: web_url.Host?) -> bool:
    if let value = host:
        return web_url.host_has_i_pv4_address(&value) or web_url.host_has_i_pv6_address(&value)
    return false

## `response->url().has_value() && is_url_potentially_trustworthy(response->url().value()) == PotentiallyTrustworthy`.
func mixed_content_url_is_potentially_trustworthy(url: web_url.Url?) -> bool:
    if let value = url:
        return secure_contexts_is_url_potentially_trustworthy(value) == SecureContextsTrustworthiness.potentially_trustworthy
    return false

## Ported from Libraries/LibWeb/MixedContent/AbstractOperations.cpp:50 Web::MixedContent::does_settings_prohibit_mixed_security_contexts.
# https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object
pub func mixed_content_does_settings_prohibit_mixed_security_contexts(settings: HtmlEnvironmentSettingsObject*?) -> MixedContentProhibitsMixedSecurityContexts:
    # (C++ dereferences the pointer: a null settings object is not expected here.)
    let settings_object = settings else trap("VERIFY: does_settings_prohibit_mixed_security_contexts of a null settings object")

    # 1. If settings’ origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
    if secure_contexts_is_origin_potentially_trustworthy(html_environment_settings_object_origin(settings_object)) == SecureContextsTrustworthiness.potentially_trustworthy:
        return MixedContentProhibitsMixedSecurityContexts.prohibits_mixed_security_contexts

    # 2. If settings’ global object is a window, then:
    if let window = as_if_html_window((gc.Cell*)html_environment_settings_object_global_object(settings_object)):
        # 1. Set document to settings’ global object's associated Document.
        let document = html_window_document(window)

        # 2. For each navigable navigable in document’s ancestor navigables:
        let ancestor_navigables = dom_document_ancestor_navigables(document)
        for navigable in ak.vector_span_const(&ancestor_navigables):
            # 1. If navigable’s active document's origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
            let active_document = html_navigable_active_document(navigable else trap("VERIFY: an ancestor navigable")) else trap("VERIFY: the navigable's active document")
            if secure_contexts_is_origin_potentially_trustworthy(dom_document_origin(active_document)) == SecureContextsTrustworthiness.potentially_trustworthy:
                return MixedContentProhibitsMixedSecurityContexts.prohibits_mixed_security_contexts

    # 3. Return "Does Not Restrict Mixed Security Contexts".
    return MixedContentProhibitsMixedSecurityContexts.does_not_restrict_mixed_security_contexts

## `request.destination() == Document && request.client()->target_browsing_context &&
## request.client()->target_browsing_context->is_top_level()`, condition 4 of both blocking checks.
func mixed_content_is_top_level_document_request(request: FetchRequest*) -> bool:
    if fetch_request_destination(request) != FetchRequestDestination.document:
        return false
    let client = fetch_request_client(request) else trap("VERIFY: the request's client")
    if let target_browsing_context = client.html_environment().target_browsing_context:
        return html_browsing_context_is_top_level(target_browsing_context)
    return false

## Ported from Libraries/LibWeb/MixedContent/AbstractOperations.cpp:76 Web::MixedContent::should_fetching_request_be_blocked_as_mixed_content.
# https://w3c.github.io/webappsec-mixed-content/#should-block-fetch
pub func mixed_content_should_fetching_request_be_blocked_as_mixed_content(request: FetchRequest*) -> FetchRequestOrResponseBlocking:
    # 1. Return allowed if one or more of the following conditions are met:
    if (mixed_content_does_settings_prohibit_mixed_security_contexts(fetch_request_client(request)) == MixedContentProhibitsMixedSecurityContexts.does_not_restrict_mixed_security_contexts or secure_contexts_is_url_potentially_trustworthy(fetch_request_url(request)) == SecureContextsTrustworthiness.potentially_trustworthy or false or mixed_content_is_top_level_document_request(request)):
        # 1. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contexts" when applied to request’s client.

        # 2. request’s URL is a potentially trustworthy URL.

        # FIXME: 3. The user agent has been instructed to allow mixed content, as described in § 7.2 User Controls).

        # 4. request’s destination is "document", and request’s target browsing context has no parent browsing context.
        # TODO: "parent browsing context" doesn't exist anymore and is a spec bug, seems like it should be `is_top_level`
        return FetchRequestOrResponseBlocking.allowed

    # 2. Return blocked.
    let url = fetch_request_url(request)
    let url_string = web_url.url_to_string(&url)
    ak.dbgln(f"MixedContent: Blocked '{ak.string_view_text(ak.string_bytes_as_string_view(&url_string))}' (request)")
    return FetchRequestOrResponseBlocking.blocked

## Ported from Libraries/LibWeb/MixedContent/AbstractOperations.cpp:100 Web::MixedContent::should_response_to_request_be_blocked_as_mixed_content.
# https://w3c.github.io/webappsec-mixed-content/#should-block-response
pub func mixed_content_should_response_to_request_be_blocked_as_mixed_content(request: FetchRequest*, response: FetchResponse*) -> FetchRequestOrResponseBlocking:
    # 1. Return allowed if one or more of the following conditions are met:
    if (mixed_content_does_settings_prohibit_mixed_security_contexts(fetch_request_client(request)) == MixedContentProhibitsMixedSecurityContexts.does_not_restrict_mixed_security_contexts or mixed_content_url_is_potentially_trustworthy(fetch_response_url(response)) or false or mixed_content_is_top_level_document_request(request)):
        # 1. § 4.3 Does settings prohibit mixed security contexts? returns Does Not Restrict Mixed Content when applied to request’s client.

        # 2. response’s url is a potentially trustworthy URL.

        # FIXME: 3. The user agent has been instructed to allow mixed content, as described in § 7.2 User Controls).

        # 4. request’s destination is "document", and request’s target browsing context has no parent browsing context.
        # TODO: "parent browsing context" doesn't exist anymore and is a spec bug, seems like it should be `is_top_level`
        return FetchRequestOrResponseBlocking.allowed

    # 2. Return blocked.
    let url = fetch_request_url(request)
    let url_string = web_url.url_to_string(&url)
    ak.dbgln(f"MixedContent: Blocked '{ak.string_view_text(ak.string_bytes_as_string_view(&url_string))}' (response to request)")
    return FetchRequestOrResponseBlocking.blocked