-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathheuristicblocking.js
719 lines (659 loc) · 18.8 KB
/
heuristicblocking.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
* This file is part of Privacy Badger <https://www.eff.org/privacybadger>
* Copyright (C) 2014 Electronic Frontier Foundation
*
* Privacy Badger is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Privacy Badger is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
*/
/* globals badger:false, log:false, URI:false */
var constants = require("constants");
var utils = require("utils");
var incognito = require("incognito");
require.scopes.heuristicblocking = (function() {
/*********************** heuristicblocking scope **/
// make heuristic obj with utils and storage properties and put the things on it
function HeuristicBlocker(pbStorage) {
this.storage = pbStorage;
}
// TODO roll into tabData? -- 6/10/2019 not for now, since tabData is populated
// by the synchronous listeners in webrequests.js and tabOrigins is used by the
// async listeners here; there's no way to enforce ordering of requests among
// those two. Also, tabData is cleaned up every time a tab is closed, so
// dangling requests that don't trigger listeners until after the tab closes are
// impossible to attribute to a tab.
var tabOrigins = { };
var tabURLs = { };
HeuristicBlocker.prototype = {
/**
* Adds Cookie blocking for all more specific domains than the blocked origin
* - if they're on the cb list
*
* @param {String} origin Origin to check
*/
setupSubdomainsForCookieblock: function(origin) {
var cbl = this.storage.getBadgerStorageObject("cookieblock_list");
for (var domain in cbl.getItemClones()) {
if (origin == window.getBaseDomain(domain)) {
this.storage.setupHeuristicAction(domain, constants.COOKIEBLOCK);
}
}
// iterate through all elements of cookie block list
// if element has basedomain add it to action_map
// or update it's action with cookieblock
origin = null;
return false;
},
/**
* Decide if to blacklist and add blacklist filters
* @param {String} baseDomain The base domain (etld+1) to blacklist
* @param {String} fqdn The FQDN
*/
blacklistOrigin: function(baseDomain, fqdn) {
var cbl = this.storage.getBadgerStorageObject("cookieblock_list");
// Setup Cookieblock or block for base domain and fqdn
if (cbl.hasItem(baseDomain)) {
this.storage.setupHeuristicAction(baseDomain, constants.COOKIEBLOCK);
} else {
this.storage.setupHeuristicAction(baseDomain, constants.BLOCK);
}
// Check if a parent domain of the fqdn is on the cookie block list
var set = false;
var thisStorage = this.storage;
_.each(utils.explodeSubdomains(fqdn, true), function(domain) {
if (cbl.hasItem(domain)) {
thisStorage.setupHeuristicAction(fqdn, constants.COOKIEBLOCK);
set = true;
}
});
// if no parent domains are on the cookie block list then block fqdn
if (!set) {
this.storage.setupHeuristicAction(fqdn, constants.BLOCK);
}
this.setupSubdomainsForCookieblock(baseDomain);
},
/**
* Wraps _recordPrevalence for use from webRequest listeners.
* Also saves tab (page) origins. TODO Should be handled by tabData instead.
*
* Called from performance-critical webRequest listeners!
* Use updateTrackerPrevalence for non-webRequest initiated bookkeeping.
*
* @param details are those from onBeforeSendHeaders
* @param {Boolean} check_for_cookie_share whether to check for cookie sharing
* @returns {*}
*/
heuristicBlockingAccounting: function (details, check_for_cookie_share) {
// ignore requests that are outside a tabbed window
if (details.tabId < 0 || !incognito.learningEnabled(details.tabId)) {
return {};
}
let fqdn = (new URI(details.url)).host,
origin = window.getBaseDomain(fqdn);
// if this is a main window request, update tab data and quit
if (details.type == "main_frame") {
tabOrigins[details.tabId] = origin;
tabURLs[details.tabId] = details.url;
return {};
}
let tab_origin = tabOrigins[details.tabId],
self = this;
// ignore first-party requests
if (!tab_origin || origin == tab_origin) {
return {};
}
// short-circuit if we already observed this origin tracking on this site
let firstParties = self.storage.getBadgerStorageObject('snitch_map').getItem(origin);
if (firstParties && firstParties.indexOf(tab_origin) > -1) {
return {};
}
// abort if we already made a decision for this FQDN
let action = self.storage.getAction(fqdn);
if (action != constants.NO_TRACKING && action != constants.ALLOW) {
return {};
}
// check if there are tracking cookies
if (hasCookieTracking(details, origin)) {
self._recordPrevalence(fqdn, origin, tab_origin);
return {};
}
// check for cookie sharing iff this is an image and the request URL has parameters
if (check_for_cookie_share && details.type == 'image' && details.url.indexOf('?') > -1) {
// get all cookies for the top-level frame and pass those to the
// cookie-share accounting function
let tab_url = tabURLs[details.tabId];
chrome.cookies.getAll({
url: tab_url
}, function(cookies) {
if (cookies.length >= 1) {
self.pixelCookieShareAccounting(tab_url, tab_origin, details.url, fqdn, origin, cookies);
}
});
}
},
/**
* Checks for cookie sharing: requests to third-party domains that include
* high entropy data from first-party cookies (associated with the top-level
* frame). Only catches plain-text verbatim sharing (b64 encoding + the like
* defeat it). Assumes any long string that doesn't contain URL fragments or
* stopwords is an identifier. Doesn't catch cookie syncing (3rd party -> 3rd
* party), but most of those tracking cookies should be blocked anyway.
*
* @param details are those from onBeforeSendHeaders
* @param cookies are the result of chrome.cookies.getAll()
* @returns {*}
*/
pixelCookieShareAccounting: function (tab_url, tab_origin, request_url, request_fqdn, request_origin, cookies) {
let params = (new URL(request_url)).searchParams,
TRACKER_ENTROPY_THRESHOLD = 33,
MIN_STR_LEN = 8;
for (let p of params) {
let key = p[0],
value = p[1];
// the argument must be sufficiently long
if (!value || value.length < MIN_STR_LEN) {
continue;
}
// check if this argument is derived from a high-entropy first-party cookie
for (let cookie of cookies) {
// the cookie value must be sufficiently long
if (!cookie.value || cookie.value.length < MIN_STR_LEN) {
continue;
}
// find the longest common substring between this arg and the cookies
// associated with the document
let substrings = utils.findCommonSubstrings(cookie.value, value) || [];
for (let s of substrings) {
// ignore the substring if it's part of the first-party URL. sometimes
// content servers take the url of the page they're hosting content
// for as an argument. e.g.
// https://example-cdn.com/content?u=http://example.com/index.html
if (tab_url.indexOf(s) != -1) {
continue;
}
// elements of the user agent string are also commonly included in
// both cookies and arguments; e.g. "Mozilla/5.0" might be in both.
// This is not a special tracking risk since third parties can see
// this info anyway.
if (navigator.userAgent.indexOf(s) != -1) {
continue;
}
// Sometimes the entire url and then some is included in the
// substring -- the common string might be "https://example.com/:true"
// In that case, we only care about the information around the URL.
if (s.indexOf(tab_url) != -1) {
s = s.replace(tab_url, "");
}
// During testing we found lots of common values like "homepage",
// "referrer", etc. were being flagged as high entropy. This searches
// for a few of those and removes them before we go further.
let lower = s.toLowerCase();
lowEntropyQueryValues.forEach(function (qv) {
let start = lower.indexOf(qv);
if (start != -1) {
s = s.replace(s.substring(start, start + qv.length), "");
}
});
// at this point, since we might have removed things, make sure the
// string is still long enough to bother with
if (s.length < MIN_STR_LEN) {
continue;
}
// compute the entropy of this common substring. if it's greater than
// our threshold, record the tracking action and exit the function.
let entropy = utils.estimateMaxEntropy(s);
if (entropy > TRACKER_ENTROPY_THRESHOLD) {
log("Found high-entropy cookie share from", tab_origin, "to", request_fqdn,
":", entropy, "bits\n cookie:", cookie.name, '=', cookie.value,
"\n arg:", key, "=", value, "\n substring:", s);
this._recordPrevalence(request_fqdn, request_origin, tab_origin);
return;
}
}
}
}
},
/**
* Wraps _recordPrevalence for use outside of webRequest listeners.
*
* @param {String} tracker_fqdn The fully qualified domain name of the tracker
* @param {String} page_origin The base domain of the page
* where the tracker was detected.
* @param {Boolean} skip_dnt_check Skip DNT policy checking if flag is true.
*
*/
updateTrackerPrevalence: function(tracker_fqdn, page_origin, skip_dnt_check) {
// abort if we already made a decision for this fqdn
let action = this.storage.getAction(tracker_fqdn);
if (action != constants.NO_TRACKING && action != constants.ALLOW) {
return;
}
this._recordPrevalence(
tracker_fqdn,
window.getBaseDomain(tracker_fqdn),
page_origin,
skip_dnt_check
);
},
/**
* Record HTTP request prevalence. Block a tracker if seen on more
* than constants.TRACKING_THRESHOLD pages
*
* NOTE: This is a private function and should never be called directly.
* All calls should be routed through heuristicBlockingAccounting for normal usage
* and updateTrackerPrevalence for manual modifications (e.g. importing
* tracker lists).
*
* @param {String} tracker_fqdn The FQDN of the third party tracker
* @param {String} tracker_origin Base domain of the third party tracker
* @param {String} page_origin The origin of the page where the third party
* tracker was loaded.
* @param {Boolean} skip_dnt_check Skip DNT policy checking if flag is true.
*/
_recordPrevalence: function (tracker_fqdn, tracker_origin, page_origin, skip_dnt_check) {
var snitchMap = this.storage.getBadgerStorageObject('snitch_map');
var firstParties = [];
if (snitchMap.hasItem(tracker_origin)) {
firstParties = snitchMap.getItem(tracker_origin);
}
if (firstParties.indexOf(page_origin) != -1) {
return; // We already know about the presence of this tracker on the given domain
}
// Check this just-seen-tracking-on-this-site,
// not-yet-blocked domain for DNT policy.
// We check heuristically-blocked domains in webrequest.js.
if (!skip_dnt_check) {
setTimeout(function () {
badger.checkForDNTPolicy(tracker_fqdn);
}, 0);
}
// record that we've seen this tracker on this domain (in snitch map)
firstParties.push(page_origin);
snitchMap.setItem(tracker_origin, firstParties);
// ALLOW indicates this is a tracker still below TRACKING_THRESHOLD
// (vs. NO_TRACKING for resources we haven't seen perform tracking yet).
// see https://github.com/EFForg/privacybadger/pull/1145#discussion_r96676710
// TODO missing tests: removing below lines/messing up parameters
// should break integration tests, but currently does not
this.storage.setupHeuristicAction(tracker_fqdn, constants.ALLOW);
this.storage.setupHeuristicAction(tracker_origin, constants.ALLOW);
// Blocking based on outbound cookies
var httpRequestPrevalence = firstParties.length;
// block the origin if it has been seen on multiple first party domains
if (httpRequestPrevalence >= constants.TRACKING_THRESHOLD) {
log('blacklisting origin', tracker_fqdn);
this.blacklistOrigin(tracker_origin, tracker_fqdn);
}
}
};
// This maps cookies to a rough estimate of how many bits of
// identifying info we might be letting past by allowing them.
// (map values to lower case before using)
// TODO: We need a better heuristic
var lowEntropyCookieValues = {
"":3,
"nodata":3,
"no_data":3,
"yes":3,
"no":3,
"true":3,
"false":3,
"dnt":3,
"opt-out":3,
"optout":3,
"opt_out":3,
"0":4,
"1":4,
"2":4,
"3":4,
"4":4,
"5":4,
"6":4,
"7":4,
"8":4,
"9":4,
// ISO 639-1 language codes
"aa":8,
"ab":8,
"ae":8,
"af":8,
"ak":8,
"am":8,
"an":8,
"ar":8,
"as":8,
"av":8,
"ay":8,
"az":8,
"ba":8,
"be":8,
"bg":8,
"bh":8,
"bi":8,
"bm":8,
"bn":8,
"bo":8,
"br":8,
"bs":8,
"by":8,
"ca":8,
"ce":8,
"ch":8,
"co":8,
"cr":8,
"cs":8,
"cu":8,
"cv":8,
"cy":8,
"da":8,
"de":8,
"dv":8,
"dz":8,
"ee":8,
"el":8,
"en":8,
"eo":8,
"es":8,
"et":8,
"eu":8,
"fa":8,
"ff":8,
"fi":8,
"fj":8,
"fo":8,
"fr":8,
"fy":8,
"ga":8,
"gd":8,
"gl":8,
"gn":8,
"gu":8,
"gv":8,
"ha":8,
"he":8,
"hi":8,
"ho":8,
"hr":8,
"ht":8,
"hu":8,
"hy":8,
"hz":8,
"ia":8,
"id":8,
"ie":8,
"ig":8,
"ii":8,
"ik":8,
"in":8,
"io":8,
"is":8,
"it":8,
"iu":8,
"ja":8,
"jv":8,
"ka":8,
"kg":8,
"ki":8,
"kj":8,
"kk":8,
"kl":8,
"km":8,
"kn":8,
"ko":8,
"kr":8,
"ks":8,
"ku":8,
"kv":8,
"kw":8,
"ky":8,
"la":8,
"lb":8,
"lg":8,
"li":8,
"ln":8,
"lo":8,
"lt":8,
"lu":8,
"lv":8,
"mg":8,
"mh":8,
"mi":8,
"mk":8,
"ml":8,
"mn":8,
"mr":8,
"ms":8,
"mt":8,
"my":8,
"na":8,
"nb":8,
"nd":8,
"ne":8,
"ng":8,
"nl":8,
"nn":8,
"nr":8,
"nv":8,
"ny":8,
"oc":8,
"of":8,
"oj":8,
"om":8,
"or":8,
"os":8,
"pa":8,
"pi":8,
"pl":8,
"ps":8,
"pt":8,
"qu":8,
"rm":8,
"rn":8,
"ro":8,
"ru":8,
"rw":8,
"sa":8,
"sc":8,
"sd":8,
"se":8,
"sg":8,
"si":8,
"sk":8,
"sl":8,
"sm":8,
"sn":8,
"so":8,
"sq":8,
"sr":8,
"ss":8,
"st":8,
"su":8,
"sv":8,
"sw":8,
"ta":8,
"te":8,
"tg":8,
"th":8,
"ti":8,
"tk":8,
"tl":8,
"tn":8,
"to":8,
"tr":8,
"ts":8,
"tt":8,
"tw":8,
"ty":8,
"ug":8,
"uk":8,
"ur":8,
"uz":8,
"ve":8,
"vi":8,
"vo":8,
"wa":8,
"wo":8,
"xh":8,
"yi":8,
"yo":8,
"za":8,
"zh":8,
"zu":8
};
const lowEntropyQueryValues = [
"https",
"http",
"://",
"%3A%2F%2F",
"www",
"url",
"undefined",
"impression",
"session",
"homepage",
"client",
"version",
"business",
"title",
"get",
"site",
"name",
"category",
"account_id",
"smartadserver",
"front",
"page",
"view",
"first",
"visit",
"platform",
"language",
"automatic",
"disabled",
"landing",
"entertainment",
"amazon",
"official",
"webvisor",
"anonymous",
"across",
"narrative",
"\":null",
"\":false",
"\":\"",
"\",\"",
"\",\"",
];
/**
* Extract cookies from onBeforeSendHeaders
*
* @param details Details for onBeforeSendHeaders
* @returns {*} an array combining all Cookies
*/
function _extractCookies(details) {
let cookies = [],
headers = [];
if (details.requestHeaders) {
headers = details.requestHeaders;
} else if (details.responseHeaders) {
headers = details.responseHeaders;
}
for (let i = 0; i < headers.length; i++) {
let header = headers[i];
if (header.name.toLowerCase() == "cookie" || header.name.toLowerCase() == "set-cookie") {
cookies.push(header.value);
}
}
return cookies;
}
/**
* Check if page is doing cookie tracking. Doing this by estimating the entropy of the cookies
*
* @param details details onBeforeSendHeaders details
* @param {String} origin URL
* @returns {boolean} true if it has cookie tracking
*/
function hasCookieTracking(details, origin) {
let cookies = _extractCookies(details);
if (!cookies.length) {
return false;
}
let estimatedEntropy = 0;
// loop over every cookie
for (let i = 0; i < cookies.length; i++) {
let cookie = utils.parseCookie(cookies[i], {
noDecode: true,
skipAttributes: true,
skipNonValues: true
});
// loop over every name/value pair in every cookie
for (let name in cookie) {
if (!cookie.hasOwnProperty(name)) {
continue;
}
// ignore CloudFlare
if (name == "__cfduid") {
continue;
}
let value = cookie[name].toLowerCase();
if (!(value in lowEntropyCookieValues)) {
return true;
}
estimatedEntropy += lowEntropyCookieValues[value];
}
}
log("All cookies for " + origin + " deemed low entropy...");
if (estimatedEntropy > constants.MAX_COOKIE_ENTROPY) {
log("But total estimated entropy is " + estimatedEntropy + " bits, so blocking");
return true;
}
return false;
}
function startListeners() {
/**
* Adds heuristicBlockingAccounting as listened to onBeforeSendHeaders request
*/
let extraInfoSpec = ['requestHeaders'];
if (chrome.webRequest.OnBeforeSendHeadersOptions.hasOwnProperty('EXTRA_HEADERS')) {
extraInfoSpec.push('extraHeaders');
}
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
return badger.heuristicBlocking.heuristicBlockingAccounting(details, true);
}, {urls: ["<all_urls>"]}, extraInfoSpec);
/**
* Adds onResponseStarted listener. Monitor for cookies
*/
extraInfoSpec = ['responseHeaders'];
if (chrome.webRequest.OnResponseStartedOptions.hasOwnProperty('EXTRA_HEADERS')) {
extraInfoSpec.push('extraHeaders');
}
chrome.webRequest.onResponseStarted.addListener(function(details) {
var hasSetCookie = false;
for (var i = 0; i < details.responseHeaders.length; i++) {
if (details.responseHeaders[i].name.toLowerCase() == "set-cookie") {
hasSetCookie = true;
break;
}
}
if (hasSetCookie) {
return badger.heuristicBlocking.heuristicBlockingAccounting(details, false);
}
},
{urls: ["<all_urls>"]}, extraInfoSpec);
}
/************************************** exports */
var exports = {};
exports.HeuristicBlocker = HeuristicBlocker;
exports.startListeners = startListeners;
exports.hasCookieTracking = hasCookieTracking;
return exports;
/************************************** exports */
})();