tor-browser

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

browser_protectionsUI_state.js (12546B)


      1 /*
      2 * Test that the Tracking Protection section is visible in the Control Center
      3 * and has the correct state for the cases when:
      4 *
      5 * In a normal window as well as a private window,
      6 *   With TP enabled
      7 *     1) A page with no tracking elements is loaded.
      8 *     2) A page with tracking elements is loaded and they are blocked.
      9 *     3) A page with tracking elements is loaded and they are not blocked.
     10 *   With TP disabled
     11 *     1) A page with no tracking elements is loaded.
     12 *     2) A page with tracking elements is loaded.
     13 *
     14 * See also Bugs 1175327, 1043801, 1178985
     15 */
     16 
     17 const TP_PREF = "privacy.trackingprotection.enabled";
     18 const TP_PB_PREF = "privacy.trackingprotection.pbmode.enabled";
     19 const TPC_PREF = "network.cookie.cookieBehavior";
     20 const BENIGN_PAGE =
     21  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     22  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/benignPage.html";
     23 const TRACKING_PAGE =
     24  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     25  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
     26 const COOKIE_PAGE =
     27  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     28  "http://not-tracking.example.com/browser/browser/base/content/test/protectionsUI/cookiePage.html";
     29 var gProtectionsHandler = null;
     30 var TrackingProtection = null;
     31 var ThirdPartyCookies = null;
     32 var tabbrowser = null;
     33 var gTrackingPageURL = TRACKING_PAGE;
     34 
     35 registerCleanupFunction(function () {
     36  TrackingProtection =
     37    gProtectionsHandler =
     38    ThirdPartyCookies =
     39    tabbrowser =
     40      null;
     41  UrlClassifierTestUtils.cleanupTestTrackers();
     42  Services.prefs.clearUserPref(TP_PREF);
     43  Services.prefs.clearUserPref(TP_PB_PREF);
     44  Services.prefs.clearUserPref(TPC_PREF);
     45 });
     46 
     47 function notFound(id) {
     48  let doc = tabbrowser.ownerGlobal.document;
     49  return doc.getElementById(id).classList.contains("notFound");
     50 }
     51 
     52 async function testBenignPage() {
     53  info("Non-tracking content must not be blocked");
     54  ok(!gProtectionsHandler.anyDetected, "no trackers are detected");
     55  ok(!gProtectionsHandler.hasException, "content shows no exception");
     56 
     57  ok(
     58    !gProtectionsHandler.iconBox.hasAttribute("active"),
     59    "shield is not active"
     60  );
     61  ok(
     62    !gProtectionsHandler.iconBox.hasAttribute("hasException"),
     63    "icon box shows no exception"
     64  );
     65  is(
     66    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
     67      "data-l10n-id"
     68    ),
     69    "tracking-protection-icon-no-trackers-detected",
     70    "correct tooltip"
     71  );
     72  ok(
     73    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
     74    "icon box is visible"
     75  );
     76 
     77  let win = tabbrowser.ownerGlobal;
     78  await openProtectionsPanel(false, win);
     79  ok(
     80    notFound("protections-popup-category-cookies"),
     81    "Cookie restrictions category is not found"
     82  );
     83  ok(
     84    notFound("protections-popup-category-trackers"),
     85    "Trackers category is not found"
     86  );
     87  await closeProtectionsPanel(win);
     88 }
     89 
     90 async function testBenignPageWithException() {
     91  info("Non-tracking content must not be blocked");
     92  ok(!gProtectionsHandler.anyDetected, "no trackers are detected");
     93  ok(gProtectionsHandler.hasException, "content shows exception");
     94 
     95  ok(
     96    !gProtectionsHandler.iconBox.hasAttribute("active"),
     97    "shield is not active"
     98  );
     99  ok(
    100    gProtectionsHandler.iconBox.hasAttribute("hasException"),
    101    "shield shows exception"
    102  );
    103  is(
    104    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
    105      "data-l10n-id"
    106    ),
    107    "tracking-protection-icon-disabled",
    108    "correct tooltip"
    109  );
    110 
    111  ok(
    112    !BrowserTestUtils.isHidden(gProtectionsHandler.iconBox),
    113    "icon box is not hidden"
    114  );
    115 
    116  let win = tabbrowser.ownerGlobal;
    117  await openProtectionsPanel(false, win);
    118  ok(
    119    notFound("protections-popup-category-cookies"),
    120    "Cookie restrictions category is not found"
    121  );
    122  ok(
    123    notFound("protections-popup-category-trackers"),
    124    "Trackers category is not found"
    125  );
    126  await closeProtectionsPanel(win);
    127 }
    128 
    129 function areTrackersBlocked(isPrivateBrowsing) {
    130  let blockedByTP = Services.prefs.getBoolPref(
    131    isPrivateBrowsing ? TP_PB_PREF : TP_PREF
    132  );
    133  let blockedByTPC = [
    134    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
    135    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
    136  ].includes(Services.prefs.getIntPref(TPC_PREF));
    137  return blockedByTP || blockedByTPC;
    138 }
    139 
    140 async function testTrackingPage(window) {
    141  info("Tracking content must be blocked");
    142  ok(gProtectionsHandler.anyDetected, "trackers are detected");
    143  ok(!gProtectionsHandler.hasException, "content shows no exception");
    144 
    145  let isWindowPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
    146  let blockedByTP = areTrackersBlocked(isWindowPrivate);
    147  ok(
    148    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
    149    "icon box is always visible"
    150  );
    151  is(
    152    gProtectionsHandler.iconBox.hasAttribute("active"),
    153    blockedByTP,
    154    "shield is" + (blockedByTP ? "" : " not") + " active"
    155  );
    156  ok(
    157    !gProtectionsHandler.iconBox.hasAttribute("hasException"),
    158    "icon box shows no exception"
    159  );
    160  is(
    161    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
    162      "data-l10n-id"
    163    ),
    164    blockedByTP
    165      ? "tracking-protection-icon-active"
    166      : "tracking-protection-icon-no-trackers-detected",
    167    "correct tooltip"
    168  );
    169 
    170  await openProtectionsPanel(false, window);
    171  ok(
    172    !notFound("protections-popup-category-trackers"),
    173    "Trackers category is detected"
    174  );
    175  if (gTrackingPageURL == COOKIE_PAGE) {
    176    ok(
    177      !notFound("protections-popup-category-cookies"),
    178      "Cookie restrictions category is detected"
    179    );
    180  } else {
    181    ok(
    182      notFound("protections-popup-category-cookies"),
    183      "Cookie restrictions category is not found"
    184    );
    185  }
    186  await closeProtectionsPanel(window);
    187 }
    188 
    189 async function testTrackingPageUnblocked(blockedByTP, window) {
    190  info("Tracking content must be in the exception list and not blocked");
    191  ok(gProtectionsHandler.anyDetected, "trackers are detected");
    192  ok(gProtectionsHandler.hasException, "content shows exception");
    193 
    194  ok(
    195    !gProtectionsHandler.iconBox.hasAttribute("active"),
    196    "shield is not active"
    197  );
    198  ok(
    199    gProtectionsHandler.iconBox.hasAttribute("hasException"),
    200    "shield shows exception"
    201  );
    202  is(
    203    gProtectionsHandler._trackingProtectionIconTooltipLabel.getAttribute(
    204      "data-l10n-id"
    205    ),
    206    "tracking-protection-icon-disabled",
    207    "correct tooltip"
    208  );
    209 
    210  ok(
    211    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
    212    "icon box is visible"
    213  );
    214 
    215  await openProtectionsPanel(false, window);
    216  ok(
    217    !notFound("protections-popup-category-trackers"),
    218    "Trackers category is detected"
    219  );
    220  if (gTrackingPageURL == COOKIE_PAGE) {
    221    ok(
    222      !notFound("protections-popup-category-cookies"),
    223      "Cookie restrictions category is detected"
    224    );
    225  } else {
    226    ok(
    227      notFound("protections-popup-category-cookies"),
    228      "Cookie restrictions category is not found"
    229    );
    230  }
    231  await closeProtectionsPanel(window);
    232 }
    233 
    234 async function testContentBlocking(tab) {
    235  info("Testing with Tracking Protection ENABLED.");
    236 
    237  info("Load a test page not containing tracking elements");
    238  await BrowserTestUtils.loadURIString({
    239    browser: tab.linkedBrowser,
    240    uriString: BENIGN_PAGE,
    241  });
    242  await testBenignPage();
    243 
    244  info(
    245    "Load a test page not containing tracking elements which has an exception."
    246  );
    247 
    248  await BrowserTestUtils.loadURIString({
    249    browser: tab.linkedBrowser,
    250    uriString: "https://example.org/?round=1",
    251  });
    252 
    253  ContentBlockingAllowList.add(tab.linkedBrowser);
    254  // Load another page from the same origin to ensure there is an onlocationchange
    255  // notification which would trigger an oncontentblocking notification for us.
    256  await BrowserTestUtils.loadURIString({
    257    browser: tab.linkedBrowser,
    258    uriString: "https://example.org/?round=2",
    259  });
    260 
    261  await testBenignPageWithException();
    262 
    263  ContentBlockingAllowList.remove(tab.linkedBrowser);
    264 
    265  info("Load a test page containing tracking elements");
    266  await BrowserTestUtils.loadURIString({
    267    browser: tab.linkedBrowser,
    268    uriString: gTrackingPageURL,
    269  });
    270  await testTrackingPage(tab.ownerGlobal);
    271 
    272  info("Disable CB for the page (which reloads the page)");
    273  let reloadURI = tab.linkedBrowser.currentURI.spec;
    274  let tabReloadPromise = BrowserTestUtils.loadURIString({
    275    browser: tab.linkedBrowser,
    276    uriString: reloadURI,
    277  });
    278  tab.ownerGlobal.gProtectionsHandler.disableForCurrentPage();
    279  await tabReloadPromise;
    280  let isPrivateBrowsing = PrivateBrowsingUtils.isWindowPrivate(tab.ownerGlobal);
    281  let blockedByTP = areTrackersBlocked(isPrivateBrowsing);
    282  await testTrackingPageUnblocked(blockedByTP, tab.ownerGlobal);
    283 
    284  info("Re-enable TP for the page (which reloads the page)");
    285  reloadURI = tab.linkedBrowser.currentURI.spec;
    286  tabReloadPromise = BrowserTestUtils.loadURIString({
    287    browser: tab.linkedBrowser,
    288    uriString: reloadURI,
    289  });
    290  tab.ownerGlobal.gProtectionsHandler.enableForCurrentPage();
    291  await tabReloadPromise;
    292  await testTrackingPage(tab.ownerGlobal);
    293 }
    294 
    295 add_task(async function testNormalBrowsing() {
    296  await UrlClassifierTestUtils.addTestTrackers();
    297 
    298  tabbrowser = gBrowser;
    299  let tab = (tabbrowser.selectedTab = BrowserTestUtils.addTab(tabbrowser));
    300 
    301  gProtectionsHandler = gBrowser.ownerGlobal.gProtectionsHandler;
    302  ok(
    303    gProtectionsHandler,
    304    "gProtectionsHandler is attached to the browser window"
    305  );
    306 
    307  TrackingProtection =
    308    gBrowser.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection;
    309  ok(TrackingProtection, "TP is attached to the browser window");
    310  is(
    311    TrackingProtection.enabled,
    312    Services.prefs.getBoolPref(TP_PREF),
    313    "TP.enabled is based on the original pref value"
    314  );
    315 
    316  Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
    317 
    318  await testContentBlocking(tab);
    319 
    320  Services.prefs.setBoolPref(TP_PREF, true);
    321  ok(TrackingProtection.enabled, "TP is enabled after setting the pref");
    322 
    323  await testContentBlocking(tab);
    324 
    325  gBrowser.removeCurrentTab();
    326 
    327  Services.prefs.clearUserPref(TPC_PREF);
    328 });
    329 
    330 add_task(async function testPrivateBrowsing() {
    331  await SpecialPowers.pushPrefEnv({
    332    set: [["dom.security.https_first_pbm", false]],
    333  });
    334  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    335    private: true,
    336  });
    337  tabbrowser = privateWin.gBrowser;
    338  let tab = (tabbrowser.selectedTab = BrowserTestUtils.addTab(tabbrowser));
    339 
    340  // Set the normal mode pref to false to check the pbmode pref.
    341  Services.prefs.setBoolPref(TP_PREF, false);
    342 
    343  Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
    344 
    345  gProtectionsHandler = tabbrowser.ownerGlobal.gProtectionsHandler;
    346  ok(
    347    gProtectionsHandler,
    348    "gProtectionsHandler is attached to the private window"
    349  );
    350 
    351  TrackingProtection =
    352    tabbrowser.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection;
    353  ok(TrackingProtection, "TP is attached to the private window");
    354  is(
    355    TrackingProtection.enabled,
    356    Services.prefs.getBoolPref(TP_PB_PREF),
    357    "TP.enabled is based on the pb pref value"
    358  );
    359 
    360  await testContentBlocking(tab);
    361 
    362  Services.prefs.setBoolPref(TP_PB_PREF, true);
    363  ok(TrackingProtection.enabled, "TP is enabled after setting the pref");
    364 
    365  await testContentBlocking(tab);
    366 
    367  privateWin.close();
    368 
    369  Services.prefs.clearUserPref(TPC_PREF);
    370 });
    371 
    372 add_task(async function testThirdPartyCookies() {
    373  requestLongerTimeout(3);
    374 
    375  await UrlClassifierTestUtils.addTestTrackers();
    376  gTrackingPageURL = COOKIE_PAGE;
    377 
    378  tabbrowser = gBrowser;
    379  let tab = (tabbrowser.selectedTab = BrowserTestUtils.addTab(tabbrowser));
    380 
    381  gProtectionsHandler = gBrowser.ownerGlobal.gProtectionsHandler;
    382  ok(
    383    gProtectionsHandler,
    384    "gProtectionsHandler is attached to the browser window"
    385  );
    386  ThirdPartyCookies =
    387    gBrowser.ownerGlobal.gProtectionsHandler.blockers.ThirdPartyCookies;
    388  ok(ThirdPartyCookies, "TP is attached to the browser window");
    389  is(
    390    ThirdPartyCookies.enabled,
    391    [
    392      Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
    393      Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
    394    ].includes(Services.prefs.getIntPref(TPC_PREF)),
    395    "TPC.enabled is based on the original pref value"
    396  );
    397 
    398  await testContentBlocking(tab);
    399 
    400  Services.prefs.setIntPref(
    401    TPC_PREF,
    402    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
    403  );
    404  ok(ThirdPartyCookies.enabled, "TPC is enabled after setting the pref");
    405 
    406  await testContentBlocking(tab);
    407 
    408  Services.prefs.clearUserPref(TPC_PREF);
    409  gBrowser.removeCurrentTab();
    410 });