fingerprinting-flags-client-rects-and-measuretext.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # Adds two flags:
  2. # 1. --fingerprinting-client-rects-noise to enable fingerprinting deception for Range::getClientRects and Element::getBoundingClientRect
  3. # 2. --fingerprinting-canvas-measuretext-noise to enable fingerprinting deception for Canvas::measureText
  4. # Tweaks based on https://github.com/bromite/bromite/blob/b1bc96bbd9ec549cf496e87f487a0ac35c83df0a/patches/BRM052_getClientRects-getBoundingClientRect-measureText-add-fingerprinting-mitigation.patch
  5. # Originally based on https://github.com/ungoogled-software/ungoogled-chromium/pull/377/commits/4151259b3248f0fc5c42fa262a1d1dd43c39fb60
  6. # chrome://flag setting added by ungoogled-chromium developers
  7. #
  8. # Unlike the latest Bromite patch, it was chosen to not regenerate the noise value each time the value is read to prevent potential efficiency issues with the load on the RNG.
  9. --- a/chrome/browser/BUILD.gn
  10. +++ b/chrome/browser/BUILD.gn
  11. @@ -2626,6 +2626,7 @@ static_library("browser") {
  12. "//third_party/libyuv",
  13. "//third_party/metrics_proto",
  14. "//third_party/re2",
  15. + "//components/ungoogled:ungoogled_switches",
  16. "//third_party/webrtc_overrides:webrtc_component",
  17. "//third_party/widevine/cdm:buildflags",
  18. "//third_party/widevine/cdm:headers",
  19. --- a/chrome/browser/about_flags.cc
  20. +++ b/chrome/browser/about_flags.cc
  21. @@ -172,6 +172,7 @@
  22. #include "components/translate/core/common/translate_util.h"
  23. #include "components/trusted_vault/features.h"
  24. #include "components/ui_devtools/switches.h"
  25. +#include "components/ungoogled/ungoogled_switches.h"
  26. #include "components/variations/variations_switches.h"
  27. #include "components/version_info/channel.h"
  28. #include "components/version_info/version_info.h"
  29. --- a/chrome/browser/bromite_flag_entries.h
  30. +++ b/chrome/browser/bromite_flag_entries.h
  31. @@ -4,4 +4,12 @@
  32. #ifndef CHROME_BROWSER_BROMITE_FLAG_ENTRIES_H_
  33. #define CHROME_BROWSER_BROMITE_FLAG_ENTRIES_H_
  34. + {"fingerprinting-client-rects-noise",
  35. + "Enable get*ClientRects() fingerprint deception",
  36. + "Scale the output values of Range::getClientRects() and Element::getBoundingClientRect() with a randomly selected factor in the range -0.0003% to 0.0003%, which are recomputed on every document initialization. ungoogled-chromium flag, Bromite feature.",
  37. + kOsAll, SINGLE_VALUE_TYPE(switches::kFingerprintingClientRectsNoise)},
  38. + {"fingerprinting-canvas-measuretext-noise",
  39. + "Enable Canvas::measureText() fingerprint deception",
  40. + "Scale the output values of Canvas::measureText() with a randomly selected factor in the range -0.0003% to 0.0003%, which are recomputed on every document initialization. ungoogled-chromium flag, Bromite feature.",
  41. + kOsAll, SINGLE_VALUE_TYPE(switches::kFingerprintingCanvasMeasureTextNoise)},
  42. #endif // CHROME_BROWSER_BROMITE_FLAG_ENTRIES_H_
  43. --- a/content/browser/BUILD.gn
  44. +++ b/content/browser/BUILD.gn
  45. @@ -253,6 +253,7 @@ source_set("browser") {
  46. "//third_party/libyuv",
  47. "//third_party/re2",
  48. "//third_party/sqlite",
  49. + "//components/ungoogled:ungoogled_switches",
  50. "//third_party/webrtc_overrides:webrtc_component",
  51. "//third_party/zlib",
  52. "//tools/v8_context_snapshot:buildflags",
  53. --- a/content/browser/renderer_host/render_process_host_impl.cc
  54. +++ b/content/browser/renderer_host/render_process_host_impl.cc
  55. @@ -78,6 +78,7 @@
  56. #include "components/services/storage/public/cpp/quota_error_or.h"
  57. #include "components/services/storage/public/mojom/cache_storage_control.mojom.h"
  58. #include "components/tracing/common/tracing_switches.h"
  59. +#include "components/ungoogled/ungoogled_switches.h"
  60. #include "components/viz/common/switches.h"
  61. #include "components/viz/host/gpu_client.h"
  62. #include "content/browser/bad_message.h"
  63. @@ -3465,6 +3466,8 @@ void RenderProcessHostImpl::PropagateBro
  64. switches::kEnableWebGLImageChromium,
  65. switches::kEnableWebGPUDeveloperFeatures,
  66. switches::kFileUrlPathAlias,
  67. + switches::kFingerprintingClientRectsNoise,
  68. + switches::kFingerprintingCanvasMeasureTextNoise,
  69. switches::kForceDeviceScaleFactor,
  70. switches::kForceDisplayColorProfile,
  71. switches::kForceGpuMemAvailableMb,
  72. --- a/content/child/BUILD.gn
  73. +++ b/content/child/BUILD.gn
  74. @@ -105,6 +105,7 @@ target(link_target_type, "child") {
  75. "//third_party/blink/public/common:buildflags",
  76. "//third_party/blink/public/strings",
  77. "//third_party/ced",
  78. + "//components/ungoogled:ungoogled_switches",
  79. "//third_party/zlib/google:compression_utils",
  80. "//ui/base",
  81. "//ui/events/blink",
  82. --- a/content/child/runtime_features.cc
  83. +++ b/content/child/runtime_features.cc
  84. @@ -40,6 +40,7 @@
  85. #include "third_party/blink/public/common/loader/referrer_utils.h"
  86. #include "third_party/blink/public/common/switches.h"
  87. #include "third_party/blink/public/platform/web_runtime_features.h"
  88. +#include "components/ungoogled/ungoogled_switches.h"
  89. #include "ui/accessibility/accessibility_features.h"
  90. #include "ui/base/ui_base_features.h"
  91. #include "ui/events/blink/blink_features.h"
  92. @@ -488,6 +489,10 @@ void SetRuntimeFeaturesFromCommandLine(c
  93. switches::kEnableWebGPUDeveloperFeatures, true},
  94. {wrf::EnableWebGPUExperimentalFeatures, switches::kEnableUnsafeWebGPU,
  95. true},
  96. + {wrf::EnableFingerprintingClientRectsNoise,
  97. + switches::kFingerprintingClientRectsNoise, true},
  98. + {wrf::EnableFingerprintingCanvasMeasureTextNoise,
  99. + switches::kFingerprintingCanvasMeasureTextNoise, true},
  100. };
  101. for (const auto& mapping : switchToFeatureMapping) {
  102. --- a/third_party/blink/public/platform/web_runtime_features.h
  103. +++ b/third_party/blink/public/platform/web_runtime_features.h
  104. @@ -69,6 +69,9 @@ class BLINK_PLATFORM_EXPORT WebRuntimeFe
  105. static void EnableFluentScrollbars(bool);
  106. static void EnableFluentOverlayScrollbars(bool);
  107. + static void EnableFingerprintingClientRectsNoise(bool);
  108. + static void EnableFingerprintingCanvasMeasureTextNoise(bool);
  109. +
  110. WebRuntimeFeatures() = delete;
  111. };
  112. --- a/third_party/blink/renderer/core/dom/document.cc
  113. +++ b/third_party/blink/renderer/core/dom/document.cc
  114. @@ -40,6 +40,7 @@
  115. #include "base/i18n/time_formatting.h"
  116. #include "base/metrics/histogram_functions.h"
  117. #include "base/notreached.h"
  118. +#include "base/rand_util.h"
  119. #include "base/ranges/algorithm.h"
  120. #include "base/task/single_thread_task_runner.h"
  121. #include "base/time/time.h"
  122. @@ -906,6 +907,11 @@ Document::Document(const DocumentInit& i
  123. TRACE_EVENT_WITH_FLOW0("blink", "Document::Document", TRACE_ID_LOCAL(this),
  124. TRACE_EVENT_FLAG_FLOW_OUT);
  125. DCHECK(agent_);
  126. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  127. + // Precompute -0.0003% to 0.0003% noise factor for get*ClientRect*() fingerprinting
  128. + noise_factor_x_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  129. + noise_factor_y_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  130. + }
  131. if (base::FeatureList::IsEnabled(features::kDelayAsyncScriptExecution) &&
  132. features::kDelayAsyncScriptExecutionDelayByDefaultParam.Get()) {
  133. script_runner_delayer_->Activate();
  134. @@ -1021,6 +1027,14 @@ const Position Document::PositionAdjuste
  135. return Position::BeforeNode(*shadow_host);
  136. }
  137. +double Document::GetNoiseFactorX() {
  138. + return noise_factor_x_;
  139. +}
  140. +
  141. +double Document::GetNoiseFactorY() {
  142. + return noise_factor_y_;
  143. +}
  144. +
  145. SelectorQueryCache& Document::GetSelectorQueryCache() {
  146. if (!selector_query_cache_)
  147. selector_query_cache_ = std::make_unique<SelectorQueryCache>();
  148. --- a/third_party/blink/renderer/core/dom/document.h
  149. +++ b/third_party/blink/renderer/core/dom/document.h
  150. @@ -548,6 +548,10 @@ class CORE_EXPORT Document : public Cont
  151. has_xml_declaration_ = has_xml_declaration ? 1 : 0;
  152. }
  153. + // Values for get*ClientRect fingerprint deception
  154. + double GetNoiseFactorX();
  155. + double GetNoiseFactorY();
  156. +
  157. AtomicString visibilityState() const;
  158. bool IsPageVisible() const;
  159. bool hidden() const;
  160. @@ -2545,6 +2549,9 @@ class CORE_EXPORT Document : public Cont
  161. base::ElapsedTimer start_time_;
  162. + double noise_factor_x_ = 1;
  163. + double noise_factor_y_ = 1;
  164. +
  165. Member<ScriptRunner> script_runner_;
  166. Member<ScriptRunnerDelayer> script_runner_delayer_;
  167. --- a/third_party/blink/renderer/core/dom/element.cc
  168. +++ b/third_party/blink/renderer/core/dom/element.cc
  169. @@ -2432,6 +2432,11 @@ DOMRectList* Element::getClientRects() {
  170. DCHECK(element_layout_object);
  171. GetDocument().AdjustQuadsForScrollAndAbsoluteZoom(quads,
  172. *element_layout_object);
  173. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  174. + for (gfx::QuadF& quad : quads) {
  175. + quad.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  176. + }
  177. + }
  178. return MakeGarbageCollected<DOMRectList>(quads);
  179. }
  180. @@ -2459,6 +2464,9 @@ gfx::RectF Element::GetBoundingClientRec
  181. DCHECK(element_layout_object);
  182. GetDocument().AdjustRectForScrollAndAbsoluteZoom(result,
  183. *element_layout_object);
  184. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  185. + result.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  186. + }
  187. return result;
  188. }
  189. --- a/third_party/blink/renderer/core/dom/range.cc
  190. +++ b/third_party/blink/renderer/core/dom/range.cc
  191. @@ -1607,6 +1607,12 @@ DOMRectList* Range::getClientRects() con
  192. Vector<gfx::QuadF> quads;
  193. GetBorderAndTextQuads(quads);
  194. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  195. + for (gfx::QuadF& quad : quads) {
  196. + quad.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  197. + }
  198. + }
  199. +
  200. return MakeGarbageCollected<DOMRectList>(quads);
  201. }
  202. @@ -1614,7 +1620,11 @@ DOMRect* Range::getBoundingClientRect()
  203. // TODO(crbug.com/1499981): This should be removed once synchronized scrolling
  204. // impact is understood.
  205. SyncScrollAttemptHeuristic::DidAccessScrollOffset();
  206. - return DOMRect::FromRectF(BoundingRect());
  207. + auto rect = BoundingRect();
  208. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  209. + rect.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  210. + }
  211. + return DOMRect::FromRectF(rect);
  212. }
  213. // TODO(editing-dev): We should make
  214. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  215. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  216. @@ -69,6 +69,24 @@ TextMetrics::TextMetrics(const Font& fon
  217. Update(font, direction, baseline, align, text);
  218. }
  219. +void TextMetrics::Shuffle(const double factor) {
  220. + // x-direction
  221. + width_ *= factor;
  222. + actual_bounding_box_left_ *= factor;
  223. + actual_bounding_box_right_ *= factor;
  224. +
  225. + // y-direction
  226. + font_bounding_box_ascent_ *= factor;
  227. + font_bounding_box_descent_ *= factor;
  228. + actual_bounding_box_ascent_ *= factor;
  229. + actual_bounding_box_descent_ *= factor;
  230. + em_height_ascent_ *= factor;
  231. + em_height_descent_ *= factor;
  232. + baselines_->setAlphabetic(baselines_->alphabetic() * factor);
  233. + baselines_->setHanging(baselines_->hanging() * factor);
  234. + baselines_->setIdeographic(baselines_->ideographic() * factor);
  235. +}
  236. +
  237. void TextMetrics::Update(const Font& font,
  238. const TextDirection& direction,
  239. const TextBaseline& baseline,
  240. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.h
  241. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.h
  242. @@ -66,6 +66,8 @@ class CORE_EXPORT TextMetrics final : pu
  243. void Trace(Visitor*) const override;
  244. + void Shuffle(const double factor);
  245. +
  246. private:
  247. void Update(const Font&,
  248. const TextDirection&,
  249. --- a/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc
  250. +++ b/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc
  251. @@ -90,6 +90,9 @@
  252. #include "ui/gfx/geometry/quad_f.h"
  253. #include "ui/gfx/geometry/skia_conversions.h"
  254. +#include "third_party/blink/renderer/core/offscreencanvas/offscreen_canvas.h"
  255. +#include "third_party/blink/renderer/core/frame/local_dom_window.h"
  256. +
  257. namespace blink {
  258. BASE_FEATURE(kDisableCanvasOverdrawOptimization,
  259. @@ -3279,8 +3282,22 @@ TextMetrics* BaseRenderingContext2D::mea
  260. const CanvasRenderingContext2DState& state = GetState();
  261. TextDirection direction = ToTextDirection(state.GetDirection(), canvas);
  262. - return MakeGarbageCollected<TextMetrics>(
  263. - font, direction, state.GetTextBaseline(), state.GetTextAlign(), text);
  264. + TextMetrics* text_metrics = MakeGarbageCollected<TextMetrics>(font, direction,
  265. + GetState().GetTextBaseline(),
  266. + GetState().GetTextAlign(), text);
  267. +
  268. + // Scale text metrics if enabled
  269. + if (RuntimeEnabledFeatures::FingerprintingCanvasMeasureTextNoiseEnabled()) {
  270. + if (HostAsOffscreenCanvas()) {
  271. + if (auto* window = DynamicTo<LocalDOMWindow>(GetTopExecutionContext())) {
  272. + if (window->GetFrame() && window->GetFrame()->GetDocument())
  273. + text_metrics->Shuffle(window->GetFrame()->GetDocument()->GetNoiseFactorX());
  274. + }
  275. + } else if (canvas) {
  276. + text_metrics->Shuffle(canvas->GetDocument().GetNoiseFactorX());
  277. + }
  278. + }
  279. + return text_metrics;
  280. }
  281. void BaseRenderingContext2D::SnapshotStateForFilter() {
  282. --- a/third_party/blink/renderer/platform/BUILD.gn
  283. +++ b/third_party/blink/renderer/platform/BUILD.gn
  284. @@ -1733,6 +1733,7 @@ component("platform") {
  285. "//cc/mojo_embedder",
  286. "//components/paint_preview/common",
  287. "//components/search_engines:search_engine_utils",
  288. + "//components/ungoogled:ungoogled_switches",
  289. "//components/viz/client",
  290. "//components/viz/common",
  291. "//components/webrtc:net_address_utils",
  292. --- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  293. +++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  294. @@ -68,4 +68,12 @@ void WebRuntimeFeatures::EnableFluentScr
  295. void WebRuntimeFeatures::EnableFluentOverlayScrollbars(bool enable) {
  296. RuntimeEnabledFeatures::SetFluentOverlayScrollbarsEnabled(enable);
  297. }
  298. +void WebRuntimeFeatures::EnableFingerprintingClientRectsNoise(bool enable) {
  299. + RuntimeEnabledFeatures::SetFingerprintingClientRectsNoiseEnabled(enable);
  300. +}
  301. +
  302. +void WebRuntimeFeatures::EnableFingerprintingCanvasMeasureTextNoise(bool enable) {
  303. + RuntimeEnabledFeatures::SetFingerprintingCanvasMeasureTextNoiseEnabled(enable);
  304. +}
  305. +
  306. } // namespace blink
  307. --- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
  308. +++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
  309. @@ -1724,6 +1724,12 @@
  310. status: "experimental",
  311. },
  312. {
  313. + name: "FingerprintingClientRectsNoise",
  314. + },
  315. + {
  316. + name: "FingerprintingCanvasMeasureTextNoise",
  317. + },
  318. + {
  319. name: "Fledge",
  320. base_feature: "none",
  321. origin_trial_feature_name: "PrivacySandboxAdsAPIs",