Verified Commit d3b88f8a authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

Bug 41116: Normalize system fonts.

System fonts are an enormous fingerprinting vector.
Even with font allow lists and with our custom configuration on Linux,
which counter metrics measurements, getComputedStyle leaks several
details.
This patch counters both these kinds of attacks.
parent 66b93b7e
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -1901,7 +1901,8 @@ static void GetSystemUIFontFamilies([[maybe_unused]] nsAtom* aLangGroup,
  nsFont systemFont;
  gfxFontStyle fontStyle;
  nsAutoString systemFontName;
  if (!LookAndFeel::GetFont(StyleSystemFont::Menu, systemFontName, fontStyle)) {
  if (!LookAndFeel::GetFont(StyleSystemFont::Menu, systemFontName, fontStyle,
                            nsContentUtils::ShouldResistFingerprinting())) {
    return;
  }
  systemFontName.Trim("\"'");
+4 −1
Original line number Diff line number Diff line
@@ -9564,7 +9564,10 @@ void nsLayoutUtils::ComputeSystemFont(nsFont* aSystemFont,
                                      const Document* aDocument) {
  gfxFontStyle fontStyle;
  nsAutoString systemFontName;
  if (!LookAndFeel::GetFont(aFontID, systemFontName, fontStyle)) {
  const bool rfp = aDocument
                       ? aDocument->ShouldResistFingerprinting()
                       : nsContentUtils::ShouldResistFingerprinting();
  if (!LookAndFeel::GetFont(aFontID, systemFontName, fontStyle, rfp)) {
    return;
  }
  systemFontName.Trim("\"'");
+3 −1
Original line number Diff line number Diff line
@@ -486,8 +486,10 @@ class LookAndFeel {
   * @param aID    Which system-theme font is wanted.
   * @param aName  The name of the font to use.
   * @param aStyle Styling to apply to the font.
   * @param aRFP   Whether RFP is enabled in the caller's context
   */
  static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
  static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle,
                      bool aRFP);

  /**
   * GetPasswordCharacter() returns a unicode character which should be used
+5 −2
Original line number Diff line number Diff line
@@ -1369,11 +1369,14 @@ nsNativeThemeGTK::GetMinimumWidgetSize(nsPresContext* aPresContext,
      // smaller than the default one. This prevents <input type=text
      // style="font-size: .5em"> from keeping a ridiculously large size, for
      // example.
      const gfxFloat fieldFontSizeInCSSPixels = [] {
      const gfxFloat fieldFontSizeInCSSPixels = [aPresContext] {
        const bool rfp = (aPresContext && aPresContext->Document())
                             ? aPresContext->Document()->ShouldResistFingerprinting()
                             : nsContentUtils::ShouldResistFingerprinting();
        gfxFontStyle fieldFontStyle;
        nsAutoString unusedFontName;
        DebugOnly<bool> result = LookAndFeel::GetFont(
            LookAndFeel::FontID::MozField, unusedFontName, fieldFontStyle);
            LookAndFeel::FontID::MozField, unusedFontName, fieldFontStyle, rfp);
        MOZ_ASSERT(result, "GTK look and feel supports the field font");
        // NOTE: GetFont returns font sizes in CSS pixels, and we want just
        // that.
+15 −3
Original line number Diff line number Diff line
@@ -999,7 +999,18 @@ widget::LookAndFeelFont nsXPLookAndFeel::StyleToLookAndFeelFont(
}

bool nsXPLookAndFeel::GetFontValue(FontID aID, nsString& aName,
                                   gfxFontStyle& aStyle) {
                                   gfxFontStyle& aStyle, bool aRFP) {
  if (aRFP) {
#ifdef XP_MACOSX
    aName = u"-apple-system"_ns;
#else
    aName = u"sans-serif"_ns;
#endif
    aStyle = gfxFontStyle();
    aStyle.size = 12;
    return true;
  }

  if (const LookAndFeelFont* cached = sFontCache.Get(aID)) {
    return LookAndFeelFontToStyle(*cached, aName, aStyle);
  }
@@ -1350,8 +1361,9 @@ nsresult LookAndFeel::GetFloat(FloatID aID, float* aResult) {
}

// static
bool LookAndFeel::GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle) {
  return nsLookAndFeel::GetInstance()->GetFontValue(aID, aName, aStyle);
bool LookAndFeel::GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle,
                          bool aRFP) {
  return nsLookAndFeel::GetInstance()->GetFontValue(aID, aName, aStyle, aRFP);
}

// static
Loading