getClientRects-getBoundingClientRect-measureText-add-fingerprinting-mitigation.patch 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. From: csagan5 <32685696+csagan5@users.noreply.github.com>
  2. Date: Fri, 30 Mar 2018 10:09:03 +0200
  3. Subject: getClientRects, getBoundingClientRect, measureText: add
  4. fingerprinting mitigation
  5. Scale the result of Range::getClientRects, Element::getBoundingClientRect and
  6. Canvas::measureText by a random +/-3/1000000th of the original value for each
  7. float in the returned Rect/Quad.
  8. Rationale is that the returned values are within the same order of magnitude
  9. of the floating point precision being used for fingerprinting and sufficient
  10. to poison the well.
  11. See also: http://www.gsd.inesc-id.pt/~mpc/pubs/fingerprinting-trustcom2016.pdf
  12. ---
  13. third_party/blink/renderer/core/dom/document.cc | 12 ++++++++++++
  14. third_party/blink/renderer/core/dom/document.h | 5 +++++
  15. third_party/blink/renderer/core/dom/element.cc | 16 ++++++++++++++++
  16. third_party/blink/renderer/core/dom/range.cc | 18 +++++++++++++++++-
  17. .../blink/renderer/core/html/canvas/text_metrics.cc | 18 ++++++++++++++++++
  18. .../blink/renderer/core/html/canvas/text_metrics.h | 2 ++
  19. .../canvas/canvas2d/canvas_rendering_context_2d.cc | 6 ++++++
  20. 7 files changed, 76 insertions(+), 1 deletion(-)
  21. diff --git a/third_party/blink/renderer/core/dom/document.cc b/third_party/blink/renderer/core/dom/document.cc
  22. --- a/third_party/blink/renderer/core/dom/document.cc
  23. +++ b/third_party/blink/renderer/core/dom/document.cc
  24. @@ -1102,6 +1102,10 @@ Document::Document(const DocumentInit& initializer,
  25. // also depend on the url NOT getting immediately set in opened windows.
  26. // See fast/dom/early-frame-url.html
  27. // and fast/dom/location-new-window-no-crash.html, respectively.
  28. + // add X/Y noise factors that will be used to mitigate fingerprinting
  29. + shuffleFactorX_ = base::RandDouble();
  30. + shuffleFactorY_ = base::RandDouble();
  31. +
  32. // FIXME: Can/should we unify this behavior?
  33. if (initializer.ShouldSetURL()) {
  34. SetURL(initializer.Url());
  35. @@ -1162,6 +1166,14 @@ Range* Document::CreateRangeAdjustedToTreeScope(const TreeScope& tree_scope,
  36. Position::BeforeNode(*shadow_host));
  37. }
  38. +double Document::GetShuffleFactorX() {
  39. + return shuffleFactorX_;
  40. +}
  41. +
  42. +double Document::GetShuffleFactorY() {
  43. + return shuffleFactorY_;
  44. +}
  45. +
  46. SelectorQueryCache& Document::GetSelectorQueryCache() {
  47. if (!selector_query_cache_)
  48. selector_query_cache_ = std::make_unique<SelectorQueryCache>();
  49. diff --git a/third_party/blink/renderer/core/dom/document.h b/third_party/blink/renderer/core/dom/document.h
  50. --- a/third_party/blink/renderer/core/dom/document.h
  51. +++ b/third_party/blink/renderer/core/dom/document.h
  52. @@ -398,6 +398,9 @@ class CORE_EXPORT Document : public ContainerNode,
  53. has_xml_declaration_ = has_xml_declaration ? 1 : 0;
  54. }
  55. + double GetShuffleFactorX();
  56. + double GetShuffleFactorY();
  57. +
  58. String visibilityState() const;
  59. bool IsPageVisible() const;
  60. bool hidden() const;
  61. @@ -1871,6 +1874,8 @@ class CORE_EXPORT Document : public ContainerNode,
  62. Vector<AXContext*> ax_contexts_;
  63. Member<AXObjectCache> ax_object_cache_;
  64. Member<DocumentMarkerController> markers_;
  65. + double shuffleFactorX_, shuffleFactorY_;
  66. +
  67. bool update_focus_appearance_after_layout_ = false;
  68. diff --git a/third_party/blink/renderer/core/dom/element.cc b/third_party/blink/renderer/core/dom/element.cc
  69. --- a/third_party/blink/renderer/core/dom/element.cc
  70. +++ b/third_party/blink/renderer/core/dom/element.cc
  71. @@ -1370,6 +1370,15 @@ DOMRectList* Element::getClientRects() {
  72. DCHECK(element_layout_object);
  73. GetDocument().AdjustFloatQuadsForScrollAndAbsoluteZoom(
  74. quads, *element_layout_object);
  75. +
  76. + // scale all quads
  77. + auto shuffleX = 1 + (GetDocument().GetShuffleFactorX() - 0.5) * 0.000003;
  78. + auto shuffleY = 1 + (GetDocument().GetShuffleFactorY() - 0.5) * 0.000003;
  79. +
  80. + for (FloatQuad& quad : quads) {
  81. + quad.Scale(shuffleX, shuffleY);
  82. + }
  83. +
  84. return DOMRectList::Create(quads);
  85. }
  86. @@ -1387,6 +1396,13 @@ DOMRect* Element::getBoundingClientRect() {
  87. DCHECK(element_layout_object);
  88. GetDocument().AdjustFloatRectForScrollAndAbsoluteZoom(result,
  89. *element_layout_object);
  90. +
  91. + // scale rect by 3/1000000th
  92. + auto shuffleX = 1 + (GetDocument().GetShuffleFactorX() - 0.5) * 0.000003;
  93. + auto shuffleY = 1 + (GetDocument().GetShuffleFactorY() - 0.5) * 0.000003;
  94. +
  95. + result.Scale(shuffleX, shuffleY);
  96. +
  97. return DOMRect::FromFloatRect(result);
  98. }
  99. diff --git a/third_party/blink/renderer/core/dom/range.cc b/third_party/blink/renderer/core/dom/range.cc
  100. --- a/third_party/blink/renderer/core/dom/range.cc
  101. +++ b/third_party/blink/renderer/core/dom/range.cc
  102. @@ -1631,11 +1631,27 @@ DOMRectList* Range::getClientRects() const {
  103. Vector<FloatQuad> quads;
  104. GetBorderAndTextQuads(quads);
  105. + // scale all quads by 3/1000000th
  106. + auto shuffleX = 1 + (owner_document_->GetShuffleFactorX() - 0.5) * 0.000003;
  107. + auto shuffleY = 1 + (owner_document_->GetShuffleFactorY() - 0.5) * 0.000003;
  108. +
  109. + for (FloatQuad& quad : quads) {
  110. + quad.Scale(shuffleX, shuffleY);
  111. + }
  112. +
  113. return DOMRectList::Create(quads);
  114. }
  115. DOMRect* Range::getBoundingClientRect() const {
  116. - return DOMRect::FromFloatRect(BoundingRect());
  117. + auto rect = BoundingRect();
  118. +
  119. + // scale rect by 3/1000000th
  120. + auto shuffleX = 1 + (owner_document_->GetShuffleFactorX() - 0.5) * 0.000003;
  121. + auto shuffleY = 1 + (owner_document_->GetShuffleFactorY() - 0.5) * 0.000003;
  122. +
  123. + rect.Scale(shuffleX, shuffleY);
  124. +
  125. + return DOMRect::FromFloatRect(rect);
  126. }
  127. // TODO(editing-dev): We should make
  128. diff --git a/third_party/blink/renderer/core/html/canvas/text_metrics.cc b/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  129. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  130. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.cc
  131. @@ -54,6 +54,24 @@ TextMetrics::TextMetrics(const Font& font,
  132. Update(font, direction, baseline, align, text);
  133. }
  134. +void TextMetrics::Shuffle(const double factor) {
  135. + // x-direction
  136. + width_ *= factor;
  137. + actual_bounding_box_left_ *= factor;
  138. + actual_bounding_box_right_ *= factor;
  139. +
  140. + // y-direction
  141. + font_bounding_box_ascent_ *= factor;
  142. + font_bounding_box_descent_ *= factor;
  143. + actual_bounding_box_ascent_ *= factor;
  144. + actual_bounding_box_descent_ *= factor;
  145. + em_height_ascent_ *= factor;
  146. + em_height_descent_ *= factor;
  147. + baselines_->setAlphabetic(baselines_->alphabetic() * factor);
  148. + baselines_->setHanging(baselines_->hanging() * factor);
  149. + baselines_->setIdeographic(baselines_->ideographic() * factor);
  150. +}
  151. +
  152. void TextMetrics::Update(const Font& font,
  153. const TextDirection& direction,
  154. const TextBaseline& baseline,
  155. diff --git a/third_party/blink/renderer/core/html/canvas/text_metrics.h b/third_party/blink/renderer/core/html/canvas/text_metrics.h
  156. --- a/third_party/blink/renderer/core/html/canvas/text_metrics.h
  157. +++ b/third_party/blink/renderer/core/html/canvas/text_metrics.h
  158. @@ -64,6 +64,8 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable {
  159. void Trace(Visitor*) override;
  160. + void Shuffle(const double factor);
  161. +
  162. private:
  163. void Update(const Font&,
  164. const TextDirection&,
  165. diff --git a/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc b/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  166. --- a/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  167. +++ b/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.cc
  168. @@ -33,6 +33,7 @@
  169. #include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.h"
  170. +#include "base/rand_util.h"
  171. #include "base/metrics/histogram_functions.h"
  172. #include "third_party/blink/public/common/features.h"
  173. #include "third_party/blink/public/platform/platform.h"
  174. @@ -814,6 +815,11 @@ TextMetrics* CanvasRenderingContext2D::measureText(const String& text) {
  175. base::TimeDelta elapsed = base::TimeTicks::Now() - start_time;
  176. base::UmaHistogramMicrosecondsTimesUnderTenMilliseconds(
  177. "Canvas.TextMetrics.MeasureText", elapsed);
  178. +
  179. + // scale text metrics by 3/1000000th
  180. + auto shuffleFactor = 1 + (base::RandDouble() - 0.5) * 0.000003;
  181. + text_metrics->Shuffle(shuffleFactor);
  182. +
  183. return text_metrics;
  184. }
  185. --
  186. 2.11.0