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/. */
|
|
|
|
|
|
|
|
#include "mozilla/net/CookieSettings.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"
|
2019-05-02 19:07:51 +03:00
|
|
|
#if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE)
|
|
|
|
# include "nsIProtocolHandler.h"
|
|
|
|
#endif
|
2019-03-08 12:00:06 +03:00
|
|
|
#include "nsPermission.h"
|
|
|
|
#include "nsPermissionManager.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PermissionComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(nsIPermission* aA, nsIPermission* aB) const {
|
|
|
|
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
|
|
|
|
already_AddRefed<nsICookieSettings> CookieSettings::CreateBlockingAll() {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
RefPtr<CookieSettings> cookieSettings =
|
|
|
|
new CookieSettings(nsICookieService::BEHAVIOR_REJECT, eFixed);
|
|
|
|
return cookieSettings.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<nsICookieSettings> CookieSettings::Create() {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
RefPtr<CookieSettings> cookieSettings = new CookieSettings(
|
|
|
|
StaticPrefs::network_cookie_cookieBehavior(), eProgressive);
|
|
|
|
return cookieSettings.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieSettings::CookieSettings(uint32_t aCookieBehavior, State aState)
|
2019-03-14 09:57:45 +03:00
|
|
|
: mCookieBehavior(aCookieBehavior), mState(aState), mToBeMerged(false) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieSettings::~CookieSettings() {
|
|
|
|
if (!NS_IsMainThread() && !mCookiePermissions.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIEventTarget> systemGroupEventTarget =
|
|
|
|
mozilla::SystemGroup::EventTargetFor(mozilla::TaskCategory::Other);
|
|
|
|
MOZ_ASSERT(systemGroupEventTarget);
|
|
|
|
|
|
|
|
RefPtr<Runnable> r = new ReleaseCookiePermissions(mCookiePermissions);
|
|
|
|
MOZ_ASSERT(mCookiePermissions.IsEmpty());
|
2019-03-08 12:00:06 +03:00
|
|
|
|
2019-03-08 12:04:11 +03:00
|
|
|
systemGroupEventTarget->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 12:00:06 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieSettings::GetCookieBehavior(uint32_t* aCookieBehavior) {
|
|
|
|
*aCookieBehavior = mCookieBehavior;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:25:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieSettings::GetRejectThirdPartyTrackers(bool* aRejectThirdPartyTrackers) {
|
|
|
|
*aRejectThirdPartyTrackers =
|
|
|
|
mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER ||
|
|
|
|
mCookieBehavior ==
|
|
|
|
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-08 12:00:06 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieSettings::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 =
|
|
|
|
nsPermission::ClonePrincipalForPermission(aPrincipal);
|
|
|
|
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.
|
|
|
|
nsPermissionManager* pm = nsPermissionManager::GetInstance();
|
|
|
|
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;
|
|
|
|
aPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_FORBIDS_COOKIE_ACCESS,
|
|
|
|
&hasFlags);
|
|
|
|
if (NS_FAILED(rv) || hasFlags) {
|
|
|
|
*aCookiePermission = nsPermissionManager::DENY_ACTION;
|
|
|
|
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.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermission> permission = nsPermission::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieSettings::Serialize(CookieSettingsArgs& 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;
|
|
|
|
|
|
|
|
for (const RefPtr<nsIPermission>& permission : mCookiePermissions) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsresult rv = permission->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void CookieSettings::Deserialize(
|
|
|
|
const CookieSettingsArgs& aData, nsICookieSettings** aCookieSettings) {
|
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()) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
PrincipalInfoToPrincipal(data.principalInfo());
|
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:43:10 +03:00
|
|
|
nsCOMPtr<nsIPermission> permission =
|
|
|
|
nsPermission::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);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<CookieSettings> cookieSettings = new CookieSettings(
|
|
|
|
aData.cookieBehavior(), aData.isFixed() ? eFixed : eProgressive);
|
|
|
|
|
|
|
|
cookieSettings->mCookiePermissions.SwapElements(list);
|
|
|
|
|
|
|
|
cookieSettings.forget(aCookieSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieSettings::Merge(const CookieSettingsArgs& aData) {
|
2019-03-08 12:04:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-03-08 12:00:06 +03:00
|
|
|
MOZ_ASSERT(mCookieBehavior == aData.cookieBehavior());
|
|
|
|
|
|
|
|
if (mState == eFixed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionComparator comparator;
|
|
|
|
|
|
|
|
for (const CookiePermissionData& data : aData.cookiePermissions()) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
PrincipalInfoToPrincipal(data.principalInfo());
|
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:43:10 +03:00
|
|
|
nsCOMPtr<nsIPermission> permission =
|
|
|
|
nsPermission::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CookieSettings, nsICookieSettings)
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|