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 (b6d82b1a6b02)

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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/* -*- 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 "ClearSiteData.h"

#include "mozilla/OriginAttributes.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/Unused.h"
#include "nsASCIIMask.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentSecurityManager.h"
#include "nsContentUtils.h"
#include "nsIClearDataService.h"
#include "nsIHttpChannel.h"
#include "nsIHttpProtocolHandler.h"
#include "nsIObserverService.h"
#include "nsIPrincipal.h"
#include "nsIScriptError.h"
#include "nsIScriptSecurityManager.h"
#include "nsNetUtil.h"

using namespace mozilla;

namespace {

StaticRefPtr<ClearSiteData> gClearSiteData;

}  // namespace

// This object is used to suspend/resume the channel.
class ClearSiteData::PendingCleanupHolder final : public nsIClearDataCallback {
 public:
  NS_DECL_ISUPPORTS

  explicit PendingCleanupHolder(nsIHttpChannel* aChannel)
      : mChannel(aChannel), mPendingOp(false) {}

  nsresult Start() {
    MOZ_ASSERT(!mPendingOp);
    nsresult rv = mChannel->Suspend();
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    mPendingOp = true;
    return NS_OK;
  }

  // nsIClearDataCallback interface

  NS_IMETHOD
  OnDataDeleted(uint32_t aFailedFlags) override {
    MOZ_ASSERT(mPendingOp);
    mPendingOp = false;

    mChannel->Resume();
    mChannel = nullptr;

    return NS_OK;
  }

 private:
  ~PendingCleanupHolder() {
    if (mPendingOp) {
      mChannel->Resume();
    }
  }

  nsCOMPtr<nsIHttpChannel> mChannel;
  bool mPendingOp;
};

NS_INTERFACE_MAP_BEGIN(ClearSiteData::PendingCleanupHolder)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIClearDataCallback)
  NS_INTERFACE_MAP_ENTRY(nsIClearDataCallback)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(ClearSiteData::PendingCleanupHolder)
NS_IMPL_RELEASE(ClearSiteData::PendingCleanupHolder)

/* static */
void ClearSiteData::Initialize() {
  MOZ_ASSERT(!gClearSiteData);
  MOZ_ASSERT(NS_IsMainThread());

  if (!XRE_IsParentProcess()) {
    return;
  }

  RefPtr<ClearSiteData> service = new ClearSiteData();

  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
  if (NS_WARN_IF(!obs)) {
    return;
  }

  obs->AddObserver(service, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC, false);
  obs->AddObserver(service, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
  gClearSiteData = service;
}

/* static */
void ClearSiteData::Shutdown() {
  MOZ_ASSERT(NS_IsMainThread());

  if (!gClearSiteData) {
    return;
  }

  RefPtr<ClearSiteData> service = gClearSiteData;
  gClearSiteData = nullptr;

  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
  if (NS_WARN_IF(!obs)) {
    return;
  }

  obs->RemoveObserver(service, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC);
  obs->RemoveObserver(service, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
}

ClearSiteData::ClearSiteData() = default;
ClearSiteData::~ClearSiteData() = default;

NS_IMETHODIMP
ClearSiteData::Observe(nsISupports* aSubject, const char* aTopic,
                       const char16_t* aData) {
  if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
    Shutdown();
    return NS_OK;
  }

  MOZ_ASSERT(!strcmp(aTopic, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC));

  nsCOMPtr<nsIHttpChannel> channel = do_QueryInterface(aSubject);
  if (NS_WARN_IF(!channel)) {
    return NS_OK;
  }

  ClearDataFromChannel(channel);
  return NS_OK;
}

void ClearSiteData::ClearDataFromChannel(nsIHttpChannel* aChannel) {
  MOZ_ASSERT(aChannel);

  nsresult rv;
  nsCOMPtr<nsIURI> uri;

  nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
  if (NS_WARN_IF(!ssm)) {
    return;
  }

  nsCOMPtr<nsIPrincipal> principal;
  rv = ssm->GetChannelResultPrincipal(aChannel, getter_AddRefs(principal));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  nsCOMPtr<nsIContentSecurityManager> csm =
      do_GetService(NS_CONTENTSECURITYMANAGER_CONTRACTID);

  bool secure;
  rv = csm->IsOriginPotentiallyTrustworthy(principal, &secure);
  if (NS_WARN_IF(NS_FAILED(rv)) || !secure) {
    return;
  }

  // We want to use the final URI to check if Clear-Site-Data should be allowed
  // or not.
  rv = aChannel->GetURI(getter_AddRefs(uri));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  uint32_t flags = ParseHeader(aChannel, uri);
  if (flags == 0) {
    // Nothing to do.
    return;
  }

  int32_t cleanFlags = 0;
  RefPtr<PendingCleanupHolder> holder = new PendingCleanupHolder(aChannel);

  if (flags & eCache) {
    LogOpToConsole(aChannel, uri, eCache);
    cleanFlags |= nsIClearDataService::CLEAR_ALL_CACHES;
  }

  if (flags & eCookies) {
    LogOpToConsole(aChannel, uri, eCookies);
    cleanFlags |= nsIClearDataService::CLEAR_COOKIES;
  }

  if (flags & eStorage) {
    LogOpToConsole(aChannel, uri, eStorage);
    cleanFlags |= nsIClearDataService::CLEAR_DOM_STORAGES;
  }

  if (cleanFlags) {
    nsCOMPtr<nsIClearDataService> csd =
        do_GetService("@mozilla.org/clear-data-service;1");
    MOZ_ASSERT(csd);

    rv = holder->Start();
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return;
    }

    rv = csd->DeleteDataFromPrincipal(principal, false /* user request */,
                                      cleanFlags, holder);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return;
    }
  }
}

uint32_t ClearSiteData::ParseHeader(nsIHttpChannel* aChannel,
                                    nsIURI* aURI) const {
  MOZ_ASSERT(aChannel);

  nsAutoCString headerValue;
  nsresult rv = aChannel->GetResponseHeader(
      NS_LITERAL_CSTRING("Clear-Site-Data"), headerValue);
  if (NS_FAILED(rv)) {
    return 0;
  }

  uint32_t flags = 0;

  nsCCharSeparatedTokenizer token(headerValue, ',');
  while (token.hasMoreTokens()) {
    auto value = token.nextToken();
    value.StripTaggedASCII(mozilla::ASCIIMask::MaskWhitespace());

    if (value.EqualsLiteral("\"cache\"")) {
      flags |= eCache;
      continue;
    }

    if (value.EqualsLiteral("\"cookies\"")) {
      flags |= eCookies;
      continue;
    }

    if (value.EqualsLiteral("\"storage\"")) {
      flags |= eStorage;
      continue;
    }

    if (value.EqualsLiteral("\"*\"")) {
      flags = eCache | eCookies | eStorage;
      break;
    }

    LogErrorToConsole(aChannel, aURI, value);
  }

  return flags;
}

void ClearSiteData::LogOpToConsole(nsIHttpChannel* aChannel, nsIURI* aURI,
                                   Type aType) const {
  nsAutoString type;
  TypeToString(aType, type);

  nsTArray<nsString> params;
  params.AppendElement(type);

  LogToConsoleInternal(aChannel, aURI, "RunningClearSiteDataValue", params);
}

void ClearSiteData::LogErrorToConsole(nsIHttpChannel* aChannel, nsIURI* aURI,
                                      const nsACString& aUnknownType) const {
  nsTArray<nsString> params;
  params.AppendElement(NS_ConvertUTF8toUTF16(aUnknownType));

  LogToConsoleInternal(aChannel, aURI, "UnknownClearSiteDataValue", params);
}

void ClearSiteData::LogToConsoleInternal(
    nsIHttpChannel* aChannel, nsIURI* aURI, const char* aMsg,
    const nsTArray<nsString>& aParams) const {
  MOZ_ASSERT(aChannel);
  MOZ_ASSERT(aURI);

  uint64_t windowID = 0;

  nsresult rv = aChannel->GetTopLevelContentWindowId(&windowID);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  if (!windowID) {
    nsCOMPtr<nsILoadGroup> loadGroup;
    nsresult rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return;
    }

    if (loadGroup) {
      windowID = nsContentUtils::GetInnerWindowID(loadGroup);
    }
  }

  nsAutoString localizedMsg;
  rv = nsContentUtils::FormatLocalizedString(
      nsContentUtils::eSECURITY_PROPERTIES, aMsg, aParams, localizedMsg);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  rv = nsContentUtils::ReportToConsoleByWindowID(
      localizedMsg, nsIScriptError::infoFlag,
      NS_LITERAL_CSTRING("Clear-Site-Data"), windowID, aURI);
  Unused << NS_WARN_IF(NS_FAILED(rv));
}

void ClearSiteData::TypeToString(Type aType, nsAString& aStr) const {
  switch (aType) {
    case eCache:
      aStr.AssignLiteral("cache");
      break;

    case eCookies:
      aStr.AssignLiteral("cookies");
      break;

    case eStorage:
      aStr.AssignLiteral("storage");
      break;

    default:
      MOZ_CRASH("Unknown type.");
  }
}

NS_INTERFACE_MAP_BEGIN(ClearSiteData)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(ClearSiteData)
NS_IMPL_RELEASE(ClearSiteData)