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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. @@ -2412,6 +2412,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. @@ -146,6 +146,7 @@
  22. #include "components/translate/core/browser/translate_ranker_impl.h"
  23. #include "components/translate/core/common/translate_util.h"
  24. #include "components/ui_devtools/switches.h"
  25. +#include "components/ungoogled/ungoogled_switches.h"
  26. #include "components/version_info/version_info.h"
  27. #include "components/viz/common/features.h"
  28. #include "components/viz/common/switches.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. @@ -234,6 +234,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/wtl",
  52. "//third_party/zlib",
  53. --- a/content/browser/renderer_host/render_process_host_impl.cc
  54. +++ b/content/browser/renderer_host/render_process_host_impl.cc
  55. @@ -76,6 +76,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. @@ -3242,6 +3243,8 @@ void RenderProcessHostImpl::PropagateBro
  64. switches::kEnableWebGLDraftExtensions,
  65. switches::kEnableWebGLImageChromium,
  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. @@ -102,6 +102,7 @@ target(link_target_type, "child") {
  75. "//third_party/blink/public/common",
  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. @@ -33,6 +33,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. @@ -472,6 +473,10 @@ void SetRuntimeFeaturesFromCommandLine(c
  93. {wrf::ForceOverlayFullscreenVideo, switches::kForceOverlayFullscreenVideo,
  94. true},
  95. {wrf::EnableDirectSockets, switches::kIsolatedAppOrigins, false},
  96. + {wrf::EnableFingerprintingClientRectsNoise,
  97. + switches::kFingerprintingClientRectsNoise, true},
  98. + {wrf::EnableFingerprintingCanvasMeasureTextNoise,
  99. + switches::kFingerprintingCanvasMeasureTextNoise, true},
  100. };
  101. for (const auto& mapping : switchToFeatureMapping) {
  102. if (command_line.HasSwitch(mapping.switch_name))
  103. --- a/third_party/blink/public/platform/web_runtime_features.h
  104. +++ b/third_party/blink/public/platform/web_runtime_features.h
  105. @@ -212,6 +212,8 @@ class WebRuntimeFeatures {
  106. BLINK_PLATFORM_EXPORT static void EnableMediaControlsExpandGesture(bool);
  107. BLINK_PLATFORM_EXPORT static void EnableGetDisplayMedia(bool);
  108. BLINK_PLATFORM_EXPORT static void EnableAllowSyncXHRInPageDismissal(bool);
  109. + BLINK_PLATFORM_EXPORT static void EnableFingerprintingClientRectsNoise(bool);
  110. + BLINK_PLATFORM_EXPORT static void EnableFingerprintingCanvasMeasureTextNoise(bool);
  111. BLINK_PLATFORM_EXPORT static void EnableSignedExchangeSubresourcePrefetch(
  112. bool);
  113. BLINK_PLATFORM_EXPORT static void EnableSubresourceWebBundles(bool);
  114. --- a/third_party/blink/renderer/core/dom/document.cc
  115. +++ b/third_party/blink/renderer/core/dom/document.cc
  116. @@ -43,6 +43,7 @@
  117. #include "base/debug/dump_without_crashing.h"
  118. #include "base/metrics/histogram_functions.h"
  119. #include "base/notreached.h"
  120. +#include "base/rand_util.h"
  121. #include "base/time/time.h"
  122. #include "cc/animation/animation_timeline.h"
  123. #include "cc/input/overscroll_behavior.h"
  124. @@ -888,6 +889,14 @@ Range* Document::CreateRangeAdjustedToTr
  125. Position::BeforeNode(*shadow_host));
  126. }
  127. +double Document::GetNoiseFactorX() {
  128. + return noise_factor_x_;
  129. +}
  130. +
  131. +double Document::GetNoiseFactorY() {
  132. + return noise_factor_y_;
  133. +}
  134. +
  135. SelectorQueryCache& Document::GetSelectorQueryCache() {
  136. if (!selector_query_cache_)
  137. selector_query_cache_ = std::make_unique<SelectorQueryCache>();
  138. @@ -2197,6 +2206,15 @@ void Document::UpdateStyleAndLayoutTreeF
  139. #if DCHECK_IS_ON()
  140. AssertLayoutTreeUpdated(*this, true /* allow_dirty_container_subtrees */);
  141. #endif
  142. +
  143. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  144. + // Precompute -0.0003% to 0.0003% noise factor for get*ClientRect*() fingerprinting
  145. + noise_factor_x_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  146. + noise_factor_y_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  147. + } else {
  148. + noise_factor_x_ = 1;
  149. + noise_factor_y_ = 1;
  150. + }
  151. }
  152. void Document::InvalidateStyleAndLayoutForFontUpdates() {
  153. --- a/third_party/blink/renderer/core/dom/document.h
  154. +++ b/third_party/blink/renderer/core/dom/document.h
  155. @@ -498,6 +498,10 @@ class CORE_EXPORT Document : public Cont
  156. has_xml_declaration_ = has_xml_declaration ? 1 : 0;
  157. }
  158. + // Values for get*ClientRect fingerprint deception
  159. + double GetNoiseFactorX();
  160. + double GetNoiseFactorY();
  161. +
  162. AtomicString visibilityState() const;
  163. bool IsPageVisible() const;
  164. bool hidden() const;
  165. @@ -2237,6 +2241,9 @@ class CORE_EXPORT Document : public Cont
  166. base::ElapsedTimer start_time_;
  167. + double noise_factor_x_;
  168. + double noise_factor_y_;
  169. +
  170. Member<ScriptRunner> script_runner_;
  171. HeapVector<Member<ScriptElementBase>> current_script_stack_;
  172. --- a/third_party/blink/renderer/core/dom/element.cc
  173. +++ b/third_party/blink/renderer/core/dom/element.cc
  174. @@ -2118,6 +2118,11 @@ DOMRectList* Element::getClientRects() {
  175. DCHECK(element_layout_object);
  176. GetDocument().AdjustQuadsForScrollAndAbsoluteZoom(quads,
  177. *element_layout_object);
  178. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  179. + for (gfx::QuadF& quad : quads) {
  180. + quad.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  181. + }
  182. + }
  183. return MakeGarbageCollected<DOMRectList>(quads);
  184. }
  185. @@ -2135,6 +2140,9 @@ gfx::RectF Element::GetBoundingClientRec
  186. DCHECK(element_layout_object);
  187. GetDocument().AdjustRectForScrollAndAbsoluteZoom(result,
  188. *element_layout_object);
  189. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  190. + result.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  191. + }
  192. return result;
  193. }
  194. --- a/third_party/blink/renderer/core/dom/range.cc
  195. +++ b/third_party/blink/renderer/core/dom/range.cc
  196. @@ -1601,11 +1601,21 @@ DOMRectList* Range::getClientRects() con
  197. Vector<gfx::QuadF> quads;
  198. GetBorderAndTextQuads(quads);
  199. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  200. + for (gfx::QuadF& quad : quads) {
  201. + quad.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  202. + }
  203. + }
  204. +
  205. return MakeGarbageCollected<DOMRectList>(quads);
  206. }
  207. DOMRect* Range::getBoundingClientRect() const {
  208. - return DOMRect::FromRectF(BoundingRect());
  209. + auto rect = BoundingRect();
  210. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  211. + rect.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  212. + }
  213. + return DOMRect::FromRectF(rect);
  214. }
  215. // TODO(editing-dev): We should make
  216. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  217. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  218. @@ -55,6 +55,24 @@ TextMetrics::TextMetrics(const Font& fon
  219. Update(font, direction, baseline, align, text);
  220. }
  221. +void TextMetrics::Shuffle(const double factor) {
  222. + // x-direction
  223. + width_ *= factor;
  224. + actual_bounding_box_left_ *= factor;
  225. + actual_bounding_box_right_ *= factor;
  226. +
  227. + // y-direction
  228. + font_bounding_box_ascent_ *= factor;
  229. + font_bounding_box_descent_ *= factor;
  230. + actual_bounding_box_ascent_ *= factor;
  231. + actual_bounding_box_descent_ *= factor;
  232. + em_height_ascent_ *= factor;
  233. + em_height_descent_ *= factor;
  234. + baselines_->setAlphabetic(baselines_->alphabetic() * factor);
  235. + baselines_->setHanging(baselines_->hanging() * factor);
  236. + baselines_->setIdeographic(baselines_->ideographic() * factor);
  237. +}
  238. +
  239. void TextMetrics::Update(const Font& font,
  240. const TextDirection& direction,
  241. const TextBaseline& baseline,
  242. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.h
  243. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.h
  244. @@ -64,6 +64,8 @@ class CORE_EXPORT TextMetrics final : pu
  245. void Trace(Visitor*) const override;
  246. + void Shuffle(const double factor);
  247. +
  248. private:
  249. void Update(const Font&,
  250. const TextDirection&,
  251. --- a/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  252. +++ b/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  253. @@ -920,9 +920,15 @@ TextMetrics* CanvasRenderingContext2D::m
  254. TextDirection direction =
  255. ToTextDirection(GetState().GetDirection(), canvas());
  256. - return MakeGarbageCollected<TextMetrics>(font, direction,
  257. + TextMetrics* text_metrics = MakeGarbageCollected<TextMetrics>(font, direction,
  258. GetState().GetTextBaseline(),
  259. GetState().GetTextAlign(), text);
  260. +
  261. + // Scale text metrics if enabled
  262. + if (RuntimeEnabledFeatures::FingerprintingCanvasMeasureTextNoiseEnabled()) {
  263. + text_metrics->Shuffle(canvas()->GetDocument().GetNoiseFactorX());
  264. + }
  265. + return text_metrics;
  266. }
  267. void CanvasRenderingContext2D::fillFormattedText(
  268. --- a/third_party/blink/renderer/platform/BUILD.gn
  269. +++ b/third_party/blink/renderer/platform/BUILD.gn
  270. @@ -1637,6 +1637,7 @@ component("platform") {
  271. "//components/paint_preview/common",
  272. "//components/power_scheduler",
  273. "//components/search_engines:search_engine_utils",
  274. + "//components/ungoogled:ungoogled_switches",
  275. "//components/viz/client",
  276. "//components/viz/common",
  277. "//components/webrtc:net_address_utils",
  278. --- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  279. +++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  280. @@ -659,4 +659,12 @@ void WebRuntimeFeatures::EnableWebAuthen
  281. enable);
  282. }
  283. +void WebRuntimeFeatures::EnableFingerprintingClientRectsNoise(bool enable) {
  284. + RuntimeEnabledFeatures::SetFingerprintingClientRectsNoiseEnabled(enable);
  285. +}
  286. +
  287. +void WebRuntimeFeatures::EnableFingerprintingCanvasMeasureTextNoise(bool enable) {
  288. + RuntimeEnabledFeatures::SetFingerprintingCanvasMeasureTextNoiseEnabled(enable);
  289. +}
  290. +
  291. } // namespace blink
  292. --- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
  293. +++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
  294. @@ -1099,6 +1099,12 @@
  295. status: "experimental",
  296. },
  297. {
  298. + name: "FingerprintingClientRectsNoise",
  299. + },
  300. + {
  301. + name: "FingerprintingCanvasMeasureTextNoise",
  302. + },
  303. + {
  304. name: "FocuslessSpatialNavigation",
  305. settable_from_internals: true,
  306. },