Bug 1280590 - nsICookieManager2.cookieExists must use the originAttributes, r=smaug

This commit is contained in:
Andrea Marchesini 2016-09-28 08:05:12 +02:00
Родитель 7a2b5bfab6
Коммит cd5d3f1870
4 изменённых файлов: 66 добавлений и 11 удалений

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

@ -115,6 +115,7 @@ var SessionCookiesInternal = {
* Restores a given list of session cookies.
*/
restore(cookies) {
for (let cookie of cookies) {
let expiry = "expiry" in cookie ? cookie.expiry : MAX_EXPIRY;
let cookieObj = {
@ -122,7 +123,7 @@ var SessionCookiesInternal = {
path: cookie.path || "",
name: cookie.name || ""
};
if (!Services.cookies.cookieExists(cookieObj)) {
if (!Services.cookies.cookieExists(cookieObj, cookie.originAttributes || {})) {
Services.cookies.add(cookie.host, cookie.path || "", cookie.name || "",
cookie.value, !!cookie.secure, !!cookie.httponly,
/* isSession = */ true, expiry, cookie.originAttributes || {});

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

@ -4356,10 +4356,51 @@ nsCookieService::PurgeCookies(int64_t aCurrentTimeInUsec)
// find whether a given cookie has been previously set. this is provided by the
// nsICookieManager2 interface.
NS_IMETHODIMP
nsCookieService::CookieExists(nsICookie2 *aCookie,
bool *aFoundCookie)
nsCookieService::CookieExists(nsICookie2* aCookie,
JS::HandleValue aOriginAttributes,
JSContext* aCx,
uint8_t aArgc,
bool* aFoundCookie)
{
NS_ENSURE_ARG_POINTER(aCookie);
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_ARG_POINTER(aFoundCookie);
MOZ_ASSERT(aArgc == 0 || aArgc == 1);
nsresult rv;
NeckoOriginAttributes attrs;
if (aArgc == 0) {
JS::Rooted<JS::Value> value(aCx);
rv = aCookie->GetOriginAttributes(aCx, &value);
// We ignore this failure because nsICookie2 could be implemented in JS and
// this getter will failed because marked [implicit_jscontext].
if (NS_SUCCEEDED(rv) &&
(!value.isObject() || !attrs.Init(aCx, value))) {
return NS_ERROR_INVALID_ARG;
}
} else {
rv = InitializeOriginAttributes(&attrs,
aOriginAttributes,
aCx,
aArgc,
u"nsICookieManager2.cookieExists()",
u"2");
NS_ENSURE_SUCCESS(rv, rv);
}
return CookieExistsNative(aCookie, &attrs, aFoundCookie);
}
NS_IMETHODIMP_(nsresult)
nsCookieService::CookieExistsNative(nsICookie2* aCookie,
NeckoOriginAttributes* aOriginAttributes,
bool* aFoundCookie)
{
NS_ENSURE_ARG_POINTER(aCookie);
NS_ENSURE_ARG_POINTER(aOriginAttributes);
NS_ENSURE_ARG_POINTER(aFoundCookie);
if (!mDBState) {
NS_WARNING("No DBState! Profile already closed?");
@ -4379,7 +4420,8 @@ nsCookieService::CookieExists(nsICookie2 *aCookie,
NS_ENSURE_SUCCESS(rv, rv);
nsListIter iter;
*aFoundCookie = FindCookie(DEFAULT_APP_KEY(baseDomain), host, name, path, iter);
*aFoundCookie = FindCookie(nsCookieKey(baseDomain, *aOriginAttributes),
host, name, path, iter);
return NS_OK;
}

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

@ -76,11 +76,23 @@ interface nsICookieManager2 : nsICookieManager
*
* @param aCookie
* the cookie to look for
* @param aOriginAttributes
* nsICookie2 contains an originAttributes but if nsICookie2 is
* implemented in JS, we can't retrieve its originAttributes because
* the getter is marked [implicit_jscontext]. This optional parameter
* is a workaround.
*
* @return true if a cookie was found which matches the host, path, and name
* fields of aCookie
*/
boolean cookieExists(in nsICookie2 aCookie);
[implicit_jscontext, optional_argc]
boolean cookieExists(in nsICookie2 aCookie,
[optional] in jsval aOriginAttributes);
[notxpcom]
nsresult cookieExistsNative(in nsICookie2 aCookie,
in NeckoOriginAttributesPtr aOriginAttributes,
out boolean aExists);
/**
* Count how many cookies exist within the base domain of 'aHost'.

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

@ -757,9 +757,9 @@ main(int32_t argc, char *argv[])
uint32_t hostCookies = 0;
rv[8] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
hostCookies == 2;
// check CookieExists() using the third cookie
// check CookieExistsNative() using the third cookie
bool found;
rv[9] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && found;
rv[9] = NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)) && found;
// remove the cookie, block it, and ensure it can't be added again
@ -768,7 +768,7 @@ main(int32_t argc, char *argv[])
NS_LITERAL_CSTRING("/rabbit"), // path
true, // is blocked
&attrs)); // originAttributes
rv[11] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
rv[11] = NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)) && !found;
rv[12] = NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("new.domain"), // domain
NS_LITERAL_CSTRING("/rabbit"), // path
NS_LITERAL_CSTRING("test3"), // name
@ -778,14 +778,14 @@ main(int32_t argc, char *argv[])
true, // is session
INT64_MIN, // expiry time
&attrs)); // originAttributes
rv[13] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
rv[13] = NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)) && !found;
// sleep four seconds, to make sure the second cookie has expired
PR_Sleep(4 * PR_TicksPerSecond());
// check that both CountCookiesFromHost() and CookieExists() count the
// check that both CountCookiesFromHost() and CookieExistsNative() count the
// expired cookie
rv[14] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
hostCookies == 2;
rv[15] = NS_SUCCEEDED(cookieMgr2->CookieExists(expiredCookie, &found)) && found;
rv[15] = NS_SUCCEEDED(cookieMgr2->CookieExistsNative(expiredCookie, &attrs, &found)) && found;
// double-check RemoveAll() using the enumerator
rv[16] = NS_SUCCEEDED(cookieMgr->RemoveAll());
rv[17] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator))) &&