Commit 474ef9a0 authored by prathiksha's avatar prathiksha
Browse files

Bug 1561443 - Move _getErrorMessageFromCode from NetErrorChild.jsm to...

Bug 1561443 - Move _getErrorMessageFromCode from NetErrorChild.jsm to aboutNetError.js. r=fluent-reviewers,flod,johannh

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

--HG--
extra : moz-landing-system : lando
parent 3e3db51f
Loading
Loading
Loading
Loading
+0 −132
Original line number Diff line number Diff line
@@ -5,34 +5,10 @@

var EXPORTED_SYMBOLS = ["NetErrorChild"];

const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { ActorChild } = ChromeUtils.import(
  "resource://gre/modules/ActorChild.jsm"
);

XPCOMUtils.defineLazyGetter(this, "gPipNSSBundle", function() {
  return Services.strings.createBundle(
    "chrome://pipnss/locale/pipnss.properties"
  );
});
XPCOMUtils.defineLazyGetter(this, "gNSSErrorsBundle", function() {
  return Services.strings.createBundle(
    "chrome://pipnss/locale/nsserrors.properties"
  );
});

const SEC_ERROR_BASE = Ci.nsINSSErrorsService.NSS_SEC_ERROR_BASE;
const SEC_ERROR_REUSED_ISSUER_AND_SERIAL = SEC_ERROR_BASE + 138;

const SSL_ERROR_BASE = Ci.nsINSSErrorsService.NSS_SSL_ERROR_BASE;
const SSL_ERROR_SSL_DISABLED = SSL_ERROR_BASE + 20;
const SSL_ERROR_SSL2_DISABLED = SSL_ERROR_BASE + 14;

const PREF_SSL_IMPACT_ROOTS = ["security.tls.version.", "security.ssl3."];

function getSerializedSecurityInfo(docShell) {
  let serhelper = Cc["@mozilla.org/network/serialization-helper;1"].getService(
    Ci.nsISerializationHelper
@@ -60,9 +36,6 @@ class NetErrorChild extends ActorChild {
    let doc = aEvent.originalTarget.ownerDocument || aEvent.originalTarget;

    switch (aEvent.type) {
      case "AboutNetErrorLoad":
        this.onPageLoad(doc.defaultView);
        break;
      case "AboutNetErrorSetAutomatic":
        this.onSetAutomatic(aEvent);
        break;
@@ -87,111 +60,6 @@ class NetErrorChild extends ActorChild {
    }
  }

  changedCertPrefs() {
    let prefSSLImpact = PREF_SSL_IMPACT_ROOTS.reduce((prefs, root) => {
      return prefs.concat(Services.prefs.getChildList(root));
    }, []);
    for (let prefName of prefSSLImpact) {
      if (Services.prefs.prefHasUserValue(prefName)) {
        return true;
      }
    }

    return false;
  }

  _getErrorMessageFromCode(securityInfo, doc) {
    let uri = Services.io.newURI(doc.location);
    let hostString = uri.host;
    if (uri.port != 443 && uri.port != -1) {
      hostString = uri.hostPort;
    }

    let id_str = "";
    switch (securityInfo.errorCode) {
      case SSL_ERROR_SSL_DISABLED:
        id_str = "PSMERR_SSL_Disabled";
        break;
      case SSL_ERROR_SSL2_DISABLED:
        id_str = "PSMERR_SSL2_Disabled";
        break;
      case SEC_ERROR_REUSED_ISSUER_AND_SERIAL:
        id_str = "PSMERR_HostReusedIssuerSerial";
        break;
    }
    let nss_error_id_str = securityInfo.errorCodeString;
    let msg2 = "";
    try {
      if (id_str) {
        msg2 = gPipNSSBundle.GetStringFromName(id_str) + "\n";
      } else if (nss_error_id_str) {
        msg2 = gNSSErrorsBundle.GetStringFromName(nss_error_id_str) + "\n";
      }
    } catch (e) {
      msg2 = "";
    }

    if (!msg2) {
      // We couldn't get an error message. Use the error string.
      // Note that this is different from before where we used PR_ErrorToString.
      msg2 = nss_error_id_str;
    }
    let msg = gPipNSSBundle.formatStringFromName("SSLConnectionErrorPrefix2", [
      hostString,
      msg2,
    ]);

    if (nss_error_id_str && msg2 != nss_error_id_str) {
      msg +=
        gPipNSSBundle.formatStringFromName("certErrorCodePrefix3", [
          nss_error_id_str,
        ]) + "\n";
    }
    return msg;
  }

  onPageLoad(win) {
    // Values for telemtery bins: see TLS_ERROR_REPORT_UI in Histograms.json
    const TLS_ERROR_REPORT_TELEMETRY_UI_SHOWN = 0;

    if (this.isAboutNetError(win.document)) {
      let docShell = win.docShell;
      if (docShell) {
        let { securityInfo } = docShell.failedChannel;
        // We don't have a securityInfo when this is for example a DNS error.
        if (securityInfo) {
          securityInfo.QueryInterface(Ci.nsITransportSecurityInfo);
          let msg = this._getErrorMessageFromCode(securityInfo, win.document);
          let id = win.document.getElementById("errorShortDescText");
          id.textContent = msg;
        }
      }

      let learnMoreLink = win.document.getElementById("learnMoreLink");
      let baseURL = Services.urlFormatter.formatURLPref("app.support.baseURL");
      learnMoreLink.setAttribute("href", baseURL + "connection-not-secure");

      let automatic = Services.prefs.getBoolPref(
        "security.ssl.errorReporting.automatic"
      );
      win.dispatchEvent(
        new win.CustomEvent("AboutNetErrorOptions", {
          detail: JSON.stringify({
            enabled: Services.prefs.getBoolPref(
              "security.ssl.errorReporting.enabled"
            ),
            changedCertPrefs: this.changedCertPrefs(),
            automatic,
          }),
        })
      );

      this.mm.sendAsyncMessage("Browser:SSLErrorReportTelemetry", {
        reportStatus: TLS_ERROR_REPORT_TELEMETRY_UI_SHOWN,
      });
    }
  }

  onResetPreferences(evt) {
    this.mm.sendAsyncMessage("Browser:ResetSSLPreferences");
  }
+110 −72
Original line number Diff line number Diff line
@@ -251,58 +251,44 @@ function initPage() {
    document.getElementById("netErrorButtonContainer").style.display = "none";
  }

  window.addEventListener(
    "AboutNetErrorOptions",
    function(evt) {
  // Dispatch this event only for tests.
  let event = new CustomEvent("AboutNetErrorLoad", { bubbles: true });
  document.dispatchEvent(event);

  setNetErrorMessageFromCode();
  let learnMoreLink = document.getElementById("learnMoreLink");
  let baseURL = RPMGetFormatURLPref("app.support.baseURL");
  learnMoreLink.setAttribute("href", baseURL + "connection-not-secure");

  // Pinning errors are of type nssFailure2
      if (getErrorCode() == "nssFailure2") {
        let shortDesc = document.getElementById("errorShortDescText")
          .textContent;
        document.getElementById("learnMoreContainer").style.display = "block";
        var options = JSON.parse(evt.detail);
        if (options && options.enabled) {
          var checkbox = document.getElementById("automaticallyReportInFuture");
          showCertificateErrorReporting();
          if (options.automatic) {
            // set the checkbox
            checkbox.checked = true;
          }
  if (err == "nssFailure2") {
    setupErrorUI();

          checkbox.addEventListener("change", function(changeEvt) {
            var event = new CustomEvent("AboutNetErrorSetAutomatic", {
              bubbles: true,
              detail: changeEvt.target.checked,
            });
            document.dispatchEvent(event);
          });
        }
        const hasPrefStyleError = [
    RPMAddMessageListener("HasChangedCertPrefs", msg => {
      let hasChangedCertPrefs = msg.data.hasChangedCertPrefs;

      let errorCode = document.getNetErrorInfo().errorCodeString;
      let hasPrefStyleError = [
        "interrupted", // This happens with subresources that are above the max tls
        "SSL_ERROR_PROTOCOL_VERSION_ALERT",
        "SSL_ERROR_UNSUPPORTED_VERSION",
        "SSL_ERROR_NO_CYPHER_OVERLAP",
        "SSL_ERROR_NO_CIPHERS_SUPPORTED",
        ].some(substring => shortDesc.includes(substring));
      ].some(substring => {
        return substring == errorCode;
      });

      // If it looks like an error that is user config based
        if (
          getErrorCode() == "nssFailure2" &&
          hasPrefStyleError &&
          options &&
          options.changedCertPrefs
        ) {
      if (hasPrefStyleError && hasChangedCertPrefs) {
        showPrefChangeContainer();
      }
    });
    RPMSendAsyncMessage("GetChangedCertPrefs");
  }
      if (getErrorCode() == "sslv3Used") {

  if (err == "sslv3Used") {
    document.getElementById("advancedButton").style.display = "none";
  }
    },
    true,
    true
  );

  var event = new CustomEvent("AboutNetErrorLoad", { bubbles: true });
  document.dispatchEvent(event);

  if (err == "inadequateSecurityError" || err == "blockedByPolicy") {
    // Remove the "Try again" button from pages that don't need it.
@@ -317,6 +303,80 @@ function initPage() {
  }
}

function setupErrorUI() {
  document.getElementById("learnMoreContainer").style.display = "block";

  let checkbox = document.getElementById("automaticallyReportInFuture");
  checkbox.addEventListener("change", function({ target: { checked } }) {
    document.dispatchEvent(
      new CustomEvent("AboutNetErrorSetAutomatic", {
        detail: checked,
        bubbles: true,
      })
    );
  });

  let errorReportingEnabled = RPMGetBoolPref(
    "security.ssl.errorReporting.enabled"
  );
  if (errorReportingEnabled) {
    showCertificateErrorReporting();
    let errorReportingAutomatic = RPMGetBoolPref(
      "security.ssl.errorReporting.automatic"
    );
    checkbox.checked = !!errorReportingAutomatic;
  }

  // Values for telemtery bins: see TLS_ERROR_REPORT_UI in Histograms.json
  const TLS_ERROR_REPORT_TELEMETRY_UI_SHOWN = 0;
  RPMSendAsyncMessage("Browser:SSLErrorReportTelemetry", {
    reportStatus: TLS_ERROR_REPORT_TELEMETRY_UI_SHOWN,
  });
}

async function setNetErrorMessageFromCode() {
  let hostString = document.location.hostname;
  let port = document.location.port;
  if (port && port != 443) {
    hostString += ":" + port;
  }

  let securityInfo;
  try {
    securityInfo = document.getNetErrorInfo();
  } catch (ex) {
    // We don't have a securityInfo when this is for example a DNS error.
    return;
  }

  let desc = document.getElementById("errorShortDescText");
  let errorCodeStr = securityInfo.errorCodeString;
  try {
    let [errorCodeMsg] = await document.l10n.formatValues([
      {
        id: errorCodeStr
          .split("_")
          .join("-")
          .toLowerCase(),
      },
    ]);
    document.l10n.setAttributes(desc, "ssl-connection-error", {
      errorMessage: errorCodeMsg,
      hostname: hostString,
    });
    let desc2 = document.getElementById("errorShortDescText2");
    document.l10n.setAttributes(desc2, "cert-error-code-prefix", {
      error: errorCodeStr,
    });
  } catch (e) {
    console.error("No strings exist for this error type");
    document.l10n.setAttributes(desc, "ssl-connection-error", {
      errorMsg: errorCodeStr,
      hostname: hostString,
    });
  }
}

// This function centers the error container after its content updates.
// It is currently duplicated in NetErrorChild.jsm to avoid having to do
// async communication to the page that would result in flicker.
@@ -365,30 +425,8 @@ function initPageCertError() {

  addAutofocus("#returnButton");
  setupAdvancedButton();
  setupErrorUI();

  document.getElementById("learnMoreContainer").style.display = "block";

  let checkbox = document.getElementById("automaticallyReportInFuture");
  checkbox.addEventListener("change", function({ target: { checked } }) {
    document.dispatchEvent(
      new CustomEvent("AboutNetErrorSetAutomatic", {
        detail: checked,
        bubbles: true,
      })
    );
  });

  let errorReportingEnabled = RPMGetBoolPref(
    "security.ssl.errorReporting.enabled"
  );
  if (errorReportingEnabled) {
    document.getElementById("certificateErrorReporting").style.display =
      "block";
    let errorReportingAutomatic = RPMGetBoolPref(
      "security.ssl.errorReporting.automatic"
    );
    checkbox.checked = !!errorReportingAutomatic;
  }
  let hideAddExceptionButton = RPMGetBoolPref(
    "security.certerror.hideAddException",
    false
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
         toolkit/components/places/src/nsFaviconService.h should be updated. -->
    <link rel="icon" id="favicon" href="chrome://global/skin/icons/warning.svg"/>
    <link rel="localization" href="browser/aboutCertError.ftl" />
    <link rel="localization" href="browser/nsserrors.ftl" />
    <link rel="localization" href="branding/brand.ftl"/>
  </head>

+0 −8
Original line number Diff line number Diff line
@@ -3559,7 +3559,6 @@ var BrowserOnClick = {
    mm.addMessageListener("Browser:SiteBlockedError", this);
    mm.addMessageListener("Browser:SetSSLErrorReportAuto", this);
    mm.addMessageListener("Browser:ResetSSLPreferences", this);
    mm.addMessageListener("Browser:SSLErrorReportTelemetry", this);
  },

  uninit() {
@@ -3568,7 +3567,6 @@ var BrowserOnClick = {
    mm.removeMessageListener("Browser:SiteBlockedError", this);
    mm.removeMessageListener("Browser:SetSSLErrorReportAuto", this);
    mm.removeMessageListener("Browser:ResetSSLPreferences", this);
    mm.removeMessageListener("Browser:SSLErrorReportTelemetry", this);
  },

  receiveMessage(msg) {
@@ -3610,12 +3608,6 @@ var BrowserOnClick = {
        }
        Services.telemetry.getHistogramById("TLS_ERROR_REPORT_UI").add(bin);
        break;
      case "Browser:SSLErrorReportTelemetry":
        let reportStatus = msg.data.reportStatus;
        Services.telemetry
          .getHistogramById("TLS_ERROR_REPORT_UI")
          .add(reportStatus);
        break;
    }
  },

+1 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ var successfulPinningPageListener = {
// to load the pinning domain again, this time removing the pinning information
function errorPageLoaded() {
  ContentTask.spawn(gBrowser.selectedBrowser, null, async function() {
    let textElement = content.document.getElementById("errorShortDescText");
    let textElement = content.document.getElementById("errorShortDescText2");
    let text = textElement.innerHTML;
    ok(
      text.indexOf("MOZILLA_PKIX_ERROR_KEY_PINNING_FAILURE") > 0,
Loading