2022-10-23 21:58:18 +01:00
|
|
|
|
/*
|
2023-03-03 09:27:51 +00:00
|
|
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
2022-10-23 21:58:18 +01:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/NoSniffBlocking.h>
|
2023-02-17 15:21:32 +00:00
|
|
|
|
#include <LibWeb/Infra/Strings.h>
|
2022-10-23 21:58:18 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::Fetch::Infrastructure {
|
|
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#determine-nosniff
|
2024-04-26 13:24:20 -04:00
|
|
|
|
bool determine_nosniff(HeaderList const& list)
|
2022-10-23 21:58:18 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. Let values be the result of getting, decoding, and splitting `X-Content-Type-Options` from list.
|
2024-04-26 13:24:20 -04:00
|
|
|
|
auto values = list.get_decode_and_split("X-Content-Type-Options"sv.bytes());
|
2022-10-23 21:58:18 +01:00
|
|
|
|
|
|
|
|
|
// 2. If values is null, then return false.
|
|
|
|
|
if (!values.has_value())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 3. If values[0] is an ASCII case-insensitive match for "nosniff", then return true.
|
2025-05-18 15:04:56 +12:00
|
|
|
|
if (!values->is_empty() && values->at(0).equals_ignoring_ascii_case("nosniff"sv))
|
2022-10-23 21:58:18 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// 4. Return false.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff?
|
2024-04-26 13:24:20 -04:00
|
|
|
|
RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_nosniff(Response const& response, Request const& request)
|
2022-10-23 21:58:18 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. If determine nosniff with response’s header list is false, then return allowed.
|
2024-04-26 13:24:20 -04:00
|
|
|
|
if (!determine_nosniff(response.header_list()))
|
2022-10-23 21:58:18 +01:00
|
|
|
|
return RequestOrResponseBlocking::Allowed;
|
|
|
|
|
|
|
|
|
|
// 2. Let mimeType be the result of extracting a MIME type from response’s header list.
|
2024-04-26 13:24:20 -04:00
|
|
|
|
auto mime_type = response.header_list()->extract_mime_type();
|
2022-10-23 21:58:18 +01:00
|
|
|
|
|
|
|
|
|
// 3. Let destination be request’s destination.
|
|
|
|
|
auto const& destination = request.destination();
|
|
|
|
|
|
|
|
|
|
// 4. If destination is script-like and mimeType is failure or is not a JavaScript MIME type, then return blocked.
|
|
|
|
|
if (request.destination_is_script_like() && (!mime_type.has_value() || !mime_type->is_javascript()))
|
|
|
|
|
return RequestOrResponseBlocking::Blocked;
|
|
|
|
|
|
|
|
|
|
// 5. If destination is "style" and mimeType is failure or its essence is not "text/css", then return blocked.
|
|
|
|
|
if (destination == Request::Destination::Style && (!mime_type.has_value() || mime_type->essence() != "text/css"sv))
|
|
|
|
|
return RequestOrResponseBlocking::Blocked;
|
|
|
|
|
|
|
|
|
|
// 6. Return allowed.
|
|
|
|
|
return RequestOrResponseBlocking::Allowed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|