ladybird/Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp
Shannon Booth e800605ad3 AK+LibURL: Move AK::URL into a new URL library
This URL library ends up being a relatively fundamental base library of
the system, as LibCore depends on LibURL.

This change has two main benefits:
 * Moving AK back more towards being an agnostic library that can
   be used between the kernel and userspace. URL has never really fit
   that description - and is not used in the kernel.
 * URL _should_ depend on LibUnicode, as it needs punnycode support.
   However, it's not really possible to do this inside of AK as it can't
   depend on any external library. This change brings us a little closer
   to being able to do that, but unfortunately we aren't there quite
   yet, as the code generators depend on LibCore.
2024-03-18 14:06:28 -04:00

199 lines
8.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/URL.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWeb/SecureContexts/AbstractOperations.h>
namespace Web::ReferrerPolicy {
// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer
Optional<URL::URL> determine_requests_referrer(Fetch::Infrastructure::Request const& request)
{
// 1. Let policy be requests referrer policy.
auto const& policy = request.referrer_policy();
// 2. Let environment be requests client.
auto environment = request.client();
// 3. Switch on requests referrer:
auto referrer_source = request.referrer().visit(
// "client"
[&](Fetch::Infrastructure::Request::Referrer referrer) -> Optional<URL::URL> {
// Note: If requests referrer is "no-referrer", Fetch will not call into this algorithm.
VERIFY(referrer == Fetch::Infrastructure::Request::Referrer::Client);
// FIXME: Add a const global_object() getter to ESO
auto& global_object = const_cast<HTML::EnvironmentSettingsObject&>(*environment).global_object();
// 1. If environments global object is a Window object, then
if (is<HTML::Window>(global_object)) {
// 1. Let document be the associated Document of environments global object.
auto const& document = static_cast<HTML::Window const&>(global_object).associated_document();
// 2. If documents origin is an opaque origin, return no referrer.
if (document.origin().is_opaque())
return {};
// FIXME: 3. While document is an iframe srcdoc document, let document be documents browsing contexts
// browsing context containers node document.
// 4. Let referrerSource be documents URL.
return document.url();
}
// 2. Otherwise, let referrerSource be environments creation URL.
else {
return environment->creation_url;
}
},
// a URL
[&](URL::URL const& url) -> Optional<URL::URL> {
// Let referrerSource be requests referrer.
return url;
});
// NOTE: This only happens in step 1.2. of the "client" case above.
if (!referrer_source.has_value())
return {};
// 4. Let requests referrerURL be the result of stripping referrerSource for use as a referrer.
auto referrer_url = strip_url_for_use_as_referrer(referrer_source);
// 5. Let referrerOrigin be the result of stripping referrerSource for use as a referrer, with the origin-only flag
// set to true.
auto referrer_origin = strip_url_for_use_as_referrer(referrer_source, OriginOnly::Yes);
// 6. If the result of serializing referrerURL is a string whose length is greater than 4096, set referrerURL to
// referrerOrigin.
if (referrer_url.has_value() && referrer_url.value().serialize().length() > 4096)
referrer_url = referrer_origin;
// 7. The user agent MAY alter referrerURL or referrerOrigin at this point to enforce arbitrary policy
// considerations in the interests of minimizing data leakage. For example, the user agent could strip the URL
// down to an origin, modify its host, replace it with an empty string, etc.
// 8. Execute the statements corresponding to the value of policy:
// Note: If requests referrer policy is the empty string, Fetch will not call into this algorithm.
VERIFY(policy != ReferrerPolicy::EmptyString);
switch (policy) {
// "no-referrer"
case ReferrerPolicy::NoReferrer:
// Return no referrer
return {};
// "origin"
case ReferrerPolicy::Origin:
// Return referrerOrigin
return referrer_origin;
// "unsafe-url"
case ReferrerPolicy::UnsafeURL:
// Return referrerURL.
return referrer_url;
// "strict-origin"
case ReferrerPolicy::StrictOrigin:
// 1. If referrerURL is a potentially trustworthy URL and requests current URL is not a potentially
// trustworthy URL, then return no referrer.
if (referrer_url.has_value()
&& SecureContexts::is_url_potentially_trustworthy(*referrer_url) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
&& SecureContexts::is_url_potentially_trustworthy(request.current_url()) == SecureContexts::Trustworthiness::NotTrustworthy) {
return {};
}
// 2. Return referrerOrigin.
return referrer_origin;
// "strict-origin-when-cross-origin"
case ReferrerPolicy::StrictOriginWhenCrossOrigin:
// 1. If the origin of referrerURL and the origin of requests current URL are the same, then return
// referrerURL.
if (referrer_url.has_value() && DOMURL::url_origin(*referrer_url).is_same_origin(DOMURL::url_origin(request.current_url())))
return referrer_url;
// 2. If referrerURL is a potentially trustworthy URL and requests current URL is not a potentially
// trustworthy URL, then return no referrer.
if (referrer_url.has_value()
&& SecureContexts::is_url_potentially_trustworthy(*referrer_url) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
&& SecureContexts::is_url_potentially_trustworthy(request.current_url()) == SecureContexts::Trustworthiness::NotTrustworthy) {
return {};
}
// 3. Return referrerOrigin.
return referrer_origin;
// "same-origin"
case ReferrerPolicy::SameOrigin:
// 1. If the origin of referrerURL and the origin of requests current URL are the same, then return
// referrerURL.
if (referrer_url.has_value()
&& DOMURL::url_origin(*referrer_url).is_same_origin(DOMURL::url_origin(request.current_url()))) {
return referrer_url;
}
// 2. Return no referrer.
return {};
// "origin-when-cross-origin"
case ReferrerPolicy::OriginWhenCrossOrigin:
// 1. If the origin of referrerURL and the origin of requests current URL are the same, then return
// referrerURL.
if (referrer_url.has_value()
&& DOMURL::url_origin(*referrer_url).is_same_origin(DOMURL::url_origin(request.current_url()))) {
return referrer_url;
}
// 2. Return referrerOrigin.
return referrer_origin;
// "no-referrer-when-downgrade"
case ReferrerPolicy::NoReferrerWhenDowngrade:
// 1. If referrerURL is a potentially trustworthy URL and requests current URL is not a potentially
// trustworthy URL, then return no referrer.
if (referrer_url.has_value()
&& SecureContexts::is_url_potentially_trustworthy(*referrer_url) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
&& SecureContexts::is_url_potentially_trustworthy(request.current_url()) == SecureContexts::Trustworthiness::NotTrustworthy) {
return {};
}
// 2. Return referrerURL.
return referrer_url;
default:
VERIFY_NOT_REACHED();
}
}
Optional<URL::URL> strip_url_for_use_as_referrer(Optional<URL::URL> url, OriginOnly origin_only)
{
// 1. If url is null, return no referrer.
if (!url.has_value())
return {};
// 2. If urls scheme is a local scheme, then return no referrer.
if (Fetch::Infrastructure::LOCAL_SCHEMES.span().contains_slow(url->scheme()))
return {};
// 3. Set urls username to the empty string.
MUST(url->set_username(""sv));
// 4. Set urls password to the empty string.
MUST(url->set_password(""sv));
// 5. Set urls fragment to null.
url->set_fragment({});
// 6. If the origin-only flag is true, then:
if (origin_only == OriginOnly::Yes) {
// 1. Set urls path to « the empty string ».
url->set_paths({ ""sv });
// 2. Set urls query to null.
url->set_query({});
}
// 7. Return url.
return url;
}
}