2010-03-26 02:02:28 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-03-26 02:02:28 +03:00
|
|
|
|
2020-06-02 15:23:00 +03:00
|
|
|
#include "CookieCommons.h"
|
2020-04-06 15:26:14 +03:00
|
|
|
#include "mozilla/net/CookieService.h"
|
2010-03-26 02:02:28 +03:00
|
|
|
#include "mozilla/net/CookieServiceParent.h"
|
2012-12-29 13:02:16 +04:00
|
|
|
#include "mozilla/net/NeckoParent.h"
|
2012-08-23 23:33:46 +04:00
|
|
|
|
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2019-04-12 08:30:43 +03:00
|
|
|
#include "mozilla/StoragePrincipalHelper.h"
|
2020-04-06 15:27:05 +03:00
|
|
|
#include "mozIThirdPartyUtil.h"
|
2017-08-03 13:00:12 +03:00
|
|
|
#include "nsArrayUtils.h"
|
2015-11-22 12:43:34 +03:00
|
|
|
#include "nsIChannel.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2010-03-26 02:02:28 +03:00
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
using namespace mozilla::ipc;
|
2015-07-17 15:52:00 +03:00
|
|
|
|
2012-12-29 13:02:16 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2010-03-26 02:02:28 +03:00
|
|
|
CookieServiceParent::CookieServiceParent() {
|
|
|
|
// Instantiate the cookieservice via the service manager, so it sticks around
|
|
|
|
// until shutdown.
|
|
|
|
nsCOMPtr<nsICookieService> cs = do_GetService(NS_COOKIESERVICE_CONTRACTID);
|
|
|
|
|
2020-04-06 15:26:14 +03:00
|
|
|
// Get the CookieService instance directly, so we can call internal methods.
|
|
|
|
mCookieService = CookieService::GetSingleton();
|
2010-03-26 02:02:28 +03:00
|
|
|
NS_ASSERTION(mCookieService, "couldn't get nsICookieService");
|
2018-04-19 14:18:50 +03:00
|
|
|
mProcessingCookie = false;
|
2010-03-26 02:02:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 13:00:12 +03:00
|
|
|
void CookieServiceParent::RemoveBatchDeletedCookies(nsIArray* aCookieList) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
aCookieList->GetLength(&len);
|
|
|
|
OriginAttributes attrs;
|
|
|
|
CookieStruct cookieStruct;
|
|
|
|
nsTArray<CookieStruct> cookieStructList;
|
|
|
|
nsTArray<OriginAttributes> attrsList;
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsCOMPtr<nsICookie> xpcCookie = do_QueryElementAt(aCookieList, i);
|
2020-04-06 15:22:01 +03:00
|
|
|
auto cookie = static_cast<Cookie*>(xpcCookie.get());
|
2017-08-03 13:00:12 +03:00
|
|
|
attrs = cookie->OriginAttributesRef();
|
2019-05-31 12:31:47 +03:00
|
|
|
cookieStruct = cookie->ToIPC();
|
2018-09-26 18:39:33 +03:00
|
|
|
if (cookie->IsHttpOnly()) {
|
|
|
|
// Child only needs to exist if an HttpOnly cookie exists, not its value
|
|
|
|
cookieStruct.value() = "";
|
2017-08-03 13:00:12 +03:00
|
|
|
}
|
2018-09-26 18:39:33 +03:00
|
|
|
cookieStructList.AppendElement(cookieStruct);
|
|
|
|
attrsList.AppendElement(attrs);
|
2017-08-03 13:00:12 +03:00
|
|
|
}
|
|
|
|
Unused << SendRemoveBatchDeletedCookies(cookieStructList, attrsList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieServiceParent::RemoveAll() { Unused << SendRemoveAll(); }
|
|
|
|
|
|
|
|
void CookieServiceParent::RemoveCookie(nsICookie* aCookie) {
|
2020-04-06 15:22:01 +03:00
|
|
|
auto cookie = static_cast<Cookie*>(aCookie);
|
2020-04-06 15:32:45 +03:00
|
|
|
const OriginAttributes& attrs = cookie->OriginAttributesRef();
|
2019-05-31 12:31:47 +03:00
|
|
|
CookieStruct cookieStruct = cookie->ToIPC();
|
2018-09-26 18:39:33 +03:00
|
|
|
if (cookie->IsHttpOnly()) {
|
|
|
|
cookieStruct.value() = "";
|
2017-08-03 13:00:12 +03:00
|
|
|
}
|
2018-09-26 18:39:33 +03:00
|
|
|
Unused << SendRemoveCookie(cookieStruct, attrs);
|
2017-08-03 13:00:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieServiceParent::AddCookie(nsICookie* aCookie) {
|
2020-04-06 15:22:01 +03:00
|
|
|
auto cookie = static_cast<Cookie*>(aCookie);
|
2020-04-06 15:32:45 +03:00
|
|
|
const OriginAttributes& attrs = cookie->OriginAttributesRef();
|
2019-05-31 12:31:47 +03:00
|
|
|
CookieStruct cookieStruct = cookie->ToIPC();
|
2018-09-26 18:39:33 +03:00
|
|
|
if (cookie->IsHttpOnly()) {
|
|
|
|
cookieStruct.value() = "";
|
|
|
|
}
|
2017-12-14 22:29:32 +03:00
|
|
|
Unused << SendAddCookie(cookieStruct, attrs);
|
2017-08-03 13:00:12 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 14:00:41 +03:00
|
|
|
void CookieServiceParent::TrackCookieLoad(nsIChannel* aChannel) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
|
2020-04-06 15:33:45 +03:00
|
|
|
OriginAttributes attrs = loadInfo->GetOriginAttributes();
|
2020-06-02 15:23:00 +03:00
|
|
|
bool isSafeTopLevelNav = CookieCommons::IsSafeTopLevelNav(aChannel);
|
|
|
|
bool aIsSameSiteForeign = CookieCommons::IsSameSiteForeign(aChannel, uri);
|
2017-08-03 14:00:41 +03:00
|
|
|
|
2020-05-05 23:56:04 +03:00
|
|
|
StoragePrincipalHelper::PrepareEffectiveStoragePrincipalOriginAttributes(
|
|
|
|
aChannel, attrs);
|
2019-04-12 08:30:43 +03:00
|
|
|
|
2017-08-03 14:00:41 +03:00
|
|
|
// Send matching cookies to Child.
|
|
|
|
nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil;
|
|
|
|
thirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
|
2018-06-20 20:38:22 +03:00
|
|
|
|
2019-05-25 04:25:41 +03:00
|
|
|
uint32_t rejectedReason = 0;
|
2020-01-08 13:25:44 +03:00
|
|
|
ThirdPartyAnalysisResult result = thirdPartyUtil->AnalyzeChannel(
|
|
|
|
aChannel, false, nullptr, nullptr, &rejectedReason);
|
2018-07-10 11:09:59 +03:00
|
|
|
|
2020-04-06 15:22:01 +03:00
|
|
|
nsTArray<Cookie*> foundCookieList;
|
2019-11-02 02:24:25 +03:00
|
|
|
mCookieService->GetCookiesForURI(
|
2020-01-08 13:25:44 +03:00
|
|
|
uri, aChannel, result.contains(ThirdPartyAnalysis::IsForeign),
|
2020-02-20 10:27:24 +03:00
|
|
|
result.contains(ThirdPartyAnalysis::IsThirdPartyTrackingResource),
|
|
|
|
result.contains(ThirdPartyAnalysis::IsThirdPartySocialTrackingResource),
|
2020-06-03 09:12:06 +03:00
|
|
|
result.contains(ThirdPartyAnalysis::IsStorageAccessPermissionGranted),
|
2020-01-08 13:25:44 +03:00
|
|
|
rejectedReason, isSafeTopLevelNav, aIsSameSiteForeign, false, attrs,
|
|
|
|
foundCookieList);
|
2017-08-03 14:00:41 +03:00
|
|
|
nsTArray<CookieStruct> matchingCookiesList;
|
2020-04-06 15:32:45 +03:00
|
|
|
SerialializeCookieList(foundCookieList, matchingCookiesList);
|
2017-08-03 14:00:41 +03:00
|
|
|
Unused << SendTrackCookiesLoad(matchingCookiesList, attrs);
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:32:45 +03:00
|
|
|
// static
|
2017-08-03 14:00:41 +03:00
|
|
|
void CookieServiceParent::SerialializeCookieList(
|
2020-04-06 15:22:01 +03:00
|
|
|
const nsTArray<Cookie*>& aFoundCookieList,
|
2020-04-06 15:32:45 +03:00
|
|
|
nsTArray<CookieStruct>& aCookiesList) {
|
2017-08-03 14:00:41 +03:00
|
|
|
for (uint32_t i = 0; i < aFoundCookieList.Length(); i++) {
|
2020-04-06 15:22:01 +03:00
|
|
|
Cookie* cookie = aFoundCookieList.ElementAt(i);
|
2017-08-03 14:00:41 +03:00
|
|
|
CookieStruct* cookieStruct = aCookiesList.AppendElement();
|
2019-05-31 12:31:47 +03:00
|
|
|
*cookieStruct = cookie->ToIPC();
|
|
|
|
if (cookie->IsHttpOnly()) {
|
|
|
|
// Value only needs to exist if an HttpOnly cookie exists.
|
|
|
|
cookieStruct->value() = "";
|
2018-09-26 18:39:33 +03:00
|
|
|
}
|
2017-08-03 14:00:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:33:45 +03:00
|
|
|
IPCResult CookieServiceParent::RecvPrepareCookieList(
|
2020-04-25 22:10:00 +03:00
|
|
|
nsIURI* aHost, const bool& aIsForeign,
|
2020-02-20 10:27:24 +03:00
|
|
|
const bool& aIsThirdPartyTrackingResource,
|
|
|
|
const bool& aIsThirdPartySocialTrackingResource,
|
2020-06-03 09:12:06 +03:00
|
|
|
const bool& aStorageAccessPermissionGranted,
|
2019-05-25 04:25:41 +03:00
|
|
|
const uint32_t& aRejectedReason, const bool& aIsSafeTopLevelNav,
|
2017-08-03 14:00:41 +03:00
|
|
|
const bool& aIsSameSiteForeign, const OriginAttributes& aAttrs) {
|
|
|
|
// Send matching cookies to Child.
|
2020-04-25 22:10:00 +03:00
|
|
|
if (!aHost) {
|
|
|
|
return IPC_FAIL(this, "aHost must not be null");
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:22:01 +03:00
|
|
|
nsTArray<Cookie*> foundCookieList;
|
2019-01-22 22:46:10 +03:00
|
|
|
// Note: passing nullptr as aChannel to GetCookiesForURI() here is fine since
|
|
|
|
// this argument is only used for proper reporting of cookie loads, but the
|
|
|
|
// child process already does the necessary reporting in this case for us.
|
2018-06-20 20:38:22 +03:00
|
|
|
mCookieService->GetCookiesForURI(
|
2020-04-25 22:10:00 +03:00
|
|
|
aHost, nullptr, aIsForeign, aIsThirdPartyTrackingResource,
|
2020-06-03 09:12:06 +03:00
|
|
|
aIsThirdPartySocialTrackingResource, aStorageAccessPermissionGranted,
|
2019-11-02 02:24:25 +03:00
|
|
|
aRejectedReason, aIsSafeTopLevelNav, aIsSameSiteForeign, false, aAttrs,
|
|
|
|
foundCookieList);
|
2017-08-03 14:00:41 +03:00
|
|
|
nsTArray<CookieStruct> matchingCookiesList;
|
2020-04-06 15:32:45 +03:00
|
|
|
SerialializeCookieList(foundCookieList, matchingCookiesList);
|
2017-08-03 14:00:41 +03:00
|
|
|
Unused << SendTrackCookiesLoad(matchingCookiesList, aAttrs);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2014-05-02 22:44:13 +04:00
|
|
|
void CookieServiceParent::ActorDestroy(ActorDestroyReason aWhy) {
|
2016-08-23 23:21:00 +03:00
|
|
|
// Nothing needed here. Called right before destructor since this is a
|
|
|
|
// non-refcounted class.
|
2014-05-02 22:44:13 +04:00
|
|
|
}
|
|
|
|
|
2020-04-06 17:03:59 +03:00
|
|
|
IPCResult CookieServiceParent::RecvSetCookies(
|
|
|
|
const nsCString& aBaseDomain, const OriginAttributes& aOriginAttributes,
|
2020-04-25 22:10:00 +03:00
|
|
|
nsIURI* aHost, bool aFromHttp, const nsTArray<CookieStruct>& aCookies) {
|
2020-04-06 15:32:45 +03:00
|
|
|
if (!mCookieService) {
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
2010-03-26 02:02:28 +03:00
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// Deserialize URI. Having a host URI is mandatory and should always be
|
2010-03-26 02:02:28 +03:00
|
|
|
// provided by the child; thus we consider failure fatal.
|
2020-04-25 22:10:00 +03:00
|
|
|
if (!aHost) {
|
|
|
|
return IPC_FAIL(this, "aHost must not be null");
|
2020-04-06 15:32:45 +03:00
|
|
|
}
|
2010-03-26 02:02:28 +03:00
|
|
|
|
2018-04-19 14:18:50 +03:00
|
|
|
// We set this to true while processing this cookie update, to make sure
|
|
|
|
// we don't send it back to the same content process.
|
|
|
|
mProcessingCookie = true;
|
2020-04-06 17:03:59 +03:00
|
|
|
|
|
|
|
bool ok = mCookieService->SetCookiesFromIPC(aBaseDomain, aOriginAttributes,
|
2020-04-25 22:10:00 +03:00
|
|
|
aHost, aFromHttp, aCookies);
|
2018-04-19 14:18:50 +03:00
|
|
|
mProcessingCookie = false;
|
2020-04-06 17:03:59 +03:00
|
|
|
return ok ? IPC_OK() : IPC_FAIL(this, "Invalid cookie received.");
|
2010-03-26 02:02:28 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|