Debian Codegraph @

tests/selenium/dnt_test.py

raw
1 #!/usr/bin/env python
2
3 import json
4 import unittest
5
6 import pytest
7
8 import pbtest
9
10 from functools import partial
11
12 from selenium.common.exceptions import NoSuchElementException
13 from selenium.webdriver.common.by import By
14
15 from pbtest import retry_until
16
17
18 class DntTest(pbtest.PBSeleniumTest):
19 """Tests to make sure DNT policy checking works as expected."""
20
21 def setUp(self):
22 self.FIXTURE_DOMAIN = "efforg.github.io"
23 self.FIXTURE_PARENT_DOMAIN = "github.io"
24 self.FIXTURE_URL = (
25 f"https://{self.FIXTURE_DOMAIN}/privacybadger-test-fixtures/html/")
26 # TODO switch to scripting in Firefox (MV2) to remove delay
27 # TODO https://github.com/EFForg/privacybadger/issues/2948
28 self.FIXTURE_URL += "navigator_donottrack_delayed.html"
29
30 def get_first_party_headers(self, url):
31 self.load_url(url)
32
33 text = self.driver.find_element(By.TAG_NAME, 'body').text
34
35 try:
36 # work around MS Edge JSON viewer garbage
37 if text.startswith('1\n'):
38 text = '{' + text.partition('{')[2]
39 headers = json.loads(text)['headers']
40 except ValueError:
41 print(f"\nFailed to parse JSON from {repr(text)}")
42 return None
43
44 return headers
45
46 def set_dnt_hashes(self):
47 # MEGAHACK: make sha1 of "cookies=0" a valid DNT hash
48 # so that the DNT policy checks to the domain that replies with cookies=X
49 # will succeed when we don't send cookies along with the request
50 self.load_url(self.options_url)
51 self.driver.execute_async_script(
52 "let done = arguments[arguments.length - 1];"
53 "chrome.runtime.sendMessage({"
54 " type: 'setDntHashes',"
55 " value: { 'cookies=0 test policy': 'f63ee614ebd77f8634b92633c6bb809a64b9a3d7' }"
56 "}, done);")
57
58 def assert_navigator_gpc_unset(self, msg=""):
59 # GPC on Navigator should be unset (Chrome) or False (Firefox)
60 assert self.js("""
61 return (typeof navigator.globalPrivacyControl == 'undefined' ||
62 navigator.globalPrivacyControl === false);"""), msg
63
64 def test_dnt_policy_check_should_happen_for_blocked_domains(self):
65 PAGE_URL = (
66 "https://efforg.github.io/privacybadger-test-fixtures/html/"
67 "recording_nontracking_domains.html"
68 )
69 DNT_DOMAIN = "dnt-request-cookies-test.trackersimulator.org"
70
71 self.clear_tracker_data()
72
73 self.set_dnt_hashes()
74
75 # mark the "DNT-compliant" domain for blocking
76 self.block_domain(DNT_DOMAIN)
77
78 # visit a page that loads a resource from that domain
79 self.load_url(PAGE_URL)
80
81 # verify that the domain is blocked
82 self.open_popup(PAGE_URL)
83 assert self.get_domain_slider_state(DNT_DOMAIN) == "block", (
84 "DNT-compliant resource should have been blocked at first")
85
86 def reload_and_see_if_unblocked():
87 # switch back to the page with the DNT-compliant resource
88 self.switch_to_window_with_url(PAGE_URL)
89
90 # reload it
91 self.load_url(PAGE_URL)
92
93 self.open_popup(PAGE_URL)
94 return self.get_domain_slider_state(DNT_DOMAIN) == "block"
95
96 # verify that the domain is allowed
97 was_blocked = retry_until(
98 reload_and_see_if_unblocked,
99 tester=lambda x: not x,
100 msg="Waiting a bit for DNT check to complete and retrying ...")
101
102 assert not was_blocked, (
103 "DNT-compliant resource should have gotten unblocked")
104
105 def test_dnt_policy_check_should_not_set_cookies(self):
106 TEST_DOMAIN = "dnt-test.trackersimulator.org"
107 TEST_URL = f"https://{TEST_DOMAIN}/"
108
109 # verify that the domain itself doesn't set cookies
110 self.load_url(TEST_URL)
111 assert not self.driver.get_cookies(), "Expect no cookies at first"
112
113 # directly visit a DNT policy URL known to set cookies
114 self.load_url(TEST_URL + ".well-known/dnt-policy.txt")
115 assert len(self.driver.get_cookies()) == 1, (
116 "DNT policy URL should have set a cookie")
117
118 # verify we got a cookie
119 self.load_url(TEST_URL)
120 assert len(self.driver.get_cookies()) == 1, "Should still have one cookie"
121
122 # clear cookies and verify
123 self.driver.delete_all_cookies()
124 self.load_url(TEST_URL)
125 assert not self.driver.get_cookies(), "Should have no cookies again"
126
127 # perform a DNT policy check
128 self.check_dnt(TEST_DOMAIN)
129
130 # check that we didn't get cookied by the DNT URL
131 self.load_url(TEST_URL)
132 assert not self.driver.get_cookies(), (
133 "Shouldn't have any cookies after the DNT check")
134
135 def test_dnt_policy_check_should_not_send_cookies(self):
136 TEST_DOMAIN = "dnt-request-cookies-test.trackersimulator.org"
137 TEST_URL = f"https://{TEST_DOMAIN}/"
138
139 # directly visit a DNT policy URL known to set cookies
140 self.load_url(TEST_URL + ".well-known/dnt-policy.txt")
141 assert len(self.driver.get_cookies()) == 1, (
142 "DNT policy URL should have set a cookie")
143
144 # how to check we didn't send a cookie along with request?
145 # the DNT policy URL used by this test returns "cookies=X"
146 # where X is the number of cookies it got
147 self.set_dnt_hashes()
148
149 # perform a DNT policy check
150 result = self.check_dnt(TEST_DOMAIN)
151 assert result, "One or more cookies were sent (cookies=0 policy hash did not match)"
152
153 @pytest.mark.flaky(reruns=3, condition=pbtest.shim.browser_type in ("chrome", "edge"))
154 def test_should_not_record_nontracking_domains(self):
155 NONTRACKING_FIXTURE_URL = (
156 "https://efforg.github.io/privacybadger-test-fixtures/html/"
157 "recording_nontracking_domains.html"
158 )
159 TRACKING_DOMAIN = "dnt-request-cookies-test.trackersimulator.org"
160 NON_TRACKING_DOMAIN = "www.eff.org"
161
162 # clear pre-trained/seed tracker data
163 self.clear_tracker_data()
164
165 # enable local learning
166 self.wait_for_script("return window.OPTIONS_INITIALIZED")
167 self.find_el_by_css('a[href="#tab-general-settings"]').click()
168 self.find_el_by_css('#local-learning-checkbox').click()
169
170 # visit a page containing two third-party resources,
171 # one from a cookie-tracking domain
172 # and one from a non-tracking domain
173 self.load_url(NONTRACKING_FIXTURE_URL)
174
175 # verify both domains are present on the page
176 try:
177 selector = f"iframe[src*='{TRACKING_DOMAIN}']"
178 self.driver.find_element(By.CSS_SELECTOR, selector)
179 except NoSuchElementException:
180 self.fail("Unable to find the tracking domain on the page")
181 try:
182 selector = f"img[src*='{NON_TRACKING_DOMAIN}']"
183 self.driver.find_element(By.CSS_SELECTOR, selector)
184 except NoSuchElementException:
185 self.fail("Unable to find the non-tracking domain on the page")
186
187 action_map = self.get_badger_storage('action_map')
188
189 # verify that the cookie-tracking domain was recorded
190 assert TRACKING_DOMAIN in action_map, (
191 "Tracking domain should have gotten recorded")
192
193 # verify that the non-tracking domain was not recorded
194 assert NON_TRACKING_DOMAIN not in action_map, (
195 "Non-tracking domain should not have gotten recorded")
196
197 def test_first_party_dnt_header(self):
198 TEST_URL = "https://httpbingo.org/get"
199 headers = retry_until(partial(self.get_first_party_headers, TEST_URL),
200 times=8)
201 assert headers is not None, "It seems we failed to get headers"
202 assert 'Dnt' in headers, "DNT header should have been present"
203 assert 'Sec-Gpc' in headers, "GPC header should have been present"
204 assert headers['Dnt'] == ["1"], 'DNT header should have been set to "1"'
205 assert headers['Sec-Gpc'] == ["1"], 'Sec-Gpc header should have been set to "1"'
206
207 def test_no_dnt_header_when_disabled_on_site(self):
208 TEST_URL = "https://httpbingo.org/get"
209 self.disable_badger_on_site(TEST_URL)
210 headers = retry_until(partial(self.get_first_party_headers, TEST_URL),
211 times=8)
212 assert headers is not None, "It seems we failed to get headers"
213 assert 'Dnt' not in headers, "DNT header should have been missing"
214 assert 'Sec-Gpc' not in headers, "GPC header should have been missing"
215
216 def test_no_dnt_header_when_dnt_disabled(self):
217 TEST_URL = "https://httpbingo.org/get"
218
219 self.load_url(self.options_url)
220 self.wait_for_script("return window.OPTIONS_INITIALIZED")
221 self.find_el_by_css('a[href="#tab-general-settings"]').click()
222 self.find_el_by_css('#enable_dnt_checkbox').click()
223
224 headers = retry_until(partial(self.get_first_party_headers, TEST_URL),
225 times=8)
226 assert headers is not None, "It seems we failed to get headers"
227 assert 'Dnt' not in headers, "DNT header should have been missing"
228 assert 'Sec-Gpc' not in headers, "GPC header should have been missing"
229
230 def test_navigator_object(self):
231 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
232 body_text = self.driver.find_element(By.TAG_NAME, 'body').text
233 assert body_text == 'no tracking (navigator.doNotTrack="1")', (
234 'navigator.doNotTrack should have been set to "1"')
235 assert self.js("return navigator.globalPrivacyControl === true"), (
236 "navigator.globalPrivacyControl should have been set to true")
237 assert self.js("""
238 return Object.getOwnPropertyDescriptor(
239 Navigator.prototype, 'globalPrivacyControl')?.get?.call(navigator);
240 """), "GPC should be set on Navigator.prototype"
241
242 def test_navigator_unmodified_when_disabled_on_site(self):
243 self.disable_badger_on_site(self.FIXTURE_URL)
244
245 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
246
247 # navigator.doNotTrack defaults to null in Chrome, "unspecified" in Firefox
248 body_text = self.driver.find_element(By.TAG_NAME, 'body').text
249 assert body_text[0:5] == 'unset', (
250 "navigator.doNotTrack should be unset or \"unspecified\"")
251
252 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should be unset or False")
253
254 def test_navigator_disabling_on_site_parent_domain(self):
255 """Needs to be consistent with test_disabling_on_site_parent_domain()"""
256 self.disable_badger_on_site(self.FIXTURE_PARENT_DOMAIN)
257 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
258 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should be unset or False")
259
260 def test_navigator_disabling_on_site_wildcard(self):
261 """Needs to be consistent with test_disabling_on_site_wildcard()"""
262 self.disable_badger_on_site("*." + self.FIXTURE_PARENT_DOMAIN)
263 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
264 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should be unset or False")
265
266 def test_navigator_unmodified_when_dnt_disabled(self):
267 self.load_url(self.options_url)
268 self.wait_for_script("return window.OPTIONS_INITIALIZED")
269 self.find_el_by_css('a[href="#tab-general-settings"]').click()
270 self.find_el_by_css('#enable_dnt_checkbox').click()
271
272 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
273
274 # navigator.doNotTrack defaults to null in Chrome, "unspecified" in Firefox
275 body_text = self.driver.find_element(By.TAG_NAME, 'body').text
276 assert body_text[0:5] == 'unset', (
277 "navigator.doNotTrack should be unset or \"unspecified\"")
278
279 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should be unset or False")
280
281 def test_navigator_toggling_dnt_and_disabled_sites(self):
282 # disable on site
283 self.disable_badger_on_site(self.FIXTURE_URL)
284
285 # disable sending DNT signals
286 self.load_url(self.options_url)
287 self.wait_for_script("return window.OPTIONS_INITIALIZED")
288 self.find_el_by_css('a[href="#tab-general-settings"]').click()
289 self.find_el_by_css('#enable_dnt_checkbox').click()
290
291 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
292 assert self.js("return navigator.doNotTrack") != "1", (
293 "navigator.doNotTrack should not be set")
294 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should be unset or False")
295
296 # re-enable sending DNT signals
297 self.load_url(self.options_url)
298 self.wait_for_script("return window.OPTIONS_INITIALIZED")
299 self.find_el_by_css('a[href="#tab-general-settings"]').click()
300 self.find_el_by_css('#enable_dnt_checkbox').click()
301
302 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
303 assert self.js("return navigator.doNotTrack") != "1", (
304 "navigator.doNotTrack should still not be set")
305 self.assert_navigator_gpc_unset("navigator.globalPrivacyControl should still be unset or False")
306
307 # re-enable on site
308 self.reenable_badger_on_site("efforg.github.io")
309
310 self.load_url(self.FIXTURE_URL, wait_for_body_text=True)
311 assert self.js("return navigator.doNotTrack") == "1", (
312 "navigator.doNotTrack should now be set")
313 assert self.js("return navigator.globalPrivacyControl"), (
314 "navigator.globalPrivacyControl should also be set")
315
316
317 if __name__ == "__main__":
318 unittest.main()