Bug 1399590 - Modify the argument of nsICookiePermission::CanAccess for changing nsIURI to nsIPrincipal. r=jdm

This commit is contained in:
Amy Chung 2017-10-03 14:37:11 +08:00
Родитель 8c18d9409a
Коммит 3eec3617b1
8 изменённых файлов: 37 добавлений и 27 удалений

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

@ -57,6 +57,7 @@ LOCAL_INCLUDES += [
'/dom/base', '/dom/base',
'/js/xpconnect/src', '/js/xpconnect/src',
'/netwerk/base', '/netwerk/base',
'/netwerk/cookie',
] ]
if CONFIG['ENABLE_TESTS']: if CONFIG['ENABLE_TESTS']:

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

@ -618,7 +618,7 @@ Navigator::CookieEnabled()
// Pass null for the channel, just like the cookie service does. // Pass null for the channel, just like the cookie service does.
nsCookieAccess access; nsCookieAccess access;
nsresult rv = permMgr->CanAccess(codebaseURI, nullptr, &access); nsresult rv = permMgr->CanAccess(doc->NodePrincipal(), &access);
NS_ENSURE_SUCCESS(rv, cookieEnabled); NS_ENSURE_SUCCESS(rv, cookieEnabled);
if (access != nsICookiePermission::ACCESS_DEFAULT) { if (access != nsICookiePermission::ACCESS_DEFAULT) {

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

@ -47,6 +47,5 @@ function runTest() {
// Disable third-party cookies for this test. // Disable third-party cookies for this test.
addEventListener('testready', function() { addEventListener('testready', function() {
SpecialPowers.pushPrefEnv({'set': [['network.cookie.cookieBehavior', 1], SpecialPowers.pushPrefEnv({'set': [['network.cookie.cookieBehavior', 1]]}, runTest);
['network.cookie.ipc.sync', true]]}, runTest);
}); });

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

@ -139,14 +139,15 @@ nsCookiePermission::SetAccess(nsIURI *aURI,
} }
NS_IMETHODIMP NS_IMETHODIMP
nsCookiePermission::CanAccess(nsIURI *aURI, nsCookiePermission::CanAccess(nsIPrincipal *aPrincipal,
nsIChannel *aChannel,
nsCookieAccess *aResult) nsCookieAccess *aResult)
{ {
// Check this protocol doesn't allow cookies // Check this protocol doesn't allow cookies
bool hasFlags; bool hasFlags;
nsCOMPtr<nsIURI> uri;
aPrincipal->GetURI(getter_AddRefs(uri));
nsresult rv = nsresult rv =
NS_URIChainHasFlags(aURI, nsIProtocolHandler::URI_FORBIDS_COOKIE_ACCESS, NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_FORBIDS_COOKIE_ACCESS,
&hasFlags); &hasFlags);
if (NS_FAILED(rv) || hasFlags) { if (NS_FAILED(rv) || hasFlags) {
*aResult = ACCESS_DENY; *aResult = ACCESS_DENY;
@ -158,7 +159,7 @@ nsCookiePermission::CanAccess(nsIURI *aURI,
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
// finally, check with permission manager... // finally, check with permission manager...
rv = mPermMgr->TestPermission(aURI, kPermissionType, (uint32_t *) aResult); rv = mPermMgr->TestPermissionFromPrincipal(aPrincipal, kPermissionType, (uint32_t *) aResult);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
if (*aResult == nsICookiePermission::ACCESS_SESSION) { if (*aResult == nsICookiePermission::ACCESS_SESSION) {
*aResult = nsICookiePermission::ACCESS_ALLOW; *aResult = nsICookiePermission::ACCESS_ALLOW;

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

@ -237,7 +237,7 @@ CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
mIPCSync = !!boolval; mIPCSync = !!boolval;
if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kCookieLeaveSecurityAlone, &boolval))) if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kCookieLeaveSecurityAlone, &boolval)))
mLeaveSecureAlone = !!boolval; mLeaveSecureAlone = !!boolval;
if (!mThirdPartyUtil && RequireThirdPartyCheck()) { if (!mThirdPartyUtil && RequireThirdPartyCheck()) {
mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID); mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
@ -279,7 +279,8 @@ CookieServiceChild::GetCookieStringFromCookieHashTable(nsIURI *a
nsCookieService::CheckPrefs(permissionService, mCookieBehavior, nsCookieService::CheckPrefs(permissionService, mCookieBehavior,
mThirdPartySession, aHostURI, mThirdPartySession, aHostURI,
aIsForeign, nullptr, aIsForeign, nullptr,
CountCookiesFromHashTable(baseDomain, aOriginAttrs)); CountCookiesFromHashTable(baseDomain, aOriginAttrs),
aOriginAttrs);
if (cookieStatus != STATUS_ACCEPTED && cookieStatus != STATUS_ACCEPT_SESSION) { if (cookieStatus != STATUS_ACCEPTED && cookieStatus != STATUS_ACCEPT_SESSION) {
return; return;
@ -526,7 +527,8 @@ CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI,
nsCookieService::CheckPrefs(permissionService, mCookieBehavior, nsCookieService::CheckPrefs(permissionService, mCookieBehavior,
mThirdPartySession, aHostURI, mThirdPartySession, aHostURI,
!!isForeign, aCookieString, !!isForeign, aCookieString,
CountCookiesFromHashTable(baseDomain, attrs)); CountCookiesFromHashTable(baseDomain, attrs),
attrs);
if (cookieStatus != STATUS_ACCEPTED && cookieStatus != STATUS_ACCEPT_SESSION) { if (cookieStatus != STATUS_ACCEPTED && cookieStatus != STATUS_ACCEPT_SESSION) {
return NS_OK; return NS_OK;

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

@ -2150,7 +2150,7 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
CountCookiesFromHost(hostFromURI, &priorCookieCount); CountCookiesFromHost(hostFromURI, &priorCookieCount);
CookieStatus cookieStatus = CheckPrefs(mPermissionService, mCookieBehavior, CookieStatus cookieStatus = CheckPrefs(mPermissionService, mCookieBehavior,
mThirdPartySession, aHostURI, aIsForeign, mThirdPartySession, aHostURI, aIsForeign,
aCookieHeader.get(), priorCookieCount); aCookieHeader.get(), priorCookieCount, aOriginAttrs);
// fire a notification if third party or if cookie was rejected // fire a notification if third party or if cookie was rejected
// (but not if there was an error) // (but not if there was an error)
@ -3308,7 +3308,7 @@ nsCookieService::GetCookiesForURI(nsIURI *aHostURI,
CountCookiesFromHost(hostFromURI, &priorCookieCount); CountCookiesFromHost(hostFromURI, &priorCookieCount);
CookieStatus cookieStatus = CheckPrefs(mPermissionService, mCookieBehavior, CookieStatus cookieStatus = CheckPrefs(mPermissionService, mCookieBehavior,
mThirdPartySession, aHostURI, aIsForeign, mThirdPartySession, aHostURI, aIsForeign,
nullptr, priorCookieCount); nullptr, priorCookieCount, aOriginAttrs);
// for GetCookie(), we don't fire rejection notifications. // for GetCookie(), we don't fire rejection notifications.
switch (cookieStatus) { switch (cookieStatus) {
@ -4207,13 +4207,14 @@ static inline bool IsSubdomainOf(const nsCString &a, const nsCString &b)
} }
CookieStatus CookieStatus
nsCookieService::CheckPrefs(nsICookiePermission *aPermissionService, nsCookieService::CheckPrefs(nsICookiePermission *aPermissionService,
uint8_t aCookieBehavior, uint8_t aCookieBehavior,
bool aThirdPartySession, bool aThirdPartySession,
nsIURI *aHostURI, nsIURI *aHostURI,
bool aIsForeign, bool aIsForeign,
const char *aCookieHeader, const char *aCookieHeader,
const int aNumOfCookies) const int aNumOfCookies,
const OriginAttributes &aOriginAttrs)
{ {
nsresult rv; nsresult rv;
@ -4224,13 +4225,21 @@ nsCookieService::CheckPrefs(nsICookiePermission *aPermissionService,
return STATUS_REJECTED_WITH_ERROR; return STATUS_REJECTED_WITH_ERROR;
} }
nsCOMPtr<nsIPrincipal> principal =
BasePrincipal::CreateCodebasePrincipal(aHostURI, aOriginAttrs);
if (!principal) {
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "non-codebase principals cannot get/set cookies");
return STATUS_REJECTED_WITH_ERROR;
}
// check the permission list first; if we find an entry, it overrides // check the permission list first; if we find an entry, it overrides
// default prefs. see bug 184059. // default prefs. see bug 184059.
if (aPermissionService) { if (aPermissionService) {
nsCookieAccess access; nsCookieAccess access;
// Not passing an nsIChannel here is probably OK; our implementation // Not passing an nsIChannel here is probably OK; our implementation
// doesn't do anything with it anyway. // doesn't do anything with it anyway.
rv = aPermissionService->CanAccess(aHostURI, nullptr, &access); rv = aPermissionService->CanAccess(principal, &access);
// if we found an entry, use it // if we found an entry, use it
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {

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

@ -232,7 +232,7 @@ class nsCookieService final : public nsICookieService
static bool DomainMatches(nsCookie* aCookie, const nsACString& aHost); static bool DomainMatches(nsCookie* aCookie, const nsACString& aHost);
static bool PathMatches(nsCookie* aCookie, const nsACString& aPath); static bool PathMatches(nsCookie* aCookie, const nsACString& aPath);
static bool CanSetCookie(nsIURI *aHostURI, const nsCookieKey& aKey, nsCookieAttributes &aCookieAttributes, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, int64_t aServerTime, bool aFromHttp, nsIChannel* aChannel, bool aLeaveSercureAlone, bool &aSetCookie); static bool CanSetCookie(nsIURI *aHostURI, const nsCookieKey& aKey, nsCookieAttributes &aCookieAttributes, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, int64_t aServerTime, bool aFromHttp, nsIChannel* aChannel, bool aLeaveSercureAlone, bool &aSetCookie);
static CookieStatus CheckPrefs(nsICookiePermission *aPermissionServices, uint8_t aCookieBehavior, bool aThirdPartyUtil, nsIURI *aHostURI, bool aIsForeign, const char *aCookieHeader, const int aNumOfCookies); static CookieStatus CheckPrefs(nsICookiePermission *aPermissionServices, uint8_t aCookieBehavior, bool aThirdPartyUtil, nsIURI *aHostURI, bool aIsForeign, const char *aCookieHeader, const int aNumOfCookies, const OriginAttributes& aOriginAttrs);
static int64_t ParseServerTime(const nsCString &aServerTime); static int64_t ParseServerTime(const nsCString &aServerTime);
void GetCookiesForURI(nsIURI *aHostURI, bool aIsForeign, bool aHttpBound, const OriginAttributes& aOriginAttrs, nsTArray<nsCookie*>& aCookieList); void GetCookiesForURI(nsIURI *aHostURI, bool aIsForeign, bool aHttpBound, const OriginAttributes& aOriginAttrs, nsTArray<nsCookie*>& aCookieList);

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

@ -7,6 +7,7 @@
interface nsICookie2; interface nsICookie2;
interface nsIURI; interface nsIURI;
interface nsIChannel; interface nsIChannel;
interface nsIPrincipal;
typedef long nsCookieAccess; typedef long nsCookieAccess;
@ -54,17 +55,14 @@ interface nsICookiePermission : nsISupports
* this method is called to test whether or not the given URI/channel may * this method is called to test whether or not the given URI/channel may
* access the cookie database, either to set or get cookies. * access the cookie database, either to set or get cookies.
* *
* @param aURI * @param aPrincipal
* the URI trying to access cookies * the principal trying to access cookies.
* @param aChannel
* the channel corresponding to aURI
* *
* @return one of the following nsCookieAccess values: * @return one of the following nsCookieAccess values:
* ACCESS_DEFAULT, ACCESS_ALLOW, ACCESS_DENY, or * ACCESS_DEFAULT, ACCESS_ALLOW, ACCESS_DENY, or
* ACCESS_ALLOW_FIRST_PARTY_ONLY * ACCESS_ALLOW_FIRST_PARTY_ONLY
*/ */
nsCookieAccess canAccess(in nsIURI aURI, nsCookieAccess canAccess(in nsIPrincipal aPrincipal);
in nsIChannel aChannel);
/** /**
* canSetCookie * canSetCookie