Skip to content

Truly independent web browser

LibWeb: Expose what only one engine has in that engine's mode alone

sideshowbarkera173bb452701
diff --git a/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl
index 3e28ce2ba00..ac218591cdb 100644
--- a/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl
+++ b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl
@@ -1,5 +1,5 @@
// https://w3c.github.io/device-memory/#sec-device-memory-js-api
[SecureContext, Exposed=(Window,Worker)]
interface mixin NavigatorDeviceMemory {
- readonly attribute double deviceMemory;
+ [NavigatorCompatibilityMode=Chrome] readonly attribute double deviceMemory;
};
diff --git a/Libraries/LibWeb/HTML/NavigatorID.cpp b/Libraries/LibWeb/HTML/NavigatorID.cpp
index f4c9ca768c8..cd0686c0e3e 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.cpp
+++ b/Libraries/LibWeb/HTML/NavigatorID.cpp
@@ -6,6 +6,7 @@
*/
#include <AK/ByteString.h>
+#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
#include <LibWeb/HTML/NavigatorID.h>
#include <LibWeb/Loader/ResourceLoader.h>
@@ -64,6 +65,32 @@ Utf16String NavigatorIDMixin::platform() const
return Utf16String::from_ascii_without_validation(ResourceLoader::the().platform().bytes());
}
+// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-oscpu
+Utf16String NavigatorIDMixin::oscpu() const
+{
+ // The oscpu attribute's getter must return either the empty string or a string representing the platform on which
+ // 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 = ResourceLoader::the().user_agent_for_url(navigator_id_url());
+ auto open = user_agent.find_byte_offset('(');
+ auto close = user_agent.find_byte_offset(')');
+ if (!open.has_value() || !close.has_value() || *close <= *open)
+ return {};
+
+ auto tokens = user_agent.bytes_as_string_view().substring_view(*open + 1, *close - *open - 1).split_view(';');
+ StringBuilder builder;
+ for (auto token : tokens) {
+ token = token.trim_whitespace();
+ if (token.is_empty() || token.is_one_of("Macintosh"sv, "X11"sv) || token.starts_with("rv:"sv))
+ continue;
+ if (!builder.is_empty())
+ builder.append("; "sv);
+ builder.append(token);
+ }
+ return Utf16String::from_utf8(builder.string_view());
+}
+
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-productsub
Utf16FlyString NavigatorIDMixin::product_sub() const
{
diff --git a/Libraries/LibWeb/HTML/NavigatorID.h b/Libraries/LibWeb/HTML/NavigatorID.h
index c5774f82d40..ffe0ce58182 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.h
+++ b/Libraries/LibWeb/HTML/NavigatorID.h
@@ -47,9 +47,14 @@ public:
// https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-vendorsub
Utf16FlyString vendor_sub() const { return ""_utf16_fly_string; }
- // FIXME: If the navigator compatibility mode is Gecko, then the user agent must also support the following partial interface:
- // bool taint_enabled()
- // ByteString oscpu()
+ // If the navigator compatibility mode is Gecko, then the user agent must also support the following partial
+ // interface:
+ // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-tainted
+ // "The taintEnabled() method must return false."
+ bool taint_enabled() const { return false; }
+ Utf16String oscpu() const;
+ // The build date every Gecko release reports since its build identifier was frozen.
+ Utf16FlyString build_id() const { return "20181001000000"_utf16_fly_string; }
protected:
virtual URL::URL navigator_id_url() const = 0;
diff --git a/Libraries/LibWeb/HTML/NavigatorID.idl b/Libraries/LibWeb/HTML/NavigatorID.idl
index 6c5778e0723..2bdad3fc000 100644
--- a/Libraries/LibWeb/HTML/NavigatorID.idl
+++ b/Libraries/LibWeb/HTML/NavigatorID.idl
@@ -9,4 +9,11 @@ interface mixin NavigatorID {
readonly attribute DOMString userAgent;
[Exposed=Window] readonly attribute DOMString vendor;
[Exposed=Window] readonly attribute DOMString vendorSub; // constant ""
+
+ // If the navigator compatibility mode is Gecko, then the user agent must also support the following partial interface:
+ [Exposed=Window, NavigatorCompatibilityMode=Gecko] boolean taintEnabled(); // constant false
+ [Exposed=Window, NavigatorCompatibilityMode=Gecko] readonly attribute DOMString oscpu;
+
+ // AD-HOC: No standard defines buildID; a navigator that reports Gecko's product has it, and nothing else does.
+ [Exposed=Window, NavigatorCompatibilityMode=Gecko, ImplementedAs=build_id] readonly attribute DOMString buildID;
};
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index a2035175b24..f471bb5160b 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -1060,6 +1060,21 @@ WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTar
return target.dispatch_event(event);
}
+void Internals::set_navigator_compatibility_mode(Bindings::InternalsNavigatorCompatibilityMode mode)
+{
+ switch (mode) {
+ case Bindings::InternalsNavigatorCompatibilityMode::Chrome:
+ ResourceLoader::the().set_navigator_compatibility_mode(NavigatorCompatibilityMode::Chrome);
+ break;
+ case Bindings::InternalsNavigatorCompatibilityMode::Gecko:
+ ResourceLoader::the().set_navigator_compatibility_mode(NavigatorCompatibilityMode::Gecko);
+ break;
+ case Bindings::InternalsNavigatorCompatibilityMode::Webkit:
+ ResourceLoader::the().set_navigator_compatibility_mode(NavigatorCompatibilityMode::WebKit);
+ break;
+ }
+}
+
void Internals::spoof_current_url(Utf16String const& url_string)
{
auto url = DOMURL::parse(url_string.utf16_view());
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 0aec48d9d22..09b7ef795a9 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -140,6 +140,7 @@ public:
WebIDL::ExceptionOr<bool> dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event);
void spoof_current_url(Utf16String const& url);
+ void set_navigator_compatibility_mode(Bindings::InternalsNavigatorCompatibilityMode);
void load_url(Utf16String const& url);
GC::Ref<InternalAnimationTimeline> create_internal_animation_timeline();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index c69f17835b5..400caeaa5a9 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -130,6 +130,8 @@ interface Internals {
boolean dispatchUserActivatedEvent(EventTarget target, Event event);
undefined spoofCurrentURL(USVString url);
+ // Sets the navigator compatibility mode of this process; the realms created afterwards expose what that mode has.
+ undefined setNavigatorCompatibilityMode(InternalsNavigatorCompatibilityMode mode);
// Loads a URL in the top-level traversable — deferred so it starts outside the calling task and can land between
// session-history traversal-queue steps, as can one requested by ConnectionFromClient::load_url in the UI process.
@@ -379,6 +381,7 @@ interface Internals {
};
+enum InternalsNavigatorCompatibilityMode { "chrome", "gecko", "webkit" };
enum SpellingMenuItem { "none", "guess", "ignore", "learn" };
enum ScrollGesturePhase { "none", "ongoing", "momentum", "ended" };
enum MouseEventThroughUIProcessType { "mousedown", "mouseup", "mousemove", "mouseleave" };
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 3a9dd525bee..0990c258ce6 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -76,11 +76,17 @@ ResourceLoader::ResourceLoader(GC::Heap& heap, NonnullRefPtr<Requests::RequestCl
, m_user_agent(MUST(String::from_utf8(default_user_agent)))
, m_platform(MUST(String::from_utf8(default_platform)))
, m_preferred_languages(default_preferred_languages())
- , m_navigator_compatibility_mode(default_navigator_compatibility_mode)
{
set_client(move(request_client));
}
+NavigatorCompatibilityMode ResourceLoader::s_navigator_compatibility_mode = default_navigator_compatibility_mode;
+
+void ResourceLoader::set_navigator_compatibility_mode(NavigatorCompatibilityMode mode)
+{
+ s_navigator_compatibility_mode = mode;
+}
+
void ResourceLoader::set_client(NonnullRefPtr<Requests::RequestClient> request_client)
{
m_request_client = move(request_client);
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.h
index 1a077dc2bcf..3ed2ff284cf 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.h
+++ b/Libraries/LibWeb/Loader/ResourceLoader.h
@@ -69,8 +69,10 @@ public:
VERIFY(!m_preferred_languages.is_empty());
}
- NavigatorCompatibilityMode navigator_compatibility_mode() { return m_navigator_compatibility_mode; }
- void set_navigator_compatibility_mode(NavigatorCompatibilityMode mode) { m_navigator_compatibility_mode = mode; }
+ // The engine the navigator claims to be. A process-wide setting, readable before the loader exists: the bindings
+ // consult it while they build a realm's global object.
+ static NavigatorCompatibilityMode navigator_compatibility_mode() { return s_navigator_compatibility_mode; }
+ static void set_navigator_compatibility_mode(NavigatorCompatibilityMode);
bool enable_global_privacy_control() const { return m_enable_global_privacy_control; }
void set_enable_global_privacy_control(bool enable) { m_enable_global_privacy_control = enable; }
@@ -101,7 +103,7 @@ private:
SiteCompatibilityData m_site_compatibility_data;
String m_platform;
Vector<String> m_preferred_languages = { "en"_string };
- NavigatorCompatibilityMode m_navigator_compatibility_mode;
+ static NavigatorCompatibilityMode s_navigator_compatibility_mode;
bool m_enable_global_privacy_control { false };
};
diff --git a/Libraries/LibWeb/WebGL/WebGLObject.idl b/Libraries/LibWeb/WebGL/WebGLObject.idl
index e8215b3cb6d..02bc0a3d043 100644
--- a/Libraries/LibWeb/WebGL/WebGLObject.idl
+++ b/Libraries/LibWeb/WebGL/WebGLObject.idl
@@ -1,5 +1,5 @@
// https://registry.khronos.org/webgl/specs/latest/1.0/#5.3
-[Exposed=(Window,Worker), ImplementedIn=Rust]
+[Exposed=(Window,Worker), ImplementedIn=Rust, NavigatorCompatibilityMode=Chrome]
interface WebGLObject {
attribute USVString label;
};
diff --git a/Meta/Generators/libweb_bindings/extended_attributes.py b/Meta/Generators/libweb_bindings/extended_attributes.py
index a24e586e26c..5de252ddbf9 100644
--- a/Meta/Generators/libweb_bindings/extended_attributes.py
+++ b/Meta/Generators/libweb_bindings/extended_attributes.py
@@ -27,6 +27,16 @@ def wrap_with_extended_attribute_exposure_checks(
{text} }}
"""
+ if "NavigatorCompatibilityMode" in extended_attributes:
+ # Only the named engine has this member, so it exists only while the navigator claims to be that engine.
+ includes.add("LibWeb/Loader/ResourceLoader.h")
+ includes.add("LibWebCommon/Loader/NavigatorCompatibilityMode.h")
+ mode = extended_attributes["NavigatorCompatibilityMode"]
+ text = text.replace("\n", "\n ")
+ text = f""" if (ResourceLoader::navigator_compatibility_mode() == NavigatorCompatibilityMode::{mode}) {{
+ {text} }}
+"""
+
if "Experimental" in extended_attributes:
# The name is what a site-compatibility rule lists to expose this member for the sites it matches.
if experimental_name is None:
diff --git a/Meta/Generators/libweb_bindings/intrinsics.py b/Meta/Generators/libweb_bindings/intrinsics.py
index f51c6f77396..65765d2f290 100644
--- a/Meta/Generators/libweb_bindings/intrinsics.py
+++ b/Meta/Generators/libweb_bindings/intrinsics.py
@@ -249,7 +249,9 @@ def write_intrinsic_definitions_implementation(out: TextIO, interface_sets: Inte
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
#include <LibWeb/HTML/SharedWorkerGlobalScope.h>
+#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/WebAudio/AudioWorkletGlobalScope.h>
+#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
"""
)
@@ -269,6 +271,7 @@ namespace Web::Bindings {
write_secure_context_switch(out, interface_sets.intrinsics)
write_experimental_switch(out, interface_sets.intrinsics)
write_experimental_name_switch(out, interface_sets.intrinsics)
+ write_navigator_compatibility_mode_switch(out, interface_sets.intrinsics)
write_global_exposed_switch(out, "window", interface_sets.window_exposed)
write_global_exposed_switch(out, "dedicated_worker", interface_sets.dedicated_worker_exposed)
write_global_exposed_switch(out, "shared_worker", interface_sets.shared_worker_exposed)
@@ -307,6 +310,10 @@ bool is_exposed(InterfaceName name, JS::Realm& realm)
if (is_experimental_interface(name) && !HTML::WindowOrWorkerGlobalScopeMixin::expose_experimental_interface(principal_host_defined_environment_settings_object(realm), experimental_interface_name(name)))
return false;
+ // AD-HOC: An interface only one engine has exists only while the navigator claims to be that engine.
+ if (auto mode = navigator_compatibility_mode_of_interface(name); mode.has_value() && *mode != ResourceLoader::navigator_compatibility_mode())
+ return false;
+
// FIXME: 3. If realm’s settings object’s cross-origin isolated capability is false, and construct is
// conditionally exposed on [CrossOriginIsolated], then return false.
@@ -377,6 +384,30 @@ def write_experimental_name_switch(out: TextIO, interfaces: List[Interface]) ->
)
+def write_navigator_compatibility_mode_switch(out: TextIO, interfaces: List[Interface]) -> None:
+ # The engine an interface belongs to, for one that only exists while the navigator claims that engine.
+ out.write(
+ """static constexpr Optional<NavigatorCompatibilityMode> navigator_compatibility_mode_of_interface(InterfaceName name)
+{
+ switch (name) {
+"""
+ )
+ for interface in interfaces:
+ if "NavigatorCompatibilityMode" not in interface.extended_attributes:
+ continue
+ mode = interface.extended_attributes["NavigatorCompatibilityMode"]
+ out.write(f" case InterfaceName::{interface.name}:\n return NavigatorCompatibilityMode::{mode};\n")
+
+ out.write(
+ """ default:
+ return {};
+ }
+}
+
+"""
+ )
+
+
def write_experimental_switch(out: TextIO, interfaces: List[Interface]) -> None:
out.write(
"""static constexpr bool is_experimental_interface(InterfaceName name)
@@ -572,6 +603,8 @@ def write_exposed_interface_implementation(out: TextIO, class_name: str, exposed
#include <LibWeb/Bindings/{class_name}ExposedInterfaces.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
+#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
"""
)
@@ -637,6 +670,14 @@ def write_interface_global_accessor(out: TextIO, class_name: str, interface: Int
)
indentation += " "
+ if "NavigatorCompatibilityMode" in interface.extended_attributes:
+ mode = interface.extended_attributes["NavigatorCompatibilityMode"]
+ out.write(
+ f"""{indentation}if (ResourceLoader::navigator_compatibility_mode() == NavigatorCompatibilityMode::{mode}) {{
+"""
+ )
+ indentation += " "
+
write_constructor_accessor(interface.namespaced_name, interface.namespaced_name)
if class_name == "Window":
@@ -654,6 +695,13 @@ def write_interface_global_accessor(out: TextIO, class_name: str, interface: Int
if legacy_constructor is not None:
write_constructor_accessor(legacy_constructor.name, legacy_constructor.name)
+ if "NavigatorCompatibilityMode" in interface.extended_attributes:
+ indentation = indentation[:-4]
+ out.write(
+ f"""{indentation}}}
+"""
+ )
+
if "Experimental" in interface.extended_attributes:
indentation = indentation[:-4]
out.write(
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 9dd2d73ec76..0a6aa8eab5d 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -64,6 +64,7 @@ SUPPORTED_INTERFACE_EXTENDED_ATTRIBUTES = {
"ImplementedIn",
# NB: The C++ bindings only leave the interface object of such an interface off the global object.
"LegacyNoInterfaceObject",
+ "NavigatorCompatibilityMode",
"SecureContext",
}
diff --git a/Meta/Utils/webidl_parser.py b/Meta/Utils/webidl_parser.py
index 419ac052056..c436e343728 100644
--- a/Meta/Utils/webidl_parser.py
+++ b/Meta/Utils/webidl_parser.py
@@ -54,6 +54,8 @@ ALLOWED_EXTENDED_ATTRIBUTES = frozenset(
"LegacyUnforgeable",
"LegacyWindowAlias",
"MissingValueDefault",
+ # The construct exists only while the navigator claims the named engine, which alone has it.
+ "NavigatorCompatibilityMode",
"NeedsArgumentCount",
"NeedsCallerRealm",
"NewObject",
diff --git a/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt
new file mode 100644
index 00000000000..9ed2593d8c6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt
@@ -0,0 +1,3 @@
+chrome: WebGLObject function, deviceMemory true, buildID absent, oscpu absent, taintEnabled undefined, vendor "Google Inc."
+gecko: WebGLObject undefined, deviceMemory false, buildID 20181001000000, oscpu within the User-Agent, taintEnabled function, vendor ""
+webkit: WebGLObject undefined, deviceMemory false, buildID absent, oscpu absent, taintEnabled undefined, vendor "Apple Computer, Inc."
diff --git a/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html b/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html
new file mode 100644
index 00000000000..5af5721f114
--- /dev/null
+++ b/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ // What only one engine has exists only while the navigator claims to be that engine: a realm created under each
+ // navigator compatibility mode sees that mode's interfaces and attributes and none of the others'.
+ asyncTest(async done => {
+ const probe = async mode => {
+ internals.setNavigatorCompatibilityMode(mode);
+ const iframe = document.createElement("iframe");
+ iframe.srcdoc = "<!DOCTYPE html>";
+ const loaded = new Promise(resolve => iframe.onload = resolve);
+ document.body.append(iframe);
+ await loaded;
+ const { navigator } = iframe.contentWindow;
+ println(`${mode}: WebGLObject ${typeof iframe.contentWindow.WebGLObject}, deviceMemory ${"deviceMemory" in navigator}, `
+ + `buildID ${"buildID" in navigator ? navigator.buildID : "absent"}, oscpu ${"oscpu" in navigator ? `${navigator.userAgent.includes(navigator.oscpu) ? "within" : "outside"} the User-Agent` : "absent"}, `
+ + `taintEnabled ${typeof navigator.taintEnabled}, vendor ${JSON.stringify(navigator.vendor)}`);
+ iframe.remove();
+ };
+ try {
+ for (const mode of ["chrome", "gecko", "webkit"])
+ await probe(mode);
+ } finally {
+ internals.setNavigatorCompatibilityMode("chrome");
+ done();
+ }
+ });
+</script>