LibWeb: Implement "Set request’s referrer policy on redirect" AO

This commit is contained in:
Jamie Mansfield 2024-06-05 20:15:56 +01:00 committed by Andreas Kling
parent 5a40a00d9e
commit ab6b687d4c
Notes: sideshowbarker 2024-07-17 20:33:50 +09:00
3 changed files with 40 additions and 1 deletions

View file

@ -1236,7 +1236,8 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
// 18. Append locationURL to requests URL list.
request->url_list().append(location_url);
// FIXME: 19. Invoke set requests referrer policy on redirect on request and internalResponse.
// 19. Invoke set requests referrer policy on redirect on request and internalResponse.
ReferrerPolicy::set_request_referrer_policy_on_redirect(request, internal_response);
// 20. Let recursive be true.
auto recursive = Recursive::Yes;

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,6 +17,39 @@
namespace Web::ReferrerPolicy {
// https://w3c.github.io/webappsec-referrer-policy/#parse-referrer-policy-from-header
ReferrerPolicy parse_a_referrer_policy_from_a_referrer_policy_header(Fetch::Infrastructure::Response const& response)
{
// 1. Let policy-tokens be the result of extracting header list values given `Referrer-Policy` and responses header list.
auto policy_tokens_or_failure = Fetch::Infrastructure::extract_header_list_values("Referrer-Policy"sv.bytes(), response.header_list());
auto policy_tokens = policy_tokens_or_failure.has<Vector<ByteBuffer>>() ? policy_tokens_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
// 2. Let policy be the empty string.
auto policy = ReferrerPolicy::EmptyString;
// 3. For each token in policy-tokens, if token is a referrer policy and token is not the empty string, then set policy to token.
for (auto token : policy_tokens) {
auto referrer_policy = from_string(token);
if (referrer_policy.has_value() && referrer_policy.release_value() != ReferrerPolicy::EmptyString)
policy = referrer_policy.release_value();
}
// 4. Return policy.
return policy;
}
// https://w3c.github.io/webappsec-referrer-policy/#set-requests-referrer-policy-on-redirect
void set_request_referrer_policy_on_redirect(Fetch::Infrastructure::Request& request, Fetch::Infrastructure::Response const& response)
{
// 1. Let policy be the result of executing §8.1 Parse a referrer policy from a Referrer-Policy header on
// actualResponse.
auto policy = parse_a_referrer_policy_from_a_referrer_policy_header(response);
// 2. If policy is not the empty string, then set requests referrer policy to policy.
if (policy != ReferrerPolicy::EmptyString)
request.set_referrer_policy(policy);
}
// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer
Optional<URL::URL> determine_requests_referrer(Fetch::Infrastructure::Request const& request)
{

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,6 +9,7 @@
#include <AK/Forward.h>
#include <LibWeb/Forward.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
namespace Web::ReferrerPolicy {
@ -16,6 +18,8 @@ enum class OriginOnly {
No,
};
ReferrerPolicy parse_a_referrer_policy_from_a_referrer_policy_header(Fetch::Infrastructure::Response const&);
void set_request_referrer_policy_on_redirect(Fetch::Infrastructure::Request&, Fetch::Infrastructure::Response const&);
Optional<URL::URL> determine_requests_referrer(Fetch::Infrastructure::Request const&);
Optional<URL::URL> strip_url_for_use_as_referrer(Optional<URL::URL>, OriginOnly origin_only = OriginOnly::No);