Bug 846843 - Scrub profile directory from error strings; r=rnewman, a=bajaj
authorGregory Szorc <gps@mozilla.com>
Tue, 05 Mar 2013 10:31:12 -0800
changeset 128611 2baf49a3b9ccb3b812a1d8f63f7249423bf5e8ed
parent 128610 eb018f4077d44c6834202f54161059840b764576
child 128612 93755f9fbbc10afa7b74fb37580619a3fc813a17
push id3511
push userrnewman@mozilla.com
push dateThu, 14 Mar 2013 21:48:54 +0000
treeherdermozilla-aurora@2baf49a3b9cc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, bajaj
bugs846843
milestone21.0a2
Bug 846843 - Scrub profile directory from error strings; r=rnewman, a=bajaj
services/healthreport/healthreporter.jsm
services/healthreport/tests/xpcshell/test_healthreporter.js
--- a/services/healthreport/healthreporter.jsm
+++ b/services/healthreport/healthreporter.jsm
@@ -171,17 +171,17 @@ AbstractHealthReporter.prototype = Objec
 
   _initializeCollector: function () {
     if (this._collector) {
       throw new Error("Collector has already been initialized.");
     }
 
     this._log.info("Initializing collector.");
     this._collector = new Metrics.Collector(this._storage);
-    this._collector.onProviderError = this._errors.push.bind(this._errors);
+    this._collector.onProviderError = this._recordError.bind(this);
     this._collectorInProgress = true;
 
     let catString = this._prefs.get("service.providerCategories") || "";
     if (catString.length) {
       for (let category of catString.split(",")) {
         yield this.registerProvidersFromCategoryManager(category);
       }
     }
@@ -589,16 +589,46 @@ AbstractHealthReporter.prototype = Objec
     let recordMessage = message;
     let logMessage = message;
 
     if (ex) {
       recordMessage += ": " + ex.message;
       logMessage += ": " + CommonUtils.exceptionStr(ex);
     }
 
+    // Scrub out potentially identifying information from strings that could
+    // make the payload.
+    let appData = Services.dirsvc.get("UAppData", Ci.nsIFile);
+    let profile = Services.dirsvc.get("ProfD", Ci.nsIFile);
+
+    let appDataURI = Services.io.newFileURI(appData);
+    let profileURI = Services.io.newFileURI(profile);
+
+    // Order of operation is important here. We do the URI before the path version
+    // because the path may be a subset of the URI. We also have to check for the case
+    // where UAppData is underneath the profile directory (or vice-versa) so we
+    // don't substitute incomplete strings.
+
+    function replace(uri, path, thing) {
+      // Try is because .spec can throw on invalid URI.
+      try {
+        recordMessage = recordMessage.replace(uri.spec, '<' + thing + 'URI>', 'g');
+      } catch (ex) { }
+
+      recordMessage = recordMessage.replace(path, '<' + thing + 'Path>', 'g');
+    }
+
+    if (appData.path.contains(profile.path)) {
+      replace(appDataURI, appData.path, 'AppData');
+      replace(profileURI, profile.path, 'Profile');
+    } else {
+      replace(profileURI, profile.path, 'Profile');
+      replace(appDataURI, appData.path, 'AppData');
+    }
+
     this._log.warn(logMessage);
     this._errors.push(recordMessage);
   },
 
   /**
    * Collect all measurements for all registered providers.
    */
   collectMeasurements: function () {
--- a/services/healthreport/tests/xpcshell/test_healthreporter.js
+++ b/services/healthreport/tests/xpcshell/test_healthreporter.js
@@ -83,17 +83,17 @@ function getReporterAndServer(name, name
 function shutdownServer(server) {
   let deferred = Promise.defer();
   server.stop(deferred.resolve.bind(deferred));
 
   return deferred.promise;
 }
 
 function run_test() {
-  run_next_test();
+  makeFakeAppDir().then(run_next_test, do_throw);
 }
 
 add_task(function test_constructor() {
   let reporter = yield getReporter("constructor");
 
   do_check_eq(reporter.lastPingDate.getTime(), 0);
   do_check_null(reporter.lastSubmitID);
 
@@ -542,8 +542,30 @@ add_task(function test_upload_save_paylo
   yield reporter._uploadData(request);
   let json = yield reporter.getLastPayload();
   do_check_true("thisPingDate" in json);
 
   reporter._shutdown();
   yield shutdownServer(server);
 });
 
+add_task(function test_error_message_scrubbing() {
+  let reporter = yield getReporter("error_message_scrubbing");
+
+  try {
+    let profile = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
+    reporter._recordError("Foo " + profile);
+
+    do_check_eq(reporter._errors.length, 1);
+    do_check_eq(reporter._errors[0], "Foo <ProfilePath>");
+
+    reporter._errors = [];
+
+    let appdata = Services.dirsvc.get("UAppData", Ci.nsIFile);
+    let uri = Services.io.newFileURI(appdata);
+
+    reporter._recordError("Foo " + uri.spec);
+    do_check_eq(reporter._errors[0], "Foo <AppDataURI>");
+  } finally {
+    reporter._shutdown();
+  }
+});
+