Bug 1632853 - Remove the support for cookie.txt import, r=mayhemer
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 28 Apr 2020 15:40:37 +0000
changeset 526543 70fe927b2a53b6236972f50f16a81681063e3589
parent 526542 7013b18788c1b6f516af9a100f3573f02723263b
child 526544 7b3b764a0e4d63788d4bc7b0a24fc45310ed273a
push id37358
push useropoprus@mozilla.com
push dateWed, 29 Apr 2020 03:05:14 +0000
treeherdermozilla-central@6bb8423186c1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1632853
milestone77.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1632853 - Remove the support for cookie.txt import, r=mayhemer Differential Revision: https://phabricator.services.mozilla.com/D72392
netwerk/cookie/CookiePersistentStorage.cpp
netwerk/cookie/CookiePersistentStorage.h
netwerk/cookie/CookieService.cpp
netwerk/cookie/nsICookieManager.idl
netwerk/test/unit/test_cookies_profile_close.js
--- a/netwerk/cookie/CookiePersistentStorage.cpp
+++ b/netwerk/cookie/CookiePersistentStorage.cpp
@@ -39,17 +39,16 @@ constexpr auto IDX_LAST_ACCESSED = 5;
 constexpr auto IDX_CREATION_TIME = 6;
 constexpr auto IDX_SECURE = 7;
 constexpr auto IDX_HTTPONLY = 8;
 constexpr auto IDX_ORIGIN_ATTRIBUTES = 9;
 constexpr auto IDX_SAME_SITE = 10;
 constexpr auto IDX_RAW_SAME_SITE = 11;
 
 #define COOKIES_FILE "cookies.sqlite"
-#define OLD_COOKIE_FILE_NAME "cookies.txt"
 
 namespace mozilla {
 namespace net {
 
 namespace {
 
 void BindCookieParameters(mozIStorageBindingParamsArray* aParamsArray,
                           const CookieKey& aKey, const Cookie* aCookie) {
@@ -576,185 +575,16 @@ void CookiePersistentStorage::Close() {
   }
 
   CleanupDBConnection();
 
   mInitialized = false;
   mInitializedDBConn = false;
 }
 
-nsresult CookiePersistentStorage::ImportCookies(nsIFile* aCookieFile) {
-  MOZ_ASSERT(aCookieFile);
-
-  nsresult rv;
-  nsCOMPtr<nsIInputStream> fileInputStream;
-  rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), aCookieFile);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-
-  nsCOMPtr<nsILineInputStream> lineInputStream =
-      do_QueryInterface(fileInputStream, &rv);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-
-  static const char kTrue[] = "TRUE";
-
-  nsAutoCString buffer;
-  nsAutoCString baseDomain;
-  bool isMore = true;
-  int32_t hostIndex;
-  int32_t isDomainIndex;
-  int32_t pathIndex;
-  int32_t secureIndex;
-  int32_t expiresIndex;
-  int32_t nameIndex;
-  int32_t cookieIndex;
-  int32_t numInts;
-  int64_t expires;
-  bool isDomain;
-  bool isHttpOnly = false;
-  uint32_t originalCookieCount = mCookieCount;
-
-  int64_t currentTimeInUsec = PR_Now();
-  int64_t currentTime = currentTimeInUsec / PR_USEC_PER_SEC;
-  // we use lastAccessedCounter to keep cookies in recently-used order,
-  // so we start by initializing to currentTime (somewhat arbitrary)
-  int64_t lastAccessedCounter = currentTimeInUsec;
-
-  /* file format is:
-   *
-   * host \t isDomain \t path \t secure \t expires \t name \t cookie
-   *
-   * if this format isn't respected we move onto the next line in the file.
-   * isDomain is "TRUE" or "FALSE" (default to "FALSE")
-   * isSecure is "TRUE" or "FALSE" (default to "TRUE")
-   * expires is a int64_t integer
-   * note 1: cookie can contain tabs.
-   * note 2: cookies will be stored in order of lastAccessed time:
-   *         most-recently used come first; least-recently-used come last.
-   */
-
-  /*
-   * ...but due to bug 178933, we hide HttpOnly cookies from older code
-   * in a comment, so they don't expose HttpOnly cookies to JS.
-   *
-   * The format for HttpOnly cookies is
-   *
-   * #HttpOnly_host \t isDomain \t path \t secure \t expires \t name \t cookie
-   *
-   */
-
-  // We will likely be adding a bunch of cookies to the DB, so we use async
-  // batching with storage to make this super fast.
-  nsCOMPtr<mozIStorageBindingParamsArray> paramsArray;
-  if (originalCookieCount == 0 && mDBConn) {
-    mStmtInsert->NewBindingParamsArray(getter_AddRefs(paramsArray));
-  }
-
-  while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
-    if (StringBeginsWith(buffer, NS_LITERAL_CSTRING(HTTP_ONLY_PREFIX))) {
-      isHttpOnly = true;
-      hostIndex = sizeof(HTTP_ONLY_PREFIX) - 1;
-    } else if (buffer.IsEmpty() || buffer.First() == '#') {
-      continue;
-    } else {
-      isHttpOnly = false;
-      hostIndex = 0;
-    }
-
-    // this is a cheap, cheesy way of parsing a tab-delimited line into
-    // string indexes, which can be lopped off into substrings. just for
-    // purposes of obfuscation, it also checks that each token was found.
-    // todo: use iterators?
-    if ((isDomainIndex = buffer.FindChar('\t', hostIndex) + 1) == 0 ||
-        (pathIndex = buffer.FindChar('\t', isDomainIndex) + 1) == 0 ||
-        (secureIndex = buffer.FindChar('\t', pathIndex) + 1) == 0 ||
-        (expiresIndex = buffer.FindChar('\t', secureIndex) + 1) == 0 ||
-        (nameIndex = buffer.FindChar('\t', expiresIndex) + 1) == 0 ||
-        (cookieIndex = buffer.FindChar('\t', nameIndex) + 1) == 0) {
-      continue;
-    }
-
-    // check the expirytime first - if it's expired, ignore
-    // nullstomp the trailing tab, to avoid copying the string
-    auto iter = buffer.BeginWriting() + nameIndex - 1;
-    *iter = char(0);
-    numInts = PR_sscanf(buffer.get() + expiresIndex, "%lld", &expires);
-    if (numInts != 1 || expires < currentTime) {
-      continue;
-    }
-
-    isDomain = Substring(buffer, isDomainIndex, pathIndex - isDomainIndex - 1)
-                   .EqualsLiteral(kTrue);
-    const nsACString& host =
-        Substring(buffer, hostIndex, isDomainIndex - hostIndex - 1);
-    // check for bad legacy cookies (domain not starting with a dot, or
-    // containing a port), and discard
-    if ((isDomain && !host.IsEmpty() && host.First() != '.') ||
-        host.Contains(':')) {
-      continue;
-    }
-
-    // compute the baseDomain from the host
-    rv = CookieCommons::GetBaseDomainFromHost(mTLDService, host, baseDomain);
-    if (NS_FAILED(rv)) {
-      continue;
-    }
-
-    // pre-existing cookies have inIsolatedMozBrowser=false set by default
-    // constructor of OriginAttributes().
-
-    // Create a new Cookie and assign the data. We don't know the cookie
-    // creation time, so just use the current time to generate a unique one.
-    CookieStruct cookieData(
-        nsCString(Substring(buffer, nameIndex, cookieIndex - nameIndex - 1)),
-        nsCString(
-            Substring(buffer, cookieIndex, buffer.Length() - cookieIndex)),
-        nsCString(host),
-        nsCString(Substring(buffer, pathIndex, secureIndex - pathIndex - 1)),
-        expires, lastAccessedCounter,
-        Cookie::GenerateUniqueCreationTime(currentTimeInUsec), isHttpOnly,
-        false /* aIsSession */,
-        Substring(buffer, secureIndex, expiresIndex - secureIndex - 1)
-            .EqualsLiteral(kTrue),
-        nsICookie::SAMESITE_NONE, nsICookie::SAMESITE_NONE);
-
-    RefPtr<Cookie> newCookie = Cookie::Create(cookieData, OriginAttributes());
-    if (!newCookie) {
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
-
-    // trick: preserve the most-recently-used cookie ordering,
-    // by successively decrementing the lastAccessed time
-    lastAccessedCounter--;
-
-    if (originalCookieCount == 0) {
-      AddCookieToList(baseDomain, OriginAttributes(), newCookie);
-      if (!newCookie->IsSession() && mDBConn) {
-        CookieKey key(baseDomain, OriginAttributes());
-        BindCookieParameters(paramsArray, key, newCookie);
-      }
-    } else {
-      AddCookie(baseDomain, OriginAttributes(), newCookie, currentTimeInUsec,
-                nullptr, VoidCString(), true);
-    }
-  }
-
-  // If we need to write to disk, do so now.
-  MaybeStoreCookiesToDB(paramsArray);
-
-  COOKIE_LOGSTRING(
-      LogLevel::Debug,
-      ("ImportCookies(): %" PRIu32 " cookies imported", mCookieCount));
-
-  return NS_OK;
-}
-
 void CookiePersistentStorage::StoreCookie(
     const nsACString& aBaseDomain, const OriginAttributes& aOriginAttributes,
     Cookie* aCookie) {
   // if it's a non-session cookie and hasn't just been read from the db, write
   // it out.
   if (aCookie->IsSession() || !mDBConn) {
     return;
   }
@@ -1623,37 +1453,16 @@ CookiePersistentStorage::OpenDBResult Co
     return RESULT_OK;
   }
 
   // check whether to import or just read in the db
   if (tableExists) {
     return Read();
   }
 
-  RefPtr<CookiePersistentStorage> self = this;
-  nsCOMPtr<nsIRunnable> runnable =
-      NS_NewRunnableFunction("TryInitDB.ImportCookies", [self] {
-        nsCOMPtr<nsIFile> oldCookieFile;
-        nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
-                                             getter_AddRefs(oldCookieFile));
-        if (NS_FAILED(rv)) {
-          return;
-        }
-
-        // Import cookies, and clean up the old file regardless of success or
-        // failure. Note that we have to switch out our CookieStorage
-        // temporarily, in case we're in private browsing mode; otherwise
-        // ImportCookies() won't be happy.
-        oldCookieFile->AppendNative(NS_LITERAL_CSTRING(OLD_COOKIE_FILE_NAME));
-        self->ImportCookies(oldCookieFile);
-        oldCookieFile->Remove(false);
-      });
-
-  NS_DispatchToMainThread(runnable);
-
   return RESULT_OK;
 }
 
 void CookiePersistentStorage::RebuildCorruptDB() {
   NS_ASSERTION(!mDBConn, "shouldn't have an open db connection");
   NS_ASSERTION(mCorruptFlag == CookiePersistentStorage::CLOSING_FOR_REBUILD,
                "should be in CLOSING_FOR_REBUILD state");
 
--- a/netwerk/cookie/CookiePersistentStorage.h
+++ b/netwerk/cookie/CookiePersistentStorage.h
@@ -46,18 +46,16 @@ class CookiePersistentStorage final : pu
 
   void Close() override;
 
   void EnsureReadComplete();
 
   void CleanupCachedStatements();
   void CleanupDBConnection();
 
-  nsresult ImportCookies(nsIFile* aCookieFile);
-
   void Activate();
 
   void RebuildCorruptDB();
   void HandleDBClosed();
 
   nsresult RunInTransaction(nsICookieTransactionCallback* aCallback);
 
   // State of the database connection.
--- a/netwerk/cookie/CookieService.cpp
+++ b/netwerk/cookie/CookieService.cpp
@@ -715,27 +715,16 @@ CookieService::RemoveNative(const nsACSt
   nsresult rv = Remove(aHost, *aOriginAttributes, aName, aPath);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
-CookieService::ImportCookies(nsIFile* aCookieFile) {
-  if (!IsInitialized()) {
-    return NS_ERROR_NOT_AVAILABLE;
-  }
-
-  mPersistentStorage->EnsureReadComplete();
-
-  return mPersistentStorage->ImportCookies(aCookieFile);
-}
-
 void CookieService::GetCookiesForURI(
     nsIURI* aHostURI, nsIChannel* aChannel, bool aIsForeign,
     bool aIsThirdPartyTrackingResource,
     bool aIsThirdPartySocialTrackingResource,
     bool aFirstPartyStorageAccessGranted, uint32_t aRejectedReason,
     bool aIsSafeTopLevelNav, bool aIsSameSiteForeign, bool aHttpBound,
     const OriginAttributes& aOriginAttrs, nsTArray<Cookie*>& aCookieList) {
   NS_ASSERTION(aHostURI, "null host!");
--- a/netwerk/cookie/nsICookieManager.idl
+++ b/netwerk/cookie/nsICookieManager.idl
@@ -193,25 +193,16 @@ interface nsICookieManager : nsISupports
    *
    * @see countCookiesFromHost
    */
   [implicit_jscontext]
   Array<nsICookie> getCookiesFromHost(in AUTF8String aHost,
                                       in jsval aOriginAttributes);
 
   /**
-   * Import an old-style cookie file. Imported cookies will be added to the
-   * existing database. If the database contains any cookies the same as those
-   * being imported (i.e. domain, name, and path match), they will be replaced.
-   *
-   * @param aCookieFile the file to import, usually cookies.txt
-   */
-  void importCookies(in nsIFile aCookieFile);
-
-  /**
    * Returns an array of all cookies whose origin attributes matches aPattern
    *
    * @param aPattern origin attribute pattern in JSON format
    *
    * @param aHost
    *        the host string to search for, e.g. "google.com". this should consist
    *        of only the host portion of a URI. see @add for a description of
    *        acceptable host strings. This attribute is optional. It will search
--- a/netwerk/test/unit/test_cookies_profile_close.js
+++ b/netwerk/test/unit/test_cookies_profile_close.js
@@ -84,22 +84,16 @@ function* do_run_test() {
     );
   }, Cr.NS_ERROR_NOT_AVAILABLE);
 
   do_check_throws(function() {
     Services.cookiemgr.remove("foo.com", "", "oh4", {});
   }, Cr.NS_ERROR_NOT_AVAILABLE);
 
   do_check_throws(function() {
-    let file = profile.clone();
-    file.append("cookies.txt");
-    Services.cookiemgr.importCookies(file);
-  }, Cr.NS_ERROR_NOT_AVAILABLE);
-
-  do_check_throws(function() {
     Services.cookiemgr.cookieExists(cookie.host, cookie.path, cookie.name, {});
   }, Cr.NS_ERROR_NOT_AVAILABLE);
 
   do_check_throws(function() {
     Services.cookies.countCookiesFromHost("foo.com");
   }, Cr.NS_ERROR_NOT_AVAILABLE);
 
   do_check_throws(function() {