Bug 1515913 - Implement nsICookieService.removeCookiesFromRootDomain, r=Ehsan

Differential Revision: https://phabricator.services.mozilla.com/D27289

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrea Marchesini 2019-04-12 20:27:21 +00:00
Родитель b19088bb41
Коммит 6f470dae23
5 изменённых файлов: 144 добавлений и 5 удалений

Просмотреть файл

@ -47,12 +47,62 @@ add_task(async function subDomains() {
// Check again
ok(!(await checkCookie(uriA.host, {})), "We should not have cookies for URI: " + uriA.host);
ok(!(await checkIndexedDB(uriA.host, {})), "We should not have IDB for URI: " + uriA.host);
// Note that cookies are stored per base domain...
ok(!(await checkCookie(uriB.host, {})), "We should not have cookies for URI: " + uriB.host);
ok(await checkCookie(uriB.host, {}), "We should have cookies for URI: " + uriB.host);
ok(await checkIndexedDB(uriB.host, {}), "We should have IDB for URI: " + uriB.host);
// Cleaning up permissions
Services.perms.remove(uriA, "cookie");
Services.perms.remove(uriB, "cookie");
});
// session only cookie life-time, 2 domains (mozilla.org, www.mozilla.org),
// only the latter has a cookie permission.
add_task(async function subDomains() {
info("Test subdomains and custom setting with cookieBehavior == 2");
// Let's clean up all the data.
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve);
});
await SpecialPowers.pushPrefEnv({"set": [
["network.cookie.lifetimePolicy", Ci.nsICookieService.ACCEPT_SESSION ],
["browser.sanitizer.loglevel", "All"],
]});
// Domains and data
let uriA = Services.io.newURI("https://sub.mozilla.org");
Services.perms.add(uriA, "cookie", Ci.nsICookiePermission.ACCESS_ALLOW);
Services.cookies.add(uriA.host, "/test", "a", "b",
false, false, false, Date.now() + 24000 * 60 * 60, {},
Ci.nsICookie2.SAMESITE_UNSET);
await createIndexedDB(uriA.host, {});
let uriB = Services.io.newURI("https://www.mozilla.org");
Services.cookies.add(uriB.host, "/test", "c", "d",
false, false, false, Date.now() + 24000 * 60 * 60, {},
Ci.nsICookie2.SAMESITE_UNSET);
await createIndexedDB(uriB.host, {});
// Check
ok(await checkCookie(uriA.host, {}), "We have cookies for URI: " + uriA.host);
ok(await checkIndexedDB(uriA.host, {}), "We have IDB for URI: " + uriA.host);
ok(await checkCookie(uriB.host, {}), "We have cookies for URI: " + uriB.host);
ok(await checkIndexedDB(uriB.host, {}), "We have IDB for URI: " + uriB.host);
// Cleaning up
await Sanitizer.runSanitizeOnShutdown();
// Check again
ok(await checkCookie(uriA.host, {}), "We should have cookies for URI: " + uriA.host);
ok(await checkIndexedDB(uriA.host, {}), "We should have IDB for URI: " + uriA.host);
ok(!await checkCookie(uriB.host, {}), "We should not have cookies for URI: " + uriB.host);
ok(!await checkIndexedDB(uriB.host, {}), "We should not have IDB for URI: " + uriB.host);
// Cleaning up permissions
Services.perms.remove(uriA, "cookie");
});

Просмотреть файл

@ -4791,6 +4791,82 @@ nsresult nsCookieService::RemoveCookiesWithOriginAttributes(
return NS_OK;
}
NS_IMETHODIMP
nsCookieService::RemoveCookiesFromRootDomain(const nsACString &aHost,
const nsAString &aPattern) {
MOZ_ASSERT(XRE_IsParentProcess());
mozilla::OriginAttributesPattern pattern;
if (!pattern.Init(aPattern)) {
return NS_ERROR_INVALID_ARG;
}
return RemoveCookiesFromRootDomain(aHost, pattern);
}
nsresult nsCookieService::RemoveCookiesFromRootDomain(
const nsACString &aHost, const mozilla::OriginAttributesPattern &aPattern) {
nsAutoCString host(aHost);
nsresult rv = NormalizeHost(host);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString baseDomain;
rv = GetBaseDomainFromHost(mTLDService, host, baseDomain);
NS_ENSURE_SUCCESS(rv, rv);
if (!mDBState) {
NS_WARNING("No DBState! Profile already close?");
return NS_ERROR_NOT_AVAILABLE;
}
EnsureReadComplete(true);
AutoRestore<DBState *> savePrevDBState(mDBState);
mDBState = (aPattern.mPrivateBrowsingId.WasPassed() &&
aPattern.mPrivateBrowsingId.Value() > 0)
? mPrivateDBState
: mDefaultDBState;
mozStorageTransaction transaction(mDBState->dbConn, false);
// Iterate the hash table of nsCookieEntry.
for (auto iter = mDBState->hostTable.Iter(); !iter.Done(); iter.Next()) {
nsCookieEntry *entry = iter.Get();
if (!baseDomain.Equals(entry->mBaseDomain)) {
continue;
}
if (!aPattern.Matches(entry->mOriginAttributes)) {
continue;
}
uint32_t cookiesCount = entry->GetCookies().Length();
for (nsCookieEntry::IndexType i = cookiesCount; i != 0; --i) {
nsListIter iter(entry, i - 1);
RefPtr<nsCookie> cookie = iter.Cookie();
bool hasRootDomain = false;
rv = mTLDService->HasRootDomain(cookie->Host(), aHost, &hasRootDomain);
NS_ENSURE_SUCCESS(rv, rv);
if (!hasRootDomain) {
continue;
}
// Remove the cookie.
RemoveCookieFromList(iter);
if (cookie) {
NotifyChanged(cookie, u"deleted");
}
}
}
rv = transaction.Commit();
MOZ_ASSERT(NS_SUCCEEDED(rv));
return NS_OK;
}
// find an secure cookie specified by host and name
bool nsCookieService::FindSecureCookie(const nsCookieKey &aKey,
nsCookie *aCookie) {

Просмотреть файл

@ -387,6 +387,10 @@ class nsCookieService final : public nsICookieService,
bool aBlocked);
protected:
nsresult RemoveCookiesFromRootDomain(
const nsACString &aHost,
const mozilla::OriginAttributesPattern &aPattern);
// cached members.
nsCOMPtr<nsICookiePermission> mPermissionService;
nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;

Просмотреть файл

@ -238,4 +238,13 @@ interface nsICookieManager : nsISupports
*/
void removeCookiesWithOriginAttributes(in AString aPattern,
[optional] in AUTF8String aHost);
/**
* Remove all the cookies whose origin attributes matches aPattern and the
* host is the root domain of aHost.
*
* @param aHost the host to match the root domain
* @param aPattern origin attribute pattern in JSON format
*/
void removeCookiesFromRootDomain(in AUTF8String aHost, in AString aPattern);
};

Просмотреть файл

@ -37,8 +37,8 @@ XPCOMUtils.defineLazyServiceGetter(this, "sas",
const CookieCleaner = {
deleteByHost(aHost, aOriginAttributes) {
return new Promise(aResolve => {
Services.cookies.removeCookiesWithOriginAttributes(JSON.stringify(aOriginAttributes),
aHost);
Services.cookies.removeCookiesFromRootDomain(aHost,
JSON.stringify(aOriginAttributes));
aResolve();
});
},