Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / secure_contexts / abstract_operations.lucb

#==============================================================================================
#
#   abstract_operations - Secure Contexts: whether an origin or a URL is potentially
#   trustworthy
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/SecureContexts/AbstractOperations.cpp (lines 17-90).
#
#       Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/SecureContexts/AbstractOperations.cpp:17 Web::SecureContexts::is_origin_potentially_trustworthy.
# https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy
pub func secure_contexts_is_origin_potentially_trustworthy(origin: web_url.Origin) -> SecureContextsTrustworthiness:
    # 1. If origin is an opaque origin, return "Not Trustworthy".
    if web_url.origin_is_opaque(&origin):
        # AD-HOC: The secure context spec assumes file scheme origins are tuple origins.
        #         Therefore, we need to special case file scheme origins here. See spec issue:
        #         https://github.com/w3c/webappsec-secure-contexts/issues/66
        return SecureContextsTrustworthiness.potentially_trustworthy if web_url.origin_is_opaque_file_origin(&origin) else SecureContextsTrustworthiness.not_trustworthy

    # 2. Assert: origin is a tuple origin.

    # 3. If origin’s scheme is either "https" or "wss", return "Potentially Trustworthy".
    # Note: This is meant to be analog to the a priori authenticated URL concept in [MIX].
    let scheme = web_url.origin_scheme(&origin)
    if let present_scheme = scheme:
        if ak.string_eq_string_view(&present_scheme, ak.sv("https")) or ak.string_eq_string_view(&present_scheme, ak.sv("wss")):
            return SecureContextsTrustworthiness.potentially_trustworthy

    # 4. If origin’s host matches one of the CIDR notations 127.0.0.0/8 or ::1/128 [RFC4632], return "Potentially Trustworthy".
    let host = web_url.origin_host(&origin)
    if web_url.host_has_i_pv4_address(&host):
        let ipv4_address = web_url.host_get_i_pv4_address(&host)
        if (ak.i_pv4_address_to_u32(&ipv4_address) & 0xff000000) != 0:
            return SecureContextsTrustworthiness.potentially_trustworthy
    elif web_url.host_has_i_pv6_address(&host):
        let ipv6_address = web_url.host_get_i_pv6_address(&host)
        if ipv6_address == ak.i_pv6_address_loopback():
            return SecureContextsTrustworthiness.potentially_trustworthy

    # 5. If the user agent conforms to the name resolution rules in [let-localhost-be-localhost] and one of the following is true:
    # - origin’s host is "localhost" or "localhost."
    # - origin’s host ends with ".localhost" or ".localhost."
    # then return "Potentially Trustworthy".
    # Note: See § 5.2 localhost for details on the requirements here.
    if web_url.host_has_string(&host):
        let host_string = web_url.host_get_string(&host)
        if ak.string_eq_string_view(&host_string, ak.sv("localhost")) or ak.string_eq_string_view(&host_string, ak.sv("localhost.")) or ak.string_ends_with_bytes(&host_string, ak.sv(".localhost")) or ak.string_ends_with_bytes(&host_string, ak.sv(".localhost.")):
            return SecureContextsTrustworthiness.potentially_trustworthy

    # 6. If origin’s scheme is "file", return "Potentially Trustworthy".
    # AD-HOC: Our resource:// is basically an alias to file://
    if let present_scheme = scheme:
        if ak.string_eq_string_view(&present_scheme, ak.sv("file")) or ak.string_eq_string_view(&present_scheme, ak.sv("resource")):
            return SecureContextsTrustworthiness.potentially_trustworthy

    # 7. If origin’s scheme component is one which the user agent considers to be authenticated, return "Potentially Trustworthy".
    # Note: See § 7.1 Packaged Applications for detail here.

    # 8. If origin has been configured as a trustworthy origin, return "Potentially Trustworthy".
    # Note: See § 7.2 Development Environments for detail here.

    # 9. Return "Not Trustworthy".
    return SecureContextsTrustworthiness.not_trustworthy

## Ported from Libraries/LibWeb/SecureContexts/AbstractOperations.cpp:74 Web::SecureContexts::is_url_potentially_trustworthy.
# https://w3c.github.io/webappsec-secure-contexts/#is-url-trustworthy
pub func secure_contexts_is_url_potentially_trustworthy(url: web_url.Url) -> SecureContextsTrustworthiness:
    # 1. If url is "about:blank" or "about:srcdoc", return "Potentially Trustworthy".
    if web_url.url_eq(&url, web_url.about_blank()) or web_url.url_eq(&url, web_url.about_srcdoc()):
        return SecureContextsTrustworthiness.potentially_trustworthy

    # AD-HOC: Browser-internal about: pages (WebUI) are served directly by the user agent.
    if web_url.is_webui_url(url):
        return SecureContextsTrustworthiness.potentially_trustworthy

    # 2. If url's scheme is "data", return "Potentially Trustworthy".
    let scheme = web_url.url_scheme(&url)
    if ak.string_eq_string_view(&scheme, ak.sv("data")):
        return SecureContextsTrustworthiness.potentially_trustworthy

    # 3. Return the result of executing § 3.1 Is origin potentially trustworthy? on url’s origin.
    return secure_contexts_is_origin_potentially_trustworthy(web_url.url_origin(&url))