From 1e5cac9e9c5d9a546df15dd31f5178b0f7c22301 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 13 Oct 2022 19:10:27 +0200 Subject: [PATCH] LibWeb: Implement 'Strip url for use as a referrer' AO --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../ReferrerPolicy/AbstractOperations.cpp | 47 +++++++++++++++++++ .../ReferrerPolicy/AbstractOperations.h | 20 ++++++++ 3 files changed, 68 insertions(+) create mode 100644 Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp create mode 100644 Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index ba69a82fe1..54efccac01 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -377,6 +377,7 @@ set(SOURCES Platform/ImageCodecPlugin.cpp Platform/Timer.cpp Platform/TimerSerenity.cpp + ReferrerPolicy/AbstractOperations.cpp RequestIdleCallback/IdleDeadline.cpp ResizeObserver/ResizeObserver.cpp SecureContexts/AbstractOperations.cpp diff --git a/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp b/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp new file mode 100644 index 0000000000..6fb09c47d3 --- /dev/null +++ b/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Web::ReferrerPolicy { + +Optional strip_url_for_use_as_referrer(Optional url, OriginOnly origin_only) +{ + // 1. If url is null, return no referrer. + if (!url.has_value()) + return {}; + + // 2. If url’s scheme is a local scheme, then return no referrer. + if (Fetch::Infrastructure::LOCAL_SCHEMES.span().contains_slow(url->scheme())) + return {}; + + // 3. Set url’s username to the empty string. + url->set_username(""sv); + + // 4. Set url’s password to the empty string. + url->set_password(""sv); + + // 5. Set url’s fragment to null. + url->set_fragment({}); + + // 6. If the origin-only flag is true, then: + if (origin_only == OriginOnly::Yes) { + // 1. Set url’s path to « the empty string ». + url->set_paths({ ""sv }); + + // 2. Set url’s query to null. + url->set_query({}); + } + + // 7. Return url. + return url; +} + +} diff --git a/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.h b/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.h new file mode 100644 index 0000000000..509277d0bb --- /dev/null +++ b/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::ReferrerPolicy { + +enum class OriginOnly { + Yes, + No, +}; + +Optional strip_url_for_use_as_referrer(Optional, OriginOnly origin_only = OriginOnly::No); + +}