| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
import pbtest |
| 6 |
|
| 7 |
from selenium.common.exceptions import TimeoutException |
| 8 |
|
| 9 |
from pbtest import retry_until |
| 10 |
|
| 11 |
|
| 12 |
class SurrogatesTest(pbtest.PBSeleniumTest): |
| 13 |
"""Integration tests to verify surrogate script functionality.""" |
| 14 |
|
| 15 |
FIXTURE_HOST = "efforg.github.io" |
| 16 |
FIXTURE_URL = ( |
| 17 |
f"https://{FIXTURE_HOST}/privacybadger-test-fixtures/html/" |
| 18 |
"ga_surrogate.html" |
| 19 |
) |
| 20 |
SURROGATE_HOST_BASE = "google-analytics.com" |
| 21 |
SURROGATE_HOST = f"www.{SURROGATE_HOST_BASE}" |
| 22 |
|
| 23 |
def load_ga_js_fixture(self, cache_workaround=False, timeout=12): |
| 24 |
self.load_url(self.FIXTURE_URL) |
| 25 |
|
| 26 |
if cache_workaround and self.is_firefox_nightly(): |
| 27 |
self.driver.refresh() |
| 28 |
|
| 29 |
load_status_sel = '#third-party-load-result' |
| 30 |
self.wait_for_script( |
| 31 |
"return document.querySelector(arguments[0]).textContent", |
| 32 |
load_status_sel, timeout=timeout) |
| 33 |
if self.find_el_by_css(load_status_sel).text == "error": |
| 34 |
return False |
| 35 |
|
| 36 |
try: |
| 37 |
self.wait_for_and_switch_to_frame('iframe', timeout=timeout) |
| 38 |
self.wait_for_text('h1', "It worked!", timeout=timeout) |
| 39 |
self.driver.switch_to.default_content() |
| 40 |
return True |
| 41 |
except TimeoutException: |
| 42 |
return False |
| 43 |
|
| 44 |
def test_ga_js_surrogate(self): |
| 45 |
# clear pre-trained/seed tracker data |
| 46 |
self.clear_tracker_data() |
| 47 |
|
| 48 |
# verify site loads |
| 49 |
assert self.load_ga_js_fixture(), ( |
| 50 |
"page failed to load even before we did anything") |
| 51 |
|
| 52 |
# block ga.js (should break the site) |
| 53 |
self.block_domain(self.SURROGATE_HOST) |
| 54 |
# disable surrogates |
| 55 |
self.driver.execute_async_script( |
| 56 |
"let done = arguments[arguments.length - 1];" |
| 57 |
"chrome.runtime.sendMessage({" |
| 58 |
" type: 'disableSurrogates'" |
| 59 |
"}, done);") |
| 60 |
|
| 61 |
# verify site breaks |
| 62 |
assert not self.load_ga_js_fixture(True), ( |
| 63 |
"page loaded successfully when it should have failed") |
| 64 |
|
| 65 |
# re-enable surrogates |
| 66 |
self.load_url(self.options_url) |
| 67 |
self.driver.execute_async_script( |
| 68 |
"let done = arguments[arguments.length - 1];" |
| 69 |
"chrome.runtime.sendMessage({" |
| 70 |
" type: 'restoreSurrogates'" |
| 71 |
"}, done);") |
| 72 |
|
| 73 |
# verify site loads again |
| 74 |
assert retry_until(self.load_ga_js_fixture), ( |
| 75 |
"page failed to load after surrogation") |
| 76 |
|
| 77 |
def test_cookieblocking_base_overwrites_subdomain_surrogate(self): |
| 78 |
SURROGATE_TOSTRING = "function() {\n }" |
| 79 |
|
| 80 |
def get_tracker_tostring(): |
| 81 |
return self.js("return _gat._getTrackers.toString();") |
| 82 |
|
| 83 |
self.block_domain(self.SURROGATE_HOST) |
| 84 |
|
| 85 |
assert self.load_ga_js_fixture() |
| 86 |
# verify we replaced SURROGATE_HOST with surrogate |
| 87 |
assert get_tracker_tostring() == SURROGATE_TOSTRING, ( |
| 88 |
"tracker does not appear to have been replaced with surrogate") |
| 89 |
|
| 90 |
self.cookieblock_domain(self.SURROGATE_HOST_BASE) |
| 91 |
|
| 92 |
assert self.load_ga_js_fixture() |
| 93 |
# verify that we are no longer replacing SURROGATE_HOST with surrogate |
| 94 |
assert get_tracker_tostring() != SURROGATE_TOSTRING, ( |
| 95 |
"surrogation took place when it shouldn't have") |
| 96 |
|
| 97 |
def test_userblock_applies_surrogate(self): |
| 98 |
self.block_domain(self.SURROGATE_HOST) |
| 99 |
self.set_user_action(self.SURROGATE_HOST, "block") |
| 100 |
assert retry_until(self.load_ga_js_fixture), ( |
| 101 |
"page widget unexpectedly failed") |
| 102 |
# also verify that surrogation took place |
| 103 |
text_sel = "#script-string" |
| 104 |
self.wait_for_any_text(text_sel) |
| 105 |
assert self.find_el_by_css(text_sel).text == "function() { }", ( |
| 106 |
"tracker does not appear to have been replaced with surrogate") |
| 107 |
|
| 108 |
def test_site_override_for_domain_with_surrogate(self): |
| 109 |
text_sel = "#script-string" |
| 110 |
|
| 111 |
self.block_domain(self.SURROGATE_HOST) |
| 112 |
self.add_site_override(self.SURROGATE_HOST, self.FIXTURE_HOST) |
| 113 |
|
| 114 |
# assert the actual tracker loaded, not the surrogate |
| 115 |
self.load_url(self.FIXTURE_URL) |
| 116 |
self.wait_for_any_text(text_sel) |
| 117 |
assert self.find_el_by_css(text_sel).text != "function() { }", ( |
| 118 |
"still redirecting to surrogate despite site-specific override") |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == "__main__": |
| 122 |
unittest.main() |