tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_warning_pages.py (6205B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import time
      6 
      7 from marionette_driver import By, Wait, expected
      8 from marionette_harness import MarionetteTestCase, WindowManagerMixin
      9 
     10 
     11 class TestSafeBrowsingWarningPages(WindowManagerMixin, MarionetteTestCase):
     12    def setUp(self):
     13        super().setUp()
     14 
     15        self.urls = [
     16            # Unwanted software URL
     17            "https://www.itisatrap.org/firefox/unwanted.html",
     18            # Phishing URL
     19            "https://www.itisatrap.org/firefox/its-a-trap.html",
     20            # Malware URL
     21            "https://www.itisatrap.org/firefox/its-an-attack.html",
     22        ]
     23 
     24        self.default_homepage = self.marionette.get_pref("browser.startup.homepage")
     25        self.support_page = self.marionette.absolute_url("support.html?topic=")
     26 
     27        self.marionette.set_pref("app.support.baseURL", self.support_page)
     28        self.marionette.set_pref("browser.safebrowsing.phishing.enabled", True)
     29        self.marionette.set_pref("browser.safebrowsing.malware.enabled", True)
     30        self.marionette.set_pref("network.localhost.prompt.testing", True)
     31        self.marionette.set_pref("network.localhost.prompt.testing.allow", True)
     32 
     33        # Give the browser a little time, because SafeBrowsing.sys.mjs takes a
     34        # while between start up and adding the example urls to the db.
     35        # hg.mozilla.org/mozilla-central/file/46aebcd9481e/browser/base/content/browser.js#l1194
     36        time.sleep(3)
     37 
     38        # Run this test in a new tab.
     39        new_tab = self.open_tab()
     40        self.marionette.switch_to_window(new_tab)
     41 
     42    def tearDown(self):
     43        try:
     44            self.marionette.clear_pref("app.support.baseURL")
     45            self.marionette.clear_pref("browser.safebrowsing.malware.enabled")
     46            self.marionette.clear_pref("browser.safebrowsing.phishing.enabled")
     47            self.marionette.clear_pref("network.localhost.prompt.testing")
     48            self.marionette.clear_pref("network.localhost.prompt.testing.allow")
     49 
     50            self.remove_permission("https://www.itisatrap.org", "safe-browsing")
     51            self.close_all_tabs()
     52        finally:
     53            super().tearDown()
     54 
     55    def test_warning_pages(self):
     56        for unsafe_page in self.urls:
     57            # Load a test page, then test the get me out button
     58            self.marionette.navigate(unsafe_page)
     59            # Wait for the DOM to receive events for about:blocked
     60            time.sleep(1)
     61            self.check_get_me_out_of_here_button(unsafe_page)
     62 
     63            # Load the test page again, then test the report button
     64            self.marionette.navigate(unsafe_page)
     65            # Wait for the DOM to receive events for about:blocked
     66            time.sleep(1)
     67            self.check_report_link(unsafe_page)
     68 
     69            # Load the test page again, then test the ignore warning button
     70            self.marionette.navigate(unsafe_page)
     71            # Wait for the DOM to receive events for about:blocked
     72            time.sleep(1)
     73            self.check_ignore_warning_button(unsafe_page)
     74 
     75    def get_final_url(self, url):
     76        self.marionette.navigate(url)
     77        return self.marionette.get_url()
     78 
     79    def remove_permission(self, host, permission):
     80        with self.marionette.using_context("chrome"):
     81            self.marionette.execute_script(
     82                """
     83              let uri = Services.io.newURI(arguments[0], null, null);
     84              let principal = Services.scriptSecurityManager.createContentPrincipal(uri, {});
     85              Services.perms.removeFromPrincipal(principal, arguments[1]);
     86            """,
     87                script_args=[host, permission],
     88            )
     89 
     90    def check_get_me_out_of_here_button(self, unsafe_page):
     91        button = self.marionette.find_element(By.ID, "goBackButton")
     92        button.click()
     93 
     94        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
     95            lambda mn: self.default_homepage in mn.get_url()
     96        )
     97 
     98    def check_report_link(self, unsafe_page):
     99        # Get the URL of the support site for phishing and malware. This may result in a redirect.
    100        with self.marionette.using_context("chrome"):
    101            url = self.marionette.execute_script(
    102                """
    103              return Services.urlFormatter.formatURLPref("app.support.baseURL")
    104                                                         + "phishing-malware";
    105            """
    106            )
    107 
    108        button = self.marionette.find_element(By.ID, "seeDetailsButton")
    109        button.click()
    110        link = self.marionette.find_element(By.ID, "firefox_support")
    111        link.click()
    112 
    113        # Wait for the button to become stale, whereby a longer timeout is needed
    114        # here to not fail in case of slow connections.
    115        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
    116            expected.element_stale(button)
    117        )
    118 
    119        # Wait for page load to be completed, so we can verify the URL even if a redirect happens.
    120        # TODO: Bug 1140470: use replacement for mozmill's waitforPageLoad
    121        expected_url = self.get_final_url(url)
    122        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
    123            lambda mn: expected_url == mn.get_url(),
    124            message=f"The expected URL '{expected_url}' has not been loaded",
    125        )
    126 
    127        topic = self.marionette.find_element(By.ID, "topic")
    128        self.assertEqual(topic.text, "phishing-malware")
    129 
    130    def check_ignore_warning_button(self, unsafe_page):
    131        button = self.marionette.find_element(By.ID, "seeDetailsButton")
    132        button.click()
    133        link = self.marionette.find_element(By.ID, "ignore_warning_link")
    134        link.click()
    135 
    136        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
    137            expected.element_present(By.ID, "main-feature")
    138        )
    139        self.assertEqual(self.marionette.get_url(), self.get_final_url(unsafe_page))
    140 
    141        # Clean up by removing safe browsing permission for unsafe page
    142        self.remove_permission("https://www.itisatrap.org", "safe-browsing")