Skip to content

Commit

Permalink
Bug 1773701 - Part 2: Implement the email tracking data collection fe…
Browse files Browse the repository at this point in the history
…ature. r=dimi

Differential Revision: https://phabricator.services.mozilla.com/D151522
  • Loading branch information
artines1 committed Jul 19, 2022
1 parent 95975ea commit 0850157
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11725,6 +11725,12 @@
value: @IS_NIGHTLY_BUILD@
mirror: always

# Collecting 3rd party emailtracking telemetry.
- name: privacy.trackingprotection.emailtracking.data_collection.enabled
type: bool
value: true
mirror: always

# Whether Origin Telemetry should be enabled.
# NOTE: if telemetry.origin_telemetry_test_mode.enabled is enabled, this pref
# won't have any effect.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/* -*- 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/net/UrlClassifierCommon.h"
#include "mozilla/StaticPrefs_privacy.h"
#include "mozilla/StaticPtr.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;
} // 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;
}

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 - "
"annotating 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},
};

uint32_t flags = UrlClassifierCommon::TablesToClassificationFlags(
aList, sClassificationData,
nsIClassifiedChannel::ClassificationFlags::CLASSIFIED_EMAILTRACKING);

// The flags will be used for Telemetry in the later patch.
Unused << flags;

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* -*- 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/. */

#ifndef mozilla_net_UrlClassifierFeatureEmailTrackingDataCollection_h
#define mozilla_net_UrlClassifierFeatureEmailTrackingDataCollection_h

#include "UrlClassifierFeatureBase.h"

namespace mozilla::net {

class UrlClassifierFeatureEmailTrackingDataCollection final
: public UrlClassifierFeatureAntiTrackingBase {
public:
static const char* Name();

static void MaybeShutdown();

static already_AddRefed<UrlClassifierFeatureEmailTrackingDataCollection>
MaybeCreate(nsIChannel* aChannel);

static already_AddRefed<nsIUrlClassifierFeature> GetIfNameMatches(
const nsACString& aName);

NS_IMETHOD ProcessChannel(nsIChannel* aChannel,
const nsTArray<nsCString>& aList,
const nsTArray<nsCString>& aHashes,
bool* aShouldContinue) override;

NS_IMETHOD GetURIByListType(nsIChannel* aChannel,
nsIUrlClassifierFeature::listType aListType,
nsIUrlClassifierFeature::URIType* aURIType,
nsIURI** aURI) override;

private:
UrlClassifierFeatureEmailTrackingDataCollection();

static void MaybeInitialize();
};

} // namespace mozilla::net

#endif // mozilla_net_UrlClassifierFeatureEmailTrackingDataCollection_h
25 changes: 25 additions & 0 deletions netwerk/url-classifier/UrlClassifierFeatureFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// List of Features
#include "UrlClassifierFeatureCryptominingAnnotation.h"
#include "UrlClassifierFeatureCryptominingProtection.h"
#include "UrlClassifierFeatureEmailTrackingDataCollection.h"
#include "UrlClassifierFeatureEmailTrackingProtection.h"
#include "UrlClassifierFeatureFingerprintingAnnotation.h"
#include "UrlClassifierFeatureFingerprintingProtection.h"
Expand All @@ -35,6 +36,7 @@ void UrlClassifierFeatureFactory::Shutdown() {

UrlClassifierFeatureCryptominingAnnotation::MaybeShutdown();
UrlClassifierFeatureCryptominingProtection::MaybeShutdown();
UrlClassifierFeatureEmailTrackingDataCollection::MaybeShutdown();
UrlClassifierFeatureEmailTrackingProtection::MaybeShutdown();
UrlClassifierFeatureFingerprintingAnnotation::MaybeShutdown();
UrlClassifierFeatureFingerprintingProtection::MaybeShutdown();
Expand All @@ -60,6 +62,16 @@ void UrlClassifierFeatureFactory::GetFeaturesFromChannel(
// feature order, and this could produce different results with a different
// feature ordering.

// Email Tracking Data Collection
// This needs to be run before other features so that other blocking features
// won't stop us to collect data for email trackers. Note that this feature
// is not a blocking feature.
feature =
UrlClassifierFeatureEmailTrackingDataCollection::MaybeCreate(aChannel);
if (feature) {
aFeatures.AppendElement(feature);
}

// Email Tracking Protection
feature = UrlClassifierFeatureEmailTrackingProtection::MaybeCreate(aChannel);
if (feature) {
Expand Down Expand Up @@ -148,6 +160,13 @@ UrlClassifierFeatureFactory::GetFeatureByName(const nsACString& aName) {
return feature.forget();
}

// Email Tracking Data Collection
feature =
UrlClassifierFeatureEmailTrackingDataCollection::GetIfNameMatches(aName);
if (feature) {
return feature.forget();
}

// Email Tracking Protection
feature =
UrlClassifierFeatureEmailTrackingProtection::GetIfNameMatches(aName);
Expand Down Expand Up @@ -230,6 +249,12 @@ void UrlClassifierFeatureFactory::GetFeatureNames(nsTArray<nsCString>& aArray) {
aArray.AppendElement(name);
}

// Email Tracking Data Collection
name.Assign(UrlClassifierFeatureEmailTrackingDataCollection::Name());
if (!name.IsEmpty()) {
aArray.AppendElement(name);
}

// Email Tracking Protection
name.Assign(UrlClassifierFeatureEmailTrackingProtection::Name());
if (!name.IsEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions netwerk/url-classifier/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ UNIFIED_SOURCES += [
"UrlClassifierFeatureCryptominingAnnotation.cpp",
"UrlClassifierFeatureCryptominingProtection.cpp",
"UrlClassifierFeatureCustomTables.cpp",
"UrlClassifierFeatureEmailTrackingDataCollection.cpp",
"UrlClassifierFeatureEmailTrackingProtection.cpp",
"UrlClassifierFeatureFactory.cpp",
"UrlClassifierFeatureFingerprintingAnnotation.cpp",
Expand Down

0 comments on commit 0850157

Please sign in to comment.