Skip to content

Truly independent web browser

LibCore+LibWeb: Cap navigator.hardwareConcurrency at 8 in WebKit mode

sideshowbarker41003af5d820
diff --git a/Libraries/LibCore/System.cpp b/Libraries/LibCore/System.cpp
index 4fa25a73c96..0357386b7b7 100644
--- a/Libraries/LibCore/System.cpp
+++ b/Libraries/LibCore/System.cpp
@@ -842,7 +842,9 @@ ErrorOr<int> poll(Span<struct pollfd> poll_fds, int timeout)
unsigned hardware_concurrency()
{
- return sysconf(_SC_NPROCESSORS_ONLN);
+ // A count the system can't report is one processor, rather than the failure's -1 cast to a huge count.
+ auto count = sysconf(_SC_NPROCESSORS_ONLN);
+ return count < 1 ? 1 : count;
}
u64 physical_memory_bytes()
diff --git a/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp
index c5ccee160cb..535dff71160 100644
--- a/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp
+++ b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp
@@ -7,13 +7,27 @@
#include <LibCore/System.h>
#include <LibWeb/HTML/NavigatorConcurrentHardware.h>
+#include <LibWeb/Loader/ResourceLoader.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/workers.html#dom-navigator-hardwareconcurrency
WebIDL::UnsignedLongLong NavigatorConcurrentHardwareMixin::hardware_concurrency()
{
- return Core::System::hardware_concurrency();
+ // The navigator.hardwareConcurrency attribute's getter must return a number between 1 and the number of logical
+ // processors potentially available to the user agent. If this cannot be determined, the getter must return 1.
+ // User agents should err toward exposing the number of logical processors available, using lower values only in
+ // cases where there are user-agent specific limits in place (such as a limitation on the number of workers that
+ // can be created) or when the user agent desires to limit fingerprinting possibilities.
+ auto logical_processors = Core::System::hardware_concurrency();
+
+ // In WebKit mode, the count is capped the way the claimed engine caps it: 8 on a machine with eight or more
+ // logical processors, and 4 on the rest. The real count would single the navigator out as not that engine. But
+ // the count never exceeds the real one, which the spec's range above forbids.
+ if (ResourceLoader::navigator_compatibility_mode() == NavigatorCompatibilityMode::WebKit)
+ return min(logical_processors, logical_processors < 8 ? 4u : 8u);
+
+ return logical_processors;
}
}
diff --git a/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt
index 9ed2593d8c6..1283df53ac0 100644
--- a/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt
+++ b/Tests/LibWeb/Text/expected/navigator-compatibility-mode-exposure.txt
@@ -1,3 +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."
+chrome: WebGLObject function, deviceMemory true, buildID absent, oscpu absent, taintEnabled undefined, vendor "Google Inc.", hardwareConcurrency real true
+gecko: WebGLObject undefined, deviceMemory false, buildID 20181001000000, oscpu within the User-Agent, taintEnabled function, vendor "", hardwareConcurrency real true
+webkit: WebGLObject undefined, deviceMemory false, buildID absent, oscpu absent, taintEnabled undefined, vendor "Apple Computer, Inc.", hardwareConcurrency capped true
diff --git a/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html b/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html
index 5af5721f114..f9c6457020a 100644
--- a/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html
+++ b/Tests/LibWeb/Text/input/navigator-compatibility-mode-exposure.html
@@ -4,6 +4,7 @@
// 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 logicalProcessors = navigator.hardwareConcurrency;
const probe = async mode => {
internals.setNavigatorCompatibilityMode(mode);
const iframe = document.createElement("iframe");
@@ -14,7 +15,10 @@
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)}`);
+ + `taintEnabled ${typeof navigator.taintEnabled}, vendor ${JSON.stringify(navigator.vendor)}, `
+ + `hardwareConcurrency ${mode === "webkit"
+ ? `capped ${[4, 8].includes(navigator.hardwareConcurrency) && navigator.hardwareConcurrency <= logicalProcessors}`
+ : `real ${navigator.hardwareConcurrency === logicalProcessors}`}`);
iframe.remove();
};
try {