gecko-dev/toolkit/components/antitracking/StoragePrincipalHelper.cpp
Andrea Marchesini a2db742a8c Bug 1536411 - StoragePrincipal - part 6 - Cookies, r=Ehsan
Differential Revision: https://phabricator.services.mozilla.com/D24864

--HG--
extra : moz-landing-system : lando
2019-04-12 05:30:43 +00:00

100 lines
2.7 KiB
C++

/* -*- 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 "StoragePrincipalHelper.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPrefs.h"
#include "nsContentUtils.h"
#include "nsIHttpChannel.h"
namespace mozilla {
namespace {
already_AddRefed<nsIURI> MaybeGetFirstPartyURI(nsIChannel* aChannel) {
MOZ_ASSERT(aChannel);
if (!StaticPrefs::privacy_storagePrincipal_enabledForTrackers()) {
return nullptr;
}
// Let's use the storage principal only if we need to partition the cookie
// jar.
nsContentUtils::StorageAccess access =
nsContentUtils::StorageAllowedForChannel(aChannel);
if (access != nsContentUtils::StorageAccess::ePartitionedOrDeny) {
return nullptr;
}
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
if (!httpChannel) {
return nullptr;
}
MOZ_ASSERT(httpChannel->IsThirdPartyTrackingResource());
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
nsCOMPtr<nsIPrincipal> toplevelPrincipal = loadInfo->GetTopLevelPrincipal();
if (!toplevelPrincipal) {
return nullptr;
}
nsCOMPtr<nsIURI> principalURI;
nsresult rv = toplevelPrincipal->GetURI(getter_AddRefs(principalURI));
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return principalURI.forget();
}
} // namespace
// static
nsresult StoragePrincipalHelper::Create(nsIChannel* aChannel,
nsIPrincipal* aPrincipal,
nsIPrincipal** aStoragePrincipal) {
MOZ_ASSERT(aChannel);
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aStoragePrincipal);
auto scopeExit = MakeScopeExit([&] {
nsCOMPtr<nsIPrincipal> storagePrincipal = aPrincipal;
storagePrincipal.forget(aStoragePrincipal);
});
nsCOMPtr<nsIURI> principalURI = MaybeGetFirstPartyURI(aChannel);
if (!principalURI) {
return NS_OK;
}
scopeExit.release();
nsCOMPtr<nsIPrincipal> storagePrincipal =
BasePrincipal::Cast(aPrincipal)
->CloneForcingFirstPartyDomain(principalURI);
storagePrincipal.forget(aStoragePrincipal);
return NS_OK;
}
// static
nsresult StoragePrincipalHelper::PrepareOriginAttributes(
nsIChannel* aChannel, OriginAttributes& aOriginAttributes) {
MOZ_ASSERT(aChannel);
nsCOMPtr<nsIURI> principalURI = MaybeGetFirstPartyURI(aChannel);
if (!principalURI) {
return NS_OK;
}
aOriginAttributes.SetFirstPartyDomain(false, principalURI,
true /* aForced */);
return NS_OK;
}
} // namespace mozilla