Bug 1434252 - nsICookieService may throw an error in certain circumstances, so let's make SessionCookies::restore infallible. r?dao draft bug-1434252
authorMike de Boer <mdeboer@mozilla.com>
Thu, 17 May 2018 19:09:08 +0200
changeset 796833 5c89e1fe950787c05c5c3f42c904121ed55b36b6
parent 796662 54063deb2f1caf8c4acf6461d3ba779805835c96
push id110364
push usermdeboer@mozilla.com
push dateFri, 18 May 2018 10:48:48 +0000
reviewersdao
bugs1434252, 1462402
milestone62.0a1
Bug 1434252 - nsICookieService may throw an error in certain circumstances, so let's make SessionCookies::restore infallible. r?dao Please see bug 1462402 for more details and the root cause. SessionCookies.jsm uses nsICookieService::CookieExists and nsICookieService::Add to restore session-cookies. These currently throw for hostnames that contain a leading '.' and maybe other cases, so we need to wrap the calls in a try..catch block to prevent breakage, leading to an unrestored session. MozReview-Commit-ID: 9gZ7K6lwcQF
browser/components/sessionstore/SessionCookies.jsm
--- a/browser/components/sessionstore/SessionCookies.jsm
+++ b/browser/components/sessionstore/SessionCookies.jsm
@@ -52,20 +52,30 @@ var SessionCookiesInternal = {
       let expiry = "expiry" in cookie ? cookie.expiry : MAX_EXPIRY;
       let cookieObj = {
         host: cookie.host,
         path: cookie.path || "",
         name: cookie.name || ""
       };
 
       let originAttributes = cookie.originAttributes || {};
-      if (!Services.cookies.cookieExists(cookieObj, originAttributes)) {
-        Services.cookies.add(cookie.host, cookie.path || "", cookie.name || "",
-                             cookie.value, !!cookie.secure, !!cookie.httponly,
-                             /* isSession = */ true, expiry, originAttributes);
+      let exists = false;
+      try {
+        exists = Services.cookies.cookieExists(cookieObj, originAttributes);
+      } catch (ex) {
+        Cu.reportError(`nsCookieService::CookieExists failed with error '${ex}' for '${JSON.stringify(cookie)}'.`);
+      }
+      if (!exists) {
+        try {
+          Services.cookies.add(cookie.host, cookie.path || "", cookie.name || "",
+                               cookie.value, !!cookie.secure, !!cookie.httponly,
+                               /* isSession = */ true, expiry, originAttributes);
+        } catch (ex) {
+          Cu.reportError(`nsCookieService::Add failed with error '${ex}' for cookie ${JSON.stringify(cookie)}.`);
+        }
       }
     }
   },
 
   /**
    * Handles observers notifications that are sent whenever cookies are added,
    * changed, or removed. Ensures that the storage is updated accordingly.
    */