Debian Codegraph @

src/tests/tests/background.js

raw
1 import constants from "../../js/constants.js";
2 import mdfp from "../../js/multiDomainFirstParties.js";
3 import utils from "../../js/utils.js";
4
5 const DNT_COMPLIANT_DOMAIN = 'www.eff.org',
6 DNT_DOMAINS = [
7 DNT_COMPLIANT_DOMAIN,
8 'dnt2.example',
9 'dnt3.example',
10 'dnt4.example',
11 'dnt5.example',
12 ],
13 POLICY_URL = chrome.runtime.getURL('data/dnt-policy.txt');
14
15 let clock,
16 stubbedFetch,
17 xhrSpy,
18 dnt_policy_txt;
19
20 function setupBadgerStorage(badger) {
21 // add foo.com, allowed as seen tracking on only one site
22 badger.storage.action_map.setItem('foo.com', {
23 dnt: false,
24 heuristicAction: constants.ALLOW,
25 nextUpdateTime: 100,
26 userAction: ""
27 });
28 badger.storage.snitch_map.setItem('foo.com', ['a.co']);
29
30 // add sub.bar.com,
31 // blocked after having been recorded tracking on three sites
32 badger.storage.action_map.setItem('bar.com', {
33 dnt: false,
34 heuristicAction: constants.BLOCK,
35 nextUpdateTime: 100,
36 userAction: ""
37 });
38 badger.storage.action_map.setItem('sub.bar.com', {
39 dnt: false,
40 heuristicAction: constants.BLOCK,
41 nextUpdateTime: 100,
42 userAction: ""
43 });
44 badger.storage.snitch_map.setItem('bar.com', ['a.co', 'b.co', 'c.co']);
45 }
46
47 QUnit.module("Background", {
48 before: (assert) => {
49 let done = assert.async();
50
51 // fetch locally stored DNT policy
52 utils.fetchResource(POLICY_URL, function (_, data) {
53 dnt_policy_txt = data;
54
55 // set up fake server to simulate fetch()
56 stubbedFetch = sinon.stub(window, 'fetch');
57 DNT_DOMAINS.forEach(domain => {
58 stubbedFetch
59 .withArgs("https://" + domain + "/.well-known/dnt-policy.txt")
60 .resolves(new Response(dnt_policy_txt));
61 });
62 // all other URLs
63 stubbedFetch.resolves(new Response("not the DNT policy"));
64
65 // set up fake timers to simulate window.setTimeout and co.
66 clock = sinon.useFakeTimers(+new Date());
67
68 done();
69 });
70 },
71
72 beforeEach: (/*assert*/) => {
73 xhrSpy = sinon.spy(utils, "fetchResource");
74 },
75
76 afterEach: (/*assert*/) => {
77 // reset call counts, etc. after each test
78 utils.fetchResource.restore();
79 },
80
81 after: (/*assert*/) => {
82 clock.restore();
83 fetch.restore();
84 }
85 });
86
87 QUnit.test("DNT policy checking", (assert) => {
88 const NUM_TESTS = 2,
89 done = assert.async(NUM_TESTS);
90
91 assert.expect(NUM_TESTS);
92
93 badger.checkForDNTPolicy(DNT_COMPLIANT_DOMAIN, function (successStatus) {
94 assert.ok(successStatus, "Domain returns good DNT policy");
95 done();
96 });
97
98 badger.checkForDNTPolicy('ecorp.example', function (successStatus) {
99 assert.notOk(successStatus, "Domain returns 200 but no valid policy");
100 done();
101 });
102
103 // advance the clock enough to trigger all rate-limited calls
104 clock.tick(constants.DNT_POLICY_CHECK_INTERVAL * NUM_TESTS);
105 });
106
107 QUnit.test("Several checks for same domain resolve to one XHR", (assert) => {
108 const NUM_CHECKS = 5;
109
110 // set recheck time to now
111 badger.storage.touchDNTRecheckTime(DNT_COMPLIANT_DOMAIN, +new Date());
112
113 for (let i = 0; i < NUM_CHECKS; i++) {
114 badger.checkForDNTPolicy(DNT_COMPLIANT_DOMAIN);
115 }
116
117 // advance the clock
118 clock.tick(constants.DNT_POLICY_CHECK_INTERVAL * NUM_CHECKS);
119
120 assert.equal(xhrSpy.callCount, 1, "XHR method gets called exactly once");
121 assert.equal(
122 xhrSpy.getCall(0).args[0],
123 "https://" + DNT_COMPLIANT_DOMAIN + "/.well-known/dnt-policy.txt",
124 "XHR method gets called with expected DNT URL"
125 );
126 });
127
128 QUnit.test("DNT checking is rate limited", (assert) => {
129 const NUM_TESTS = DNT_DOMAINS.length;
130
131 let done = assert.async(NUM_TESTS);
132
133 assert.expect(NUM_TESTS);
134
135 for (let i = 0; i < NUM_TESTS; i++) {
136 badger.checkForDNTPolicy(
137 DNT_DOMAINS[i],
138 function () { // eslint-disable-line no-loop-func
139 assert.equal(xhrSpy.callCount, i+1);
140 clock.tick(constants.DNT_POLICY_CHECK_INTERVAL);
141 done();
142 }
143 );
144 }
145 });
146
147 QUnit.test("DNT checking obeys user setting", (assert) => {
148 const NUM_TESTS = DNT_DOMAINS.length;
149
150 let done = assert.async(NUM_TESTS);
151 let old_dnt_check_func = badger.isCheckingDNTPolicyEnabled;
152
153 assert.expect(NUM_TESTS);
154 badger.isCheckingDNTPolicyEnabled = () => false;
155
156 for (let i = 0; i < NUM_TESTS; i++) {
157 badger.checkForDNTPolicy(DNT_DOMAINS[i]);
158 clock.tick(constants.DNT_POLICY_CHECK_INTERVAL);
159 assert.equal(xhrSpy.callCount, 0);
160 done();
161 }
162
163 badger.isCheckingDNTPolicyEnabled = old_dnt_check_func;
164 });
165
166 // test #1972
167 QUnit.test("mergeUserData does not unblock formerly blocked domains", (assert) => {
168 setupBadgerStorage(badger);
169
170 const SITE_DOMAINS = ['a.co', 'b.co', 'c.co'],
171 USER_DATA = {
172 action_map: {
173 'foo.com': {
174 dnt: false,
175 heuristicAction: constants.BLOCK,
176 nextUpdateTime: 100,
177 userAction: ""
178 }
179 },
180 snitch_map: {
181 'foo.com': SITE_DOMAINS
182 }
183 };
184
185 badger.storage.mergeUserData(USER_DATA);
186
187 assert.equal(
188 badger.storage.action_map.getItem('foo.com').heuristicAction,
189 constants.BLOCK,
190 "foo.com was blocked"
191 );
192 assert.deepEqual(
193 badger.storage.snitch_map.getItem('foo.com'),
194 SITE_DOMAINS,
195 "snitch map was migrated"
196 );
197 });
198
199 QUnit.test("user-blocked domains keep their tracking history", (assert) => {
200 const SITE_DOMAINS = ['a.co', 'b.co'],
201 USER_DATA = {
202 action_map: {
203 'foo.com': {
204 dnt: false,
205 heuristicAction: constants.ALLOW,
206 nextUpdateTime: 100,
207 userAction: constants.USER_BLOCK
208 }
209 },
210 snitch_map: {
211 'foo.com': SITE_DOMAINS
212 }
213 };
214
215 badger.storage.mergeUserData(USER_DATA);
216
217 assert.equal(
218 badger.storage.getAction('foo.com'),
219 constants.USER_BLOCK,
220 "foo.com was blocked"
221 );
222 assert.deepEqual(
223 badger.storage.snitch_map.getItem('foo.com'),
224 SITE_DOMAINS,
225 "snitch map was migrated"
226 );
227 });
228
229 QUnit.test("merging snitch maps results in a blocked domain", (assert) => {
230 setupBadgerStorage(badger);
231
232 // https://github.com/EFForg/privacybadger/pull/2082#issuecomment-401942070
233 const USER_DATA = {
234 action_map: {
235 'foo.com': {
236 dnt: false,
237 heuristicAction: constants.ALLOW,
238 nextUpdateTime: 100,
239 userAction: ""
240 }
241 },
242 snitch_map: {'foo.com': ['b.co', 'c.co']},
243 };
244
245 badger.storage.mergeUserData(USER_DATA);
246
247 assert.equal(
248 badger.storage.action_map.getItem('foo.com').heuristicAction,
249 constants.BLOCK,
250 "foo.com was blocked"
251 );
252 assert.deepEqual(
253 badger.storage.snitch_map.getItem('foo.com'),
254 ['a.co', 'b.co', 'c.co'],
255 "snitch map was combined"
256 );
257 });
258
259 QUnit.test("subdomain that is not blocked does not override subdomain that is", (assert) => {
260 setupBadgerStorage(badger);
261
262 const USER_DATA = {
263 action_map: {
264 'sub.bar.com': {
265 dnt: false,
266 heuristicAction: constants.ALLOW,
267 nextUpdateTime: 100,
268 userAction: ""
269 }
270 },
271 snitch_map: {'bar.com': ['a.co']}
272 };
273
274 badger.storage.mergeUserData(USER_DATA);
275
276 assert.equal(
277 badger.storage.action_map.getItem('sub.bar.com').heuristicAction,
278 constants.BLOCK,
279 "sub.bar.com is still blocked"
280 );
281 assert.deepEqual(
282 badger.storage.snitch_map.getItem('bar.com'),
283 ['a.co', 'b.co', 'c.co'],
284 "snitch map was preserved"
285 );
286 });
287
288 QUnit.test("subdomains on the yellowlist are preserved", (assert) => {
289 const DOMAIN = "example.com",
290 SUBDOMAIN = "cdn.example.com",
291 USER_DATA = {
292 action_map: {
293 [DOMAIN]: {
294 dnt: false,
295 heuristicAction: constants.BLOCK,
296 nextUpdateTime: 100,
297 userAction: ''
298 },
299 [SUBDOMAIN]: {
300 dnt: false,
301 heuristicAction: constants.ALLOW,
302 nextUpdateTime: 0,
303 userAction: ''
304 }
305 },
306 snitch_map: {
307 [DOMAIN]: ['a.co', 'b.co', 'c.co'],
308 }
309 };
310
311 const actionMap = badger.storage.getStore('action_map'),
312 snitchMap = badger.storage.getStore('snitch_map');
313
314 // merge in a blocked parent domain and a subdomain
315 badger.storage.mergeUserData(USER_DATA);
316
317 assert.ok(actionMap.getItem(SUBDOMAIN),
318 SUBDOMAIN + " should have been preserved during merge");
319 assert.equal(badger.storage.getBestAction(SUBDOMAIN), constants.BLOCK);
320
321 // clean up
322 actionMap.deleteItem(DOMAIN);
323 actionMap.deleteItem(SUBDOMAIN);
324 snitchMap.deleteItem(DOMAIN);
325
326 // now add subdomain to yellowlist
327 badger.storage.getStore('cookieblock_list').setItem(SUBDOMAIN, true);
328
329 // and do the merge again
330 badger.storage.mergeUserData(USER_DATA);
331
332 assert.ok(actionMap.getItem(SUBDOMAIN),
333 SUBDOMAIN + " should be present in action_map"
334 );
335 assert.equal(
336 actionMap.getItem(SUBDOMAIN).heuristicAction,
337 constants.COOKIEBLOCK,
338 SUBDOMAIN + " should be cookieblocked"
339 );
340 assert.equal(badger.storage.getBestAction(SUBDOMAIN), constants.COOKIEBLOCK);
341 });
342
343 QUnit.test("mergeUserData() preserves snitch map data when no MDFP", (assert) => {
344 const actionMap = badger.storage.getStore('action_map'),
345 snitchMap = badger.storage.getStore('snitch_map'),
346 TRACKER = 'amazon.com';
347
348 let snitch_map = {
349 [TRACKER]: ['amazonads.com', 'amazing.com', 'amazonrainforest.com']
350 };
351 let action_map = {
352 [TRACKER]: {
353 heuristicAction: "cookieblock",
354 userAction: "",
355 dnt: false,
356 nextUpdateTime: 0,
357 }
358 };
359
360 assert.notOk(actionMap.getItem(TRACKER), "no data before test");
361 assert.notOk(snitchMap.getItem(TRACKER), "no data before test");
362
363 badger.storage.mergeUserData({ action_map, snitch_map });
364
365 assert.deepEqual(
366 actionMap.getItem(TRACKER),
367 action_map[TRACKER],
368 "action map preserved for domain with no MDFP snitch entries"
369 );
370 assert.deepEqual(
371 snitchMap.getItem(TRACKER),
372 snitch_map[TRACKER],
373 "snitch map entry with no MDFP domains remains the same after migration runs"
374 );
375 });
376
377 QUnit.test("mergeUserData() removes MDFP entries from snitch map", (assert) => {
378 const actionMap = badger.storage.getStore('action_map'),
379 snitchMap = badger.storage.getStore('snitch_map'),
380 TRACKER = 'amazon.com';
381
382 let snitch_map = {
383 [TRACKER]: ['amazon.ca', 'amazon.co.jp', 'amazing.com']
384 };
385 let action_map = {
386 [TRACKER]: {
387 heuristicAction: "cookieblock",
388 userAction: "",
389 dnt: false,
390 nextUpdateTime: 0,
391 }
392 };
393
394 assert.notOk(actionMap.getItem(TRACKER), "no data before test");
395 assert.notOk(snitchMap.getItem(TRACKER), "no data before test");
396
397 badger.storage.mergeUserData({ action_map, snitch_map });
398
399 assert.equal(
400 badger.storage.getAction(TRACKER),
401 constants.ALLOW,
402 "action downgraded to not-yet-blocked for partial MDFP domain"
403 );
404 assert.deepEqual(
405 snitchMap.getItem(TRACKER),
406 ["amazing.com"],
407 "MDFP entries were removed, non-MDFP entries were left alone"
408 );
409 });
410
411 QUnit.test("mergeUserData() clears snitch_map when all items are MDFP", (assert) => {
412 const actionMap = badger.storage.getStore('action_map'),
413 snitchMap = badger.storage.getStore('snitch_map'),
414 TRACKER = 'amazon.com';
415
416 let snitch_map = {
417 [TRACKER]: ['amazon.ca', 'amazon.co.jp', 'amazon.es']
418 };
419 let action_map = {
420 [TRACKER]: {
421 heuristicAction: "cookieblock",
422 userAction: "",
423 dnt: false,
424 nextUpdateTime: 0,
425 }
426 };
427
428 assert.notOk(actionMap.getItem(TRACKER), "no data before test");
429 assert.notOk(snitchMap.getItem(TRACKER), "no data before test");
430
431 // confirm all entries are MDFP
432 snitch_map[TRACKER].forEach(domain => {
433 assert.ok(
434 mdfp.isMultiDomainFirstParty(TRACKER, domain),
435 `${domain} is indeed MDFP to ${TRACKER}`
436 );
437 });
438
439 badger.storage.mergeUserData({ action_map, snitch_map });
440
441 assert.equal(
442 badger.storage.getAction(TRACKER),
443 constants.NO_TRACKING,
444 "all MDFP domain is no longer known as a tracker"
445 );
446 assert.notOk(
447 snitchMap.getItem(TRACKER),
448 "all-MDFP snitch_map data was removed entirely"
449 );
450 });
451
452 (function () {
453 let IS_UPDATE, LEARN_LOCALLY;
454
455 let newActionMap = {
456 "google-analytics.com": {
457 dnt: false,
458 heuristicAction: constants.BLOCK,
459 nextUpdateTime: 1602152953782,
460 userAction: ""
461 },
462 "youtube.com": {
463 dnt: false,
464 heuristicAction: constants.COOKIEBLOCK,
465 nextUpdateTime: 0,
466 userAction: ""
467 },
468 };
469 let newSnitchMap = {
470 "google-analytics.com": [
471 "linkedin.com",
472 "google.com",
473 "godaddy.com"
474 ],
475 "youtube.com": [
476 "apache.org",
477 "github.com",
478 "who.int",
479 ],
480 };
481
482 QUnit.module("updateTrackerData()", {
483 before: (/*assert*/) => {
484 IS_UPDATE = badger.isUpdate;
485 LEARN_LOCALLY = badger.getSettings().getItem("learnLocally");
486
487 badger.isUpdate = true;
488 badger.getSettings().setItem("learnLocally", false);
489
490 stubbedFetch = sinon.stub(window, 'fetch');
491 },
492
493 after: (/*assert*/) => {
494 fetch.restore();
495
496 badger.getSettings().setItem("learnLocally", LEARN_LOCALLY);
497 badger.isUpdate = IS_UPDATE;
498 }
499 });
500
501 QUnit.test("user-set sliders are preserved", async (assert) => {
502 const NUM_TESTS = 2;
503 let done = assert.async();
504 assert.expect(NUM_TESTS);
505
506 // initial state
507 ["youtube.com", "linkedin.com", "netflix.com"].forEach(site => {
508 badger.heuristicBlocking.updateTrackerPrevalence(
509 "doubleclick.net", "doubleclick.net", site);
510 });
511 badger.storage.setupUserAction("example.com", constants.USER_COOKIEBLOCK);
512 let customSliders = {
513 "example.com": badger.storage.getStore('action_map').getItem("example.com"),
514 };
515
516 // perform the update
517 stubbedFetch
518 .withArgs(constants.SEED_DATA_LOCAL_URL)
519 .resolves(new Response(JSON.stringify({
520 action_map: newActionMap,
521 snitch_map: newSnitchMap
522 })));
523 await badger.updateTrackerData();
524
525 // check what happened
526 let expectedActionMap = Object.assign(customSliders, newActionMap);
527 assert.deepEqual(
528 badger.storage.getStore('action_map').getItemClones(),
529 expectedActionMap,
530 "action map was replaced but custom slider was kept"
531 );
532 assert.deepEqual(
533 badger.storage.getStore('snitch_map').getItemClones(),
534 newSnitchMap,
535 "snitch map was replaced"
536 );
537
538 done();
539 });
540
541 QUnit.test("user-set actions are added to heuristic actions", async (assert) => {
542 const NUM_TESTS = 2;
543 let done = assert.async();
544 assert.expect(NUM_TESTS);
545
546 // initial state
547 ["youtube.com", "linkedin.com", "netflix.com"].forEach(site => {
548 badger.heuristicBlocking.updateTrackerPrevalence(
549 "doubleclick.net", "doubleclick.net", site);
550 });
551 // youtube.com is also in the incoming action map
552 badger.storage.setupUserAction("youtube.com", constants.USER_BLOCK);
553
554 // perform the update
555 stubbedFetch
556 .withArgs(constants.SEED_DATA_LOCAL_URL)
557 .resolves(new Response(JSON.stringify({
558 action_map: newActionMap,
559 snitch_map: newSnitchMap
560 })));
561 await badger.updateTrackerData();
562
563 // check what happened
564 let expectedActionMap = Object.assign({}, newActionMap);
565 expectedActionMap["youtube.com"].userAction = constants.USER_BLOCK;
566 assert.deepEqual(
567 badger.storage.getStore('action_map').getItemClones(),
568 expectedActionMap,
569 "action map was replaced and custom slider was merged in"
570 );
571 assert.deepEqual(
572 badger.storage.getStore('snitch_map').getItemClones(),
573 newSnitchMap,
574 "snitch map was replaced"
575 );
576
577 done();
578 });
579
580 }());
581
582 QUnit.module("Early-warning checks");
583
584 QUnit.test("REQUESTBODY key is Firefox only", (assert) => {
585 let obro = chrome.webRequest.OnBeforeRequestOptions;
586 if (typeof browser == "object" && typeof browser.runtime.getBrowserInfo == "function") {
587 let done = assert.async();
588 browser.runtime.getBrowserInfo().then(function (info) {
589 if (info.name == "Firefox") {
590 assert.notOk(utils.hasOwn(obro, 'REQUEST_BODY'));
591 assert.ok(utils.hasOwn(obro, 'REQUESTBODY'));
592 } else {
593 assert.ok(utils.hasOwn(obro, 'REQUEST_BODY'));
594 assert.notOk(utils.hasOwn(obro, 'REQUESTBODY'));
595 }
596 done();
597 });
598 } else {
599 assert.ok(utils.hasOwn(obro, 'REQUEST_BODY'));
600 assert.notOk(utils.hasOwn(obro, 'REQUESTBODY'));
601 }
602 });