Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / fetch / fetching / checks.lucb

#==============================================================================================
#
#   checks - Fetch's CORS check and TAO check
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Fetching/Checks.cpp (lines 1-85).
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## `values->contains_slow(value)` over the Strings of a getting, decoding and splitting.
func fetch_fetching_strings_contain(values: const ak.Vector[ak.String]*, value: ak.StringView) -> bool:
    for item in ak.vector_span_const(values):
        if ak.string_eq_string_view(&item, value):
            return true
    return false

## `values.contains_slow(value)` over ByteStrings (header names and methods of extracted header list values).
func fetch_fetching_byte_strings_contain(values: const ak.Vector[ak.ByteString]*, value: ak.StringView) -> bool:
    for item in ak.vector_span_const(values):
        if ak.byte_string_eq_string_view(&item, value):
            return true
    return false

## Ported from Libraries/LibWeb/Fetch/Fetching/Checks.cpp:15 Web::Fetch::Fetching::cors_check.
# https://fetch.spec.whatwg.org/#concept-cors-check
pub func fetch_fetching_cors_check(request: FetchRequest*, response: FetchResponse*) -> bool:
    # 1. Let origin be the result of getting `Access-Control-Allow-Origin` from response’s header list.
    # 2. If origin is null, then return failure.
    # NOTE: Null is not `null`.
    let origin = http_header_list_get(fetch_response_header_list(response), ak.sv("Access-Control-Allow-Origin")) else return false

    # 3. If request’s credentials mode is not "include" and origin is `*`, then return success.
    if fetch_request_credentials_mode(request) != FetchRequestCredentialsMode.include and ak.byte_string_eq_string_view(&origin, ak.sv("*")):
        return true

    # 4. If the result of byte-serializing a request origin with request is not origin, then return failure.
    let serialized_origin = fetch_request_byte_serialize_origin(request)
    if not ak.byte_string_eq_byte_string(&serialized_origin, &origin):
        return false

    # 5. If request’s credentials mode is not "include", then return success.
    if fetch_request_credentials_mode(request) != FetchRequestCredentialsMode.include:
        return true

    # 6. Let credentials be the result of getting `Access-Control-Allow-Credentials` from response’s header list.
    let credentials = http_header_list_get(fetch_response_header_list(response), ak.sv("Access-Control-Allow-Credentials"))

    # 7. If credentials is `true`, then return success.
    if let value = credentials:
        if ak.byte_string_eq_string_view(&value, ak.sv("true")):
            return true

    # 8. Return failure.
    return false

## Ported from Libraries/LibWeb/Fetch/Fetching/Checks.cpp:49 Web::Fetch::Fetching::tao_check.
# https://fetch.spec.whatwg.org/#concept-tao-check
pub func fetch_fetching_tao_check(request: FetchRequest*, response: FetchResponse*) -> bool:
    # 1. If request’s timing allow failed flag is set, then return failure.
    if fetch_request_timing_allow_failed(request):
        return false

    # 2. Let values be the result of getting, decoding, and splitting `Timing-Allow-Origin` from response’s header list.
    let values = http_header_list_get_decode_and_split(fetch_response_header_list(response), ak.sv("Timing-Allow-Origin"))

    # 3. If values contains "*", then return success.
    if let list = values:
        if fetch_fetching_strings_contain(&list, ak.sv("*")):
            return true

    # 4. If values contains the result of serializing a request origin with request, then return success.
    if let list = values:
        let serialized_origin = fetch_request_serialize_origin(request)
        if fetch_fetching_strings_contain(&list, ak.string_bytes_as_string_view(&serialized_origin)):
            return true

    # 5. If request’s mode is "navigate" and request’s current URL’s origin is not same origin with request’s origin, then return failure.
    # NOTE: This is necessary for navigations of a nested browsing context. There, request’s origin would be the
    #       container document’s origin and the TAO check would return failure. Since navigation timing never
    #       validates the results of the TAO check, the nested document would still have access to the full timing
    #       information, but the container document would not.
    if fetch_request_mode(request) == FetchRequestMode.navigate:
        match fetch_request_origin(request):
            .origin(request_origin):
                let current_url = fetch_request_current_url(request)
                let current_origin = web_url.url_origin(&current_url)
                if not web_url.origin_is_same_origin(&current_origin, request_origin):
                    return false
            _: _ = 0

    # 6. If request’s response tainting is "basic", then return success.
    if fetch_request_response_tainting(request) == FetchRequestResponseTainting.basic:
        return true

    # 7. Return failure.
    return false