DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/CookieServiceParent.h"
#include "mozilla/dom/PContentParent.h"
#include "mozilla/net/NeckoParent.h"

#include "mozilla/BasePrincipal.h"
#include "mozilla/ipc/URIUtils.h"
#include "nsArrayUtils.h"
#include "nsCookieService.h"
#include "nsIChannel.h"
#include "nsIEffectiveTLDService.h"
#include "nsIScriptSecurityManager.h"
#include "nsIPrivateBrowsingChannel.h"
#include "nsNetCID.h"
#include "nsPrintfCString.h"

using namespace mozilla::ipc;
using mozilla::BasePrincipal;
using mozilla::OriginAttributes;
using mozilla::dom::PContentParent;
using mozilla::net::NeckoParent;

namespace {

// Ignore failures from this function, as they only affect whether we do or
// don't show a dialog box in private browsing mode if the user sets a pref.
void
CreateDummyChannel(nsIURI* aHostURI, nsIURI* aChannelURI,
                   OriginAttributes& aAttrs, nsIChannel** aChannel)
{
  nsCOMPtr<nsIPrincipal> principal =
    BasePrincipal::CreateCodebasePrincipal(aHostURI, aAttrs);
  if (!principal) {
    return;
  }

  // The following channel is never openend, so it does not matter what
  // securityFlags we pass; let's follow the principle of least privilege.
  nsCOMPtr<nsIChannel> dummyChannel;
  NS_NewChannel(getter_AddRefs(dummyChannel), aChannelURI, principal,
                nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
                nsIContentPolicy::TYPE_INVALID);
  nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(dummyChannel);
  if (!pbChannel) {
    return;
  }

  pbChannel->SetPrivate(aAttrs.mPrivateBrowsingId > 0);
  dummyChannel.forget(aChannel);
}

}

namespace mozilla {
namespace net {

CookieServiceParent::CookieServiceParent()
{
  // Instantiate the cookieservice via the service manager, so it sticks around
  // until shutdown.
  nsCOMPtr<nsICookieService> cs = do_GetService(NS_COOKIESERVICE_CONTRACTID);

  // Get the nsCookieService instance directly, so we can call internal methods.
  mCookieService = nsCookieService::GetSingleton();
  NS_ASSERTION(mCookieService, "couldn't get nsICookieService");
  mProcessingCookie = false;
}

void
GetInfoFromCookie(nsCookie         *aCookie,
                  CookieStruct     &aCookieStruct)
{
  aCookieStruct.name() = aCookie->Name();
  aCookieStruct.value() = aCookie->Value();
  aCookieStruct.host() = aCookie->Host();
  aCookieStruct.path() = aCookie->Path();
  aCookieStruct.expiry() = aCookie->Expiry();
  aCookieStruct.lastAccessed() = aCookie->LastAccessed();
  aCookieStruct.creationTime() = aCookie->CreationTime();
  aCookieStruct.isSession() = aCookie->IsSession();
  aCookieStruct.isSecure() = aCookie->IsSecure();
  aCookieStruct.isHttpOnly() = aCookie->IsHttpOnly();
  aCookieStruct.sameSite() = aCookie->SameSite();
}

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);
    auto cookie = static_cast<nsCookie*>(xpcCookie.get());
    attrs = cookie->OriginAttributesRef();
    GetInfoFromCookie(cookie, cookieStruct);
    if (!cookie->IsHttpOnly()) {
      cookieStructList.AppendElement(cookieStruct);
      attrsList.AppendElement(attrs);
    }
  }
  Unused << SendRemoveBatchDeletedCookies(cookieStructList, attrsList);
}

void
CookieServiceParent::RemoveAll()
{
  Unused << SendRemoveAll();
}

void
CookieServiceParent::RemoveCookie(nsICookie *aCookie)
{
  auto cookie = static_cast<nsCookie*>(aCookie);
  OriginAttributes attrs = cookie->OriginAttributesRef();
  CookieStruct cookieStruct;
  GetInfoFromCookie(cookie, cookieStruct);
  if (!cookie->IsHttpOnly()) {
    Unused << SendRemoveCookie(cookieStruct, attrs);
  }
}

void
CookieServiceParent::AddCookie(nsICookie *aCookie)
{
  auto cookie = static_cast<nsCookie*>(aCookie);
  OriginAttributes attrs = cookie->OriginAttributesRef();
  CookieStruct cookieStruct;
  GetInfoFromCookie(cookie, cookieStruct);
  Unused << SendAddCookie(cookieStruct, attrs);
}

void
CookieServiceParent::TrackCookieLoad(nsIChannel *aChannel)
{
  nsCOMPtr<nsIURI> uri;
  aChannel->GetURI(getter_AddRefs(uri));

  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
  mozilla::OriginAttributes attrs;
  if (loadInfo) {
    attrs = loadInfo->GetOriginAttributes();
  }
  bool isSafeTopLevelNav = NS_IsSafeTopLevelNav(aChannel);
  bool aIsSameSiteForeign = NS_IsSameSiteForeign(aChannel, uri);

  // Send matching cookies to Child.
  nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil;
  thirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
  bool isForeign = true;
  thirdPartyUtil->IsThirdPartyChannel(aChannel, uri, &isForeign);

  bool isTrackingResource = false;
  bool storageAccessGranted = false;
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
  if (httpChannel) {
    isTrackingResource = httpChannel->GetIsTrackingResource();
    // Check first-party storage access even for non-tracking resources, since
    // we will need the result when computing the access rights for the reject
    // foreign cookie behavior mode.
    if (isForeign &&
        AntiTrackingCommon::IsFirstPartyStorageAccessGrantedFor(httpChannel,
                                                                uri,
                                                                nullptr)) {
      storageAccessGranted = true;
    }
  }

  nsTArray<nsCookie*> foundCookieList;
  mCookieService->GetCookiesForURI(uri, isForeign, isTrackingResource,
                                   storageAccessGranted, isSafeTopLevelNav,
                                   aIsSameSiteForeign, false, attrs,
                                   foundCookieList);
  nsTArray<CookieStruct> matchingCookiesList;
  SerialializeCookieList(foundCookieList, matchingCookiesList, uri);
  Unused << SendTrackCookiesLoad(matchingCookiesList, attrs);
}

void
CookieServiceParent::SerialializeCookieList(const nsTArray<nsCookie*> &aFoundCookieList,
                                            nsTArray<CookieStruct>    &aCookiesList,
                                            nsIURI                    *aHostURI)
{
  for (uint32_t i = 0; i < aFoundCookieList.Length(); i++) {
    nsCookie *cookie = aFoundCookieList.ElementAt(i);
    CookieStruct* cookieStruct = aCookiesList.AppendElement();
    cookieStruct->name() = cookie->Name();
    cookieStruct->value() = cookie->Value();
    cookieStruct->host() = cookie->Host();
    cookieStruct->path() = cookie->Path();
    cookieStruct->expiry() = cookie->Expiry();
    cookieStruct->lastAccessed() = cookie->LastAccessed();
    cookieStruct->creationTime() = cookie->CreationTime();
    cookieStruct->isSession() = cookie->IsSession();
    cookieStruct->isSecure() = cookie->IsSecure();
    cookieStruct->sameSite() = cookie->SameSite();
  }
}

mozilla::ipc::IPCResult
CookieServiceParent::RecvPrepareCookieList(const URIParams        &aHost,
                                           const bool             &aIsForeign,
                                           const bool             &aIsTrackingResource,
                                           const bool             &aFirstPartyStorageAccessGranted,
                                           const bool             &aIsSafeTopLevelNav,
                                           const bool             &aIsSameSiteForeign,
                                           const OriginAttributes &aAttrs)
{
  nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);

  // Send matching cookies to Child.
  nsTArray<nsCookie*> foundCookieList;
  mCookieService->GetCookiesForURI(hostURI, aIsForeign, aIsTrackingResource,
                                   aFirstPartyStorageAccessGranted, aIsSafeTopLevelNav,
                                   aIsSameSiteForeign, false, aAttrs,
                                   foundCookieList);
  nsTArray<CookieStruct> matchingCookiesList;
  SerialializeCookieList(foundCookieList, matchingCookiesList, hostURI);
  Unused << SendTrackCookiesLoad(matchingCookiesList, aAttrs);
  return IPC_OK();
}

void
CookieServiceParent::ActorDestroy(ActorDestroyReason aWhy)
{
  // Nothing needed here. Called right before destructor since this is a
  // non-refcounted class.
}

mozilla::ipc::IPCResult
CookieServiceParent::RecvGetCookieString(const URIParams& aHost,
                                         const bool& aIsForeign,
                                         const bool& aIsTrackingResource,
                                         const bool& aFirstPartyStorageAccessGranted,
                                         const bool& aIsSafeTopLevelNav,
                                         const bool& aIsSameSiteForeign,
                                         const OriginAttributes& aAttrs,
                                         nsCString* aResult)
{
  if (!mCookieService)
    return IPC_OK();

  // Deserialize URI. Having a host URI is mandatory and should always be
  // provided by the child; thus we consider failure fatal.
  nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
  if (!hostURI)
    return IPC_FAIL_NO_REASON(this);
  mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aIsTrackingResource,
                                          aFirstPartyStorageAccessGranted, aIsSafeTopLevelNav,
                                          aIsSameSiteForeign, false, aAttrs, *aResult);
  return IPC_OK();
}

mozilla::ipc::IPCResult
CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
                                         const URIParams& aChannelURI,
                                         const bool& aIsForeign,
                                         const bool& aIsTrackingResource,
                                         const bool& aFirstPartyStorageAccessGranted,
                                         const nsCString& aCookieString,
                                         const nsCString& aServerTime,
                                         const OriginAttributes& aAttrs,
                                         const bool& aFromHttp)
{
  if (!mCookieService)
    return IPC_OK();

  // Deserialize URI. Having a host URI is mandatory and should always be
  // provided by the child; thus we consider failure fatal.
  nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
  if (!hostURI)
    return IPC_FAIL_NO_REASON(this);

  nsCOMPtr<nsIURI> channelURI = DeserializeURI(aChannelURI);
  if (!channelURI)
    return IPC_FAIL_NO_REASON(this);

  // This is a gross hack. We've already computed everything we need to know
  // for whether to set this cookie or not, but we need to communicate all of
  // this information through to nsICookiePermission, which indirectly
  // computes the information from the channel. We only care about the
  // aIsPrivate argument as nsCookieService::SetCookieStringInternal deals
  // with aIsForeign before we have to worry about nsCookiePermission trying
  // to use the channel to inspect it.
  nsCOMPtr<nsIChannel> dummyChannel;
  CreateDummyChannel(hostURI, channelURI,
                     const_cast<OriginAttributes&>(aAttrs),
                     getter_AddRefs(dummyChannel));

  // NB: dummyChannel could be null if something failed in CreateDummyChannel.
  nsDependentCString cookieString(aCookieString, 0);

  // 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;
  mCookieService->SetCookieStringInternal(hostURI, aIsForeign,
                                          aIsTrackingResource,
                                          aFirstPartyStorageAccessGranted,
                                          cookieString, aServerTime, aFromHttp,
                                          aAttrs, dummyChannel);
  mProcessingCookie = false;
  return IPC_OK();
}

} // namespace net
} // namespace mozilla