From 7e46150a714f28cae12e4e9ee17e1f9d080600ba Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 13 Oct 2022 19:57:40 +0200 Subject: [PATCH] LibWeb: Implement 'Should response be blocked due to its MIME type?' AO --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Fetch/Infrastructure/MimeTypeBlocking.cpp | 37 +++++++++++++++++++ .../Fetch/Infrastructure/MimeTypeBlocking.h | 16 ++++++++ 3 files changed, 54 insertions(+) create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.cpp create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 4ae2d2db43..5c639e6559 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -131,6 +131,7 @@ set(SOURCES Fetch/Infrastructure/HTTP/Requests.cpp Fetch/Infrastructure/HTTP/Responses.cpp Fetch/Infrastructure/HTTP/Statuses.cpp + Fetch/Infrastructure/MimeTypeBlocking.cpp Fetch/Infrastructure/PortBlocking.cpp Fetch/Infrastructure/URL.cpp Fetch/Request.cpp diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.cpp new file mode 100644 index 0000000000..e784bec07f --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::Fetch::Infrastructure { + +// https://fetch.spec.whatwg.org/#ref-for-should-response-to-request-be-blocked-due-to-mime-type? +RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_its_mime_type(Response const& response, Request const& request) +{ + // 1. Let mimeType be the result of extracting a MIME type from response’s header list. + auto mime_type = response.header_list()->extract_mime_type(); + + // 2. If mimeType is failure, then return allowed. + if (!mime_type.has_value()) + return RequestOrResponseBlocking::Allowed; + + // 3. Let destination be request’s destination. + // 4. If destination is script-like and one of the following is true, then return blocked: + if (request.destination_is_script_like() && ( + // - mimeType’s essence starts with "audio/", "image/", or "video/". + any_of(Array { "audio/"sv, "image/"sv, "video/"sv }, [&](auto prefix) { return mime_type->essence().starts_with(prefix); }) + // - mimeType’s essence is "text/csv". + || mime_type->essence() == "text/csv"sv)) { + return RequestOrResponseBlocking::Blocked; + } + + // 5. Return allowed. + return RequestOrResponseBlocking::Allowed; +} + +} diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h new file mode 100644 index 0000000000..96c81a1b97 --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::Fetch::Infrastructure { + +[[nodiscard]] RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_its_mime_type(Response const&, Request const&); + +}