tor-browser

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

browser_protectionsUI_pbmode_exceptions.js (5914B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that sites added to the Tracking Protection whitelist in private
      5 // browsing mode don't persist once the private browsing window closes.
      6 
      7 const TP_PB_PREF = "privacy.trackingprotection.enabled";
      8 const TRACKING_PAGE =
      9  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     10  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
     11 var TrackingProtection = null;
     12 var gProtectionsHandler = null;
     13 var browser = null;
     14 
     15 registerCleanupFunction(function () {
     16  Services.prefs.clearUserPref(TP_PB_PREF);
     17  gProtectionsHandler = TrackingProtection = browser = null;
     18  UrlClassifierTestUtils.cleanupTestTrackers();
     19 });
     20 
     21 function hidden(sel) {
     22  let win = browser.ownerGlobal;
     23  let el = win.document.querySelector(sel);
     24  let display = win.getComputedStyle(el).getPropertyValue("display", null);
     25  return display === "none";
     26 }
     27 
     28 function protectionsPopupState() {
     29  let win = browser.ownerGlobal;
     30  return win.document.getElementById("protections-popup")?.state || "closed";
     31 }
     32 
     33 function clickButton(sel) {
     34  let win = browser.ownerGlobal;
     35  let el = win.document.querySelector(sel);
     36  el.doCommand();
     37 }
     38 
     39 function testTrackingPage() {
     40  info("Tracking content must be blocked");
     41  ok(gProtectionsHandler.anyDetected, "trackers are detected");
     42  ok(!gProtectionsHandler.hasException, "content shows no exception");
     43 
     44  ok(
     45    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
     46    "icon box is visible"
     47  );
     48  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");
     49  ok(
     50    !gProtectionsHandler.iconBox.hasAttribute("hasException"),
     51    "icon box shows no exception"
     52  );
     53  is(
     54    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
     55      "data-l10n-id"
     56    ),
     57    "tracking-protection-icon-active",
     58    "correct tooltip"
     59  );
     60 }
     61 
     62 function testTrackingPageUnblocked() {
     63  info("Tracking content must be allowlisted and not blocked");
     64  ok(gProtectionsHandler.anyDetected, "trackers are detected");
     65  ok(gProtectionsHandler.hasException, "content shows exception");
     66 
     67  ok(!gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");
     68  ok(
     69    gProtectionsHandler.iconBox.hasAttribute("hasException"),
     70    "shield shows exception"
     71  );
     72  is(
     73    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
     74      "data-l10n-id"
     75    ),
     76    "tracking-protection-icon-disabled",
     77    "correct tooltip"
     78  );
     79 
     80  ok(
     81    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
     82    "icon box is visible"
     83  );
     84 }
     85 
     86 add_task(async function testExceptionAddition() {
     87  await SpecialPowers.pushPrefEnv({
     88    set: [["dom.security.https_first_pbm", false]],
     89  });
     90 
     91  await UrlClassifierTestUtils.addTestTrackers();
     92  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
     93    private: true,
     94  });
     95  browser = privateWin.gBrowser;
     96  let tab = await BrowserTestUtils.openNewForegroundTab(browser);
     97 
     98  gProtectionsHandler = browser.ownerGlobal.gProtectionsHandler;
     99  ok(gProtectionsHandler, "CB is attached to the private window");
    100 
    101  TrackingProtection =
    102    browser.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection;
    103  ok(TrackingProtection, "TP is attached to the private window");
    104 
    105  Services.prefs.setBoolPref(TP_PB_PREF, true);
    106  ok(TrackingProtection.enabled, "TP is enabled after setting the pref");
    107 
    108  info("Load a test page containing tracking elements");
    109  await Promise.all([
    110    BrowserTestUtils.loadURIString({
    111      browser: tab.linkedBrowser,
    112      uriString: TRACKING_PAGE,
    113    }),
    114    waitForContentBlockingEvent(2, tab.ownerGlobal),
    115  ]);
    116 
    117  testTrackingPage(tab.ownerGlobal);
    118 
    119  info("Disable TP for the page (which reloads the page)");
    120  let tabReloadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    121  gProtectionsHandler.disableForCurrentPage();
    122  is(protectionsPopupState(), "closed", "protections popup is closed");
    123 
    124  await tabReloadPromise;
    125  testTrackingPageUnblocked();
    126 
    127  info(
    128    "Test that the exception is remembered across tabs in the same private window"
    129  );
    130  tab = browser.selectedTab = BrowserTestUtils.addTab(browser);
    131 
    132  info("Load a test page containing tracking elements");
    133  await BrowserTestUtils.loadURIString({
    134    browser: tab.linkedBrowser,
    135    uriString: TRACKING_PAGE,
    136  });
    137  testTrackingPageUnblocked();
    138 
    139  await BrowserTestUtils.closeWindow(privateWin);
    140 });
    141 
    142 add_task(async function testExceptionPersistence() {
    143  await SpecialPowers.pushPrefEnv({
    144    set: [["dom.security.https_first_pbm", false]],
    145  });
    146 
    147  info("Open another private browsing window");
    148  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    149    private: true,
    150  });
    151  browser = privateWin.gBrowser;
    152  let tab = await BrowserTestUtils.openNewForegroundTab(browser);
    153 
    154  gProtectionsHandler = browser.ownerGlobal.gProtectionsHandler;
    155  ok(gProtectionsHandler, "CB is attached to the private window");
    156  TrackingProtection =
    157    browser.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection;
    158  ok(TrackingProtection, "TP is attached to the private window");
    159 
    160  ok(TrackingProtection.enabled, "TP is still enabled");
    161 
    162  info("Load a test page containing tracking elements");
    163  await Promise.all([
    164    BrowserTestUtils.loadURIString({
    165      browser: tab.linkedBrowser,
    166      uriString: TRACKING_PAGE,
    167    }),
    168    waitForContentBlockingEvent(2, tab.ownerGlobal),
    169  ]);
    170 
    171  testTrackingPage(tab.ownerGlobal);
    172 
    173  info("Disable TP for the page (which reloads the page)");
    174  let tabReloadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    175  gProtectionsHandler.disableForCurrentPage();
    176  is(protectionsPopupState(), "closed", "protections popup is closed");
    177 
    178  await Promise.all([
    179    tabReloadPromise,
    180    waitForContentBlockingEvent(2, tab.ownerGlobal),
    181  ]);
    182  testTrackingPageUnblocked();
    183 
    184  await BrowserTestUtils.closeWindow(privateWin);
    185 });