| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
import pbtest |
| 6 |
|
| 7 |
from functools import partial |
| 8 |
|
| 9 |
|
| 10 |
class SupercookieTest(pbtest.PBSeleniumTest): |
| 11 |
"""Make sure we detect potential supercookies. """ |
| 12 |
|
| 13 |
def get_snitch_map_for(self, domain): |
| 14 |
self.open_window() # don't replace the test page to allow for retrying |
| 15 |
return self.get_badger_storage('snitch_map').get(domain) |
| 16 |
|
| 17 |
def setUp(self): |
| 18 |
# enable local learning |
| 19 |
self.load_url(self.options_url) |
| 20 |
self.wait_for_script("return window.OPTIONS_INITIALIZED") |
| 21 |
self.find_el_by_css('a[href="#tab-general-settings"]').click() |
| 22 |
self.find_el_by_css('#local-learning-checkbox').click() |
| 23 |
|
| 24 |
def test_should_detect_ls_of_third_party_frame(self): |
| 25 |
FIRST_PARTY_BASE = "eff.org" |
| 26 |
THIRD_PARTY_BASE = "efforg.github.io" |
| 27 |
|
| 28 |
assert not self.get_snitch_map_for(THIRD_PARTY_BASE) |
| 29 |
|
| 30 |
self.load_url(( |
| 31 |
f"https://privacybadger-tests.{FIRST_PARTY_BASE}/html/" |
| 32 |
"localstorage.html" |
| 33 |
)) |
| 34 |
|
| 35 |
# TODO FIXME We get some intermittent failures for this test. |
| 36 |
# It seems we sometimes miss the setting of localStorage items |
| 37 |
# because the script runs after we already checked what's in localStorage. |
| 38 |
# We can work around this race condition by reloading the page. |
| 39 |
self.driver.refresh() |
| 40 |
|
| 41 |
snitch_map = pbtest.retry_until( |
| 42 |
partial(self.get_snitch_map_for, THIRD_PARTY_BASE), |
| 43 |
times=3) |
| 44 |
assert snitch_map == [FIRST_PARTY_BASE] |
| 45 |
|
| 46 |
def test_should_not_detect_low_entropy_ls_of_third_party_frame(self): |
| 47 |
FIRST_PARTY_BASE = "eff.org" |
| 48 |
THIRD_PARTY_BASE = "efforg.github.io" |
| 49 |
assert not self.get_snitch_map_for(THIRD_PARTY_BASE) |
| 50 |
self.load_url(( |
| 51 |
f"https://privacybadger-tests.{FIRST_PARTY_BASE}/html/" |
| 52 |
"localstorage_low_entropy.html" |
| 53 |
)) |
| 54 |
self.driver.refresh() # TODO workaround |
| 55 |
assert not self.get_snitch_map_for(THIRD_PARTY_BASE) |
| 56 |
|
| 57 |
def test_should_not_detect_first_party_ls(self): |
| 58 |
BASE_DOMAIN = "efforg.github.io" |
| 59 |
self.load_url(( |
| 60 |
f"https://{BASE_DOMAIN}/privacybadger-test-fixtures/html/" |
| 61 |
"localstorage/set_ls.html" |
| 62 |
)) |
| 63 |
self.driver.refresh() # TODO workaround |
| 64 |
assert not self.get_snitch_map_for(BASE_DOMAIN) |
| 65 |
|
| 66 |
def test_should_not_detect_ls_of_third_party_script(self): |
| 67 |
FIRST_PARTY_BASE = "eff.org" |
| 68 |
THIRD_PARTY_BASE = "efforg.github.io" |
| 69 |
|
| 70 |
# a third-party script included by the top page (not a 3rd party frame) |
| 71 |
self.load_url(( |
| 72 |
f"https://privacybadger-tests.{FIRST_PARTY_BASE}/html/" |
| 73 |
"localstorage_from_third_party_script.html" |
| 74 |
)) |
| 75 |
|
| 76 |
self.driver.refresh() # TODO workaround |
| 77 |
|
| 78 |
assert not self.get_snitch_map_for(FIRST_PARTY_BASE) |
| 79 |
assert not self.get_snitch_map_for(THIRD_PARTY_BASE) |
| 80 |
|
| 81 |
def test_localstorage_learning(self): |
| 82 |
"""Verifies that we learn to block a third-party domain if we see |
| 83 |
non-trivial localStorage data from that third-party on three sites.""" |
| 84 |
|
| 85 |
SITE1_URL = "https://ddrybktjfxh4.cloudfront.net/localstorage.html" |
| 86 |
SITE2_URL = "https://d3syxqe9po5ji0.cloudfront.net/localstorage.html" |
| 87 |
SITE3_URL = "https://d3b37ucnz1m2l2.cloudfront.net/localstorage.html" |
| 88 |
|
| 89 |
THIRD_PARTY = "efforg.github.io" |
| 90 |
|
| 91 |
# remove pre-trained domains |
| 92 |
self.clear_tracker_data() |
| 93 |
|
| 94 |
def get_sliders(url, category): |
| 95 |
self.open_popup(url) |
| 96 |
sliders = self.get_tracker_state() |
| 97 |
return sliders[category] |
| 98 |
|
| 99 |
# load the first site |
| 100 |
self.load_url(SITE1_URL) |
| 101 |
# TODO FIXME remove workaround once we fix race conditions in contentscripts/supercookie.js |
| 102 |
self.driver.refresh() |
| 103 |
sliders = pbtest.retry_until( |
| 104 |
partial(get_sliders, SITE1_URL, 'notYetBlocked'), times=3) |
| 105 |
assert THIRD_PARTY in sliders |
| 106 |
|
| 107 |
# go to second site |
| 108 |
self.load_url(SITE2_URL) |
| 109 |
# TODO workaround |
| 110 |
self.driver.refresh() |
| 111 |
sliders = pbtest.retry_until( |
| 112 |
partial(get_sliders, SITE2_URL, 'notYetBlocked'), times=3) |
| 113 |
assert THIRD_PARTY in sliders |
| 114 |
|
| 115 |
# go to third site |
| 116 |
self.load_url(SITE3_URL) |
| 117 |
# TODO workaround |
| 118 |
self.driver.refresh() |
| 119 |
sliders = pbtest.retry_until( |
| 120 |
partial(get_sliders, SITE3_URL, 'blocked'), times=3) |
| 121 |
assert THIRD_PARTY in sliders, "third party should now be reported as blocked" |
| 122 |
|
| 123 |
|
| 124 |
if __name__ == "__main__": |
| 125 |
unittest.main() |