Skip to content

Truly independent web browser

LibWeb: Answer WebGL's vendor and version strings as the claimed engine

sideshowbarkerc59dbeeea9bd
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs
index 56e49dbe550..38ab6945aa7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use ak::Utf16String;
use libjs_rust::api::Value;
use super::byte_ranges::gl_sizei_of_array_length;
@@ -67,6 +68,110 @@ use super::web_gl_rendering_context_base::{is_valid_framebuffer_attachment, is_v
use super::web_gl_sampler::WebGLSampler;
use super::web_gl_shader::WebGLShader;
use super::web_gl_uniform_location::WebGLUniformLocation;
+
+unsafe extern "C" {
+ fn web_navigator_compatibility_mode() -> u8;
+}
+
+/// The navigator compatibility mode, numbered as Web::NavigatorCompatibilityMode is.
+const MODE_CHROME: u8 = 0;
+const MODE_GECKO: u8 = 1;
+const MODE_WEBKIT: u8 = 2;
+
+fn navigator_compatibility_mode() -> u8 {
+ // SAFETY: The function reads a process-wide setting and takes nothing.
+ unsafe { web_navigator_compatibility_mode() }
+}
+
+/// VENDOR, RENDERER, VERSION, and SHADING_LANGUAGE_VERSION name the browser's WebGL implementation rather than the
+/// GPU, whose own strings are only behind WEBGL_debug_renderer_info. Each engine words them its own way, so the answer
+/// follows the navigator compatibility mode.
+fn masked_string_parameter(context: &ContextState, cx: &CallContext, pname: u32) -> Value {
+ let mode = navigator_compatibility_mode();
+ let webgl2 = context.is_webgl2();
+ let text: &str = match (pname, mode) {
+ (GL_VENDOR, MODE_GECKO) => "Mozilla",
+ (GL_VENDOR, _) => "WebKit",
+ (GL_RENDERER, MODE_GECKO) => return string_value(cx, sanitized_renderer(context)),
+ (GL_RENDERER, _) => "WebKit WebGL",
+ (GL_VERSION, MODE_CHROME) if webgl2 => "WebGL 2.0 (OpenGL ES 3.0 Chromium)",
+ (GL_VERSION, MODE_CHROME) => "WebGL 1.0 (OpenGL ES 2.0 Chromium)",
+ (GL_VERSION, _) if webgl2 => "WebGL 2.0",
+ (GL_VERSION, _) => "WebGL 1.0",
+ (GL_SHADING_LANGUAGE_VERSION, MODE_CHROME) if webgl2 => "WebGL GLSL ES 3.00 (OpenGL ES GLSL ES 3.0 Chromium)",
+ (GL_SHADING_LANGUAGE_VERSION, MODE_CHROME) => "WebGL GLSL ES 1.0 (OpenGL ES GLSL ES 1.0 Chromium)",
+ (GL_SHADING_LANGUAGE_VERSION, _) if webgl2 => "WebGL GLSL ES 3.00",
+ (GL_SHADING_LANGUAGE_VERSION, MODE_WEBKIT) => "WebGL GLSL ES 1.0 (1.0)",
+ (GL_SHADING_LANGUAGE_VERSION, _) => "WebGL GLSL ES 1.0",
+ _ => return string_parameter(context, cx, pname),
+ };
+ string_value(cx, Utf16String::from_ascii(text.as_bytes()))
+}
+
+/// The unmasked vendor: the GPU's, except that a navigator claiming WebKit on macOS answers as that engine does there,
+/// and one claiming Gecko answers with the vendor's bare name.
+fn unmasked_vendor(context: &ContextState, cx: &CallContext) -> Value {
+ let vendor = context.proxy.get_string(GL_VENDOR);
+ let text: Option<&str> = match navigator_compatibility_mode() {
+ MODE_WEBKIT if cfg!(target_os = "macos") => Some("Apple Inc."),
+ MODE_GECKO => gpu_vendor_name(&vendor, &context.proxy.get_string(GL_RENDERER)),
+ _ => None,
+ };
+ match text {
+ Some(text) => string_value(cx, Utf16String::from_ascii(text.as_bytes())),
+ None => string_value(cx, utf16_string_from_gl_string(&vendor)),
+ }
+}
+
+/// The unmasked renderer: the GPU's, except that a navigator claiming WebKit on macOS answers as that engine does
+/// there, and one claiming Gecko answers with a representative device of the GPU's family.
+fn unmasked_renderer(context: &ContextState, cx: &CallContext) -> Value {
+ match navigator_compatibility_mode() {
+ MODE_WEBKIT if cfg!(target_os = "macos") => string_value(cx, Utf16String::from_ascii(b"Apple GPU")),
+ MODE_GECKO => string_value(cx, sanitized_renderer(context)),
+ _ => string_parameter(context, cx, GL_RENDERER),
+ }
+}
+
+/// The GPU vendor's bare name, found in either GL string (an ANGLE renderer string names the vendor in its own
+/// way), or None for a vendor without one.
+fn gpu_vendor_name(vendor: &[u8], renderer: &[u8]) -> Option<&'static str> {
+ let contains = |needle: &str| {
+ let needle = needle.as_bytes();
+ [vendor, renderer]
+ .iter()
+ .any(|haystack| haystack.windows(needle.len()).any(|window| window == needle))
+ };
+ if contains("Apple") {
+ Some("Apple")
+ } else if contains("NVIDIA") {
+ Some("NVIDIA Corporation")
+ } else if contains("Intel") {
+ Some("Intel")
+ } else if contains("AMD") || contains("ATI") {
+ Some("AMD")
+ } else if contains("Qualcomm") {
+ Some("Qualcomm")
+ } else if contains("ARM") {
+ Some("ARM")
+ } else if contains("Mesa") || contains("X.Org") {
+ Some("Mesa")
+ } else {
+ None
+ }
+}
+
+/// A Gecko-shaped renderer string: a representative device for the GPU's family, marked ", or similar". An Apple GPU
+/// of any generation reads "Apple M1, or similar"; another GPU keeps its own string with the same marker.
+fn sanitized_renderer(context: &ContextState) -> Utf16String {
+ let renderer = context.proxy.get_string(GL_RENDERER);
+ if gpu_vendor_name(&context.proxy.get_string(GL_VENDOR), &renderer) == Some("Apple") {
+ return Utf16String::from_ascii(b"Apple M1, or similar");
+ }
+ let mut text = String::from_utf8_lossy(&renderer).into_owned();
+ text.push_str(", or similar");
+ Utf16String::from_utf8(&text)
+}
use crate::bindings::interface::{Ref, wrap};
use crate::bindings::{CallContext, WebIdlResult};
@@ -231,7 +336,9 @@ fn parameter_of_either_version(context: &ContextState, cx: &CallContext, pname:
context.boolean_array(cx, &result.map(|is_written| is_written != 0))
}
- GL_RENDERER | GL_SHADING_LANGUAGE_VERSION | GL_VENDOR | GL_VERSION => string_parameter(context, cx, pname),
+ GL_RENDERER | GL_SHADING_LANGUAGE_VERSION | GL_VENDOR | GL_VERSION => {
+ masked_string_parameter(context, cx, pname)
+ }
GL_ARRAY_BUFFER_BINDING => object_or_null(cx, context.array_buffer_binding.get()),
GL_CURRENT_PROGRAM => object_or_null(cx, context.current_program.get()),
@@ -243,14 +350,14 @@ fn parameter_of_either_version(context: &ContextState, cx: &CallContext, pname:
GL_UNMASKED_VENDOR_WEBGL => {
if context.extension_enabled("WEBGL_debug_renderer_info") {
- string_parameter(context, cx, GL_VENDOR)
+ unmasked_vendor(context, cx)
} else {
null_with_invalid_enum_error(context)
}
}
GL_UNMASKED_RENDERER_WEBGL => {
if context.extension_enabled("WEBGL_debug_renderer_info") {
- string_parameter(context, cx, GL_RENDERER)
+ unmasked_renderer(context, cx)
} else {
null_with_invalid_enum_error(context)
}
diff --git a/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp b/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp
index 580fa48d120..738a96f79f2 100644
--- a/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp
+++ b/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp
@@ -14,6 +14,7 @@
#include <LibGfx/DecodedImageFrame.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibIPC/Limits.h>
+#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/WebGL/RemoteWebGLTransport.h>
// The C ABI of RemoteWebGLTransport, for the WebGL command recorder in Libraries/LibWeb/Bindings/Rust/src/web_gl,
@@ -72,6 +73,7 @@ void web_webgl_transport_read_pixels_robust_angle(void* transport, i32 x, i32 y,
bool web_webgl_transport_read_buffer_sub_data(void* transport, u32 target, i64 offset, i64 size, void const* data_anonymous_buffer);
void* web_webgl_transport_read_back_drawing_buffer(void* transport, i32 x, i32 y, i32 width, i32 height);
size_t web_webgl_max_frames_per_message();
+u8 web_navigator_compatibility_mode();
void web_webgl_constants_restated_in_rust(ConstantsRestatedInRust* out_constants);
void* web_webgl_anonymous_buffer_create(size_t size, u8** out_data);
void web_webgl_anonymous_buffer_destroy(void* anonymous_buffer);
@@ -189,6 +191,12 @@ size_t web_webgl_max_frames_per_message()
return IPC::MAX_MESSAGE_FD_COUNT - 1;
}
+// The navigator compatibility mode, numbered as Web::NavigatorCompatibilityMode is: Chrome, Gecko, WebKit.
+u8 web_navigator_compatibility_mode()
+{
+ return to_underlying(Web::ResourceLoader::navigator_compatibility_mode());
+}
+
void web_webgl_constants_restated_in_rust(ConstantsRestatedInRust* out_constants)
{
*out_constants = {
diff --git a/Tests/LibWeb/Text/expected/navigator-compatibility-mode-webgl.txt b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-webgl.txt
new file mode 100644
index 00000000000..86ef3363b16
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-webgl.txt
@@ -0,0 +1,3 @@
+chrome: "WebKit" "WebKit WebGL" "WebGL 1.0 (OpenGL ES 2.0 Chromium)" "WebGL GLSL ES 1.0 (OpenGL ES GLSL ES 1.0 Chromium)"
+gecko: "Mozilla" <device>, or similar "WebGL 1.0" "WebGL GLSL ES 1.0"
+webkit: "WebKit" "WebKit WebGL" "WebGL 1.0" "WebGL GLSL ES 1.0 (1.0)"
diff --git a/Tests/LibWeb/Text/input/navigator-compatibility-mode-webgl.html b/Tests/LibWeb/Text/input/navigator-compatibility-mode-webgl.html
new file mode 100644
index 00000000000..b08b1deaf76
--- /dev/null
+++ b/Tests/LibWeb/Text/input/navigator-compatibility-mode-webgl.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ // WebGL's VENDOR, RENDERER, VERSION, and SHADING_LANGUAGE_VERSION name the engine the navigator claims, in that
+ // engine's own words, for a context created under each navigator compatibility mode.
+ 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 gl = iframe.contentDocument.createElement("canvas").getContext("webgl");
+ // The Gecko-shaped renderer names a representative of this machine's GPU family, so only its marker is stable.
+ const renderer = gl.getParameter(gl.RENDERER);
+ println(`${mode}: ${JSON.stringify(gl.getParameter(gl.VENDOR))} ${renderer.endsWith(", or similar") ? "<device>, or similar" : JSON.stringify(renderer)} `
+ + `${JSON.stringify(gl.getParameter(gl.VERSION))} ${JSON.stringify(gl.getParameter(gl.SHADING_LANGUAGE_VERSION))}`);
+ iframe.remove();
+ };
+ try {
+ for (const mode of ["chrome", "gecko", "webkit"])
+ await probe(mode);
+ } finally {
+ internals.setNavigatorCompatibilityMode("chrome");
+ done();
+ }
+ });
+</script>