Bug 1453123: Have CertUtils export a CertUtils object. r=rhelmer
authorKris Maglione <maglione.k@gmail.com>
Tue, 10 Apr 2018 13:23:35 -0700
changeset 781500 5800890780bc9d72194621b24d8a26c00497f763
parent 781499 ff301ae67fe8d9dc43afa1aab187538881da1262
child 781501 20baffc4e0cb175327cc2edc4a6cacb762a9e378
child 781672 256d572bff475749de1056ef97bbbe2f35b307a9
push id106313
push usermaglione.k@gmail.com
push dateThu, 12 Apr 2018 23:47:02 +0000
reviewersrhelmer
bugs1453123
milestone61.0a1
Bug 1453123: Have CertUtils export a CertUtils object. r=rhelmer MozReview-Commit-ID: 1XInmbNQ2wo
toolkit/modules/CertUtils.jsm
toolkit/modules/tests/chrome/test_bug544442_checkCert.xul
toolkit/modules/tests/xpcshell/test_readCertPrefs.js
toolkit/mozapps/extensions/AddonManager.jsm
toolkit/mozapps/extensions/internal/AddonUpdateChecker.jsm
toolkit/mozapps/extensions/internal/ProductAddonChecker.jsm
toolkit/mozapps/extensions/internal/XPIInstall.jsm
toolkit/mozapps/extensions/nsBlocklistService.js
tools/lint/eslint/modules.json
--- a/toolkit/modules/CertUtils.jsm
+++ b/toolkit/modules/CertUtils.jsm
@@ -1,13 +1,13 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-var EXPORTED_SYMBOLS = [ "BadCertHandler", "checkCert", "readCertPrefs", "validateCert" ];
+var EXPORTED_SYMBOLS = ["CertUtils"];
 
 const Ce = Components.Exception;
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 /**
  * Reads a set of expected certificate attributes from preferences. The returned
  * array can be passed to validateCert or checkCert to validate that a
@@ -202,8 +202,15 @@ BadCertHandler.prototype = {
   QueryInterface(iid) {
     if (!iid.equals(Ci.nsIChannelEventSink) &&
         !iid.equals(Ci.nsIInterfaceRequestor) &&
         !iid.equals(Ci.nsISupports))
       throw Cr.NS_ERROR_NO_INTERFACE;
     return this;
   }
 };
+
+var CertUtils = {
+  BadCertHandler,
+  checkCert,
+  readCertPrefs,
+  validateCert,
+};
--- a/toolkit/modules/tests/chrome/test_bug544442_checkCert.xul
+++ b/toolkit/modules/tests/chrome/test_bug544442_checkCert.xul
@@ -21,17 +21,17 @@ SimpleTest.waitForExplicitFinish();
 
 ChromeUtils.import("resource://gre/modules/CertUtils.jsm");
 
 function testStart() {
   ok(true, "Entering testStart");
 
   var request = new XMLHttpRequest();
   request.open("GET", "https://example.com/", true);
-  request.channel.notificationCallbacks = new BadCertHandler(true);
+  request.channel.notificationCallbacks = new CertUtils.BadCertHandler(true);
   request.onerror = function(event) { testXHRError(event); };
   request.onload = function(event) { testXHRLoad(event); };
   request.send(null);
 }
 
 function testXHRError(aEvent) {
   ok(true, "Entering testXHRError - something went wrong");
 
@@ -48,17 +48,17 @@ function testXHRError(aEvent) {
 
   ok(false, "XHR onerror called: " + status);
 
   SimpleTest.finish();
 }
 
 function getCheckCertResult(aChannel, aAllowNonBuiltIn, aCerts) {
   try {
-    checkCert(aChannel, aAllowNonBuiltIn, aCerts);
+    CertUtils.checkCert(aChannel, aAllowNonBuiltIn, aCerts);
   }
   catch (e) {
     return e.result;
   }
   return Cr.NS_OK;
 }
 
 function testXHRLoad(aEvent) {
--- a/toolkit/modules/tests/xpcshell/test_readCertPrefs.js
+++ b/toolkit/modules/tests/xpcshell/test_readCertPrefs.js
@@ -37,33 +37,33 @@ function test_results(aCerts, aExpected)
     }
   }
 }
 
 add_test(function test_singleCert() {
   Services.prefs.setCharPref(PREF_PREFIX + "1.attribute1", "foo");
   Services.prefs.setCharPref(PREF_PREFIX + "1.attribute2", "bar");
 
-  var certs = readCertPrefs(PREF_PREFIX);
+  var certs = CertUtils.readCertPrefs(PREF_PREFIX);
   test_results(certs, [{
     attribute1: "foo",
     attribute2: "bar"
   }]);
 
   resetPrefs();
   run_next_test();
 });
 
 add_test(function test_multipleCert() {
   Services.prefs.setCharPref(PREF_PREFIX + "1.md5Fingerprint", "cf84a9a2a804e021f27cb5128fe151f4");
   Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert");
   Services.prefs.setCharPref(PREF_PREFIX + "2.md5Fingerprint", "9441051b7eb50e5ca2226095af710c1a");
   Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert");
 
-  var certs = readCertPrefs(PREF_PREFIX);
+  var certs = CertUtils.readCertPrefs(PREF_PREFIX);
   test_results(certs, [{
     md5Fingerprint: "cf84a9a2a804e021f27cb5128fe151f4",
     nickname: "1st cert"
   }, {
     md5Fingerprint: "9441051b7eb50e5ca2226095af710c1a",
     nickname: "2nd cert"
   }]);
 
@@ -74,17 +74,17 @@ add_test(function test_multipleCert() {
 add_test(function test_skippedCert() {
   Services.prefs.setCharPref(PREF_PREFIX + "1.issuerName", "Mozilla");
   Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert");
   Services.prefs.setCharPref(PREF_PREFIX + "2.issuerName", "Top CA");
   Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert");
   Services.prefs.setCharPref(PREF_PREFIX + "4.issuerName", "Unknown CA");
   Services.prefs.setCharPref(PREF_PREFIX + "4.nickname", "Ignored cert");
 
-  var certs = readCertPrefs(PREF_PREFIX);
+  var certs = CertUtils.readCertPrefs(PREF_PREFIX);
   test_results(certs, [{
     issuerName: "Mozilla",
     nickname: "1st cert"
   }, {
     issuerName: "Top CA",
     nickname: "2nd cert"
   }]);
 
--- a/toolkit/mozapps/extensions/AddonManager.jsm
+++ b/toolkit/mozapps/extensions/AddonManager.jsm
@@ -67,22 +67,16 @@ ChromeUtils.import("resource://gre/modul
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   AddonRepository: "resource://gre/modules/addons/AddonRepository.jsm",
   Extension: "resource://gre/modules/Extension.jsm",
   FileUtils: "resource://gre/modules/FileUtils.jsm",
   PromptUtils: "resource://gre/modules/SharedPromptUtils.jsm",
 });
 
-XPCOMUtils.defineLazyGetter(this, "CertUtils", function() {
-  let certUtils = {};
-  ChromeUtils.import("resource://gre/modules/CertUtils.jsm", certUtils);
-  return certUtils;
-});
-
 XPCOMUtils.defineLazyPreferenceGetter(this, "WEBEXT_PERMISSION_PROMPTS",
                                       PREF_WEBEXT_PERM_PROMPTS, false);
 
 // Initialize the WebExtension process script service as early as possible,
 // since it needs to be able to track things like new frameLoader globals that
 // are created before other framework code has been initialized.
 Services.ppmm.loadProcessScript(
   "data:,Components.classes['@mozilla.org/webextensions/extension-process-script;1'].getService()",
--- a/toolkit/mozapps/extensions/internal/AddonUpdateChecker.jsm
+++ b/toolkit/mozapps/extensions/internal/AddonUpdateChecker.jsm
@@ -20,29 +20,23 @@ ChromeUtils.import("resource://gre/modul
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "AddonManager",
                                "resource://gre/modules/AddonManager.jsm");
 ChromeUtils.defineModuleGetter(this, "AddonManagerPrivate",
                                "resource://gre/modules/AddonManager.jsm");
 ChromeUtils.defineModuleGetter(this, "AddonRepository",
                                "resource://gre/modules/addons/AddonRepository.jsm");
+ChromeUtils.defineModuleGetter(this, "CertUtils",
+                               "resource://gre/modules/CertUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "ServiceRequest",
                                "resource://gre/modules/ServiceRequest.jsm");
 ChromeUtils.defineModuleGetter(this, "UpdateRDFConverter",
                                "resource://gre/modules/addons/UpdateRDFConverter.jsm");
 
-
-// Shared code for suppressing bad cert dialogs.
-XPCOMUtils.defineLazyGetter(this, "CertUtils", function() {
-  let certUtils = {};
-  ChromeUtils.import("resource://gre/modules/CertUtils.jsm", certUtils);
-  return certUtils;
-});
-
 ChromeUtils.import("resource://gre/modules/Log.jsm");
 const LOGGER_ID = "addons.update-checker";
 
 // Create a new logger for use by the Addons Update Checker
 // (Requires AddonManager.jsm)
 var logger = Log.repository.getLogger(LOGGER_ID);
 
 /**
--- a/toolkit/mozapps/extensions/internal/ProductAddonChecker.jsm
+++ b/toolkit/mozapps/extensions/internal/ProductAddonChecker.jsm
@@ -17,17 +17,16 @@ const LOCAL_EME_SOURCES = [{
 var EXPORTED_SYMBOLS = [ "ProductAddonChecker" ];
 
 Cu.importGlobalProperties(["XMLHttpRequest"]);
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/Log.jsm");
 ChromeUtils.import("resource://gre/modules/CertUtils.jsm");
-/* globals checkCert, BadCertHandler*/
 ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
 ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
 ChromeUtils.import("resource://gre/modules/osfile.jsm");
 
 /* globals GMPPrefs */
 ChromeUtils.defineModuleGetter(this, "GMPPrefs",
                                "resource://gre/modules/GMPUtils.jsm");
 
@@ -98,17 +97,17 @@ function getRequestStatus(request) {
 function downloadXML(url, allowNonBuiltIn = false, allowedCerts = null) {
   return new Promise((resolve, reject) => {
     let request = CreateXHR();
     // This is here to let unit test code override XHR
     if (request.wrappedJSObject) {
       request = request.wrappedJSObject;
     }
     request.open("GET", url, true);
-    request.channel.notificationCallbacks = new BadCertHandler(allowNonBuiltIn);
+    request.channel.notificationCallbacks = new CertUtils.BadCertHandler(allowNonBuiltIn);
     // Prevent the request from reading from the cache.
     request.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
     // Prevent the request from writing to the cache.
     request.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
     // Use conservative TLS settings. See bug 1325501.
     // TODO move to ServiceRequest.
     if (request.channel instanceof Ci.nsIHttpChannelInternal) {
       request.channel.QueryInterface(Ci.nsIHttpChannelInternal).beConservative = true;
@@ -134,17 +133,17 @@ function downloadXML(url, allowNonBuiltI
       reject(ex);
     };
 
     let success = (event) => {
       logger.info("Completed downloading document");
       let request = event.target;
 
       try {
-        checkCert(request.channel, allowNonBuiltIn, allowedCerts);
+        CertUtils.checkCert(request.channel, allowNonBuiltIn, allowedCerts);
       } catch (ex) {
         logger.error("Request failed certificate checks: " + ex);
         ex.status = getRequestStatus(request);
         reject(ex);
         return;
       }
 
       resolve(request.responseXML);
--- a/toolkit/mozapps/extensions/internal/XPIInstall.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIInstall.jsm
@@ -22,18 +22,18 @@ ChromeUtils.import("resource://gre/modul
 ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
 
 ChromeUtils.defineModuleGetter(this, "AddonRepository",
                                "resource://gre/modules/addons/AddonRepository.jsm");
 ChromeUtils.defineModuleGetter(this, "AddonSettings",
                                "resource://gre/modules/addons/AddonSettings.jsm");
 ChromeUtils.defineModuleGetter(this, "AppConstants",
                                "resource://gre/modules/AppConstants.jsm");
-XPCOMUtils.defineLazyGetter(this, "CertUtils",
-                            () => ChromeUtils.import("resource://gre/modules/CertUtils.jsm", {}));
+ChromeUtils.defineModuleGetter(this, "CertUtils",
+                               "resource://gre/modules/CertUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "ExtensionData",
                                "resource://gre/modules/Extension.jsm");
 ChromeUtils.defineModuleGetter(this, "FileUtils",
                                "resource://gre/modules/FileUtils.jsm");
 XPCOMUtils.defineLazyGetter(this, "IconDetails", () => {
   return ChromeUtils.import("resource://gre/modules/ExtensionParent.jsm", {}).ExtensionParent.IconDetails;
 });
 ChromeUtils.defineModuleGetter(this, "LightweightThemeManager",
--- a/toolkit/mozapps/extensions/nsBlocklistService.js
+++ b/toolkit/mozapps/extensions/nsBlocklistService.js
@@ -14,16 +14,18 @@ try {
   // AddonManager.jsm doesn't allow itself to be imported in the child
   // process. We're used in the child process (for now), so guard against
   // this.
   ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
   /* globals AddonManagerPrivate*/
 } catch (e) {
 }
 
+ChromeUtils.defineModuleGetter(this, "CertUtils",
+                               "resource://gre/modules/CertUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "FileUtils",
                                "resource://gre/modules/FileUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "UpdateUtils",
                                "resource://gre/modules/UpdateUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "OS",
                                "resource://gre/modules/osfile.jsm");
 ChromeUtils.defineModuleGetter(this, "ServiceRequest",
                                "resource://gre/modules/ServiceRequest.jsm");
@@ -127,23 +129,16 @@ XPCOMUtils.defineLazyGetter(this, "gOSVe
     } catch (e) {
       // Not all platforms have a secondary widget library, so an error is nothing to worry about.
     }
     osVersion = encodeURIComponent(osVersion);
   }
   return osVersion;
 });
 
-// shared code for suppressing bad cert dialogs
-XPCOMUtils.defineLazyGetter(this, "gCertUtils", function() {
-  let temp = { };
-  ChromeUtils.import("resource://gre/modules/CertUtils.jsm", temp);
-  return temp;
-});
-
 /**
  * Logs a string to the error console.
  * @param   string
  *          The string to write to the error console..
  */
 function LOG(string) {
   if (gLoggingEnabled) {
     dump("*** " + string + "\n");
@@ -531,17 +526,17 @@ Blocklist.prototype = {
       LOG("Blocklist::notify: There was an error creating the blocklist URI\r\n" +
           "for: " + dsURI + ", error: " + e);
       return;
     }
 
     LOG("Blocklist::notify: Requesting " + uri.spec);
     let request = new ServiceRequest();
     request.open("GET", uri.spec, true);
-    request.channel.notificationCallbacks = new gCertUtils.BadCertHandler();
+    request.channel.notificationCallbacks = new CertUtils.BadCertHandler();
     request.overrideMimeType("text/xml");
 
     // The server will return a `304 Not Modified` response if the blocklist was
     // not changed since last check.
     const lastModified = Services.prefs.getCharPref(PREF_BLOCKLIST_LAST_MODIFIED, "");
     if (lastModified) {
       request.setRequestHeader("If-Modified-Since", lastModified);
     } else {
@@ -564,17 +559,17 @@ Blocklist.prototype = {
         // Bug 1254099 - Telemetry (success or errors) will be collected during this process.
       });
     }
   },
 
   async onXMLLoad(aEvent) {
     let request = aEvent.target;
     try {
-      gCertUtils.checkCert(request.channel);
+      CertUtils.checkCert(request.channel);
     } catch (e) {
       LOG("Blocklist::onXMLLoad: " + e);
       return;
     }
 
     let {status} = request;
     if (status == 304) {
       LOG("Blocklist::onXMLLoad: up to date.");
--- a/tools/lint/eslint/modules.json
+++ b/tools/lint/eslint/modules.json
@@ -19,17 +19,17 @@
   "bogus_element_type.jsm": [],
   "bookmark_repair.js": ["BookmarkRepairRequestor", "BookmarkRepairResponder"],
   "bookmark_validator.js": ["BookmarkValidator", "BookmarkProblemData"],
   "bookmarks.js": ["BookmarksEngine", "PlacesItem", "Bookmark", "BookmarkFolder", "BookmarkQuery", "Livemark", "BookmarkSeparator", "BufferedBookmarksEngine"],
   "bookmarks.jsm": ["PlacesItem", "Bookmark", "Separator", "Livemark", "BookmarkFolder", "DumpBookmarks"],
   "BootstrapMonitor.jsm": ["monitor"],
   "browser-loader.js": ["BrowserLoader"],
   "browserid_identity.js": ["BrowserIDManager", "AuthenticationError"],
-  "CertUtils.jsm": ["BadCertHandler", "checkCert", "readCertPrefs", "validateCert"],
+  "CertUtils.jsm": ["CertUtils"],
   "clients.js": ["ClientEngine", "ClientsRec"],
   "collection_repair.js": ["getRepairRequestor", "getAllRepairRequestors", "CollectionRepairRequestor", "getRepairResponder", "CollectionRepairResponder"],
   "collection_validator.js": ["CollectionValidator", "CollectionProblemData"],
   "Console.jsm": ["console", "ConsoleAPI"],
   "constants.js": ["WEAVE_VERSION", "SYNC_API_VERSION", "STORAGE_VERSION", "PREFS_BRANCH", "DEFAULT_KEYBUNDLE_NAME", "SYNC_KEY_ENCODED_LENGTH", "SYNC_KEY_DECODED_LENGTH", "NO_SYNC_NODE_INTERVAL", "MAX_ERROR_COUNT_BEFORE_BACKOFF", "MINIMUM_BACKOFF_INTERVAL", "MAXIMUM_BACKOFF_INTERVAL", "HMAC_EVENT_INTERVAL", "MASTER_PASSWORD_LOCKED_RETRY_INTERVAL", "DEFAULT_GUID_FETCH_BATCH_SIZE", "DEFAULT_DOWNLOAD_BATCH_SIZE", "SINGLE_USER_THRESHOLD", "MULTI_DEVICE_THRESHOLD", "SCORE_INCREMENT_SMALL", "SCORE_INCREMENT_MEDIUM", "SCORE_INCREMENT_XLARGE", "SCORE_UPDATE_DELAY", "IDLE_OBSERVER_BACK_DELAY", "URI_LENGTH_MAX", "MAX_HISTORY_UPLOAD", "MAX_HISTORY_DOWNLOAD", "STATUS_OK", "SYNC_FAILED", "LOGIN_FAILED", "SYNC_FAILED_PARTIAL", "CLIENT_NOT_CONFIGURED", "STATUS_DISABLED", "MASTER_PASSWORD_LOCKED", "LOGIN_SUCCEEDED", "SYNC_SUCCEEDED", "ENGINE_SUCCEEDED", "LOGIN_FAILED_NO_USERNAME", "LOGIN_FAILED_NO_PASSPHRASE", "LOGIN_FAILED_NETWORK_ERROR", "LOGIN_FAILED_SERVER_ERROR", "LOGIN_FAILED_INVALID_PASSPHRASE", "LOGIN_FAILED_LOGIN_REJECTED", "METARECORD_DOWNLOAD_FAIL", "VERSION_OUT_OF_DATE", "CREDENTIALS_CHANGED", "ABORT_SYNC_COMMAND", "NO_SYNC_NODE_FOUND", "OVER_QUOTA", "SERVER_MAINTENANCE", "RESPONSE_OVER_QUOTA", "ENGINE_UPLOAD_FAIL", "ENGINE_DOWNLOAD_FAIL", "ENGINE_UNKNOWN_FAIL", "ENGINE_APPLY_FAIL", "ENGINE_BATCH_INTERRUPTED", "kSyncMasterPasswordLocked", "kSyncWeaveDisabled", "kSyncNetworkOffline", "kSyncBackoffNotMet", "kFirstSyncChoiceNotMade", "kSyncNotConfigured", "kFirefoxShuttingDown", "DEVICE_TYPE_DESKTOP", "DEVICE_TYPE_MOBILE", "SQLITE_MAX_VARIABLE_NUMBER"],
   "Constants.jsm": ["Roles", "Events", "Relations", "Filters", "States", "Prefilters"],
   "ContactDB.jsm": ["ContactDB", "DB_NAME", "STORE_NAME", "SAVED_GETALL_STORE_NAME", "REVISION_STORE", "DB_VERSION"],
   "content.jsm": ["registerContentFrame"],