2023-04-17 13:21:19 -04:00
|
|
|
|
/*
|
2025-03-29 08:54:51 -04:00
|
|
|
|
* Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
|
2023-04-17 13:21:19 -04:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
2024-10-05 15:33:34 +13:00
|
|
|
|
#include <LibURL/Origin.h>
|
2025-02-16 14:45:52 +13:00
|
|
|
|
#include <LibURL/Parser.h>
|
2024-03-18 16:22:27 +13:00
|
|
|
|
#include <LibURL/URL.h>
|
2023-04-17 13:21:19 -04:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2024-02-11 19:48:56 +13:00
|
|
|
|
#include <LibWeb/DOMURL/DOMURL.h>
|
2023-04-17 13:21:19 -04:00
|
|
|
|
#include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: This is an ad-hoc implementation of the "autoplay" policy-controlled feature:
|
|
|
|
|
|
// https://w3c.github.io/webappsec-permissions-policy/#policy-controlled-feature
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::PermissionsPolicy {
|
|
|
|
|
|
|
|
|
|
|
|
AutoplayAllowlist& AutoplayAllowlist::the()
|
|
|
|
|
|
{
|
|
|
|
|
|
static AutoplayAllowlist filter;
|
|
|
|
|
|
return filter;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AutoplayAllowlist::AutoplayAllowlist() = default;
|
|
|
|
|
|
AutoplayAllowlist::~AutoplayAllowlist() = default;
|
|
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/webappsec-permissions-policy/#is-feature-enabled
|
2024-10-05 15:33:34 +13:00
|
|
|
|
Decision AutoplayAllowlist::is_allowed_for_origin(DOM::Document const& document, URL::Origin const& origin) const
|
2023-04-17 13:21:19 -04:00
|
|
|
|
{
|
|
|
|
|
|
// FIXME: 1. Let policy be document’s Permissions Policy
|
|
|
|
|
|
// FIXME: 2. If policy’s inherited policy for feature is Disabled, return "Disabled".
|
|
|
|
|
|
|
|
|
|
|
|
// 3. If feature is present in policy’s declared policy:
|
|
|
|
|
|
if (m_allowlist.has_value()) {
|
|
|
|
|
|
// 1. If the allowlist for feature in policy’s declared policy matches origin, then return "Enabled".
|
|
|
|
|
|
// 2. Otherwise return "Disabled".
|
|
|
|
|
|
return m_allowlist->visit(
|
|
|
|
|
|
[](Global) {
|
|
|
|
|
|
return Decision::Enabled;
|
|
|
|
|
|
},
|
|
|
|
|
|
[&](auto const& patterns) {
|
|
|
|
|
|
for (auto const& pattern : patterns) {
|
|
|
|
|
|
if (pattern.is_same_origin_domain(origin))
|
|
|
|
|
|
return Decision::Enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Decision::Disabled;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If feature’s default allowlist is *, return "Enabled".
|
|
|
|
|
|
// 5. If feature’s default allowlist is 'self', and origin is same origin with document’s origin, return "Enabled".
|
|
|
|
|
|
// NOTE: The "autoplay" feature's default allowlist is 'self'.
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/infrastructure.html#autoplay-feature
|
|
|
|
|
|
if (origin.is_same_origin(document.origin()))
|
|
|
|
|
|
return Decision::Enabled;
|
|
|
|
|
|
|
|
|
|
|
|
// 6. Return "Disabled".
|
|
|
|
|
|
return Decision::Disabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AutoplayAllowlist::enable_globally()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_allowlist = Global {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-29 08:54:51 -04:00
|
|
|
|
void AutoplayAllowlist::enable_for_origins(ReadonlySpan<String> origins)
|
2023-04-17 13:21:19 -04:00
|
|
|
|
{
|
|
|
|
|
|
m_allowlist = Patterns {};
|
|
|
|
|
|
|
|
|
|
|
|
auto& allowlist = m_allowlist->get<Patterns>();
|
2025-03-29 08:54:51 -04:00
|
|
|
|
allowlist.ensure_capacity(origins.size());
|
2023-04-17 13:21:19 -04:00
|
|
|
|
|
|
|
|
|
|
for (auto const& origin : origins) {
|
2025-02-16 14:45:52 +13:00
|
|
|
|
auto url = URL::Parser::basic_parse(origin);
|
2023-04-17 13:21:19 -04:00
|
|
|
|
|
2025-02-16 14:45:52 +13:00
|
|
|
|
if (!url.has_value())
|
2025-03-29 08:54:51 -04:00
|
|
|
|
url = URL::Parser::basic_parse(MUST(String::formatted("https://{}", origin)));
|
2025-02-16 14:45:52 +13:00
|
|
|
|
if (!url.has_value()) {
|
2023-04-17 13:21:19 -04:00
|
|
|
|
dbgln("Invalid origin for autoplay allowlist: {}", origin);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-29 08:54:51 -04:00
|
|
|
|
allowlist.append(url->origin());
|
2023-04-17 13:21:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|