Skip to content

Truly independent web browser

LibWeb: Apply site-compatibility rules across a page's frames

Zaggy102455175219aabc
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 7f0015f20e0..17217dfe3c4 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -2513,7 +2513,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
if (!http_request->header_list()->contains("User-Agent"sv)) {
// 1. Let userAgent be httpRequest’s client’s environment default `User-Agent` value.
// FIXME: Honor WebDriver BiDi emulated User-Agent.
- auto user_agent = Infrastructure::default_user_agent_value(fetch_params.loader_config(), http_request->current_url());
+ auto user_agent = Infrastructure::default_user_agent_value(fetch_params.loader_config(), *http_request).to_byte_string();
// 2. Append (`User-Agent`, userAgent) to httpRequest’s header list.
http_request->header_list()->append({ "User-Agent"sv, move(user_agent) });
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
index b20d37998f0..33dc906d0b5 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
@@ -4,17 +4,64 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/ByteString.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
+#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWebCommon/Loader/LoaderConfigSnapshot.h>
namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#default-user-agent-value
-ByteString default_user_agent_value(LoaderConfig const& loader_config, URL::URL const& url)
+String default_user_agent_value(LoaderConfig const& loader_config, URL::Origin const& top_level_origin, URL::Origin const& origin)
{
// A default `User-Agent` value is an implementation-defined header value for the `User-Agent` header.
- return loader_config.user_agent_for_url(url).to_byte_string();
+ return loader_config.user_agent_for(top_level_origin, origin);
+}
+
+// The top-level origin of an environment, or the origin of its top-level creation URL, as the network partition key
+// takes it.
+template<typename EnvironmentType>
+static Optional<URL::Origin> top_level_origin_of(EnvironmentType const& environment)
+{
+ if (environment.top_level_origin.has_value())
+ return environment.top_level_origin;
+ if (environment.top_level_creation_url.has_value())
+ return environment.top_level_creation_url->origin();
+ return {};
+}
+
+String default_user_agent_value(LoaderConfig const& loader_config, Request const& request)
+{
+ auto current_url_origin = request.current_url().origin();
+
+ if (request.mode() == Request::Mode::Navigate) {
+ // A top-level navigation request is for the top-level document it creates, whose site is that of the current
+ // URL, even after a cross-site redirect.
+ if (request.destination() == Request::Destination::Document)
+ return default_user_agent_value(loader_config, current_url_origin, current_url_origin);
+
+ // Any other navigation request is for a document under the top-level of its reserved client.
+ Optional<URL::Origin> top_level_origin;
+ if (auto const& reserved_client = request.reserved_client_snapshot())
+ top_level_origin = top_level_origin_of(*reserved_client);
+ return default_user_agent_value(loader_config, top_level_origin.value_or(current_url_origin), current_url_origin);
+ }
+
+ if (auto const& client = request.client_snapshot()) {
+ auto top_level_origin = top_level_origin_of(*client);
+ return default_user_agent_value(loader_config, top_level_origin.value_or(client->origin), client->origin);
+ }
+
+ return default_user_agent_value(loader_config, current_url_origin, current_url_origin);
+}
+
+// https://fetch.spec.whatwg.org/#environment-default-user-agent-value
+String environment_default_user_agent_value(LoaderConfig const& loader_config, HTML::EnvironmentSettingsObject const& environment)
+{
+ // FIXME: Honor WebDriver BiDi emulated User-Agent.
+ auto origin = environment.origin();
+ auto top_level_origin = top_level_origin_of(environment);
+ return default_user_agent_value(loader_config, top_level_origin.value_or(origin), origin);
}
}
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
index 4244e0a3e96..c11c0bdfabd 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP.h
@@ -9,11 +9,15 @@
#include <AK/Forward.h>
#include <LibURL/Forward.h>
+#include <LibWeb/Forward.h>
#include <LibWebCommon/Fetch/Infrastructure/RedirectTaint.h>
#include <LibWebCommon/Forward.h>
namespace Web::Fetch::Infrastructure {
-[[nodiscard]] ByteString default_user_agent_value(LoaderConfig const&, URL::URL const&);
+// The default value takes the site-compatibility rules of a top-level site and of the site of the document or worker.
+[[nodiscard]] String default_user_agent_value(LoaderConfig const&, URL::Origin const& top_level_origin, URL::Origin const& origin);
+[[nodiscard]] String default_user_agent_value(LoaderConfig const&, Request const&);
+[[nodiscard]] String environment_default_user_agent_value(LoaderConfig const&, HTML::EnvironmentSettingsObject const&);
}
diff --git a/Libraries/LibWeb/HTML/Navigator.cpp b/Libraries/LibWeb/HTML/Navigator.cpp
index 4d8e03dc6c3..a7874b0e2c6 100644
--- a/Libraries/LibWeb/HTML/Navigator.cpp
+++ b/Libraries/LibWeb/HTML/Navigator.cpp
@@ -45,11 +45,6 @@ Navigator::Navigator(Window& window)
Navigator::~Navigator() = default;
-URL::URL Navigator::navigator_id_url() const
-{
- return m_window->associated_document().url();
-}
-
EnvironmentSettingsObject& Navigator::navigator_storage_settings_object() const
{
return relevant_settings_object(*m_window);
diff --git a/Libraries/LibWeb/HTML/Navigator.h b/Libraries/LibWeb/HTML/Navigator.h
index 2274345d98c..8c196028796 100644
--- a/Libraries/LibWeb/HTML/Navigator.h
+++ b/Libraries/LibWeb/HTML/Navigator.h
@@ -88,7 +88,7 @@ protected:
private:
explicit Navigator(Window&);
- virtual URL::URL navigator_id_url() const override;
+ virtual EnvironmentSettingsObject const& navigator_id_settings_object() const override { return navigator_storage_settings_object(); }
// ^StorageAPI::NavigatorStorage
virtual EnvironmentSettingsObject& navigator_storage_settings_object() const override;
diff --git a/Libraries/LibWeb/HTML/NavigatorID.cpp b/Libraries/LibWeb/HTML/NavigatorID.cpp
index 42f69e3da35..3ff18badbff 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.cpp
+++ b/Libraries/LibWeb/HTML/NavigatorID.cpp
@@ -8,6 +8,7 @@
#include <AK/ByteString.h>
#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/HTML/NavigatorID.h>
#include <LibWeb/Loader/LoaderConfig.h>
#include <LibWeb/Loader/ResourceLoader.h>
@@ -19,8 +20,7 @@ namespace Web::HTML {
Utf16String NavigatorIDMixin::app_version() const
{
// 1. Let userAgent be this's relevant settings object's environment default `User-Agent` value.
- // FIXME: Store that on the settings object?
- auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
+ auto user_agent = Fetch::Infrastructure::environment_default_user_agent_value(current_loader_config()->config, navigator_id_settings_object());
// 2. If userAgent does not start with `Mozilla/5.0 (`, then return the empty string.
if (!user_agent.starts_with_bytes("Mozilla/5.0 ("sv))
@@ -73,7 +73,7 @@ Utf16String NavigatorIDMixin::oscpu() const
// the browser is executing, e.g. "Windows NT 10.0; Win64; x64", "Linux x86_64".
// The platform tokens of the User-Agent string say what it claims to run on: the parenthesized list without the
// "Macintosh" and "X11" markers and without the "rv:" version, so "Intel Mac OS X 10.15" or "Linux x86_64".
- auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
+ auto user_agent = Fetch::Infrastructure::environment_default_user_agent_value(current_loader_config()->config, navigator_id_settings_object());
auto open = user_agent.find_byte_offset('(');
auto close = user_agent.find_byte_offset(')');
if (!open.has_value() || !close.has_value() || *close <= *open)
@@ -118,7 +118,7 @@ Utf16FlyString NavigatorIDMixin::product_sub() const
Utf16String NavigatorIDMixin::user_agent() const
{
// Must return the default `User-Agent` value.
- auto user_agent = current_loader_config()->config.user_agent_for_url(navigator_id_url());
+ auto user_agent = Fetch::Infrastructure::environment_default_user_agent_value(current_loader_config()->config, navigator_id_settings_object());
return Utf16String::from_ascii_without_validation(user_agent.bytes());
}
diff --git a/Libraries/LibWeb/HTML/NavigatorID.h b/Libraries/LibWeb/HTML/NavigatorID.h
index ffe0ce58182..c6c27d7e0f4 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.h
+++ b/Libraries/LibWeb/HTML/NavigatorID.h
@@ -8,7 +8,7 @@
#include <AK/Utf16FlyString.h>
#include <AK/Utf16String.h>
-#include <LibURL/URL.h>
+#include <LibWeb/Forward.h>
namespace Web::HTML {
@@ -57,7 +57,7 @@ public:
Utf16FlyString build_id() const { return "20181001000000"_utf16_fly_string; }
protected:
- virtual URL::URL navigator_id_url() const = 0;
+ virtual EnvironmentSettingsObject const& navigator_id_settings_object() const = 0;
};
}
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index 4487ae1cd36..62c1c6c5e78 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -1518,14 +1518,17 @@ bool WindowOrWorkerGlobalScopeMixin::expose_experimental_interfaces()
}
// A site-compatibility rule can expose a gated interface or member to the documents and workers it matches, without
-// exposing it to the web at large. The rule is keyed by the URL the environment was created with, so it holds for the
-// lifetime of that global, the same way the global switch does.
+// exposing it to the web at large. The rule is keyed by the environment's top-level site and its own, so it holds for
+// the lifetime of that global, the same way the global switch does.
bool WindowOrWorkerGlobalScopeMixin::expose_experimental_interface(EnvironmentSettingsObject const& settings, StringView name)
{
if (s_experimental_interfaces_exposed)
return true;
- return current_loader_config()->config.exposes_experimental_interface(settings.creation_url, name);
+ // NB: A Window's interfaces are set up before it has a document to take the origin of.
+ auto origin = settings.creation_url.origin();
+ auto top_level_origin = settings.top_level_origin.value_or(origin);
+ return current_loader_config()->config.exposes_experimental_interface(top_level_origin, origin, name);
}
}
diff --git a/Libraries/LibWeb/HTML/WorkerNavigator.cpp b/Libraries/LibWeb/HTML/WorkerNavigator.cpp
index c90a451766d..21dbd2dfa6d 100644
--- a/Libraries/LibWeb/HTML/WorkerNavigator.cpp
+++ b/Libraries/LibWeb/HTML/WorkerNavigator.cpp
@@ -27,11 +27,6 @@ WorkerNavigator::WorkerNavigator(WorkerGlobalScope& global_scope)
WorkerNavigator::~WorkerNavigator() = default;
-URL::URL WorkerNavigator::navigator_id_url() const
-{
- return m_global_scope->url();
-}
-
void WorkerNavigator::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/HTML/WorkerNavigator.h b/Libraries/LibWeb/HTML/WorkerNavigator.h
index 9b59f0c23b4..35d37daf787 100644
--- a/Libraries/LibWeb/HTML/WorkerNavigator.h
+++ b/Libraries/LibWeb/HTML/WorkerNavigator.h
@@ -48,7 +48,7 @@ public:
private:
explicit WorkerNavigator(WorkerGlobalScope&);
- virtual URL::URL navigator_id_url() const override;
+ virtual EnvironmentSettingsObject const& navigator_id_settings_object() const override { return navigator_storage_settings_object(); }
virtual void visit_edges(GC::Cell::Visitor&) override;
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index 3bdbf813f85..71f70cad31a 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -228,7 +228,7 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
auto additional_headers = HTTP::HeaderList::create();
- auto user_agent = current_loader_config()->config.user_agent_for_websocket_url(connection_url);
+ auto user_agent = Fetch::Infrastructure::default_user_agent_value(current_loader_config()->config, request);
additional_headers->append({ "User-Agent"sv, user_agent.to_byte_string() });
if (ResourceLoader::proxy_autoconfig_url().has_value()) {
diff --git a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
index 879f645d5f4..6b092e56155 100644
--- a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
+++ b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
@@ -10,7 +10,7 @@
#include <AK/NonnullRefPtr.h>
#include <AK/String.h>
#include <AK/Vector.h>
-#include <LibURL/URL.h>
+#include <LibURL/Origin.h>
#include <LibWebCommon/Export.h>
#include <LibWebCommon/Loader/SiteCompatibility.h>
@@ -34,9 +34,8 @@ struct LoaderConfig {
Vector<String> preferred_languages;
bool enable_global_privacy_control { false };
- String user_agent_for_url(URL::URL const& url) const { return site_compatibility->data.user_agent_for_url(url, user_agent); }
- String user_agent_for_websocket_url(URL::URL const& url) const { return site_compatibility->data.user_agent_for_websocket_url(url, user_agent); }
- bool exposes_experimental_interface(URL::URL const& url, StringView name) const { return site_compatibility->data.exposes_experimental_interface(url, name); }
+ String user_agent_for(URL::Origin const& top_level_origin, URL::Origin const& origin) const { return site_compatibility->data.user_agent_for(top_level_origin, origin, user_agent); }
+ bool exposes_experimental_interface(URL::Origin const& top_level_origin, URL::Origin const& origin, StringView name) const { return site_compatibility->data.exposes_experimental_interface(top_level_origin, origin, name); }
};
// A loader configuration as it was at one moment, which a fetch keeps for its lifetime.
diff --git a/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp b/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
index 4830c2e598e..e898b54c3c8 100644
--- a/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
+++ b/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
@@ -165,34 +165,31 @@ String SiteCompatibilityRule::apply_user_agent_transformations(StringView user_a
return transformed_user_agent;
}
-String SiteCompatibilityData::user_agent_for_url(URL::URL const& url, StringView default_user_agent) const
+static bool rule_applies(SiteCompatibilityRule const& rule, URL::Site const& top_level_site, URL::Site const& site)
{
- auto site = URL::Site::obtain(url.origin());
+ return rule.matches(top_level_site) || rule.matches(site);
+}
+
+String SiteCompatibilityData::user_agent_for(URL::Origin const& top_level_origin, URL::Origin const& origin, StringView default_user_agent) const
+{
+ auto top_level_site = URL::Site::obtain(top_level_origin);
+ auto site = URL::Site::obtain(origin);
auto user_agent = MUST(String::from_utf8(default_user_agent));
for (auto const& rule : m_rules) {
- if (rule.matches(site))
+ if (rule_applies(rule, top_level_site, site))
user_agent = rule.apply_user_agent_transformations(user_agent);
}
return user_agent;
}
-String SiteCompatibilityData::user_agent_for_websocket_url(URL::URL const& url, StringView default_user_agent) const
+bool SiteCompatibilityData::exposes_experimental_interface(URL::Origin const& top_level_origin, URL::Origin const& origin, StringView name) const
{
- auto http_url = url;
- if (http_url.scheme() == "ws"sv)
- http_url.set_scheme("http"sv);
- else if (http_url.scheme() == "wss"sv)
- http_url.set_scheme("https"sv);
- return user_agent_for_url(http_url, default_user_agent);
-}
-
-bool SiteCompatibilityData::exposes_experimental_interface(URL::URL const& url, StringView name) const
-{
- auto site = URL::Site::obtain(url.origin());
+ auto top_level_site = URL::Site::obtain(top_level_origin);
+ auto site = URL::Site::obtain(origin);
for (auto const& rule : m_rules) {
- if (rule.exposes_experimental_interface(name) && rule.matches(site))
+ if (rule.exposes_experimental_interface(name) && rule_applies(rule, top_level_site, site))
return true;
}
return false;
diff --git a/Libraries/LibWebCommon/Loader/SiteCompatibility.h b/Libraries/LibWebCommon/Loader/SiteCompatibility.h
index 6f4cee2a156..8ca215ad07d 100644
--- a/Libraries/LibWebCommon/Loader/SiteCompatibility.h
+++ b/Libraries/LibWebCommon/Loader/SiteCompatibility.h
@@ -10,8 +10,8 @@
#include <AK/JsonValue.h>
#include <AK/String.h>
#include <AK/Vector.h>
+#include <LibURL/Origin.h>
#include <LibURL/Site.h>
-#include <LibURL/URL.h>
#include <LibWebCommon/Export.h>
namespace Web {
@@ -48,9 +48,9 @@ public:
void add_rule(SiteCompatibilityRule rule) { m_rules.append(move(rule)); }
Vector<SiteCompatibilityRule> const& rules() const { return m_rules; }
- String user_agent_for_url(URL::URL const&, StringView default_user_agent) const;
- String user_agent_for_websocket_url(URL::URL const&, StringView default_user_agent) const;
- bool exposes_experimental_interface(URL::URL const&, StringView name) const;
+ // A rule applies to a document or worker when it matches either its top-level site or its own site.
+ String user_agent_for(URL::Origin const& top_level_origin, URL::Origin const& origin, StringView default_user_agent) const;
+ bool exposes_experimental_interface(URL::Origin const& top_level_origin, URL::Origin const& origin, StringView name) const;
private:
Vector<SiteCompatibilityRule> m_rules;
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index d0ea4402dee..152c4185a44 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -5,6 +5,7 @@ Text/input/PDF/jbig2-image.html
[LoadFromHttpServer]
; Site-compatibility rules match the site of the page, and a file: page has none.
Text/input/Fetch/site-compatibility-user-agent-change-during-fetch.html
+Text/input/Fetch/site-compatibility-user-agent-follows-top-level-site.html
Text/input/Fetch/site-compatibility-user-agent-redirects.html
Text/input/HTML/unload-beacon-outlives-its-document.html
Text/input/HTML/reused-window-unload-beacon.html
diff --git a/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-follows-top-level-site.txt b/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-follows-top-level-site.txt
new file mode 100644
index 00000000000..84b52036eec
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/site-compatibility-user-agent-follows-top-level-site.txt
@@ -0,0 +1,8 @@
+page navigator: Ladybird/ hidden
+page fetch to another site: Ladybird/ hidden
+page worker: Ladybird/ hidden
+about:blank iframe navigator: Ladybird/ hidden
+data: iframe navigator: Ladybird/ hidden
+data: iframe fetch to another site: Ladybird/ hidden
+data: iframe worker: Ladybird/ hidden
+cross-site iframe navigation request: Ladybird/ hidden
diff --git a/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-follows-top-level-site.html b/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-follows-top-level-site.html
new file mode 100644
index 00000000000..915159f52f1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/site-compatibility-user-agent-follows-top-level-site.html
@@ -0,0 +1,100 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Every frame and worker of a page whose site a rule matches takes that rule's User-Agent, in navigator and in
+ // the requests they make, whatever their own URL.
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const crossSiteHost = uniqueLocalhostHostname("site-compatibility-cross-site");
+
+ async function reflectingEcho(name) {
+ const url = new URL(await server.createEcho("GET", `/site-compatibility-top-level-${name}`, {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": "*",
+ "Content-Type": "application/json",
+ },
+ reflect_headers_in_body: true,
+ }));
+ url.hostname = crossSiteHost;
+ return url.href;
+ }
+
+ // Starting a worker sends the process the browser's site-compatibility data, which would replace the rule
+ // this test sets, so the workers start first and report when asked.
+ const workerSource = "postMessage('ready'); onmessage = () => postMessage(navigator.userAgent);";
+ const nextMessage = target => new Promise(resolve => target.addEventListener("message", event => resolve(event.data), { once: true }));
+
+ const pageWorker = new Worker(`data:text/javascript,${encodeURIComponent(workerSource)}`);
+ await nextMessage(pageWorker);
+
+ const dataFrameFetchURL = await reflectingEcho("data-frame-fetch");
+ const dataFrameSource = `<!DOCTYPE html><script>
+ const worker = new Worker("data:text/javascript,${encodeURIComponent(workerSource)}");
+ worker.onmessage = () => {
+ parent.postMessage("ready", "*");
+ onmessage = async () => {
+ const workerUserAgent = await new Promise(resolve => {
+ worker.onmessage = event => resolve(event.data);
+ worker.postMessage("report");
+ });
+ const fetched = await (await fetch(${JSON.stringify(dataFrameFetchURL)})).json();
+ parent.postMessage({
+ navigator: navigator.userAgent,
+ fetch: fetched["User-Agent"][0],
+ worker: workerUserAgent,
+ }, "*");
+ };
+ };
+ <\/script>`;
+ const dataFrame = document.createElement("iframe");
+ const dataFrameReady = nextMessage(window);
+ dataFrame.src = `data:text/html,${encodeURIComponent(dataFrameSource)}`;
+ document.body.appendChild(dataFrame);
+ await dataFrameReady;
+
+ const hidden = userAgent => (userAgent.includes("Ladybird/") ? "Ladybird/ shown" : "Ladybird/ hidden");
+
+ try {
+ internals.setSiteCompatibilityData(JSON.stringify([{
+ sites: [`${location.protocol}//${location.hostname}`],
+ user_agent: ["hide_Ladybird"],
+ }]));
+
+ println(`page navigator: ${hidden(navigator.userAgent)}`);
+ const pageFetch = await (await fetch(await reflectingEcho("page-fetch"))).json();
+ println(`page fetch to another site: ${hidden(pageFetch["User-Agent"][0])}`);
+ const pageWorkerUserAgent = nextMessage(pageWorker);
+ pageWorker.postMessage("report");
+ println(`page worker: ${hidden(await pageWorkerUserAgent)}`);
+
+ const blank = document.createElement("iframe");
+ document.body.appendChild(blank);
+ println(`about:blank iframe navigator: ${hidden(blank.contentWindow.navigator.userAgent)}`);
+
+ const dataFrameReport = nextMessage(window);
+ dataFrame.contentWindow.postMessage("report", "*");
+ const report = await dataFrameReport;
+ println(`data: iframe navigator: ${hidden(report.navigator)}`);
+ println(`data: iframe fetch to another site: ${hidden(report.fetch)}`);
+ println(`data: iframe worker: ${hidden(report.worker)}`);
+
+ const crossSiteFrameURL = new URL(await server.createEcho("GET", "/site-compatibility-top-level-cross-site-frame", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: "<!DOCTYPE html>",
+ }));
+ crossSiteFrameURL.hostname = crossSiteHost;
+ const crossSiteFrame = document.createElement("iframe");
+ crossSiteFrame.src = crossSiteFrameURL.href;
+ document.body.appendChild(crossSiteFrame);
+ await server.waitForRequest(crossSiteFrameURL.href);
+ const navigationHeaders = await (await fetch(server.recordedRequestHeadersURL(crossSiteFrameURL.href))).json();
+ println(`cross-site iframe navigation request: ${hidden(navigationHeaders["User-Agent"][0])}`);
+ } catch (error) {
+ println(`FAIL: ${error}`);
+ } finally {
+ internals.setSiteCompatibilityData("[]");
+ }
+ });
+</script>
diff --git a/Tests/LibWebCommon/TestSiteCompatibility.cpp b/Tests/LibWebCommon/TestSiteCompatibility.cpp
index c5ed5e9634f..63c27f23861 100644
--- a/Tests/LibWebCommon/TestSiteCompatibility.cpp
+++ b/Tests/LibWebCommon/TestSiteCompatibility.cpp
@@ -14,14 +14,14 @@
static constexpr auto default_user_agent = "Mozilla/5.0 Ladybird/1.0 Chrome/146.0.0.0 Safari/537.36"sv;
-static URL::URL url(StringView input)
+static URL::Origin origin(StringView input)
{
- return URL::Parser::basic_parse(input).release_value();
+ return URL::Parser::basic_parse(input).release_value().origin();
}
static URL::Site site(StringView input)
{
- return URL::Site::obtain(url(input).origin());
+ return URL::Site::obtain(origin(input));
}
TEST_CASE(rule_matches_its_sites)
@@ -76,26 +76,43 @@ TEST_CASE(rule_transforms_user_agent)
EXPECT_EQ(rule.apply_user_agent_transformations(default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
}
-TEST_CASE(data_only_transforms_matching_urls)
+TEST_CASE(data_transforms_for_a_matching_top_level_site)
{
Web::SiteCompatibilityData data;
data.add_rule(MUST(Web::SiteCompatibilityRule::create(
{ "https://example.com"_string },
{ Web::UserAgentTransformation::HideLadybird })));
- EXPECT_EQ(data.user_agent_for_url(url("https://www.example.com/"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
- EXPECT_EQ(data.user_agent_for_url(url("https://elsewhere.example/"sv), default_user_agent), default_user_agent);
+ EXPECT_EQ(data.user_agent_for(origin("https://www.example.com"sv), origin("https://www.example.com"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
+ EXPECT_EQ(data.user_agent_for(origin("https://www.example.com"sv), origin("https://elsewhere.example"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
+ EXPECT_EQ(data.user_agent_for(origin("https://www.example.com"sv), URL::Origin::create_opaque(), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
+ EXPECT_EQ(data.user_agent_for(origin("https://elsewhere.example"sv), origin("https://elsewhere.example"sv), default_user_agent), default_user_agent);
}
-TEST_CASE(data_maps_websocket_urls_to_http_for_matching)
+TEST_CASE(data_transforms_for_a_matching_site_under_another_top_level_site)
{
Web::SiteCompatibilityData data;
data.add_rule(MUST(Web::SiteCompatibilityRule::create(
{ "https://example.com"_string },
{ Web::UserAgentTransformation::HideLadybird })));
- EXPECT_EQ(data.user_agent_for_websocket_url(url("wss://www.example.com/socket"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
- EXPECT_EQ(data.user_agent_for_websocket_url(url("ws://www.example.com/socket"sv), default_user_agent), default_user_agent);
+ EXPECT_EQ(data.user_agent_for(origin("https://elsewhere.example"sv), origin("https://player.example.com"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
+}
+
+TEST_CASE(data_matches_each_rule_on_its_own)
+{
+ Web::SiteCompatibilityData data;
+ data.add_rule(MUST(Web::SiteCompatibilityRule::create(
+ { "https://example.com"_string },
+ { Web::UserAgentTransformation::HideLadybird })));
+ data.add_rule(MUST(Web::SiteCompatibilityRule::create(
+ { "https://elsewhere.example"_string },
+ {},
+ { "Example"_string })));
+
+ EXPECT(data.exposes_experimental_interface(origin("https://example.com"sv), origin("https://elsewhere.example"sv), "Example"sv));
+ EXPECT(!data.exposes_experimental_interface(origin("https://example.com"sv), origin("https://example.com"sv), "Example"sv));
+ EXPECT_EQ(data.user_agent_for(origin("https://example.com"sv), origin("https://www.example.com"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
}
TEST_CASE(hide_ladybird_only_removes_ladybird_products)
@@ -121,7 +138,7 @@ TEST_CASE(data_parses_rule_array)
)"sv))));
EXPECT_EQ(data.rules().size(), 1u);
- EXPECT_EQ(data.user_agent_for_url(url("https://www.example.com/"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
+ EXPECT_EQ(data.user_agent_for(origin("https://www.example.com"sv), origin("https://www.example.com"sv), default_user_agent), "Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36"_string);
}
TEST_CASE(invalid_rules_are_rejected)