Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / netwerk / url-classifier / UrlClassifierFeatureCryptominingProtection.cpp
blob72406179bc7131149b1b5294ac8bd9273cf8f896
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 "UrlClassifierFeatureCryptominingProtection.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 CRYPTOMINING_FEATURE_NAME "cryptomining-protection"
26 #define URLCLASSIFIER_CRYPTOMINING_BLOCKLIST \
27 "urlclassifier.features.cryptomining.blacklistTables"
28 #define URLCLASSIFIER_CRYPTOMINING_BLOCKLIST_TEST_ENTRIES \
29 "urlclassifier.features.cryptomining.blacklistHosts"
30 #define URLCLASSIFIER_CRYPTOMINING_ENTITYLIST \
31 "urlclassifier.features.cryptomining.whitelistTables"
32 #define URLCLASSIFIER_CRYPTOMINING_ENTITYLIST_TEST_ENTRIES \
33 "urlclassifier.features.cryptomining.whitelistHosts"
34 #define URLCLASSIFIER_CRYPTOMINING_EXCEPTION_URLS \
35 "urlclassifier.features.cryptomining.skipURLs"
36 #define TABLE_CRYPTOMINING_BLOCKLIST_PREF "cryptomining-blacklist-pref"
37 #define TABLE_CRYPTOMINING_ENTITYLIST_PREF "cryptomining-whitelist-pref"
39 StaticRefPtr<UrlClassifierFeatureCryptominingProtection>
40 gFeatureCryptominingProtection;
42 } // namespace
44 UrlClassifierFeatureCryptominingProtection::
45 UrlClassifierFeatureCryptominingProtection()
46 : UrlClassifierFeatureAntiTrackingBase(
47 nsLiteralCString(CRYPTOMINING_FEATURE_NAME),
48 nsLiteralCString(URLCLASSIFIER_CRYPTOMINING_BLOCKLIST),
49 nsLiteralCString(URLCLASSIFIER_CRYPTOMINING_ENTITYLIST),
50 nsLiteralCString(URLCLASSIFIER_CRYPTOMINING_BLOCKLIST_TEST_ENTRIES),
51 nsLiteralCString(URLCLASSIFIER_CRYPTOMINING_ENTITYLIST_TEST_ENTRIES),
52 nsLiteralCString(TABLE_CRYPTOMINING_BLOCKLIST_PREF),
53 nsLiteralCString(TABLE_CRYPTOMINING_ENTITYLIST_PREF),
54 nsLiteralCString(URLCLASSIFIER_CRYPTOMINING_EXCEPTION_URLS)) {}
56 /* static */ const char* UrlClassifierFeatureCryptominingProtection::Name() {
57 return CRYPTOMINING_FEATURE_NAME;
60 /* static */
61 void UrlClassifierFeatureCryptominingProtection::MaybeInitialize() {
62 UC_LOG_LEAK(("UrlClassifierFeatureCryptominingProtection::MaybeInitialize"));
64 if (!gFeatureCryptominingProtection) {
65 gFeatureCryptominingProtection =
66 new UrlClassifierFeatureCryptominingProtection();
67 gFeatureCryptominingProtection->InitializePreferences();
71 /* static */
72 void UrlClassifierFeatureCryptominingProtection::MaybeShutdown() {
73 UC_LOG_LEAK(("UrlClassifierFeatureCryptominingProtection::MaybeShutdown"));
75 if (gFeatureCryptominingProtection) {
76 gFeatureCryptominingProtection->ShutdownPreferences();
77 gFeatureCryptominingProtection = nullptr;
81 /* static */
82 already_AddRefed<UrlClassifierFeatureCryptominingProtection>
83 UrlClassifierFeatureCryptominingProtection::MaybeCreate(nsIChannel* aChannel) {
84 MOZ_ASSERT(aChannel);
86 UC_LOG_LEAK(
87 ("UrlClassifierFeatureCryptominingProtection::MaybeCreate - channel %p",
88 aChannel));
90 if (!StaticPrefs::privacy_trackingprotection_cryptomining_enabled()) {
91 return nullptr;
94 bool isThirdParty = AntiTrackingUtils::IsThirdPartyChannel(aChannel);
95 if (!isThirdParty) {
96 UC_LOG(
97 ("UrlClassifierFeatureCryptominingProtection::MaybeCreate - "
98 "skipping first party or top-level load for channel %p",
99 aChannel));
100 return nullptr;
103 if (!UrlClassifierCommon::ShouldEnableProtectionForChannel(aChannel)) {
104 return nullptr;
107 MaybeInitialize();
108 MOZ_ASSERT(gFeatureCryptominingProtection);
110 RefPtr<UrlClassifierFeatureCryptominingProtection> self =
111 gFeatureCryptominingProtection;
112 return self.forget();
115 /* static */
116 already_AddRefed<nsIUrlClassifierFeature>
117 UrlClassifierFeatureCryptominingProtection::GetIfNameMatches(
118 const nsACString& aName) {
119 if (!aName.EqualsLiteral(CRYPTOMINING_FEATURE_NAME)) {
120 return nullptr;
123 MaybeInitialize();
124 MOZ_ASSERT(gFeatureCryptominingProtection);
126 RefPtr<UrlClassifierFeatureCryptominingProtection> self =
127 gFeatureCryptominingProtection;
128 return self.forget();
131 NS_IMETHODIMP
132 UrlClassifierFeatureCryptominingProtection::ProcessChannel(
133 nsIChannel* aChannel, const nsTArray<nsCString>& aList,
134 const nsTArray<nsCString>& aHashes, bool* aShouldContinue) {
135 NS_ENSURE_ARG_POINTER(aChannel);
136 NS_ENSURE_ARG_POINTER(aShouldContinue);
138 bool isAllowListed = UrlClassifierCommon::IsAllowListed(aChannel);
140 // This is a blocking feature.
141 *aShouldContinue = isAllowListed;
143 if (isAllowListed) {
144 return NS_OK;
147 nsAutoCString list;
148 UrlClassifierCommon::TablesToString(aList, list);
150 ChannelBlockDecision decision =
151 ChannelClassifierService::OnBeforeBlockChannel(aChannel, mName, list);
152 if (decision != ChannelBlockDecision::Blocked) {
153 uint32_t event =
154 decision == ChannelBlockDecision::Replaced
155 ? nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT
156 : nsIWebProgressListener::STATE_ALLOWED_TRACKING_CONTENT;
158 // Need to set aBlocked to True if we replace the Cryptominer with a shim,
159 // since the shim is treated as a blocked event
160 // Note: If we need to account for which kind of tracker was replaced,
161 // we need to create a new event type in nsIWebProgressListener
162 if (event == nsIWebProgressListener::STATE_REPLACED_TRACKING_CONTENT) {
163 ContentBlockingNotifier::OnEvent(aChannel, event, true);
164 } else {
165 ContentBlockingNotifier::OnEvent(aChannel, event, false);
168 *aShouldContinue = true;
169 return NS_OK;
172 UrlClassifierCommon::SetBlockedContent(aChannel, NS_ERROR_CRYPTOMINING_URI,
173 list, ""_ns, ""_ns);
175 UC_LOG(
176 ("UrlClassifierFeatureCryptominingProtection::ProcessChannel - "
177 "cancelling channel %p",
178 aChannel));
180 nsCOMPtr<nsIHttpChannelInternal> httpChannel = do_QueryInterface(aChannel);
182 if (httpChannel) {
183 Unused << httpChannel->CancelByURLClassifier(NS_ERROR_CRYPTOMINING_URI);
184 } else {
185 Unused << aChannel->Cancel(NS_ERROR_CRYPTOMINING_URI);
188 return NS_OK;
191 NS_IMETHODIMP
192 UrlClassifierFeatureCryptominingProtection::GetURIByListType(
193 nsIChannel* aChannel, nsIUrlClassifierFeature::listType aListType,
194 nsIUrlClassifierFeature::URIType* aURIType, nsIURI** aURI) {
195 NS_ENSURE_ARG_POINTER(aChannel);
196 NS_ENSURE_ARG_POINTER(aURIType);
197 NS_ENSURE_ARG_POINTER(aURI);
199 if (aListType == nsIUrlClassifierFeature::blocklist) {
200 *aURIType = nsIUrlClassifierFeature::blocklistURI;
201 return aChannel->GetURI(aURI);
204 MOZ_ASSERT(aListType == nsIUrlClassifierFeature::entitylist);
206 *aURIType = nsIUrlClassifierFeature::pairwiseEntitylistURI;
207 return UrlClassifierCommon::CreatePairwiseEntityListURI(aChannel, aURI);
210 } // namespace net
211 } // namespace mozilla