[tor-commits] [torbutton/master] Bug 3554: Make Cookie protections obey disk settings

mikeperry at torproject.org mikeperry at torproject.org
Fri Jun 24 00:00:55 UTC 2011


commit cd5f0cb10d81fb6e17f930b7f0d7711534fb415c
Author: Mike Perry <mikeperry-git at fscked.org>
Date:   Thu Jun 23 16:58:29 2011 -0700

    Bug 3554: Make Cookie protections obey disk settings
    
    The code will delete any existing protections files from the earlier alphas,
    or from simply changing prefs.
---
 src/components/cookie-jar-selector.js |  162 +++++++++++++++++++-------------
 1 files changed, 96 insertions(+), 66 deletions(-)

diff --git a/src/components/cookie-jar-selector.js b/src/components/cookie-jar-selector.js
index 7b4b841..4094a15 100644
--- a/src/components/cookie-jar-selector.js
+++ b/src/components/cookie-jar-selector.js
@@ -78,7 +78,7 @@ function CookieJarSelector() {
     } catch(e) {
         this.logger.log(4, "Cookie clearing exception: "+e);
     }
-  }
+  };
 
   // json would be a fine alternative to e4x, but is only available from
   // gecko1.9
@@ -118,7 +118,7 @@ function CookieJarSelector() {
             cookiesAsXml.appendChild(xml);
     }
     return cookiesAsXml;
-  }
+  };
 
   this._loadCookiesFromXml = function(cookiesAsXml) {
         if (typeof(cookiesAsXml) == "undefined" || !cookiesAsXml)
@@ -142,32 +142,35 @@ function CookieJarSelector() {
             cookieManager.add(host, path, cname, value, isSecure,
                     isHttpOnly, isSession, expiry);
         }
-  }
+  };
 
   this._cookiesToFile = function(name) {
     var file = getProfileFile("cookies-" + name + ".xml");
     var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
           .createInstance(Ci.nsIFileOutputStream);
-    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
+    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
     var data = this["cookiesobj-" + name].toString();
     foStream.write(data, data.length);
     foStream.close();
-  }
+  };
+
   this._protectedCookiesToFile = function(name) {
     var file = getProfileFile("protected-" + name + ".xml");
     var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
         .createInstance(Ci.nsIFileOutputStream);
-    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
+    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
     var data = this["protected-" + name].toString();
     foStream.write(data, data.length);
     foStream.close();
-  }
+  };
+
   this.addProtectedCookie = function(cookie) {
     var tor_enabled = this.prefs.getBoolPref("extensions.torbutton.tor_enabled");
     var name = tor_enabled? "tor" : "nontor";
     var cookies = this.getProtectedCookies(name);
-    
-    if (cookies == null || cookies.toString() == "")
+
+    if (typeof(cookies) == "undefined" || cookies == null
+            || cookies.toString() == "")
       cookies = new XML('<cookies/>');
     var xml = <cookie>{cookie.value}</cookie>;
     xml. at name = cookie.name;
@@ -186,15 +189,29 @@ function CookieJarSelector() {
     if (cookie.isHttpOnly)
       xml. at isHttpOnly = 1;
 
-    
     cookies.appendChild(xml);
     this["protected-" + name] = cookies;
-    this._protectedCookiesToFile(name);
-  }
+
+    if (!this.prefs.getBoolPref("extensions.torbutton." + name + "_memory_jar")) {
+      // save protected cookies to file
+      this._protectedCookiesToFile(name);
+    } else {
+      try {
+        var file = getProfileFile("protected-" + name + ".xml");
+        if (file.exists()) {
+          file.remove(false);
+        }
+      } catch(e) {
+        this.logger.log(5, "Can't remove "+name+" cookie file: "+e);
+      }
+    }
+  };
+
   this.getProtectedCookies = function(name) {
       var file = getProfileFile("protected-" + name + ".xml");
-      if (!file.exists())
-        return null;
+      if (!file.exists()) {
+        return this["protected-" + name];
+      }
       var data = "";
       var fstream = Cc["@mozilla.org/network/file-input-stream;1"]
           .createInstance(Ci.nsIFileInputStream);
@@ -223,13 +240,27 @@ function CookieJarSelector() {
           }
       }
       return ret;
-  }
+  };
+
   this.protectCookies = function(cookies) {
     var tor_enabled = this.prefs.getBoolPref("extensions.torbutton.tor_enabled");
-    var protname = tor_enabled? "tor" : "nontor";
-    this._writeProtectCookies(cookies,protname);
-    this._protectedCookiesToFile(protname);  
-  }
+    var name = tor_enabled? "tor" : "nontor";
+    this._writeProtectCookies(cookies,name);
+    if (!this.prefs.getBoolPref("extensions.torbutton." + name + "_memory_jar")) {
+      // save protected cookies to file
+      this._protectedCookiesToFile(name);
+    } else {
+      try {
+        var file = getProfileFile("protected-" + name + ".xml");
+        if (file.exists()) {
+          file.remove(false);
+        }
+      } catch(e) {
+        this.logger.log(5, "Can't remove "+name+" cookie file: "+e);
+      }
+    }
+  };
+
   this._writeProtectCookies = function(cookies, name) {
     var cookieManager =
       Cc["@mozilla.org/cookiemanager;1"]
@@ -261,6 +292,7 @@ function CookieJarSelector() {
     }
     this["protected-" + name] = cookiesAsXml;
   };
+
   this._cookiesFromFile = function(name) {
       var file = getProfileFile("cookies-" + name + ".xml");
       if (!file.exists())
@@ -293,7 +325,7 @@ function CookieJarSelector() {
           }
       }
       return ret;
-  }
+  };
 
   this.saveCookies = function(name) {
     // transition removes old tor-style cookie file
@@ -323,54 +355,52 @@ function CookieJarSelector() {
             this.logger.log(5, "Can't remove "+name+" cookie file "+e);
         }
     }
-    
+
     // ok, everything's fine
     this.logger.log(2, "Cookies saved");
   };
+
   this.clearUnprotectedCookies = function(name) {
-  try {
-  
-    var cookiesAsXml = this.getProtectedCookies(name);
-    if (cookiesAsXml == null)
-      return;//file does not exist - no protected cookies
-    var cookiemanager =
-      Cc["@mozilla.org/cookiemanager;1"]
-      .getService(Ci.nsICookieManager2);
-    
-    var enumerator = cookiemanager.enumerator;
-    var count = 0;
-    var protcookie = false;
-
-    while (enumerator.hasMoreElements()) {
-    var nextCookie = enumerator.getNext();
-    if (!nextCookie) break;
-      nextCookie = nextCookie.QueryInterface(Components.interfaces.nsICookie);
-  
-     
-    for (var i = 0; i < cookiesAsXml.cookie.length(); i++) {
-      var xml = cookiesAsXml.cookie[i];
-      var cvalue = xml.toString();
-      var cname = xml. at name; 
-      var chost = xml. at host;
-      var cpath = xml. at path;
-
-      protcookie = protcookie || (nextCookie.host == chost && nextCookie.name == cname && nextCookie.path == cpath);  
-    }
-    if (!protcookie)
-    {
-      cookiemanager.remove(nextCookie.host,
-                         nextCookie.name,
-                         nextCookie.path, false);
+    try {
+      var cookiesAsXml = this.getProtectedCookies(name);
+      if (cookiesAsXml == null || typeof(cookiesAsXml) == "undefined"
+              || cookiesAsXml.toString() == "")
+        return;//file does not exist - no protected cookies
+      var cookiemanager =
+        Cc["@mozilla.org/cookiemanager;1"]
+        .getService(Ci.nsICookieManager2);
+
+      var enumerator = cookiemanager.enumerator;
+      var count = 0;
+      var protcookie = false;
+
+      while (enumerator.hasMoreElements()) {
+        var nextCookie = enumerator.getNext();
+        if (!nextCookie) break;
+
+        nextCookie = nextCookie.QueryInterface(Components.interfaces.nsICookie);
+        for (var i = 0; i < cookiesAsXml.cookie.length(); i++) {
+          var xml = cookiesAsXml.cookie[i];
+          var cvalue = xml.toString();
+          var cname = xml. at name;
+          var chost = xml. at host;
+          var cpath = xml. at path;
+
+          protcookie = protcookie || (nextCookie.host == chost && nextCookie.name == cname && nextCookie.path == cpath);
+        }
+
+        if (!protcookie) {
+          cookiemanager.remove(nextCookie.host,
+                             nextCookie.name,
+                             nextCookie.path, false);
+        }
+        protcookie = false;
+      }
+    } catch (e) {
+      this.logger.log(3, "Error deleting unprotected cookies: " + e);
     }
-    protcookie = false;      
-   }
-   }
-   catch (e)
-   {
-      this.logger.log(2, "Error deleting unprotected cookies. " + e);
-   }                   
-  }
-  
+  };
+
   this._oldLoadCookies = function(name, deleteSavedCookieJar) {
     var cookieManager =
       Cc["@mozilla.org/cookiemanager;1"]
@@ -428,7 +458,7 @@ function CookieJarSelector() {
     // load cookies from xml objects
     this._loadCookiesFromXml(this["cookiesobj-"+name]);
     this._loadCookiesFromXml(this["session-cookiesobj-"+name]);
-    
+
     // XXX: send a profile-do-change event?
 
     // ok, everything's fine
@@ -482,7 +512,7 @@ function CookieJarSelector() {
            jarThis.logger.log(3, "Neat. Timer fired during transition.");
            return;
        }
-       
+
        this.cookie_changed = false;
 
        if(tor_enabled) {
@@ -492,7 +522,7 @@ function CookieJarSelector() {
        }
        jarThis.logger.log(2, "Timer done. Cookies saved");
     }
-  }
+  };
 
 }
 



More information about the tor-commits mailing list