From 5eb46a5f0138a4e190554d687332aae056ff6c32 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Mon, 29 Apr 2024 20:55:44 +0100 Subject: [PATCH] LibWeb/Fetch: Implement the "set the Sec-Fetch-Mode header" AO --- .../LibWeb/Fetch/Fetching/Fetching.cpp | 20 +++++++++++++++++++ .../LibWeb/Fetch/Fetching/Fetching.h | 1 + 2 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index 388dea8635f..9a310411e16 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -2019,4 +2019,24 @@ void set_sec_fetch_dest_header(Infrastructure::Request& request) request.header_list()->append(move(header)); } +// https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-dest +void set_sec_fetch_mode_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. Set header’s value to r’s mode. + auto header_value = MUST(ByteBuffer::copy(Infrastructure::request_mode_to_string(request.mode()).bytes())); + + // 4. Set a structured field value `Sec-Fetch-Mode`/header in r’s header list. + auto header = Infrastructure::Header { + .name = MUST(ByteBuffer::copy("Sec-Fetch-Mode"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 c067a5f2431..d7a2cefb57a 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.h @@ -40,4 +40,5 @@ WebIDL::ExceptionOr> http_network_or_cache_fet 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&); +void set_sec_fetch_mode_header(Infrastructure::Request&); }