mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-17 15:25:52 +00:00
Bug 1632853 - Remove the support for cookie.txt import, r=mayhemer
Differential Revision: https://phabricator.services.mozilla.com/D72392
This commit is contained in:
parent
45e518e2ee
commit
20e7f42224
@ -44,7 +44,6 @@ 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 {
|
||||
@ -581,175 +580,6 @@ void CookiePersistentStorage::Close() {
|
||||
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) {
|
||||
@ -1628,27 +1458,6 @@ CookiePersistentStorage::OpenDBResult CookiePersistentStorage::TryInitDB(
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -51,8 +51,6 @@ class CookiePersistentStorage final : public CookieStorage {
|
||||
void CleanupCachedStatements();
|
||||
void CleanupDBConnection();
|
||||
|
||||
nsresult ImportCookies(nsIFile* aCookieFile);
|
||||
|
||||
void Activate();
|
||||
|
||||
void RebuildCorruptDB();
|
||||
|
@ -720,17 +720,6 @@ CookieService::RemoveNative(const nsACString& aHost, const nsACString& aName,
|
||||
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,
|
||||
|
@ -197,15 +197,6 @@ interface nsICookieManager : nsISupports
|
||||
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
|
||||
*
|
||||
|
@ -88,12 +88,6 @@ function* do_run_test() {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user