Commit 50e67cd4 authored by Alex Catarineu's avatar Alex Catarineu Committed by Matthew Finkel
Browse files

Bug 1561322 - Allow spoofing strings in HTML forms r=baku,Pike

When privacy.spoof_english = 2, we should hide the user's
locale in content. So we use en-US default strings for HTML
form elements, such as a Submit button.

We also force GetLocalizedEllipsis() to always return the
ellipsis used by en-US.

Differential Revision: https://phabricator.services.mozilla.com/D35815

--HG--
extra : moz-landing-system : lando
parent 632a6b67
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -344,6 +344,7 @@
@RESPATH@/res/fonts/*
@RESPATH@/res/dtd/*
@RESPATH@/res/language.properties
@RESPATH@/res/locale/layout/HtmlForm.properties
#ifdef XP_MACOSX
@RESPATH@/res/MainMenu.nib/
#endif
+28 −7
Original line number Diff line number Diff line
@@ -3514,7 +3514,7 @@ void nsContentUtils::GetEventArgNames(int32_t aNameSpaceID, nsAtom* aEventName,

// Note: The list of content bundles in nsStringBundle.cpp should be updated
// whenever entries are added or removed from this list.
static const char gPropertiesFiles[nsContentUtils::PropertiesFile_COUNT][56] = {
static const char* gPropertiesFiles[nsContentUtils::PropertiesFile_COUNT] = {
    // Must line up with the enum values in |PropertiesFile| enum.
    "chrome://global/locale/css.properties",
    "chrome://global/locale/xbl.properties",
@@ -3529,7 +3529,9 @@ static const char gPropertiesFiles[nsContentUtils::PropertiesFile_COUNT][56] = {
    "chrome://global/locale/commonDialogs.properties",
    "chrome://global/locale/mathml/mathml.properties",
    "chrome://global/locale/security/security.properties",
    "chrome://necko/locale/necko.properties"};
    "chrome://necko/locale/necko.properties",
    "chrome://global/locale/layout/HtmlForm.properties",
    "resource://gre/res/locale/layout/HtmlForm.properties"};

/* static */
nsresult nsContentUtils::EnsureStringBundle(PropertiesFile aFile) {
@@ -3578,10 +3580,22 @@ void nsContentUtils::AsyncPrecreateStringBundles() {
  }
}

static bool SpoofLocaleEnglish() {
  // 0 - will prompt
  // 1 - don't spoof
  // 2 - spoof
  return StaticPrefs::privacy_spoof_english() == 2;
}

/* static */
nsresult nsContentUtils::GetLocalizedString(PropertiesFile aFile,
                                            const char* aKey,
                                            nsAString& aResult) {
  // When we spoof English, use en-US default strings in HTML forms.
  if (aFile == eFORMS_PROPERTIES_MAYBESPOOF && SpoofLocaleEnglish()) {
    aFile = eFORMS_PROPERTIES_en_US;
  }

  nsresult rv = EnsureStringBundle(aFile);
  NS_ENSURE_SUCCESS(rv, rv);
  nsIStringBundle* bundle = sStringBundles[aFile];
@@ -3594,6 +3608,11 @@ nsresult nsContentUtils::FormatLocalizedString(PropertiesFile aFile,
                                               const char16_t** aParams,
                                               uint32_t aParamsLength,
                                               nsAString& aResult) {
  // When we spoof English, use en-US default strings in HTML forms.
  if (aFile == eFORMS_PROPERTIES_MAYBESPOOF && SpoofLocaleEnglish()) {
    aFile = eFORMS_PROPERTIES_en_US;
  }

  nsresult rv = EnsureStringBundle(aFile);
  NS_ENSURE_SUCCESS(rv, rv);
  nsIStringBundle* bundle = sStringBundles[aFile];
@@ -5125,11 +5144,13 @@ nsIWidget* nsContentUtils::GetTopLevelWidget(nsIWidget* aWidget) {
const nsDependentString nsContentUtils::GetLocalizedEllipsis() {
  static char16_t sBuf[4] = {0, 0, 0, 0};
  if (!sBuf[0]) {
    if (!SpoofLocaleEnglish()) {
      nsAutoString tmp;
      Preferences::GetLocalizedString("intl.ellipsis", tmp);
      uint32_t len =
          std::min(uint32_t(tmp.Length()), uint32_t(ArrayLength(sBuf) - 1));
      CopyUnicodeTo(tmp, 0, sBuf, len);
    }
    if (!sBuf[0]) sBuf[0] = char16_t(0x2026);
  }
  return nsDependentString(sBuf);
+2 −0
Original line number Diff line number Diff line
@@ -1117,6 +1117,8 @@ class nsContentUtils {
    eMATHML_PROPERTIES,
    eSECURITY_PROPERTIES,
    eNECKO_PROPERTIES,
    eFORMS_PROPERTIES_MAYBESPOOF,
    eFORMS_PROPERTIES_en_US,
    PropertiesFile_COUNT
  };
  static nsresult ReportToConsole(
+20 −17
Original line number Diff line number Diff line
@@ -725,15 +725,15 @@ nsresult HTMLInputElement::InitFilePicker(FilePickerType aType) {
  nsAutoString title;
  nsAutoString okButtonLabel;
  if (aType == FILE_PICKER_DIRECTORY) {
    nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                       "DirectoryUpload", title);
    nsContentUtils::GetLocalizedString(
        nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "DirectoryUpload", title);

    nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                       "DirectoryPickerOkButtonLabel",
                                       okButtonLabel);
    nsContentUtils::GetLocalizedString(
        nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF,
        "DirectoryPickerOkButtonLabel", okButtonLabel);
  } else {
    nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                       "FileUpload", title);
    nsContentUtils::GetLocalizedString(
        nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "FileUpload", title);
  }

  nsCOMPtr<nsIFilePicker> filePicker =
@@ -2339,22 +2339,25 @@ void HTMLInputElement::GetDisplayFileName(nsAString& aValue) const {
    if ((IsDirPickerEnabled() && Allowdirs()) ||
        (StaticPrefs::dom_webkitBlink_dirPicker_enabled() &&
         HasAttr(kNameSpaceID_None, nsGkAtoms::webkitdirectory))) {
      nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                         "NoDirSelected", value);
      nsContentUtils::GetLocalizedString(
          nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "NoDirSelected", value);
    } else if (HasAttr(kNameSpaceID_None, nsGkAtoms::multiple)) {
      nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                         "NoFilesSelected", value);
      nsContentUtils::GetLocalizedString(
          nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "NoFilesSelected",
          value);
    } else {
      nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                         "NoFileSelected", value);
      nsContentUtils::GetLocalizedString(
          nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "NoFileSelected",
          value);
    }
  } else {
    nsString count;
    count.AppendInt(int(mFileData->mFilesOrDirectories.Length()));

    const char16_t* params[] = {count.get()};
    nsContentUtils::FormatLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                          "XFilesSelected", params, value);
    nsContentUtils::FormatLocalizedString(
        nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "XFilesSelected", params,
        value);
  }

  aValue = value;
@@ -5825,8 +5828,8 @@ HTMLInputElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) {
      !HasAttr(kNameSpaceID_None, nsGkAtoms::value)) {
    // Get our default value, which is the same as our default label
    nsAutoString defaultValue;
    nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                       "Submit", defaultValue);
    nsContentUtils::GetLocalizedString(
        nsContentUtils::eFORMS_PROPERTIES_MAYBESPOOF, "Submit", defaultValue);
    value = defaultValue;
  }

+4 −0
Original line number Diff line number Diff line
@@ -59,3 +59,7 @@ with Files("en-US/chrome/plugins.properties"):
    BUG_COMPONENT = ("Core", "Plug-ins")

JAR_MANIFESTS += ['jar.mn']

RESOURCE_FILES.locale.layout += [
    'en-US/chrome/layout/HtmlForm.properties',
]
Loading