Bug 1874684 - Part 13: Pass DateDuration to AddDate/CalendarDateAdd. r=sfink
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureFingerprintingProtection.cpp
blob16a352e4842d025edc559af6a5282696fd10b402
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "UrlClassifierFeatureFingerprintingProtection.h"
9 #include "mozilla/AntiTrackingUtils.h"
10 #include "mozilla/net/UrlClassifierCommon.h"
11 #include "ChannelClassifierService.h"
12 #include "mozilla/StaticPrefs_privacy.h"
13 #include "nsNetUtil.h"
14 #include "mozilla/StaticPtr.h"
15 #include "nsIWebProgressListener.h"
16 #include "nsIHttpChannelInternal.h"
17 #include "nsIChannel.h"
19 namespace mozilla {
20 namespace net {
22 namespace {
24 #define FINGERPRINTING_FEATURE_NAME "fingerprinting-protection"
26 #define URLCLASSIFIER_FINGERPRINTING_BLOCKLIST \
27 "urlclassifier.features.fingerprinting.blacklistTables"
28 #define URLCLASSIFIER_FINGERPRINTING_BLOCKLIST_TEST_ENTRIES \
29 "urlclassifier.features.fingerprinting.blacklistHosts"
30 #define URLCLASSIFIER_FINGERPRINTING_ENTITYLIST \
31 "urlclassifier.features.fingerprinting.whitelistTables"
32 #define URLCLASSIFIER_FINGERPRINTING_ENTITYLIST_TEST_ENTRIES \
33 "urlclassifier.features.fingerprinting.whitelistHosts"
34 #define URLCLASSIFIER_FINGERPRINTING_EXCEPTION_URLS \
35 "urlclassifier.features.fingerprinting.skipURLs"
36 #define TABLE_FINGERPRINTING_BLOCKLIST_PREF "fingerprinting-blacklist-pref"
37 #define TABLE_FINGERPRINTING_ENTITYLIST_PREF "fingerprinting-whitelist-pref"
39 StaticRefPtr<UrlClassifierFeatureFingerprintingProtection>
40 gFeatureFingerprintingProtection;
42 } // namespace
44 UrlClassifierFeatureFingerprintingProtection::
45 UrlClassifierFeatureFingerprintingProtection()
46 : UrlClassifierFeatureAntiTrackingBase(
47 nsLiteralCString(FINGERPRINTING_FEATURE_NAME),
48 nsLiteralCString(URLCLASSIFIER_FINGERPRINTING_BLOCKLIST),
49 nsLiteralCString(URLCLASSIFIER_FINGERPRINTING_ENTITYLIST),
50 nsLiteralCString(URLCLASSIFIER_FINGERPRINTING_BLOCKLIST_TEST_ENTRIES),
51 nsLiteralCString(
52 URLCLASSIFIER_FINGERPRINTING_ENTITYLIST_TEST_ENTRIES),
53 nsLiteralCString(TABLE_FINGERPRINTING_BLOCKLIST_PREF),
54 nsLiteralCString(TABLE_FINGERPRINTING_ENTITYLIST_PREF),
55 nsLiteralCString(URLCLASSIFIER_FINGERPRINTING_EXCEPTION_URLS)) {}
57 /* static */ const char* UrlClassifierFeatureFingerprintingProtection::Name() {
58 return FINGERPRINTING_FEATURE_NAME;
61 /* static */
62 void UrlClassifierFeatureFingerprintingProtection::MaybeInitialize() {
63 UC_LOG_LEAK(
64 ("UrlClassifierFeatureFingerprintingProtection::MaybeInitialize"));
66 if (!gFeatureFingerprintingProtection) {
67 gFeatureFingerprintingProtection =
68 new UrlClassifierFeatureFingerprintingProtection();
69 gFeatureFingerprintingProtection->InitializePreferences();
73 /* static */
74 void UrlClassifierFeatureFingerprintingProtection::MaybeShutdown() {
75 UC_LOG_LEAK(("UrlClassifierFeatureFingerprintingProtection::MaybeShutdown"));
77 if (gFeatureFingerprintingProtection) {
78 gFeatureFingerprintingProtection->ShutdownPreferences();
79 gFeatureFingerprintingProtection = nullptr;
83 /* static */
84 already_AddRefed<UrlClassifierFeatureFingerprintingProtection>
85 UrlClassifierFeatureFingerprintingProtection::MaybeCreate(
86 nsIChannel* aChannel) {
87 MOZ_ASSERT(aChannel);
89 UC_LOG_LEAK(
90 ("UrlClassifierFeatureFingerprintingProtection::MaybeCreate - channel %p",
91 aChannel));
93 if (!StaticPrefs::privacy_trackingprotection_fingerprinting_enabled()) {
94 return nullptr;
97 bool isThirdParty = AntiTrackingUtils::IsThirdPartyChannel(aChannel);
98 if (!isThirdParty) {
99 UC_LOG(
100 ("UrlClassifierFeatureFingerprintingProtection::MaybeCreate - "
101 "skipping first party or top-level load for channel %p",
102 aChannel));
103 return nullptr;
106 if (UrlClassifierCommon::IsPassiveContent(aChannel)) {
107 return nullptr;
110 if (!UrlClassifierCommon::ShouldEnableProtectionForChannel(aChannel)) {
111 return nullptr;
114 MaybeInitialize();
115 MOZ_ASSERT(gFeatureFingerprintingProtection);
117 RefPtr<UrlClassifierFeatureFingerprintingProtection> self =
118 gFeatureFingerprintingProtection;
119 return self.forget();
122 /* static */
123 already_AddRefed<nsIUrlClassifierFeature>
124 UrlClassifierFeatureFingerprintingProtection::GetIfNameMatches(
125 const nsACString& aName) {
126 if (!aName.EqualsLiteral(FINGERPRINTING_FEATURE_NAME)) {
127 return nullptr;
130 MaybeInitialize();
131 MOZ_ASSERT(gFeatureFingerprintingProtection);
133 RefPtr<UrlClassifierFeatureFingerprintingProtection> self =
134 gFeatureFingerprintingProtection;
135 return self.forget();
138 NS_IMETHODIMP
139 UrlClassifierFeatureFingerprintingProtection::ProcessChannel(
140 nsIChannel* aChannel, const nsTArray<nsCString>& aList,
141 const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
142 NS_ENSURE_ARG_POINTER(aChannel);
143 NS_ENSURE_ARG_POINTER(aShouldContinue);
145 bool isAllowListed = UrlClassifierCommon::IsAllowListed(aChannel);
147 // This is a blocking feature.
148 *aShouldContinue = isAllowListed;
150 if (isAllowListed) {
151 return NS_OK;
154 nsAutoCString list;
155 UrlClassifierCommon::TablesToString(aList, list);
157 ChannelBlockDecision decision =
158 ChannelClassifierService::OnBeforeBlockChannel(aChannel, mName, list);
159 if (decision != ChannelBlockDecision::Blocked) {
160 uint32_t event =
161 decision == ChannelBlockDecision::Replaced
162 ? nsIWebProgressListener::STATE_REPLACED_FINGERPRINTING_CONTENT
163 : nsIWebProgressListener::STATE_ALLOWED_FINGERPRINTING_CONTENT;
165 // Need to set aBlocked to True if we replace the Fingerprinter with a shim,
166 // since the shim is treated as a blocked event
167 if (event ==
168 nsIWebProgressListener::STATE_REPLACED_FINGERPRINTING_CONTENT) {
169 ContentBlockingNotifier::OnEvent(aChannel, event, true);
170 } else {
171 ContentBlockingNotifier::OnEvent(aChannel, event, false);
174 *aShouldContinue = true;
175 return NS_OK;
178 UrlClassifierCommon::SetBlockedContent(aChannel, NS_ERROR_FINGERPRINTING_URI,
179 list, ""_ns, ""_ns);
181 UC_LOG(
182 ("UrlClassifierFeatureFingerprintingProtection::ProcessChannel - "
183 "cancelling channel %p",
184 aChannel));
186 nsCOMPtr<nsIHttpChannelInternal> httpChannel = do_QueryInterface(aChannel);
187 if (httpChannel) {
188 Unused << httpChannel->CancelByURLClassifier(NS_ERROR_FINGERPRINTING_URI);
189 } else {
190 Unused << aChannel->Cancel(NS_ERROR_FINGERPRINTING_URI);
193 return NS_OK;
196 NS_IMETHODIMP
197 UrlClassifierFeatureFingerprintingProtection::GetURIByListType(
198 nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
199 nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
200 NS_ENSURE_ARG_POINTER(aChannel);
201 NS_ENSURE_ARG_POINTER(aURIType);
202 NS_ENSURE_ARG_POINTER(aURI);
204 if (aListType == nsIUrlClassifierFeature::blocklist) {
205 *aURIType = nsIUrlClassifierFeature::blocklistURI;
206 return aChannel->GetURI(aURI);
209 MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist);
211 *aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI;
212 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI);
215 } // namespace net
216 } // namespace mozilla