Debian Codegraph @

tests/selenium/fingerprinting_test.py

raw
1 #!/usr/bin/env python
2
3 import unittest
4
5 import pbtest
6
7 from functools import partial
8
9
10 class FingerprintingTest(pbtest.PBSeleniumTest):
11 """Tests to make sure fingerprinting detection works as expected."""
12
13 def get_fillText_source(self):
14 return self.js("""
15 const canvas = document.getElementById("writetome");
16 const ctx = canvas.getContext("2d");
17 return ctx.fillText.toString();
18 """)
19
20 def setUp(self):
21 # enable local learning
22 self.load_url(self.options_url)
23 self.wait_for_script("return window.OPTIONS_INITIALIZED")
24 self.find_el_by_css('a[href="#tab-general-settings"]').click()
25 self.find_el_by_css('#local-learning-checkbox').click()
26
27 def test_canvas_fingerprinting_detection(self, frozen_error=False):
28 SITE_DOMAIN = "efforg.github.io"
29 FIXTURE = "fingerprinting_frozen_error.html" if frozen_error else "fingerprinting.html"
30 FIXTURE_URL = f"https://{SITE_DOMAIN}/privacybadger-test-fixtures/html/{FIXTURE}"
31
32 FP_DOMAIN = "cdn.jsdelivr.net"
33 FP_BASE_DOMAIN = "jsdelivr.net"
34 FP_PATH = "/npm/[email protected]/dist/fingerprint2.min.js"
35
36 # clear pre-trained/seed tracker data
37 self.clear_tracker_data()
38
39 # visit the page
40 self.load_url(FIXTURE_URL)
41
42 # open popup and check slider state
43 def get_sliders(url, category):
44 self.open_popup(url)
45 sliders = self.get_tracker_state()
46 return sliders[category]
47 sliders = pbtest.retry_until(
48 partial(get_sliders, FIXTURE_URL, 'notYetBlocked'), times=3)
49 assert FP_DOMAIN in sliders, (
50 "Canvas fingerprinting domain should be reported in the popup")
51
52 # check that we detected canvas fingerprinting specifically
53 assert "canvas" in self.get_badger_storage('tracking_map')\
54 .get(FP_BASE_DOMAIN, {}).get(SITE_DOMAIN, []), (
55 "Failed to detect canvas fingerprinting script")
56
57 # confirm we recorded the script's domain and path combo
58 assert FP_PATH in self.get_badger_storage('fp_scripts').get(FP_DOMAIN, {}), (
59 "Failed to record the fingerprinting script's path")
60
61 def test_canvas_fingerprinting_detection_frozen(self):
62 self.test_canvas_fingerprinting_detection(True)
63
64 # Privacy Badger overrides a few functions on canvas contexts to check for fingerprinting.
65 # In previous versions, it would restore the native function after a single call. Unfortunately,
66 # this would wipe out polyfills that had also overridden the same functions, such as the very
67 # popular "hidpi-canvas-polyfill".
68 def test_canvas_polyfill_clobbering(self):
69 FIXTURE_URL = (
70 "https://efforg.github.io/privacybadger-test-fixtures/html/"
71 "fingerprinting_canvas_hidpi.html"
72 )
73
74 # visit the page
75 self.load_url(FIXTURE_URL)
76
77 # check that we did not restore the native function (should be hipdi polyfill)
78 assert "[native code]" not in self.get_fillText_source(), (
79 "Canvas context fillText is not native version (polyfill has been retained)")
80
81
82 if __name__ == "__main__":
83 unittest.main()