Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / fetch / infrastructure / no_sniff_blocking.lucb

#==============================================================================================
#
#   no_sniff_blocking - Fetch's `X-Content-Type-Options: nosniff` check and the blocking of
#   a script-like or style request's response it governs
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/NoSniffBlocking.cpp (lines 17-57).
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/NoSniffBlocking.cpp:17 Web::Fetch::Infrastructure::determine_nosniff.
# https://fetch.spec.whatwg.org/#determine-nosniff
pub func fetch_determine_nosniff(list: const HttpHeaderList*) -> bool:
    # 1. Let values be the result of getting, decoding, and splitting `X-Content-Type-Options` from list.
    let values_or_null = http_header_list_get_decode_and_split(list, ak.sv("X-Content-Type-Options"))

    # 2. If values is null, then return false.
    var values = values_or_null else return false

    # 3. If values[0] is an ASCII case-insensitive match for "nosniff", then return true.
    if not ak.vector_is_empty(&values) and ak.string_equals_ignoring_ascii_case_string_view(ak.vector_at(&values, 0), ak.sv("nosniff")):
        return true

    # 4. Return false.
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/NoSniffBlocking.cpp:35 Web::Fetch::Infrastructure::should_response_to_request_be_blocked_due_to_nosniff.
# https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff?
pub func fetch_should_response_to_request_be_blocked_due_to_nosniff(response: FetchResponse*, request: FetchRequest*) -> FetchRequestOrResponseBlocking:
    # 1. If determine nosniff with response’s header list is false, then return allowed.
    if not fetch_determine_nosniff(fetch_response_header_list(response)):
        return FetchRequestOrResponseBlocking.allowed

    # 2. Let mimeType be the result of extracting a MIME type from response’s header list.
    let mime_type = fetch_extract_mime_type(fetch_response_header_list(response))

    # 3. Let destination be request’s destination.
    let destination = fetch_request_destination(request)

    # 4. If destination is script-like and mimeType is failure or is not a JavaScript MIME type, then return blocked.
    if fetch_request_destination_is_script_like(request):
        if let mime = mime_type:
            if not mime_type_is_javascript(&mime):
                return FetchRequestOrResponseBlocking.blocked
        else:
            return FetchRequestOrResponseBlocking.blocked

    # 5. If destination is "style" and mimeType is failure or its essence is not "text/css", then return blocked.
    if destination == FetchRequestDestination.style:
        if let mime = mime_type:
            let essence = mime_type_essence(&mime)
            if not ak.string_eq_string_view(&essence, ak.sv("text/css")):
                return FetchRequestOrResponseBlocking.blocked
        else:
            return FetchRequestOrResponseBlocking.blocked

    # 6. Return allowed.
    return FetchRequestOrResponseBlocking.allowed