From fd41a60d42de2575c231315018ad1ed5f5432b6f Mon Sep 17 00:00:00 2001
From: igt0 <igt0-tor@gmail.com>
Date: Sat, 16 Sep 2017 16:50:17 -0300
Subject: [PATCH] bug 23104 - Add a default line height compensation

Many fonts have issues with their vertical metrics. they
are used to influence the height of ascenders and depth
of descenders. Gecko uses it to calculate the line height
(font height + ascender + descender), however because of
that idiosyncratic behavior across multiple operating
systems, it can be used to identify the user's OS.

The solution proposed in the patch uses a default factor
to be multiplied with the font size, simulating the concept
of ascender and descender. This way all operating
systems will have the same line height.
---
 layout/generic/ReflowInput.cpp   |  6 +++++
 tbb-tests/mochitest.ini          |  1 +
 tbb-tests/test_tor_bug23104.html | 54 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 tbb-tests/test_tor_bug23104.html

diff --git a/layout/generic/ReflowInput.cpp b/layout/generic/ReflowInput.cpp
index 42f4a24..e219ae0 100644
--- a/layout/generic/ReflowInput.cpp
+++ b/layout/generic/ReflowInput.cpp
@@ -30,6 +30,7 @@
 #include "CounterStyleManager.h"
 #include <algorithm>
 #include "mozilla/dom/HTMLInputElement.h"
+#include "nsContentUtils.h"
 
 #ifdef DEBUG
 #undef NOISY_VERTICAL_ALIGN
@@ -2768,6 +2769,11 @@ GetNormalLineHeight(nsFontMetrics* aFontMetrics)
   nscoord externalLeading = aFontMetrics->ExternalLeading();
   nscoord internalLeading = aFontMetrics->InternalLeading();
   nscoord emHeight = aFontMetrics->EmHeight();
+
+  if (nsContentUtils::ShouldResistFingerprinting()) {
+    return NSToCoordRound(emHeight * NORMAL_LINE_HEIGHT_FACTOR);
+  }
+
   switch (GetNormalLineHeightCalcControl()) {
   case eIncludeExternalLeading:
     normalLineHeight = emHeight+ internalLeading + externalLeading;
diff --git a/tbb-tests/mochitest.ini b/tbb-tests/mochitest.ini
index 3d2e7c9..fca972a 100644
--- a/tbb-tests/mochitest.ini
+++ b/tbb-tests/mochitest.ini
@@ -10,3 +10,4 @@ support-files =
 [test_tor_bug2875.html]
 [test_tor_bug4755.html]
 [test_tor_bug5856.html]
+[test_tor_bug23104.html]
diff --git a/tbb-tests/test_tor_bug23104.html b/tbb-tests/test_tor_bug23104.html
new file mode 100644
index 0000000..9146ed1
--- /dev/null
+++ b/tbb-tests/test_tor_bug23104.html
@@ -0,0 +1,54 @@
+<!DOCTYPE HTML>
+<meta charset="UTF-8">
+<html>
+<head>
+  <title>Test for Tor Bug #23104: CSS line-height reveals the platform Tor browser is running</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <style type="text/css">
+    span {
+      background-color: #000;
+      color: #fff;
+      font-size: 16.5px;
+    }
+  </style>
+</head>
+<body>
+<span id="test1">Test1</span>
+<span id="test2">كلمة</span>
+<span id="test3">ação</span>
+<script type="application/javascript;version=1.7">
+
+let setPref = function* (key, value) {
+  return new Promise(function(resolve, reject) {
+    SpecialPowers.pushPrefEnv({"set": [[key, value]]}, resolve);
+  });
+}
+
+function getStyle(el, styleprop) {
+  el = document.getElementById(el);
+  return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleprop);
+}
+
+
+function validateElement(elementName, isFingerprintResistent) {
+  var fontSize = getStyle(elementName, 'font-size');
+  var lineHeight = getStyle(elementName, 'line-height');
+  console.log(parseFloat(lineHeight) === Math.ceil(parseFloat(fontSize)) * 1.2);
+  var validationCb = isFingerprintResistent ? is : isnot;
+  validationCb(parseFloat(lineHeight), Math.ceil(parseFloat(fontSize)) * 1.2, 'Line Height validation');
+}
+
+add_task(function* () {
+  for (let resistFingerprintingValue of [true, false]) {
+  yield setPref("privacy.resistFingerprinting", resistFingerprintingValue);
+    for (let elementId of ['test1', 'test2', 'test3']) {
+      validateElement(elementId, resistFingerprintingValue);
+    }
+  }
+});
+
+</script>
+</body>
+</html>
+
-- 
2.7.4

