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
|
/* -*- 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 "UrlClassifierFeatureEmailTrackingDataCollection.h"
#include "mozilla/AntiTrackingUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/ContentBlockingNotifier.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/net/UrlClassifierCommon.h"
#include "mozilla/StaticPrefs_privacy.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "nsIChannel.h"
#include "nsIClassifiedChannel.h"
#include "nsILoadInfo.h"
namespace mozilla::net {
namespace {
#define EMAILTRACKING_DATACOLLECTION_FEATURE_NAME \
"emailtracking-data-collection"
#define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST \
"urlclassifier.features.emailtracking.datacollection.blocklistTables"
#define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_TEST_ENTRIES \
"urlclassifier.features.emailtracking.datacollection.blocklistHosts"
#define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST \
"urlclassifier.features.emailtracking.datacollection.allowlistTables"
#define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_TEST_ENTRIES \
"urlclassifier.features.emailtracking.datacollection.allowlistHosts"
#define URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_EXCEPTION_URLS \
"urlclassifier.features.emailtracking.datacollection.skipURLs"
#define TABLE_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_PREF \
"emailtracking-data-collection-blocklist-pref"
#define TABLE_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_PREF \
"emailtracking-data-collection-allowlist-pref"
StaticRefPtr<UrlClassifierFeatureEmailTrackingDataCollection>
gFeatureEmailTrackingDataCollection;
StaticAutoPtr<nsCString> gEmailWebAppDomainsPref;
static constexpr char kEmailWebAppDomainPrefName[] =
"privacy.trackingprotection.emailtracking.webapp.domains";
void EmailWebAppDomainPrefChangeCallback(const char* aPrefName, void*) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!strcmp(aPrefName, kEmailWebAppDomainPrefName));
MOZ_ASSERT(gEmailWebAppDomainsPref);
Preferences::GetCString(kEmailWebAppDomainPrefName, *gEmailWebAppDomainsPref);
}
} // namespace
UrlClassifierFeatureEmailTrackingDataCollection::
UrlClassifierFeatureEmailTrackingDataCollection()
: UrlClassifierFeatureAntiTrackingBase(
nsLiteralCString(EMAILTRACKING_DATACOLLECTION_FEATURE_NAME),
nsLiteralCString(
URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST),
nsLiteralCString(
URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST),
nsLiteralCString(
URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_TEST_ENTRIES),
nsLiteralCString(
URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_TEST_ENTRIES),
nsLiteralCString(TABLE_EMAILTRACKING_DATACOLLECTION_BLOCKLIST_PREF),
nsLiteralCString(TABLE_EMAILTRACKING_DATACOLLECTION_ENTITYLIST_PREF),
nsLiteralCString(
URLCLASSIFIER_EMAILTRACKING_DATACOLLECTION_EXCEPTION_URLS)) {}
/* static */
const char* UrlClassifierFeatureEmailTrackingDataCollection::Name() {
return EMAILTRACKING_DATACOLLECTION_FEATURE_NAME;
}
/* static */
void UrlClassifierFeatureEmailTrackingDataCollection::MaybeInitialize() {
UC_LOG_LEAK(
("UrlClassifierFeatureEmailTrackingDataCollection::MaybeInitialize"));
if (!gFeatureEmailTrackingDataCollection) {
gFeatureEmailTrackingDataCollection =
new UrlClassifierFeatureEmailTrackingDataCollection();
gFeatureEmailTrackingDataCollection->InitializePreferences();
}
}
/* static */
void UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown() {
UC_LOG_LEAK(
("UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown"));
if (gFeatureEmailTrackingDataCollection) {
gFeatureEmailTrackingDataCollection->ShutdownPreferences();
gFeatureEmailTrackingDataCollection = nullptr;
}
}
/* static */
already_AddRefed<UrlClassifierFeatureEmailTrackingDataCollection>
UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate(
nsIChannel* aChannel) {
MOZ_ASSERT(aChannel);
UC_LOG_LEAK(
("UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate - channel "
"%p",
aChannel));
if (!StaticPrefs::
privacy_trackingprotection_emailtracking_data_collection_enabled()) {
return nullptr;
}
bool isThirdParty = AntiTrackingUtils::IsThirdPartyChannel(aChannel);
// We don't need to collect data if the email tracker is loaded in first-party
// context.
if (!isThirdParty) {
return nullptr;
}
MaybeInitialize();
MOZ_ASSERT(gFeatureEmailTrackingDataCollection);
RefPtr<UrlClassifierFeatureEmailTrackingDataCollection> self =
gFeatureEmailTrackingDataCollection;
return self.forget();
}
/* static */
already_AddRefed<nsIUrlClassifierFeature>
UrlClassifierFeatureEmailTrackingDataCollection::GetIfNameMatches(
const nsACString& aName) {
if (!aName.EqualsLiteral(EMAILTRACKING_DATACOLLECTION_FEATURE_NAME)) {
return nullptr;
}
MaybeInitialize();
MOZ_ASSERT(gFeatureEmailTrackingDataCollection);
RefPtr<UrlClassifierFeatureEmailTrackingDataCollection> self =
gFeatureEmailTrackingDataCollection;
return self.forget();
}
NS_IMETHODIMP
UrlClassifierFeatureEmailTrackingDataCollection::ProcessChannel(
nsIChannel* aChannel, const nsTArray<nsCString>& aList,
const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
NS_ENSURE_ARG_POINTER(aChannel);
NS_ENSURE_ARG_POINTER(aShouldContinue);
// This is not a blocking feature.
*aShouldContinue = true;
UC_LOG(
("UrlClassifierFeatureEmailTrackingDataCollection::ProcessChannel - "
"collecting data from channel %p",
aChannel));
static std::vector<UrlClassifierCommon::ClassificationData>
sClassificationData = {
{"base-email-track-"_ns,
nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_EMAILTRACKING},
{"content-email-track-"_ns,
nsIClassifiedChannel::ClassificationFlags::
CLASSIFIED_EMAILTRACKING_CONTENT},
};
// Get if the top window is a email webapp.
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
RefPtr<dom::BrowsingContext> bc;
loadInfo->GetBrowsingContext(getter_AddRefs(bc));
if (!bc || bc->IsDiscarded()) {
return NS_OK;
}
RefPtr<dom::WindowGlobalParent> topWindowParent =
bc->Canonical()->GetTopWindowContext();
if (!topWindowParent || topWindowParent->IsDiscarded()) {
return NS_OK;
}
// Cache the email webapp domains pref value and register the callback
// function to update the cached value when the pref changes.
if (!gEmailWebAppDomainsPref) {
gEmailWebAppDomainsPref = new nsCString();
Preferences::RegisterCallbackAndCall(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
RunOnShutdown([]() {
Preferences::UnregisterCallback(EmailWebAppDomainPrefChangeCallback,
kEmailWebAppDomainPrefName);
gEmailWebAppDomainsPref = nullptr;
});
}
bool isTopEmailWebApp = topWindowParent->DocumentPrincipal()->IsURIInList(
*gEmailWebAppDomainsPref);
uint32_t flags = UrlClassifierCommon::TablesToClassificationFlags(
aList, sClassificationData,
nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_EMAILTRACKING);
if (flags & nsIClassifiedChannel::ClassificationFlags::
CLASSIFIED_EMAILTRACKING_CONTENT) {
Telemetry::AccumulateCategorical(
isTopEmailWebApp
? Telemetry::LABELS_EMAIL_TRACKER_COUNT::content_email_webapp
: Telemetry::LABELS_EMAIL_TRACKER_COUNT::content_normal);
// Notify the load event to record the content blocking log.
//
// Note that we need to change the code here if we decided to block content
// email trackers in the future.
ContentBlockingNotifier::OnEvent(
aChannel,
nsIWebProgressListener::STATE_LOADED_EMAILTRACKING_LEVEL_2_CONTENT);
} else {
Telemetry::AccumulateCategorical(
isTopEmailWebApp
? Telemetry::LABELS_EMAIL_TRACKER_COUNT::base_email_webapp
: Telemetry::LABELS_EMAIL_TRACKER_COUNT::base_normal);
// Notify to record content blocking log. Note that we don't need to notify
// if email tracking is enabled because the email tracking protection
// feature will be responsible for notifying the blocking event.
//
// Note that we need to change the code here if we decided to block content
// email trackers in the future.
if (!StaticPrefs::privacy_trackingprotection_emailtracking_enabled()) {
ContentBlockingNotifier::OnEvent(
aChannel,
nsIWebProgressListener::STATE_LOADED_EMAILTRACKING_LEVEL_1_CONTENT);
}
}
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierFeatureEmailTrackingDataCollection::GetURIByListType(
nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
NS_ENSURE_ARG_POINTER(aChannel);
NS_ENSURE_ARG_POINTER(aURIType);
NS_ENSURE_ARG_POINTER(aURI);
if (aListType == nsIUrlClassifierFeature::blocklist) {
*aURIType = nsIUrlClassifierFeature::blocklistURI;
return aChannel->GetURI(aURI);
}
MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist);
*aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI;
return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI);
}
} // namespace mozilla::net
|