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
|
/* -*- 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 "PartitioningExceptionList.h"
#include "AntiTrackingLog.h"
#include "nsContentUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
namespace mozilla {
namespace {
inline nsresult CreateExceptionListKey(const nsACString& aFirstPartyOrigin,
const nsACString& aThirdPartyOrigin,
nsACString& aExceptionListKey) {
MOZ_ASSERT(!aFirstPartyOrigin.IsEmpty());
MOZ_ASSERT(!aThirdPartyOrigin.IsEmpty());
// Don't allow exceptions for everything
if (aFirstPartyOrigin.EqualsLiteral("*") &&
aThirdPartyOrigin.EqualsLiteral("*")) {
return NS_ERROR_ILLEGAL_VALUE;
}
aExceptionListKey.Assign(aFirstPartyOrigin);
aExceptionListKey.Append(",");
aExceptionListKey.Append(aThirdPartyOrigin);
return NS_OK;
}
inline nsresult CreateUnifiedOriginString(const nsACString& aInput,
nsACString& aOutput) {
nsCOMPtr<nsIURI> uri;
if (aInput.EqualsLiteral("*")) {
aOutput.AssignLiteral("*");
return NS_OK;
}
nsresult rv = NS_NewURI(getter_AddRefs(uri), aInput);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return nsContentUtils::GetASCIIOrigin(uri, aOutput);
}
StaticRefPtr<PartitioningExceptionList> gPartitioningExceptionList;
} // namespace
NS_IMPL_ISUPPORTS(PartitioningExceptionList,
nsIPartitioningExceptionListObserver)
bool PartitioningExceptionList::Check(const nsACString& aFirstPartyOrigin,
const nsACString& aThirdPartyOrigin) {
if (aFirstPartyOrigin.IsEmpty() || aThirdPartyOrigin.IsEmpty()) {
return false;
}
LOG(("Check partitioning exception list for url %s and %s",
PromiseFlatCString(aFirstPartyOrigin).get(),
PromiseFlatCString(aThirdPartyOrigin).get()));
nsAutoCString key, wildcardFirstPartyKey, wildcardThirdPartyKey;
CreateExceptionListKey(aFirstPartyOrigin, aThirdPartyOrigin, key);
CreateExceptionListKey("*"_ns, aThirdPartyOrigin, wildcardFirstPartyKey);
CreateExceptionListKey(aFirstPartyOrigin, "*"_ns, wildcardThirdPartyKey);
if (GetOrCreate()->mExceptionList.Contains(key) ||
GetOrCreate()->mExceptionList.Contains(wildcardFirstPartyKey) ||
GetOrCreate()->mExceptionList.Contains(wildcardThirdPartyKey)) {
LOG(("URI is in exception list"));
return true;
}
return false;
}
PartitioningExceptionList* PartitioningExceptionList::GetOrCreate() {
if (!gPartitioningExceptionList) {
gPartitioningExceptionList = new PartitioningExceptionList();
gPartitioningExceptionList->Init();
RunOnShutdown([&] {
gPartitioningExceptionList->Shutdown();
gPartitioningExceptionList = nullptr;
});
}
return gPartitioningExceptionList;
}
nsresult PartitioningExceptionList::Init() {
mService =
do_GetService("@mozilla.org/partitioning/exception-list-service;1");
if (NS_WARN_IF(!mService)) {
return NS_ERROR_FAILURE;
}
mService->RegisterAndRunExceptionListObserver(this);
return NS_OK;
}
void PartitioningExceptionList::Shutdown() {
if (mService) {
mService->UnregisterExceptionListObserver(this);
mService = nullptr;
}
mExceptionList.Clear();
}
NS_IMETHODIMP
PartitioningExceptionList::OnExceptionListUpdate(const nsACString& aList) {
mExceptionList.Clear();
nsresult rv;
for (const nsACString& item : aList.Split(';')) {
auto origins = item.Split(',');
auto originsIt = origins.begin();
if (originsIt == origins.end()) {
LOG(("Ignoring empty exception entry"));
continue;
}
nsAutoCString firstPartyOrigin;
rv = CreateUnifiedOriginString(*originsIt, firstPartyOrigin);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
++originsIt;
if (originsIt == origins.end()) {
LOG(("Ignoring incomplete exception entry"));
continue;
}
nsAutoCString thirdPartyOrigin;
rv = CreateUnifiedOriginString(*originsIt, thirdPartyOrigin);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsAutoCString key;
rv = CreateExceptionListKey(firstPartyOrigin, thirdPartyOrigin, key);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
LOG(("onExceptionListUpdate: %s", key.get()));
mExceptionList.AppendElement(key);
}
return NS_OK;
}
} // namespace mozilla
|