-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathindex.js
283 lines (254 loc) · 7.91 KB
/
index.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Ghostery Browser Extension
* https://www.ghostery.com/
*
* Copyright 2017-present Ghostery GmbH. All rights reserved.
*
* 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
*/
// TODO: This is a temporary solution to avoid throwing errors in Safari
import './safari-monkey-patch.js';
import AnonymousCommunication from '@whotracksme/webextension-packages/packages/anonymous-communication';
import {
WebRequestPipeline,
UrlReporter,
RequestReporter,
setLogLevel,
describeLoggers,
} from '@whotracksme/webextension-packages/packages/reporting';
import { getBrowserInfo } from '@ghostery/libs';
import prefixedIndexedDBKeyValueStore from './storage-indexeddb.js';
import Storage from './storage-chrome-local.js';
import Options, { observe } from '/store/options.js';
import Request from '../utils/request.js';
import { updateTabStats } from '../stats.js';
import asyncSetup from '../utils/setup.js';
const webRequestPipeline = new WebRequestPipeline();
// Important to call it in a first tick as it assigns chrome. listeners
webRequestPipeline.init();
(async () => {
try {
const key = 'ghosteryReportingLoggerConfig';
const { [key]: config } = (await chrome.storage.local.get(key)) || {};
if (config) {
for (const { level, prefix = '*' } of config) {
setLogLevel(level, { prefix });
}
} else {
setLogLevel('off');
}
} catch (e) {
console.warn('Failed to apply logger overwrites', e);
}
})();
function platformSpecificSettings() {
if (
/iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
) {
// Ghostery extension for Safari on iOS and other Apple mobile devices
return {
ALLOWED_COUNTRY_CODES: ['us', 'de', 'fr'],
PATTERNS_URL: 'https://cdn2.ghostery.com/wtm-safari-ios/patterns.json',
CHANNEL: 'safari-ios',
};
}
if (
/Safari/i.test(navigator.userAgent) &&
/Apple Computer/.test(navigator.vendor) &&
!/Mobi|Android/i.test(navigator.userAgent)
) {
// Ghostery extension for Safari on MacOS (Desktop)
return {
ALLOWED_COUNTRY_CODES: ['us', 'de', 'fr'],
PATTERNS_URL:
'https://cdn2.ghostery.com/wtm-safari-desktop/patterns.json',
CHANNEL: 'safari-desktop',
};
}
if (navigator.userAgent.includes('Android')) {
return {
ALLOWED_COUNTRY_CODES: ['us', 'de', 'fr'],
PATTERNS_URL:
'https://cdn2.ghostery.com/wtm-ghostery-android/patterns.json',
CHANNEL: 'android',
};
}
console.warn(
'No matching config found. Falling back to patterns from Chrome Desktop.',
);
const settings = {
ALLOWED_COUNTRY_CODES: ['us', 'de', 'fr'],
PATTERNS_URL: 'https://cdn2.ghostery.com/wtm-chrome-desktop/patterns.json',
CHANNEL: 'ghostery',
};
if (
navigator.userAgent.includes('Opera') &&
navigator.userAgent.includes('YaBrowser') // same release channel as Opera
) {
settings.CHANNEL = 'opera';
}
return settings;
}
const COLLECTOR_DIRECT_URL = 'https://anonymous-communication.ghostery.net';
const COLLECTOR_PROXY_URL = COLLECTOR_DIRECT_URL; // current we have no proxy configured
const config = {
url: {
COLLECTOR_DIRECT_URL,
COLLECTOR_PROXY_URL,
CONFIG_URL: 'https://api.ghostery.net/api/v1/config',
SAFE_QUORUM_CONFIG_ENDPOINT:
'https://safe-browsing-quorum.privacy.ghostery.net/config',
...platformSpecificSettings(),
},
request: {
configUrl: 'https://cdn.ghostery.com/antitracking/config.json',
remoteWhitelistUrl: 'https://cdn.ghostery.com/antitracking/whitelist/2',
localWhitelistUrl: '/rule_resources/whotracksme',
},
};
const communication = new AnonymousCommunication({
config: config.url,
storage: new Storage('communication'),
connectDatabase: prefixedIndexedDBKeyValueStore('communication'),
});
const urlReporter = new UrlReporter({
config: config.url,
storage: new Storage('reporting'),
connectDatabase: prefixedIndexedDBKeyValueStore('reporting'),
communication,
});
let requestReporter = null;
let pausedDomains = [];
let isAntiTrackingEnabled = Options.blockTrackers;
if (__PLATFORM__ === 'firefox') {
requestReporter = new RequestReporter(config.request, {
webRequestPipeline,
communication,
countryProvider: urlReporter.countryProvider,
trustedClock: communication.trustedClock,
getBrowserInfo,
isRequestAllowed: (state) => {
if (!isAntiTrackingEnabled) {
return true;
}
if (
pausedDomains.some(
({ id }) => id === state.tabUrlParts.domainInfo.domain,
)
) {
return true;
}
return false;
},
onTrackerInteraction: (event, state) => {
if (event === 'observed') {
return;
}
const request = Request.fromRequestDetails({
url: state.url,
originUrl: state.tabUrl,
});
request.modified = true;
updateTabStats(state.tabId, [request]);
},
});
}
const setup = asyncSetup([
observe('terms', async (terms) => {
if (terms) {
await urlReporter.init().catch((e) => {
console.warn(
'Failed to initialize reporting. Leaving the module disabled and continue.',
e,
);
});
if (requestReporter) {
await requestReporter.init();
}
} else {
urlReporter.unload();
if (requestReporter) {
requestReporter.unload();
}
}
}),
observe('blockTrackers', (blockTrackers) => {
isAntiTrackingEnabled = blockTrackers;
}),
observe('paused', (paused) => {
pausedDomains = paused || [];
}),
]);
async function onLocationChange(details) {
try {
setup.pending && (await setup.pending);
} catch (e) {
console.warn('Reporting is unavailable:', e);
return;
}
if (!urlReporter.isActive) return;
const { url, frameId, tabId } = details;
if (frameId !== 0 || url === 'about:blank' || url.startsWith('chrome://')) {
return;
}
// Be aware that the documentation of webNavigation.onCommitted is incomplete
// (https://developer.chrome.com/docs/extensions/reference/webNavigation/#event-onCommitted):
//
// > Fired when a navigation is committed. The document (and the resources
// > it refers to, such as images and subframes) might still be downloading,
// > but at least part of the document has been received from the server and
// > the browser has decided to switch to the new document.
//
// In practice, the event may also trigger for prefetch requests for which
// no tab exists. For instance, it can be reproduced in Chrome by starting
// a Google search from the address bar. Under certain conditions, the first
// search result triggers an extra onCommitted event (even if the user didn't
// click on the link yet).
const tab = await chrome.tabs.get(tabId).catch(() => null);
if (!tab) {
return;
}
// Don't leak information in private tabs (neither by storing on disk nor
// by initiating HTTP requests).
if (tab.incognito) {
return;
}
try {
await urlReporter.analyzeUrl(url);
} catch (e) {
console.warn('Unexpected error in reporting module:', e);
}
}
chrome.webNavigation.onCommitted.addListener((details) => {
onLocationChange(details);
});
if (__PLATFORM__ !== 'safari') {
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
onLocationChange(details);
});
}
chrome.runtime.onMessage.addListener((msg, sender) => {
if (!requestReporter) {
return;
}
if (msg.action === 'mousedown') {
requestReporter.recordClick(msg.event, msg.context, msg.href, sender);
}
});
// for debugging service-workers
globalThis.ghostery = globalThis.ghostery || {};
globalThis.ghostery.WTM = {
communication,
urlReporter,
requestReporter,
config,
webRequestPipeline,
extensionStartedAt: new Date(),
logging: {
setLogLevel,
describeLoggers,
},
};