tor-browser

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

browser_identityPopup_clearSiteData.js (6724B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const TEST_ORIGIN = "https://example.com";
      5 const TEST_SUB_ORIGIN = "https://test1.example.com";
      6 const TEST_ORIGIN_2 = "https://example.net";
      7 const REMOVE_DIALOG_URL =
      8  "chrome://browser/content/preferences/dialogs/siteDataRemoveSelected.xhtml";
      9 
     10 // Greek IDN for 'example.test'.
     11 const TEST_IDN_ORIGIN =
     12  "https://\u03C0\u03B1\u03C1\u03AC\u03B4\u03B5\u03B9\u03B3\u03BC\u03B1.\u03B4\u03BF\u03BA\u03B9\u03BC\u03AE";
     13 const TEST_PUNY_ORIGIN = "https://xn--hxajbheg2az3al.xn--jxalpdlp/";
     14 const TEST_PUNY_SUB_ORIGIN = "https://sub1.xn--hxajbheg2az3al.xn--jxalpdlp/";
     15 
     16 ChromeUtils.defineESModuleGetters(this, {
     17  SiteDataTestUtils: "resource://testing-common/SiteDataTestUtils.sys.mjs",
     18 });
     19 
     20 async function testClearing(
     21  testQuota,
     22  testCookies,
     23  testURI,
     24  originA,
     25  subOriginA,
     26  originB
     27 ) {
     28  // Create a variant of originB which is partitioned under top level originA.
     29  let { scheme, host } = Services.io.newURI(originA);
     30  let partitionKey = `(${scheme},${host})`;
     31 
     32  let { origin: originBPartitioned } =
     33    Services.scriptSecurityManager.createContentPrincipal(
     34      Services.io.newURI(originB),
     35      { partitionKey }
     36    );
     37 
     38  // Add some test quota storage.
     39  if (testQuota) {
     40    await SiteDataTestUtils.addToIndexedDB(originA);
     41    await SiteDataTestUtils.addToIndexedDB(subOriginA);
     42    await SiteDataTestUtils.addToIndexedDB(originBPartitioned);
     43  }
     44 
     45  // Add some test cookies.
     46  if (testCookies) {
     47    SiteDataTestUtils.addToCookies({
     48      origin: originA,
     49      name: "test1",
     50      value: "1",
     51    });
     52    SiteDataTestUtils.addToCookies({
     53      origin: originA,
     54      name: "test2",
     55      value: "2",
     56    });
     57    SiteDataTestUtils.addToCookies({
     58      origin: subOriginA,
     59      name: "test3",
     60      value: "1",
     61    });
     62 
     63    SiteDataTestUtils.addToCookies({
     64      origin: originBPartitioned,
     65      name: "test4",
     66      value: "1",
     67    });
     68  }
     69 
     70  await BrowserTestUtils.withNewTab(testURI, async function () {
     71    // Verify we have added quota storage.
     72    if (testQuota) {
     73      let usage = await SiteDataTestUtils.getQuotaUsage(originA);
     74      Assert.greater(usage, 0, "Should have data for the base origin.");
     75 
     76      usage = await SiteDataTestUtils.getQuotaUsage(subOriginA);
     77      Assert.greater(usage, 0, "Should have data for the sub origin.");
     78 
     79      usage = await SiteDataTestUtils.getQuotaUsage(originBPartitioned);
     80      Assert.greater(usage, 0, "Should have data for the partitioned origin.");
     81    }
     82 
     83    // Open the identity popup.
     84    let { gIdentityHandler } = gBrowser.ownerGlobal;
     85    let promisePanelOpen = BrowserTestUtils.waitForEvent(
     86      gBrowser.ownerGlobal,
     87      "popupshown",
     88      true,
     89      event => event.target == gIdentityHandler._identityPopup
     90    );
     91    gIdentityHandler._identityIconBox.click();
     92    await promisePanelOpen;
     93 
     94    let clearFooter = document.getElementById(
     95      "identity-popup-clear-sitedata-footer"
     96    );
     97    let clearButton = document.getElementById(
     98      "identity-popup-clear-sitedata-button"
     99    );
    100    TestUtils.waitForCondition(
    101      () => !clearFooter.hidden,
    102      "The clear data footer is not hidden."
    103    );
    104 
    105    let cookiesCleared;
    106    if (testCookies) {
    107      let promises = ["test1", "test2", "test3", "test4"].map(cookieName =>
    108        TestUtils.topicObserved("cookie-changed", subj => {
    109          let notification = subj.QueryInterface(Ci.nsICookieNotification);
    110          return (
    111            notification.action == Ci.nsICookieNotification.COOKIE_DELETED &&
    112            notification.cookie.name == cookieName
    113          );
    114        })
    115      );
    116      cookiesCleared = Promise.all(promises);
    117    }
    118 
    119    // Click the "Clear data" button.
    120    let siteDataUpdated = TestUtils.topicObserved(
    121      "sitedatamanager:sites-updated"
    122    );
    123    let hideEvent = BrowserTestUtils.waitForEvent(
    124      gIdentityHandler._identityPopup,
    125      "popuphidden"
    126    );
    127    let removeDialogPromise = BrowserTestUtils.promiseAlertDialogOpen(
    128      "accept",
    129      REMOVE_DIALOG_URL
    130    );
    131    clearButton.click();
    132    await hideEvent;
    133    await removeDialogPromise;
    134 
    135    await siteDataUpdated;
    136 
    137    // Check that cookies were deleted.
    138    if (testCookies) {
    139      await cookiesCleared;
    140      let uri = Services.io.newURI(originA);
    141      is(
    142        Services.cookies.countCookiesFromHost(uri.host),
    143        0,
    144        "Cookies from the base domain should be cleared"
    145      );
    146      uri = Services.io.newURI(subOriginA);
    147      is(
    148        Services.cookies.countCookiesFromHost(uri.host),
    149        0,
    150        "Cookies from the sub domain should be cleared"
    151      );
    152      ok(
    153        !SiteDataTestUtils.hasCookies(originBPartitioned),
    154        "Partitioned cookies should be cleared"
    155      );
    156    }
    157 
    158    // Check that quota storage was deleted.
    159    if (testQuota) {
    160      await TestUtils.waitForCondition(async () => {
    161        let usage = await SiteDataTestUtils.getQuotaUsage(originA);
    162        return usage == 0;
    163      }, "Should have no data for the base origin.");
    164 
    165      let usage = await SiteDataTestUtils.getQuotaUsage(subOriginA);
    166      is(usage, 0, "Should have no data for the sub origin.");
    167 
    168      usage = await SiteDataTestUtils.getQuotaUsage(originBPartitioned);
    169      is(usage, 0, "Should have no data for the partitioned origin.");
    170    }
    171 
    172    // Open the site identity panel again to check that the button isn't shown anymore.
    173    promisePanelOpen = BrowserTestUtils.waitForEvent(
    174      gIdentityHandler._identityPopup,
    175      "popupshown"
    176    );
    177    gIdentityHandler._identityIconBox.click();
    178    await promisePanelOpen;
    179 
    180    // Wait for a second to see if the button is shown.
    181    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    182    await new Promise(c => setTimeout(c, 1000));
    183 
    184    ok(
    185      clearFooter.hidden,
    186      "The clear data footer is hidden after clearing data."
    187    );
    188  });
    189 }
    190 
    191 // Test removing quota managed storage.
    192 add_task(async function test_ClearSiteData() {
    193  await testClearing(
    194    true,
    195    false,
    196    TEST_ORIGIN,
    197    TEST_ORIGIN,
    198    TEST_SUB_ORIGIN,
    199    TEST_ORIGIN_2
    200  );
    201 });
    202 
    203 // Test removing cookies.
    204 add_task(async function test_ClearCookies() {
    205  await testClearing(
    206    false,
    207    true,
    208    TEST_ORIGIN,
    209    TEST_ORIGIN,
    210    TEST_SUB_ORIGIN,
    211    TEST_ORIGIN_2
    212  );
    213 });
    214 
    215 // Test removing both.
    216 add_task(async function test_ClearCookiesAndSiteData() {
    217  await testClearing(
    218    true,
    219    true,
    220    TEST_ORIGIN,
    221    TEST_ORIGIN,
    222    TEST_SUB_ORIGIN,
    223    TEST_ORIGIN_2
    224  );
    225 });
    226 
    227 // Test IDN Domains
    228 add_task(async function test_IDN_ClearCookiesAndSiteData() {
    229  await testClearing(
    230    true,
    231    true,
    232    TEST_IDN_ORIGIN,
    233    TEST_PUNY_ORIGIN,
    234    TEST_PUNY_SUB_ORIGIN,
    235    TEST_ORIGIN_2
    236  );
    237 });