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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/Eloston/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/about_flags.cc
  10. +++ b/chrome/browser/about_flags.cc
  11. @@ -137,6 +137,7 @@
  12. #include "third_party/blink/public/common/experiments/memory_ablation_experiment.h"
  13. #include "third_party/blink/public/common/features.h"
  14. #include "third_party/leveldatabase/leveldb_features.h"
  15. +#include "third_party/ungoogled/ungoogled_switches.h"
  16. #include "ui/accessibility/accessibility_switches.h"
  17. #include "ui/base/ui_base_features.h"
  18. #include "ui/base/ui_base_switches.h"
  19. @@ -1242,6 +1243,14 @@ const FeatureEntry kFeatureEntries[] = {
  20. "Force punycode hostnames",
  21. "Force punycode in hostnames instead of Unicode when displaying Internationalized Domain Names (IDNs).",
  22. kOsAll, SINGLE_VALUE_TYPE("force-punycode-hostnames")},
  23. + {"fingerprinting-client-rects-noise",
  24. + "Enable get*ClientRects() fingerprint deception",
  25. + "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.",
  26. + kOsAll, SINGLE_VALUE_TYPE(switches::kFingerprintingClientRectsNoise)},
  27. + {"fingerprinting-canvas-measuretext-noise",
  28. + "Enable Canvas::measureText() fingerprint deception",
  29. + "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.",
  30. + kOsAll, SINGLE_VALUE_TYPE(switches::kFingerprintingCanvasMeasureTextNoise)},
  31. {"ignore-gpu-blacklist", flag_descriptions::kIgnoreGpuBlacklistName,
  32. flag_descriptions::kIgnoreGpuBlacklistDescription, kOsAll,
  33. SINGLE_VALUE_TYPE(switches::kIgnoreGpuBlacklist)},
  34. --- a/content/child/runtime_features.cc
  35. +++ b/content/child/runtime_features.cc
  36. @@ -22,6 +22,7 @@
  37. #include "services/network/public/cpp/features.h"
  38. #include "third_party/blink/public/common/features.h"
  39. #include "third_party/blink/public/platform/web_runtime_features.h"
  40. +#include "third_party/ungoogled/ungoogled_switches.h"
  41. #include "ui/base/ui_base_features.h"
  42. #include "ui/events/blink/blink_features.h"
  43. #include "ui/gfx/switches.h"
  44. @@ -462,6 +463,11 @@ void SetIndividualRuntimeFeatures(
  45. WebRuntimeFeatures::EnableForbidSyncXHRInPageDismissal(true);
  46. }
  47. + WebRuntimeFeatures::EnableFingerprintingClientRectsNoise(
  48. + command_line.HasSwitch(switches::kFingerprintingClientRectsNoise));
  49. + WebRuntimeFeatures::EnableFingerprintingCanvasMeasureTextNoise(
  50. + command_line.HasSwitch(switches::kFingerprintingCanvasMeasureTextNoise));
  51. +
  52. WebRuntimeFeatures::EnableAutoplayIgnoresWebAudio(
  53. base::FeatureList::IsEnabled(media::kAutoplayIgnoreWebAudio));
  54. --- a/third_party/blink/renderer/core/dom/document.cc
  55. +++ b/third_party/blink/renderer/core/dom/document.cc
  56. @@ -35,6 +35,7 @@
  57. #include "base/auto_reset.h"
  58. #include "base/macros.h"
  59. #include "base/optional.h"
  60. +#include "base/rand_util.h"
  61. #include "cc/input/overscroll_behavior.h"
  62. #include "cc/input/scroll_snap_data.h"
  63. #include "services/metrics/public/cpp/mojo_ukm_recorder.h"
  64. @@ -1131,6 +1132,15 @@ Document::Document(const DocumentInit& i
  65. #ifndef NDEBUG
  66. liveDocumentSet().insert(this);
  67. #endif
  68. +
  69. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  70. + // Precompute -0.0003% to 0.0003% noise factor for get*ClientRect*() fingerprinting
  71. + noise_factor_x_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  72. + noise_factor_y_ = 1 + (base::RandDouble() - 0.5) * 0.000003;
  73. + } else {
  74. + noise_factor_x_ = 1;
  75. + noise_factor_y_ = 1;
  76. + }
  77. }
  78. Document::~Document() {
  79. @@ -1159,6 +1169,14 @@ Range* Document::CreateRangeAdjustedToTr
  80. Position::BeforeNode(*shadow_host));
  81. }
  82. +double Document::GetNoiseFactorX() {
  83. + return noise_factor_x_;
  84. +}
  85. +
  86. +double Document::GetNoiseFactorY() {
  87. + return noise_factor_y_;
  88. +}
  89. +
  90. SelectorQueryCache& Document::GetSelectorQueryCache() {
  91. if (!selector_query_cache_)
  92. selector_query_cache_ = std::make_unique<SelectorQueryCache>();
  93. --- a/third_party/blink/renderer/core/dom/document.h
  94. +++ b/third_party/blink/renderer/core/dom/document.h
  95. @@ -398,6 +398,10 @@ class CORE_EXPORT Document : public Cont
  96. has_xml_declaration_ = has_xml_declaration ? 1 : 0;
  97. }
  98. + // Values for get*ClientRect fingerprint deception
  99. + double GetNoiseFactorX();
  100. + double GetNoiseFactorY();
  101. +
  102. String visibilityState() const;
  103. bool IsPageVisible() const;
  104. bool hidden() const;
  105. @@ -1884,6 +1888,9 @@ class CORE_EXPORT Document : public Cont
  106. base::ElapsedTimer start_time_;
  107. + double noise_factor_x_;
  108. + double noise_factor_y_;
  109. +
  110. Member<ScriptRunner> script_runner_;
  111. HeapVector<Member<ScriptElementBase>> current_script_stack_;
  112. --- a/third_party/blink/renderer/core/dom/element.cc
  113. +++ b/third_party/blink/renderer/core/dom/element.cc
  114. @@ -1370,6 +1370,11 @@ DOMRectList* Element::getClientRects() {
  115. DCHECK(element_layout_object);
  116. GetDocument().AdjustFloatQuadsForScrollAndAbsoluteZoom(
  117. quads, *element_layout_object);
  118. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  119. + for (FloatQuad& quad : quads) {
  120. + quad.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  121. + }
  122. + }
  123. return DOMRectList::Create(quads);
  124. }
  125. @@ -1387,6 +1392,9 @@ DOMRect* Element::getBoundingClientRect(
  126. DCHECK(element_layout_object);
  127. GetDocument().AdjustFloatRectForScrollAndAbsoluteZoom(result,
  128. *element_layout_object);
  129. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  130. + result.Scale(GetDocument().GetNoiseFactorX(), GetDocument().GetNoiseFactorY());
  131. + }
  132. return DOMRect::FromFloatRect(result);
  133. }
  134. --- a/third_party/blink/renderer/core/dom/range.cc
  135. +++ b/third_party/blink/renderer/core/dom/range.cc
  136. @@ -1631,11 +1631,21 @@ DOMRectList* Range::getClientRects() con
  137. Vector<FloatQuad> quads;
  138. GetBorderAndTextQuads(quads);
  139. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  140. + for (FloatQuad& quad : quads) {
  141. + quad.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  142. + }
  143. + }
  144. +
  145. return DOMRectList::Create(quads);
  146. }
  147. DOMRect* Range::getBoundingClientRect() const {
  148. - return DOMRect::FromFloatRect(BoundingRect());
  149. + auto rect = BoundingRect();
  150. + if (RuntimeEnabledFeatures::FingerprintingClientRectsNoiseEnabled()) {
  151. + rect.Scale(owner_document_->GetNoiseFactorX(), owner_document_->GetNoiseFactorY());
  152. + }
  153. + return DOMRect::FromFloatRect(rect);
  154. }
  155. // TODO(editing-dev): We should make
  156. --- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
  157. +++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
  158. @@ -637,6 +637,12 @@
  159. name: "FileSystem",
  160. status: "stable",
  161. },
  162. + {
  163. + name: "FingerprintingClientRectsNoise",
  164. + },
  165. + {
  166. + name: "FingerprintingCanvasMeasureTextNoise",
  167. + },
  168. // FirstContentfulPaintPlusPlus enables the Largest Text Paint metric, Last
  169. // Text Paint metric, Largest Image Paint metric and Last Image Paint
  170. // metric. See also: http://bit.ly/fcp_plus_plus
  171. --- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  172. +++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  173. @@ -699,4 +699,12 @@ void WebRuntimeFeatures::EnableMouseSubf
  174. RuntimeEnabledFeatures::SetMouseSubframeNoImplicitCaptureEnabled(enable);
  175. }
  176. +void WebRuntimeFeatures::EnableFingerprintingClientRectsNoise(bool enable) {
  177. + RuntimeEnabledFeatures::SetFingerprintingClientRectsNoiseEnabled(enable);
  178. +}
  179. +
  180. +void WebRuntimeFeatures::EnableFingerprintingCanvasMeasureTextNoise(bool enable) {
  181. + RuntimeEnabledFeatures::SetFingerprintingCanvasMeasureTextNoiseEnabled(enable);
  182. +}
  183. +
  184. } // namespace blink
  185. --- a/third_party/blink/public/platform/web_runtime_features.h
  186. +++ b/third_party/blink/public/platform/web_runtime_features.h
  187. @@ -228,6 +228,8 @@ class WebRuntimeFeatures {
  188. BLINK_PLATFORM_EXPORT static void EnableMergeBlockingNonBlockingPools(bool);
  189. BLINK_PLATFORM_EXPORT static void EnableGetDisplayMedia(bool);
  190. BLINK_PLATFORM_EXPORT static void EnableForbidSyncXHRInPageDismissal(bool);
  191. + BLINK_PLATFORM_EXPORT static void EnableFingerprintingClientRectsNoise(bool);
  192. + BLINK_PLATFORM_EXPORT static void EnableFingerprintingCanvasMeasureTextNoise(bool);
  193. BLINK_PLATFORM_EXPORT static void EnableShadowDOMV0(bool);
  194. BLINK_PLATFORM_EXPORT static void EnableCustomElementsV0(bool);
  195. BLINK_PLATFORM_EXPORT static void EnableHTMLImports(bool);
  196. --- a/chrome/browser/BUILD.gn
  197. +++ b/chrome/browser/BUILD.gn
  198. @@ -2156,6 +2156,7 @@ jumbo_split_static_library("browser") {
  199. "//third_party/metrics_proto",
  200. "//third_party/re2",
  201. "//third_party/smhasher:cityhash",
  202. + "//third_party/ungoogled:switches",
  203. "//third_party/webrtc_overrides",
  204. "//third_party/webrtc_overrides:init_webrtc",
  205. "//third_party/widevine/cdm:buildflags",
  206. --- a/content/browser/BUILD.gn
  207. +++ b/content/browser/BUILD.gn
  208. @@ -207,6 +207,7 @@ jumbo_source_set("browser") {
  209. "//third_party/libyuv",
  210. "//third_party/re2",
  211. "//third_party/sqlite",
  212. + "//third_party/ungoogled:switches",
  213. "//third_party/webrtc/modules/desktop_capture:primitives",
  214. "//third_party/webrtc/rtc_base:rtc_base",
  215. "//third_party/zlib",
  216. --- a/content/browser/renderer_host/render_process_host_impl.cc
  217. +++ b/content/browser/renderer_host/render_process_host_impl.cc
  218. @@ -214,6 +214,7 @@
  219. #include "third_party/blink/public/common/page/launching_process_state.h"
  220. #include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
  221. #include "third_party/blink/public/public_buildflags.h"
  222. +#include "third_party/ungoogled/ungoogled_switches.h"
  223. #include "third_party/skia/include/core/SkBitmap.h"
  224. #include "ui/accessibility/accessibility_switches.h"
  225. #include "ui/base/ui_base_switches.h"
  226. @@ -3001,6 +3002,8 @@ void RenderProcessHostImpl::PropagateBro
  227. switches::kEnableWebGLSwapChain,
  228. switches::kEnableWebVR,
  229. switches::kFileUrlPathAlias,
  230. + switches::kFingerprintingClientRectsNoise,
  231. + switches::kFingerprintingCanvasMeasureTextNoise,
  232. switches::kForceDisplayColorProfile,
  233. switches::kForceDeviceScaleFactor,
  234. switches::kForceGpuMemAvailableMb,
  235. --- a/content/child/BUILD.gn
  236. +++ b/content/child/BUILD.gn
  237. @@ -117,6 +117,7 @@ target(link_target_type, "child") {
  238. "//third_party/blink/public:scaled_resources",
  239. "//third_party/blink/public/common",
  240. "//third_party/ced",
  241. + "//third_party/ungoogled:switches",
  242. "//third_party/zlib/google:compression_utils",
  243. "//ui/base",
  244. "//ui/events/blink",
  245. --- a/third_party/blink/renderer/platform/BUILD.gn
  246. +++ b/third_party/blink/renderer/platform/BUILD.gn
  247. @@ -1426,6 +1426,7 @@ jumbo_component("platform") {
  248. "//third_party/ced",
  249. "//third_party/emoji-segmenter",
  250. "//third_party/icu",
  251. + "//third_party/ungoogled:switches",
  252. "//third_party/webrtc/p2p:rtc_p2p",
  253. "//third_party/webrtc_overrides:init_webrtc",
  254. "//third_party/zlib/google:compression_utils",
  255. --- a/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  256. +++ b/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  257. @@ -811,6 +811,12 @@ TextMetrics* CanvasRenderingContext2D::m
  258. TextMetrics* text_metrics = MakeGarbageCollected<TextMetrics>(
  259. font, direction, GetState().GetTextBaseline(), GetState().GetTextAlign(),
  260. text);
  261. +
  262. + // Scale text metrics if enabled
  263. + if (RuntimeEnabledFeatures::FingerprintingCanvasMeasureTextNoiseEnabled()) {
  264. + text_metrics->Shuffle(canvas()->GetDocument().GetNoiseFactorX());
  265. + }
  266. +
  267. base::TimeDelta elapsed = base::TimeTicks::Now() - start_time;
  268. base::UmaHistogramMicrosecondsTimesUnderTenMilliseconds(
  269. "Canvas.TextMetrics.MeasureText", elapsed);
  270. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.h
  271. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.h
  272. @@ -64,6 +64,8 @@ class CORE_EXPORT TextMetrics final : pu
  273. void Trace(Visitor*) override;
  274. + void Shuffle(const double factor);
  275. +
  276. private:
  277. void Update(const Font&,
  278. const TextDirection&,
  279. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  280. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  281. @@ -54,6 +54,24 @@ TextMetrics::TextMetrics(const Font& fon
  282. Update(font, direction, baseline, align, text);
  283. }
  284. +void TextMetrics::Shuffle(const double factor) {
  285. + // x-direction
  286. + width_ *= factor;
  287. + actual_bounding_box_left_ *= factor;
  288. + actual_bounding_box_right_ *= factor;
  289. +
  290. + // y-direction
  291. + font_bounding_box_ascent_ *= factor;
  292. + font_bounding_box_descent_ *= factor;
  293. + actual_bounding_box_ascent_ *= factor;
  294. + actual_bounding_box_descent_ *= factor;
  295. + em_height_ascent_ *= factor;
  296. + em_height_descent_ *= factor;
  297. + baselines_->setAlphabetic(baselines_->alphabetic() * factor);
  298. + baselines_->setHanging(baselines_->hanging() * factor);
  299. + baselines_->setIdeographic(baselines_->ideographic() * factor);
  300. +}
  301. +
  302. void TextMetrics::Update(const Font& font,
  303. const TextDirection& direction,
  304. const TextBaseline& baseline,