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"
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
;
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
;
61 void UrlClassifierFeatureCryptominingProtection::MaybeInitialize() {
62 UC_LOG_LEAK(("UrlClassifierFeatureCryptominingProtection::MaybeInitialize"));
64 if (!gFeatureCryptominingProtection
) {
65 gFeatureCryptominingProtection
=
66 new UrlClassifierFeatureCryptominingProtection();
67 gFeatureCryptominingProtection
->InitializePreferences();
72 void UrlClassifierFeatureCryptominingProtection::MaybeShutdown() {
73 UC_LOG_LEAK(("UrlClassifierFeatureCryptominingProtection::MaybeShutdown"));
75 if (gFeatureCryptominingProtection
) {
76 gFeatureCryptominingProtection
->ShutdownPreferences();
77 gFeatureCryptominingProtection
= nullptr;
82 already_AddRefed
<UrlClassifierFeatureCryptominingProtection
>
83 UrlClassifierFeatureCryptominingProtection::MaybeCreate(nsIChannel
* aChannel
) {
87 ("UrlClassifierFeatureCryptominingProtection::MaybeCreate - channel %p",
90 if (!StaticPrefs::privacy_trackingprotection_cryptomining_enabled()) {
94 bool isThirdParty
= AntiTrackingUtils::IsThirdPartyChannel(aChannel
);
97 ("UrlClassifierFeatureCryptominingProtection::MaybeCreate - "
98 "skipping first party or top-level load for channel %p",
103 if (!UrlClassifierCommon::ShouldEnableProtectionForChannel(aChannel
)) {
108 MOZ_ASSERT(gFeatureCryptominingProtection
);
110 RefPtr
<UrlClassifierFeatureCryptominingProtection
> self
=
111 gFeatureCryptominingProtection
;
112 return self
.forget();
116 already_AddRefed
<nsIUrlClassifierFeature
>
117 UrlClassifierFeatureCryptominingProtection::GetIfNameMatches(
118 const nsACString
& aName
) {
119 if (!aName
.EqualsLiteral(CRYPTOMINING_FEATURE_NAME
)) {
124 MOZ_ASSERT(gFeatureCryptominingProtection
);
126 RefPtr
<UrlClassifierFeatureCryptominingProtection
> self
=
127 gFeatureCryptominingProtection
;
128 return self
.forget();
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
;
148 UrlClassifierCommon::TablesToString(aList
, list
);
150 ChannelBlockDecision decision
=
151 ChannelClassifierService::OnBeforeBlockChannel(aChannel
, mName
, list
);
152 if (decision
!= ChannelBlockDecision::Blocked
) {
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);
165 ContentBlockingNotifier::OnEvent(aChannel
, event
, false);
168 *aShouldContinue
= true;
172 UrlClassifierCommon::SetBlockedContent(aChannel
, NS_ERROR_CRYPTOMINING_URI
,
176 ("UrlClassifierFeatureCryptominingProtection::ProcessChannel - "
177 "cancelling channel %p",
180 nsCOMPtr
<nsIHttpChannelInternal
> httpChannel
= do_QueryInterface(aChannel
);
183 Unused
<< httpChannel
->CancelByURLClassifier(NS_ERROR_CRYPTOMINING_URI
);
185 Unused
<< aChannel
->Cancel(NS_ERROR_CRYPTOMINING_URI
);
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
);
211 } // namespace mozilla