diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index adf7cacc937..388dea8635f 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2022-2023, Linus Groh * Copyright (c) 2023, Luke Wilde * Copyright (c) 2023, Sam Atkins + * Copyright (c) 2024, Jamie Mansfield * * SPDX-License-Identifier: BSD-2-Clause */ @@ -44,6 +45,7 @@ #include #include #include +#include #include namespace Web::Fetch::Fetching { @@ -1992,4 +1994,29 @@ WebIDL::ExceptionOr> cors_preflight_fetch(JS:: return returned_pending_response; } +// https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-dest +void set_sec_fetch_dest_header(Infrastructure::Request& request) +{ + // 1. Assert: r’s url is a potentially trustworthy URL. + VERIFY(SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy); + + // 2. Let header be a Structured Header whose value is a token. + // FIXME: This is handled below, as Serenity doesn't have APIs for RFC 8941. + + // 3. If r’s destination is the empty string, set header’s value to the string "empty". Otherwise, set header’s value to r’s destination. + ByteBuffer header_value; + if (!request.destination().has_value()) { + header_value = MUST(ByteBuffer::copy("empty"sv.bytes())); + } else { + header_value = MUST(ByteBuffer::copy(Infrastructure::request_destination_to_string(request.destination().value()).bytes())); + } + + // 4. Set a structured field value `Sec-Fetch-Dest`/header in r’s header list. + auto header = Infrastructure::Header { + .name = MUST(ByteBuffer::copy("Sec-Fetch-Dest"sv.bytes())), + .value = move(header_value), + }; + request.header_list()->append(move(header)); +} + } diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h index 641416c0666..c067a5f2431 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Linus Groh + * Copyright (c) 2024, Jamie Mansfield * * SPDX-License-Identifier: BSD-2-Clause */ @@ -38,4 +39,5 @@ WebIDL::ExceptionOr> http_redirect_fetch(JS::Realm&, WebIDL::ExceptionOr> http_network_or_cache_fetch(JS::Realm&, Infrastructure::FetchParams const&, IsAuthenticationFetch is_authentication_fetch = IsAuthenticationFetch::No, IsNewConnectionFetch is_new_connection_fetch = IsNewConnectionFetch::No); WebIDL::ExceptionOr> nonstandard_resource_loader_file_or_http_network_fetch(JS::Realm&, Infrastructure::FetchParams const&, IncludeCredentials include_credentials = IncludeCredentials::No, IsNewConnectionFetch is_new_connection_fetch = IsNewConnectionFetch::No); WebIDL::ExceptionOr> cors_preflight_fetch(JS::Realm&, Infrastructure::Request&); +void set_sec_fetch_dest_header(Infrastructure::Request&); }