browser/base/content/sanitize.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 7 XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
michael@0 8 "resource://gre/modules/PlacesUtils.jsm");
michael@0 9 XPCOMUtils.defineLazyModuleGetter(this, "FormHistory",
michael@0 10 "resource://gre/modules/FormHistory.jsm");
michael@0 11 XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
michael@0 12 "resource://gre/modules/Downloads.jsm");
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 14 "resource://gre/modules/Promise.jsm");
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 16 "resource://gre/modules/Task.jsm");
michael@0 17 XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
michael@0 18 "resource:///modules/DownloadsCommon.jsm");
michael@0 19
michael@0 20 function Sanitizer() {}
michael@0 21 Sanitizer.prototype = {
michael@0 22 // warning to the caller: this one may raise an exception (e.g. bug #265028)
michael@0 23 clearItem: function (aItemName)
michael@0 24 {
michael@0 25 if (this.items[aItemName].canClear)
michael@0 26 this.items[aItemName].clear();
michael@0 27 },
michael@0 28
michael@0 29 canClearItem: function (aItemName, aCallback, aArg)
michael@0 30 {
michael@0 31 let canClear = this.items[aItemName].canClear;
michael@0 32 if (typeof canClear == "function") {
michael@0 33 canClear(aCallback, aArg);
michael@0 34 return false;
michael@0 35 }
michael@0 36
michael@0 37 aCallback(aItemName, canClear, aArg);
michael@0 38 return canClear;
michael@0 39 },
michael@0 40
michael@0 41 prefDomain: "",
michael@0 42
michael@0 43 getNameFromPreference: function (aPreferenceName)
michael@0 44 {
michael@0 45 return aPreferenceName.substr(this.prefDomain.length);
michael@0 46 },
michael@0 47
michael@0 48 /**
michael@0 49 * Deletes privacy sensitive data in a batch, according to user preferences.
michael@0 50 * Returns a promise which is resolved if no errors occurred. If an error
michael@0 51 * occurs, a message is reported to the console and all other items are still
michael@0 52 * cleared before the promise is finally rejected.
michael@0 53 */
michael@0 54 sanitize: function ()
michael@0 55 {
michael@0 56 var deferred = Promise.defer();
michael@0 57 var psvc = Components.classes["@mozilla.org/preferences-service;1"]
michael@0 58 .getService(Components.interfaces.nsIPrefService);
michael@0 59 var branch = psvc.getBranch(this.prefDomain);
michael@0 60 var seenError = false;
michael@0 61
michael@0 62 // Cache the range of times to clear
michael@0 63 if (this.ignoreTimespan)
michael@0 64 var range = null; // If we ignore timespan, clear everything
michael@0 65 else
michael@0 66 range = this.range || Sanitizer.getClearRange();
michael@0 67
michael@0 68 let itemCount = Object.keys(this.items).length;
michael@0 69 let onItemComplete = function() {
michael@0 70 if (!--itemCount) {
michael@0 71 seenError ? deferred.reject() : deferred.resolve();
michael@0 72 }
michael@0 73 };
michael@0 74 for (var itemName in this.items) {
michael@0 75 let item = this.items[itemName];
michael@0 76 item.range = range;
michael@0 77 if ("clear" in item && branch.getBoolPref(itemName)) {
michael@0 78 let clearCallback = (itemName, aCanClear) => {
michael@0 79 // Some of these clear() may raise exceptions (see bug #265028)
michael@0 80 // to sanitize as much as possible, we catch and store them,
michael@0 81 // rather than fail fast.
michael@0 82 // Callers should check returned errors and give user feedback
michael@0 83 // about items that could not be sanitized
michael@0 84 let item = this.items[itemName];
michael@0 85 try {
michael@0 86 if (aCanClear)
michael@0 87 item.clear();
michael@0 88 } catch(er) {
michael@0 89 seenError = true;
michael@0 90 Components.utils.reportError("Error sanitizing " + itemName +
michael@0 91 ": " + er + "\n");
michael@0 92 }
michael@0 93 onItemComplete();
michael@0 94 };
michael@0 95 this.canClearItem(itemName, clearCallback);
michael@0 96 } else {
michael@0 97 onItemComplete();
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 return deferred.promise;
michael@0 102 },
michael@0 103
michael@0 104 // Time span only makes sense in certain cases. Consumers who want
michael@0 105 // to only clear some private data can opt in by setting this to false,
michael@0 106 // and can optionally specify a specific range. If timespan is not ignored,
michael@0 107 // and range is not set, sanitize() will use the value of the timespan
michael@0 108 // pref to determine a range
michael@0 109 ignoreTimespan : true,
michael@0 110 range : null,
michael@0 111
michael@0 112 items: {
michael@0 113 cache: {
michael@0 114 clear: function ()
michael@0 115 {
michael@0 116 var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].
michael@0 117 getService(Ci.nsICacheStorageService);
michael@0 118 try {
michael@0 119 // Cache doesn't consult timespan, nor does it have the
michael@0 120 // facility for timespan-based eviction. Wipe it.
michael@0 121 cache.clear();
michael@0 122 } catch(er) {}
michael@0 123
michael@0 124 var imageCache = Cc["@mozilla.org/image/tools;1"].
michael@0 125 getService(Ci.imgITools).getImgCacheForDocument(null);
michael@0 126 try {
michael@0 127 imageCache.clearCache(false); // true=chrome, false=content
michael@0 128 } catch(er) {}
michael@0 129 },
michael@0 130
michael@0 131 get canClear()
michael@0 132 {
michael@0 133 return true;
michael@0 134 }
michael@0 135 },
michael@0 136
michael@0 137 cookies: {
michael@0 138 clear: function ()
michael@0 139 {
michael@0 140 var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
michael@0 141 .getService(Ci.nsICookieManager);
michael@0 142 if (this.range) {
michael@0 143 // Iterate through the cookies and delete any created after our cutoff.
michael@0 144 var cookiesEnum = cookieMgr.enumerator;
michael@0 145 while (cookiesEnum.hasMoreElements()) {
michael@0 146 var cookie = cookiesEnum.getNext().QueryInterface(Ci.nsICookie2);
michael@0 147
michael@0 148 if (cookie.creationTime > this.range[0])
michael@0 149 // This cookie was created after our cutoff, clear it
michael@0 150 cookieMgr.remove(cookie.host, cookie.name, cookie.path, false);
michael@0 151 }
michael@0 152 }
michael@0 153 else {
michael@0 154 // Remove everything
michael@0 155 cookieMgr.removeAll();
michael@0 156 }
michael@0 157
michael@0 158 // Clear plugin data.
michael@0 159 const phInterface = Ci.nsIPluginHost;
michael@0 160 const FLAG_CLEAR_ALL = phInterface.FLAG_CLEAR_ALL;
michael@0 161 let ph = Cc["@mozilla.org/plugin/host;1"].getService(phInterface);
michael@0 162
michael@0 163 // Determine age range in seconds. (-1 means clear all.) We don't know
michael@0 164 // that this.range[1] is actually now, so we compute age range based
michael@0 165 // on the lower bound. If this.range results in a negative age, do
michael@0 166 // nothing.
michael@0 167 let age = this.range ? (Date.now() / 1000 - this.range[0] / 1000000)
michael@0 168 : -1;
michael@0 169 if (!this.range || age >= 0) {
michael@0 170 let tags = ph.getPluginTags();
michael@0 171 for (let i = 0; i < tags.length; i++) {
michael@0 172 try {
michael@0 173 ph.clearSiteData(tags[i], null, FLAG_CLEAR_ALL, age);
michael@0 174 } catch (e) {
michael@0 175 // If the plugin doesn't support clearing by age, clear everything.
michael@0 176 if (e.result == Components.results.
michael@0 177 NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED) {
michael@0 178 try {
michael@0 179 ph.clearSiteData(tags[i], null, FLAG_CLEAR_ALL, -1);
michael@0 180 } catch (e) {
michael@0 181 // Ignore errors from the plugin
michael@0 182 }
michael@0 183 }
michael@0 184 }
michael@0 185 }
michael@0 186 }
michael@0 187 },
michael@0 188
michael@0 189 get canClear()
michael@0 190 {
michael@0 191 return true;
michael@0 192 }
michael@0 193 },
michael@0 194
michael@0 195 offlineApps: {
michael@0 196 clear: function ()
michael@0 197 {
michael@0 198 Components.utils.import("resource:///modules/offlineAppCache.jsm");
michael@0 199 OfflineAppCacheHelper.clear();
michael@0 200 },
michael@0 201
michael@0 202 get canClear()
michael@0 203 {
michael@0 204 return true;
michael@0 205 }
michael@0 206 },
michael@0 207
michael@0 208 history: {
michael@0 209 clear: function ()
michael@0 210 {
michael@0 211 if (this.range)
michael@0 212 PlacesUtils.history.removeVisitsByTimeframe(this.range[0], this.range[1]);
michael@0 213 else
michael@0 214 PlacesUtils.history.removeAllPages();
michael@0 215
michael@0 216 try {
michael@0 217 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 218 .getService(Components.interfaces.nsIObserverService);
michael@0 219 os.notifyObservers(null, "browser:purge-session-history", "");
michael@0 220 }
michael@0 221 catch (e) { }
michael@0 222
michael@0 223 try {
michael@0 224 var seer = Components.classes["@mozilla.org/network/seer;1"]
michael@0 225 .getService(Components.interfaces.nsINetworkSeer);
michael@0 226 seer.reset();
michael@0 227 } catch (e) { }
michael@0 228 },
michael@0 229
michael@0 230 get canClear()
michael@0 231 {
michael@0 232 // bug 347231: Always allow clearing history due to dependencies on
michael@0 233 // the browser:purge-session-history notification. (like error console)
michael@0 234 return true;
michael@0 235 }
michael@0 236 },
michael@0 237
michael@0 238 formdata: {
michael@0 239 clear: function ()
michael@0 240 {
michael@0 241 // Clear undo history of all searchBars
michael@0 242 var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
michael@0 243 .getService(Components.interfaces.nsIWindowMediator);
michael@0 244 var windows = windowManager.getEnumerator("navigator:browser");
michael@0 245 while (windows.hasMoreElements()) {
michael@0 246 let currentWindow = windows.getNext();
michael@0 247 let currentDocument = currentWindow.document;
michael@0 248 let searchBar = currentDocument.getElementById("searchbar");
michael@0 249 if (searchBar)
michael@0 250 searchBar.textbox.reset();
michael@0 251 let tabBrowser = currentWindow.gBrowser;
michael@0 252 for (let tab of tabBrowser.tabs) {
michael@0 253 if (tabBrowser.isFindBarInitialized(tab))
michael@0 254 tabBrowser.getFindBar(tab).clear();
michael@0 255 }
michael@0 256 // Clear any saved find value
michael@0 257 tabBrowser._lastFindValue = "";
michael@0 258 }
michael@0 259
michael@0 260 let change = { op: "remove" };
michael@0 261 if (this.range) {
michael@0 262 [ change.firstUsedStart, change.firstUsedEnd ] = this.range;
michael@0 263 }
michael@0 264 FormHistory.update(change);
michael@0 265 },
michael@0 266
michael@0 267 canClear : function(aCallback, aArg)
michael@0 268 {
michael@0 269 var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
michael@0 270 .getService(Components.interfaces.nsIWindowMediator);
michael@0 271 var windows = windowManager.getEnumerator("navigator:browser");
michael@0 272 while (windows.hasMoreElements()) {
michael@0 273 let currentWindow = windows.getNext();
michael@0 274 let currentDocument = currentWindow.document;
michael@0 275 let searchBar = currentDocument.getElementById("searchbar");
michael@0 276 if (searchBar) {
michael@0 277 let transactionMgr = searchBar.textbox.editor.transactionManager;
michael@0 278 if (searchBar.value ||
michael@0 279 transactionMgr.numberOfUndoItems ||
michael@0 280 transactionMgr.numberOfRedoItems) {
michael@0 281 aCallback("formdata", true, aArg);
michael@0 282 return false;
michael@0 283 }
michael@0 284 }
michael@0 285 let tabBrowser = currentWindow.gBrowser;
michael@0 286 let findBarCanClear = Array.some(tabBrowser.tabs, function (aTab) {
michael@0 287 return tabBrowser.isFindBarInitialized(aTab) &&
michael@0 288 tabBrowser.getFindBar(aTab).canClear;
michael@0 289 });
michael@0 290 if (findBarCanClear) {
michael@0 291 aCallback("formdata", true, aArg);
michael@0 292 return false;
michael@0 293 }
michael@0 294 }
michael@0 295
michael@0 296 let count = 0;
michael@0 297 let countDone = {
michael@0 298 handleResult : function(aResult) count = aResult,
michael@0 299 handleError : function(aError) Components.utils.reportError(aError),
michael@0 300 handleCompletion :
michael@0 301 function(aReason) { aCallback("formdata", aReason == 0 && count > 0, aArg); }
michael@0 302 };
michael@0 303 FormHistory.count({}, countDone);
michael@0 304 return false;
michael@0 305 }
michael@0 306 },
michael@0 307
michael@0 308 downloads: {
michael@0 309 clear: function ()
michael@0 310 {
michael@0 311 Task.spawn(function () {
michael@0 312 let filterByTime = null;
michael@0 313 if (this.range) {
michael@0 314 // Convert microseconds back to milliseconds for date comparisons.
michael@0 315 let rangeBeginMs = this.range[0] / 1000;
michael@0 316 let rangeEndMs = this.range[1] / 1000;
michael@0 317 filterByTime = download => download.startTime >= rangeBeginMs &&
michael@0 318 download.startTime <= rangeEndMs;
michael@0 319 }
michael@0 320
michael@0 321 // Clear all completed/cancelled downloads
michael@0 322 let list = yield Downloads.getList(Downloads.ALL);
michael@0 323 list.removeFinished(filterByTime);
michael@0 324 }.bind(this)).then(null, Components.utils.reportError);
michael@0 325 },
michael@0 326
michael@0 327 canClear : function(aCallback, aArg)
michael@0 328 {
michael@0 329 aCallback("downloads", true, aArg);
michael@0 330 return false;
michael@0 331 }
michael@0 332 },
michael@0 333
michael@0 334 passwords: {
michael@0 335 clear: function ()
michael@0 336 {
michael@0 337 var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
michael@0 338 .getService(Components.interfaces.nsILoginManager);
michael@0 339 // Passwords are timeless, and don't respect the timeSpan setting
michael@0 340 pwmgr.removeAllLogins();
michael@0 341 },
michael@0 342
michael@0 343 get canClear()
michael@0 344 {
michael@0 345 var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
michael@0 346 .getService(Components.interfaces.nsILoginManager);
michael@0 347 var count = pwmgr.countLogins("", "", ""); // count all logins
michael@0 348 return (count > 0);
michael@0 349 }
michael@0 350 },
michael@0 351
michael@0 352 sessions: {
michael@0 353 clear: function ()
michael@0 354 {
michael@0 355 // clear all auth tokens
michael@0 356 var sdr = Components.classes["@mozilla.org/security/sdr;1"]
michael@0 357 .getService(Components.interfaces.nsISecretDecoderRing);
michael@0 358 sdr.logoutAndTeardown();
michael@0 359
michael@0 360 // clear FTP and plain HTTP auth sessions
michael@0 361 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 362 .getService(Components.interfaces.nsIObserverService);
michael@0 363 os.notifyObservers(null, "net:clear-active-logins", null);
michael@0 364 },
michael@0 365
michael@0 366 get canClear()
michael@0 367 {
michael@0 368 return true;
michael@0 369 }
michael@0 370 },
michael@0 371
michael@0 372 siteSettings: {
michael@0 373 clear: function ()
michael@0 374 {
michael@0 375 // Clear site-specific permissions like "Allow this site to open popups"
michael@0 376 var pm = Components.classes["@mozilla.org/permissionmanager;1"]
michael@0 377 .getService(Components.interfaces.nsIPermissionManager);
michael@0 378 pm.removeAll();
michael@0 379
michael@0 380 // Clear site-specific settings like page-zoom level
michael@0 381 var cps = Components.classes["@mozilla.org/content-pref/service;1"]
michael@0 382 .getService(Components.interfaces.nsIContentPrefService2);
michael@0 383 cps.removeAllDomains(null);
michael@0 384
michael@0 385 // Clear "Never remember passwords for this site", which is not handled by
michael@0 386 // the permission manager
michael@0 387 var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
michael@0 388 .getService(Components.interfaces.nsILoginManager);
michael@0 389 var hosts = pwmgr.getAllDisabledHosts();
michael@0 390 for each (var host in hosts) {
michael@0 391 pwmgr.setLoginSavingEnabled(host, true);
michael@0 392 }
michael@0 393 },
michael@0 394
michael@0 395 get canClear()
michael@0 396 {
michael@0 397 return true;
michael@0 398 }
michael@0 399 }
michael@0 400 }
michael@0 401 };
michael@0 402
michael@0 403
michael@0 404
michael@0 405 // "Static" members
michael@0 406 Sanitizer.prefDomain = "privacy.sanitize.";
michael@0 407 Sanitizer.prefShutdown = "sanitizeOnShutdown";
michael@0 408 Sanitizer.prefDidShutdown = "didShutdownSanitize";
michael@0 409
michael@0 410 // Time span constants corresponding to values of the privacy.sanitize.timeSpan
michael@0 411 // pref. Used to determine how much history to clear, for various items
michael@0 412 Sanitizer.TIMESPAN_EVERYTHING = 0;
michael@0 413 Sanitizer.TIMESPAN_HOUR = 1;
michael@0 414 Sanitizer.TIMESPAN_2HOURS = 2;
michael@0 415 Sanitizer.TIMESPAN_4HOURS = 3;
michael@0 416 Sanitizer.TIMESPAN_TODAY = 4;
michael@0 417
michael@0 418 // Return a 2 element array representing the start and end times,
michael@0 419 // in the uSec-since-epoch format that PRTime likes. If we should
michael@0 420 // clear everything, return null. Use ts if it is defined; otherwise
michael@0 421 // use the timeSpan pref.
michael@0 422 Sanitizer.getClearRange = function (ts) {
michael@0 423 if (ts === undefined)
michael@0 424 ts = Sanitizer.prefs.getIntPref("timeSpan");
michael@0 425 if (ts === Sanitizer.TIMESPAN_EVERYTHING)
michael@0 426 return null;
michael@0 427
michael@0 428 // PRTime is microseconds while JS time is milliseconds
michael@0 429 var endDate = Date.now() * 1000;
michael@0 430 switch (ts) {
michael@0 431 case Sanitizer.TIMESPAN_HOUR :
michael@0 432 var startDate = endDate - 3600000000; // 1*60*60*1000000
michael@0 433 break;
michael@0 434 case Sanitizer.TIMESPAN_2HOURS :
michael@0 435 startDate = endDate - 7200000000; // 2*60*60*1000000
michael@0 436 break;
michael@0 437 case Sanitizer.TIMESPAN_4HOURS :
michael@0 438 startDate = endDate - 14400000000; // 4*60*60*1000000
michael@0 439 break;
michael@0 440 case Sanitizer.TIMESPAN_TODAY :
michael@0 441 var d = new Date(); // Start with today
michael@0 442 d.setHours(0); // zero us back to midnight...
michael@0 443 d.setMinutes(0);
michael@0 444 d.setSeconds(0);
michael@0 445 startDate = d.valueOf() * 1000; // convert to epoch usec
michael@0 446 break;
michael@0 447 default:
michael@0 448 throw "Invalid time span for clear private data: " + ts;
michael@0 449 }
michael@0 450 return [startDate, endDate];
michael@0 451 };
michael@0 452
michael@0 453 Sanitizer._prefs = null;
michael@0 454 Sanitizer.__defineGetter__("prefs", function()
michael@0 455 {
michael@0 456 return Sanitizer._prefs ? Sanitizer._prefs
michael@0 457 : Sanitizer._prefs = Components.classes["@mozilla.org/preferences-service;1"]
michael@0 458 .getService(Components.interfaces.nsIPrefService)
michael@0 459 .getBranch(Sanitizer.prefDomain);
michael@0 460 });
michael@0 461
michael@0 462 // Shows sanitization UI
michael@0 463 Sanitizer.showUI = function(aParentWindow)
michael@0 464 {
michael@0 465 var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
michael@0 466 .getService(Components.interfaces.nsIWindowWatcher);
michael@0 467 #ifdef XP_MACOSX
michael@0 468 ww.openWindow(null, // make this an app-modal window on Mac
michael@0 469 #else
michael@0 470 ww.openWindow(aParentWindow,
michael@0 471 #endif
michael@0 472 "chrome://browser/content/sanitize.xul",
michael@0 473 "Sanitize",
michael@0 474 "chrome,titlebar,dialog,centerscreen,modal",
michael@0 475 null);
michael@0 476 };
michael@0 477
michael@0 478 /**
michael@0 479 * Deletes privacy sensitive data in a batch, optionally showing the
michael@0 480 * sanitize UI, according to user preferences
michael@0 481 */
michael@0 482 Sanitizer.sanitize = function(aParentWindow)
michael@0 483 {
michael@0 484 Sanitizer.showUI(aParentWindow);
michael@0 485 };
michael@0 486
michael@0 487 Sanitizer.onStartup = function()
michael@0 488 {
michael@0 489 // we check for unclean exit with pending sanitization
michael@0 490 Sanitizer._checkAndSanitize();
michael@0 491 };
michael@0 492
michael@0 493 Sanitizer.onShutdown = function()
michael@0 494 {
michael@0 495 // we check if sanitization is needed and perform it
michael@0 496 Sanitizer._checkAndSanitize();
michael@0 497 };
michael@0 498
michael@0 499 // this is called on startup and shutdown, to perform pending sanitizations
michael@0 500 Sanitizer._checkAndSanitize = function()
michael@0 501 {
michael@0 502 const prefs = Sanitizer.prefs;
michael@0 503 if (prefs.getBoolPref(Sanitizer.prefShutdown) &&
michael@0 504 !prefs.prefHasUserValue(Sanitizer.prefDidShutdown)) {
michael@0 505 // this is a shutdown or a startup after an unclean exit
michael@0 506 var s = new Sanitizer();
michael@0 507 s.prefDomain = "privacy.clearOnShutdown.";
michael@0 508 s.sanitize().then(function() {
michael@0 509 prefs.setBoolPref(Sanitizer.prefDidShutdown, true);
michael@0 510 });
michael@0 511 }
michael@0 512 };

mercurial