2019-03-08 12:00:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2020-03-25 16:20:21 +03:00
|
|
|
#include "mozilla/AntiTrackingUtils.h"
|
2020-03-04 11:59:21 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2020-03-25 16:20:21 +03:00
|
|
|
#include "mozilla/ContentBlockingAllowList.h"
|
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2020-03-04 11:59:08 +03:00
|
|
|
#include "mozilla/net/CookieJarSettings.h"
|
|
|
|
#include "mozilla/net/NeckoChannelParams.h"
|
2020-04-10 11:36:37 +03:00
|
|
|
#include "mozilla/Permission.h"
|
2020-04-11 16:42:12 +03:00
|
|
|
#include "mozilla/PermissionManager.h"
|
2020-04-07 18:16:33 +03:00
|
|
|
#include "mozilla/SchedulerGroup.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2019-03-08 12:00:06 +03:00
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "nsGlobalWindowInner.h"
|
2020-05-12 22:02:05 +03:00
|
|
|
#include "nsIPrincipal.h"
|
2019-05-02 19:07:51 +03:00
|
|
|
#if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE)
|
|
|
|
# include "nsIProtocolHandler.h"
|
|
|
|
#endif
|
2020-05-19 17:47:57 +03:00
|
|
|
#include "nsICookieManager.h"
|
2020-03-04 11:59:08 +03:00
|
|
|
#include "nsICookieService.h"
|
2020-04-11 16:42:30 +03:00
|
|
|
#include "nsNetUtil.h"
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2020-03-04 11:59:21 +03:00
|
|
|
static StaticRefPtr<CookieJarSettings> sBlockinAll;
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PermissionComparator {
|
|
|
|
public:
|
2020-04-06 15:32:45 +03:00
|
|
|
static bool Equals(nsIPermission* aA, nsIPermission* aB) {
|
2019-03-08 12:00:06 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principalA;
|
|
|
|
nsresult rv = aA->GetPrincipal(getter_AddRefs(principalA));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principalB;
|
|
|
|
rv = aB->GetPrincipal(getter_AddRefs(principalB));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equals = false;
|
|
|
|
rv = principalA->Equals(principalB, &equals);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return equals;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-08 12:04:11 +03:00
|
|
|
class ReleaseCookiePermissions final : public Runnable {
|
|
|
|
public:
|
|
|
|
explicit ReleaseCookiePermissions(nsTArray<RefPtr<nsIPermission>>& aArray)
|
|
|
|
: Runnable("ReleaseCookiePermissions") {
|
|
|
|
mArray.SwapElements(aArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mArray.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<RefPtr<nsIPermission>> mArray;
|
|
|
|
};
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
2020-03-04 11:59:21 +03:00
|
|
|
already_AddRefed<nsICookieJarSettings> CookieJarSettings::GetBlockingAll() {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-03-04 11:59:21 +03:00
|
|
|
if (sBlockinAll) {
|
|
|
|
return do_AddRef(sBlockinAll);
|
|
|
|
}
|
|
|
|
|
|
|
|
sBlockinAll =
|
2020-05-19 17:47:57 +03:00
|
|
|
new CookieJarSettings(nsICookieService::BEHAVIOR_REJECT,
|
|
|
|
OriginAttributes::IsFirstPartyEnabled(), eFixed);
|
2020-03-04 11:59:21 +03:00
|
|
|
ClearOnShutdown(&sBlockinAll);
|
|
|
|
|
|
|
|
return do_AddRef(sBlockinAll);
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2020-03-04 11:59:08 +03:00
|
|
|
already_AddRefed<nsICookieJarSettings> CookieJarSettings::Create() {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
RefPtr<CookieJarSettings> cookieJarSettings = new CookieJarSettings(
|
2020-05-19 17:47:57 +03:00
|
|
|
nsICookieManager::GetCookieBehavior(),
|
|
|
|
OriginAttributes::IsFirstPartyEnabled(), eProgressive);
|
2020-03-04 11:59:08 +03:00
|
|
|
return cookieJarSettings.forget();
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 22:39:58 +03:00
|
|
|
// static
|
2020-03-04 11:59:08 +03:00
|
|
|
already_AddRefed<nsICookieJarSettings> CookieJarSettings::Create(
|
2020-06-04 13:51:21 +03:00
|
|
|
uint32_t aCookieBehavior, const nsAString& aPartitionKey,
|
2020-05-19 17:47:57 +03:00
|
|
|
bool aIsFirstPartyIsolated) {
|
2020-02-28 22:39:58 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-05-19 17:47:57 +03:00
|
|
|
RefPtr<CookieJarSettings> cookieJarSettings = new CookieJarSettings(
|
|
|
|
aCookieBehavior, aIsFirstPartyIsolated, eProgressive);
|
2020-06-04 13:51:21 +03:00
|
|
|
cookieJarSettings->mPartitionKey = aPartitionKey;
|
2020-05-19 12:47:42 +03:00
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
return cookieJarSettings.forget();
|
2020-02-28 22:39:58 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:47:57 +03:00
|
|
|
CookieJarSettings::CookieJarSettings(uint32_t aCookieBehavior,
|
|
|
|
bool aIsFirstPartyIsolated, State aState)
|
2020-03-25 16:20:21 +03:00
|
|
|
: mCookieBehavior(aCookieBehavior),
|
2020-05-19 17:47:57 +03:00
|
|
|
mIsFirstPartyIsolated(aIsFirstPartyIsolated),
|
2020-03-25 16:20:21 +03:00
|
|
|
mIsOnContentBlockingAllowList(false),
|
|
|
|
mState(aState),
|
|
|
|
mToBeMerged(false) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-05-19 17:47:57 +03:00
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
mIsFirstPartyIsolated,
|
|
|
|
mCookieBehavior !=
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN);
|
2019-03-08 12:04:11 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::~CookieJarSettings() {
|
2019-03-08 12:04:11 +03:00
|
|
|
if (!NS_IsMainThread() && !mCookiePermissions.IsEmpty()) {
|
|
|
|
RefPtr<Runnable> r = new ReleaseCookiePermissions(mCookiePermissions);
|
|
|
|
MOZ_ASSERT(mCookiePermissions.IsEmpty());
|
2019-03-08 12:00:06 +03:00
|
|
|
|
2020-04-07 18:16:33 +03:00
|
|
|
SchedulerGroup::Dispatch(TaskCategory::Other, r.forget());
|
2019-03-08 12:04:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::GetCookieBehavior(uint32_t* aCookieBehavior) {
|
2019-03-08 12:00:06 +03:00
|
|
|
*aCookieBehavior = mCookieBehavior;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:47:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieJarSettings::GetIsFirstPartyIsolated(bool* aIsFirstPartyIsolated) {
|
|
|
|
*aIsFirstPartyIsolated = mIsFirstPartyIsolated;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:25:43 +03:00
|
|
|
NS_IMETHODIMP
|
2020-04-02 18:30:57 +03:00
|
|
|
CookieJarSettings::GetRejectThirdPartyContexts(
|
|
|
|
bool* aRejectThirdPartyContexts) {
|
|
|
|
*aRejectThirdPartyContexts =
|
|
|
|
CookieJarSettings::IsRejectThirdPartyContexts(mCookieBehavior);
|
2019-05-25 04:25:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:07:48 +03:00
|
|
|
NS_IMETHODIMP
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::GetLimitForeignContexts(bool* aLimitForeignContexts) {
|
2020-01-14 20:07:48 +03:00
|
|
|
*aLimitForeignContexts =
|
|
|
|
mCookieBehavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN ||
|
|
|
|
(StaticPrefs::privacy_dynamic_firstparty_limitForeign() &&
|
|
|
|
mCookieBehavior ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:39:58 +03:00
|
|
|
NS_IMETHODIMP
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::GetPartitionForeign(bool* aPartitionForeign) {
|
2020-02-28 22:39:58 +03:00
|
|
|
*aPartitionForeign =
|
|
|
|
mCookieBehavior ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::SetPartitionForeign(bool aPartitionForeign) {
|
2020-05-19 17:47:57 +03:00
|
|
|
if (mIsFirstPartyIsolated) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:39:58 +03:00
|
|
|
if (aPartitionForeign) {
|
|
|
|
mCookieBehavior =
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:20:21 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieJarSettings::GetIsOnContentBlockingAllowList(
|
|
|
|
bool* aIsOnContentBlockingAllowList) {
|
|
|
|
*aIsOnContentBlockingAllowList = mIsOnContentBlockingAllowList;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:47:12 +03:00
|
|
|
NS_IMETHODIMP
|
2020-06-04 13:51:21 +03:00
|
|
|
CookieJarSettings::GetPartitionKey(nsAString& aPartitionKey) {
|
|
|
|
aPartitionKey = mPartitionKey;
|
2020-05-19 12:47:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
NS_IMETHODIMP
|
2020-03-04 11:59:08 +03:00
|
|
|
CookieJarSettings::CookiePermission(nsIPrincipal* aPrincipal,
|
|
|
|
uint32_t* aCookiePermission) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-03-08 12:00:06 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aPrincipal);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCookiePermission);
|
|
|
|
|
|
|
|
*aCookiePermission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Let's see if we know this permission.
|
2019-06-03 14:14:38 +03:00
|
|
|
if (!mCookiePermissions.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::ClonePrincipalForPermission(aPrincipal);
|
2019-06-03 14:14:38 +03:00
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 14:14:38 +03:00
|
|
|
for (const RefPtr<nsIPermission>& permission : mCookiePermissions) {
|
|
|
|
bool match = false;
|
|
|
|
rv = permission->MatchesPrincipalForPermission(principal, false, &match);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !match) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-29 09:57:29 +03:00
|
|
|
|
2019-06-03 14:14:38 +03:00
|
|
|
rv = permission->GetCapability(aCookiePermission);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let's ask the permission manager.
|
2020-04-11 16:42:12 +03:00
|
|
|
PermissionManager* pm = PermissionManager::GetInstance();
|
2019-03-08 12:00:06 +03:00
|
|
|
if (NS_WARN_IF(!pm)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-05-02 19:07:51 +03:00
|
|
|
#if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE)
|
|
|
|
// Check if this protocol doesn't allow cookies.
|
|
|
|
bool hasFlags;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2020-05-18 12:23:25 +03:00
|
|
|
BasePrincipal::Cast(aPrincipal)->GetURI(getter_AddRefs(uri));
|
|
|
|
|
2019-05-02 19:07:51 +03:00
|
|
|
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_FORBIDS_COOKIE_ACCESS,
|
|
|
|
&hasFlags);
|
|
|
|
if (NS_FAILED(rv) || hasFlags) {
|
2020-04-11 16:42:12 +03:00
|
|
|
*aCookiePermission = PermissionManager::DENY_ACTION;
|
2019-05-02 19:07:51 +03:00
|
|
|
rv = NS_OK; // Reset, so it's not caught as a bad status after the `else`.
|
|
|
|
} else // Note the tricky `else` which controls the call below.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = pm->TestPermissionFromPrincipal(
|
|
|
|
aPrincipal, NS_LITERAL_CSTRING("cookie"), aCookiePermission);
|
2019-03-08 12:00:06 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's store the permission, also if the result is UNKNOWN in order to avoid
|
|
|
|
// race conditions.
|
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
nsCOMPtr<nsIPermission> permission = Permission::Create(
|
2019-04-18 16:43:10 +03:00
|
|
|
aPrincipal, NS_LITERAL_CSTRING("cookie"), *aCookiePermission, 0, 0, 0);
|
2019-03-08 12:00:06 +03:00
|
|
|
if (permission) {
|
|
|
|
mCookiePermissions.AppendElement(permission);
|
|
|
|
}
|
|
|
|
|
2019-03-14 09:57:45 +03:00
|
|
|
mToBeMerged = true;
|
2019-03-08 12:00:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
void CookieJarSettings::Serialize(CookieJarSettingsArgs& aData) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
aData.isFixed() = mState == eFixed;
|
|
|
|
aData.cookieBehavior() = mCookieBehavior;
|
2020-05-19 17:47:57 +03:00
|
|
|
aData.isFirstPartyIsolated() = mIsFirstPartyIsolated;
|
2020-03-25 16:20:21 +03:00
|
|
|
aData.isOnContentBlockingAllowList() = mIsOnContentBlockingAllowList;
|
2020-06-04 13:51:21 +03:00
|
|
|
aData.partitionKey() = mPartitionKey;
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
for (const RefPtr<nsIPermission>& permission : mCookiePermissions) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsresult rv = permission->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
ipc::PrincipalInfo principalInfo;
|
2019-03-29 22:20:52 +03:00
|
|
|
rv = PrincipalToPrincipalInfo(principal, &principalInfo,
|
|
|
|
true /* aSkipBaseDomain */);
|
2019-03-08 12:00:06 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t cookiePermission = 0;
|
|
|
|
rv = permission->GetCapability(&cookiePermission);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aData.cookiePermissions().AppendElement(
|
|
|
|
CookiePermissionData(principalInfo, cookiePermission));
|
|
|
|
}
|
2019-03-14 09:57:45 +03:00
|
|
|
|
|
|
|
mToBeMerged = false;
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
/* static */ void CookieJarSettings::Deserialize(
|
|
|
|
const CookieJarSettingsArgs& aData,
|
|
|
|
nsICookieJarSettings** aCookieJarSettings) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
CookiePermissionList list;
|
|
|
|
for (const CookiePermissionData& data : aData.cookiePermissions()) {
|
2020-05-12 22:02:05 +03:00
|
|
|
auto principalOrErr = PrincipalInfoToPrincipal(data.principalInfo());
|
|
|
|
if (NS_WARN_IF(principalOrErr.isErr())) {
|
2019-03-08 12:00:06 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:02:05 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
|
|
|
|
|
2019-04-18 16:43:10 +03:00
|
|
|
nsCOMPtr<nsIPermission> permission =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::Create(principal, NS_LITERAL_CSTRING("cookie"),
|
|
|
|
data.cookiePermission(), 0, 0, 0);
|
2019-03-08 12:00:06 +03:00
|
|
|
if (NS_WARN_IF(!permission)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.AppendElement(permission);
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
RefPtr<CookieJarSettings> cookieJarSettings = new CookieJarSettings(
|
2020-05-19 17:47:57 +03:00
|
|
|
aData.cookieBehavior(), aData.isFirstPartyIsolated(),
|
|
|
|
aData.isFixed() ? eFixed : eProgressive);
|
2019-03-08 12:00:06 +03:00
|
|
|
|
2020-03-25 16:20:21 +03:00
|
|
|
cookieJarSettings->mIsOnContentBlockingAllowList =
|
|
|
|
aData.isOnContentBlockingAllowList();
|
2020-03-04 11:59:08 +03:00
|
|
|
cookieJarSettings->mCookiePermissions.SwapElements(list);
|
2020-06-04 13:51:21 +03:00
|
|
|
cookieJarSettings->mPartitionKey = aData.partitionKey();
|
2019-03-08 12:00:06 +03:00
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
cookieJarSettings.forget(aCookieJarSettings);
|
2019-03-08 12:00:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
void CookieJarSettings::Merge(const CookieJarSettingsArgs& aData) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-02-28 22:39:58 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
mCookieBehavior == aData.cookieBehavior() ||
|
|
|
|
(mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER &&
|
|
|
|
aData.cookieBehavior() ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) ||
|
|
|
|
(mCookieBehavior ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN &&
|
|
|
|
aData.cookieBehavior() == nsICookieService::BEHAVIOR_REJECT_TRACKER));
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
if (mState == eFixed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:39:58 +03:00
|
|
|
// Merge cookie behavior pref values
|
|
|
|
if (mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER &&
|
|
|
|
aData.cookieBehavior() ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) {
|
|
|
|
// If the other side has decided to partition third-party cookies, update
|
2020-05-19 17:47:57 +03:00
|
|
|
// our side when first-party isolation is disabled.
|
|
|
|
if (!mIsFirstPartyIsolated) {
|
|
|
|
mCookieBehavior =
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN;
|
|
|
|
}
|
2020-02-28 22:39:58 +03:00
|
|
|
}
|
|
|
|
if (mCookieBehavior ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN &&
|
|
|
|
aData.cookieBehavior() == nsICookieService::BEHAVIOR_REJECT_TRACKER) {
|
|
|
|
// If we've decided to partition third-party cookies, the other side may not
|
2020-05-19 17:47:57 +03:00
|
|
|
// have caught up yet unless it has first-party isolation enabled.
|
|
|
|
if (aData.isFirstPartyIsolated()) {
|
|
|
|
mCookieBehavior = nsICookieService::BEHAVIOR_REJECT_TRACKER;
|
|
|
|
mIsFirstPartyIsolated = true;
|
|
|
|
}
|
2020-02-28 22:39:58 +03:00
|
|
|
}
|
|
|
|
// Ignore all other cases.
|
2020-05-19 17:47:57 +03:00
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
mIsFirstPartyIsolated,
|
|
|
|
mCookieBehavior !=
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN);
|
2020-02-28 22:39:58 +03:00
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
PermissionComparator comparator;
|
|
|
|
|
|
|
|
for (const CookiePermissionData& data : aData.cookiePermissions()) {
|
2020-05-12 22:02:05 +03:00
|
|
|
auto principalOrErr = PrincipalInfoToPrincipal(data.principalInfo());
|
|
|
|
if (NS_WARN_IF(principalOrErr.isErr())) {
|
2019-03-08 12:00:06 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:02:05 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
|
2019-04-18 16:43:10 +03:00
|
|
|
nsCOMPtr<nsIPermission> permission =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::Create(principal, NS_LITERAL_CSTRING("cookie"),
|
|
|
|
data.cookiePermission(), 0, 0, 0);
|
2019-03-08 12:00:06 +03:00
|
|
|
if (NS_WARN_IF(!permission)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCookiePermissions.Contains(permission, comparator)) {
|
|
|
|
mCookiePermissions.AppendElement(permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 13:51:21 +03:00
|
|
|
void CookieJarSettings::SetPartitionKey(nsIURI* aURI) {
|
2020-05-19 12:47:12 +03:00
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
|
|
|
OriginAttributes attrs;
|
2020-06-04 13:51:21 +03:00
|
|
|
attrs.SetPartitionKey(aURI);
|
|
|
|
mPartitionKey = std::move(attrs.mPartitionKey);
|
2020-05-19 12:47:12 +03:00
|
|
|
}
|
|
|
|
|
2020-03-25 16:20:21 +03:00
|
|
|
void CookieJarSettings::UpdateIsOnContentBlockingAllowList(
|
|
|
|
nsIChannel* aChannel) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(aChannel);
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
|
|
|
|
|
2020-05-26 09:52:39 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-25 16:20:21 +03:00
|
|
|
|
|
|
|
// We need to recompute the ContentBlockingAllowListPrincipal here for the
|
|
|
|
// top level channel because we might navigate from the the initial
|
|
|
|
// about:blank page or the existing page which may have a different origin
|
|
|
|
// than the URI we are going to load here. Thus, we need to recompute the
|
|
|
|
// prinicpal in order to get the correct ContentBlockingAllowListPrincipal.
|
2020-05-26 09:52:39 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> contentBlockingAllowListPrincipal;
|
2020-03-25 16:20:21 +03:00
|
|
|
OriginAttributes attrs;
|
|
|
|
loadInfo->GetOriginAttributes(&attrs);
|
|
|
|
ContentBlockingAllowList::RecomputePrincipal(
|
2020-05-26 09:52:39 +03:00
|
|
|
uri, attrs, getter_AddRefs(contentBlockingAllowListPrincipal));
|
2020-03-25 16:20:21 +03:00
|
|
|
|
|
|
|
if (!contentBlockingAllowListPrincipal ||
|
|
|
|
!contentBlockingAllowListPrincipal->GetIsContentPrincipal()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << ContentBlockingAllowList::Check(contentBlockingAllowListPrincipal,
|
|
|
|
NS_UsePrivateBrowsing(aChannel),
|
|
|
|
mIsOnContentBlockingAllowList);
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:22:35 +03:00
|
|
|
// static
|
2020-04-02 18:30:57 +03:00
|
|
|
bool CookieJarSettings::IsRejectThirdPartyContexts(uint32_t aCookieBehavior) {
|
2020-03-26 01:22:35 +03:00
|
|
|
return aCookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER ||
|
|
|
|
aCookieBehavior ==
|
2020-04-02 18:30:57 +03:00
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN ||
|
|
|
|
IsRejectThirdPartyWithExceptions(aCookieBehavior);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CookieJarSettings::IsRejectThirdPartyWithExceptions(
|
|
|
|
uint32_t aCookieBehavior) {
|
|
|
|
return aCookieBehavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN &&
|
|
|
|
StaticPrefs::network_cookie_rejectForeignWithExceptions_enabled();
|
2020-03-26 01:22:35 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
NS_IMPL_ISUPPORTS(CookieJarSettings, nsICookieJarSettings)
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|