tor-browser

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

browser_cookie_purge_sync.js (4327B)


      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 // This test checks that cookie purge broadcast correctly
      6 // updates content process memory as triggered by:
      7 // 1. stale-cookie update from content process
      8 // 2. delete cookie by host from parent process
      9 // 3. clear all cookies from parent process
     10 
     11 const URL_EXAMPLE = "https://example.com";
     12 const COOKIE_NAMEVALUE = "name=value";
     13 const COOKIE_NAMEVALUE_2 = COOKIE_NAMEVALUE + "2";
     14 const COOKIE_STRING = COOKIE_NAMEVALUE + "; Secure; SameSite=None";
     15 const COOKIE_STRING_2 = COOKIE_NAMEVALUE_2 + "; Secure; SameSite=None";
     16 const MAX_AGE_OLD = 2; // seconds
     17 const MAX_AGE_NEW = 100; // 100 sec
     18 
     19 registerCleanupFunction(() => {
     20  info("Cleaning up the test setup");
     21  Services.prefs.clearUserPref("network.cookie.sameSite.laxByDefault");
     22  Services.cookies.removeAll();
     23 });
     24 
     25 add_setup(async function () {
     26  info("Setting up the test");
     27  Services.prefs.setBoolPref("network.cookie.sameSite.laxByDefault", false);
     28 });
     29 
     30 function waitForNotificationPromise(notification, expected) {
     31  return new Promise(resolve => {
     32    function observer() {
     33      is(content.document.cookie, expected);
     34      Services.obs.removeObserver(observer, notification);
     35      resolve();
     36    }
     37    Services.obs.addObserver(observer, notification);
     38  });
     39 }
     40 
     41 add_task(async function test_purge_sync_batch_and_deleted() {
     42  const tab1 = BrowserTestUtils.addTab(gBrowser, URL_EXAMPLE);
     43  const browser = gBrowser.getBrowserForTab(tab1);
     44  await BrowserTestUtils.browserLoaded(browser);
     45 
     46  const tab2 = BrowserTestUtils.addTab(gBrowser, URL_EXAMPLE);
     47  const browser2 = gBrowser.getBrowserForTab(tab2);
     48  await BrowserTestUtils.browserLoaded(browser2);
     49 
     50  let firstCookieAdded = SpecialPowers.spawn(
     51    browser2,
     52    ["content-added-cookie", COOKIE_NAMEVALUE],
     53    waitForNotificationPromise
     54  );
     55  await TestUtils.waitForTick(); // waiting helps --verify
     56 
     57  // set old cookie in tab 1 and check it in tab 2
     58  await SpecialPowers.spawn(
     59    browser,
     60    [COOKIE_STRING, MAX_AGE_OLD],
     61    (cookie, max_age) => {
     62      content.document.cookie = cookie + ";Max-Age=" + max_age;
     63    }
     64  );
     65  await firstCookieAdded;
     66 
     67  // wait until the first cookie expires
     68  await SpecialPowers.spawn(browser2, [], () => {
     69    return ContentTaskUtils.waitForCondition(
     70      () => content.document.cookie == "",
     71      "cookie did not expire in time",
     72      200
     73    ).catch(() => {
     74      is(false, "Cookie did not expire in time");
     75    });
     76  });
     77 
     78  // BATCH_DELETED/BatchDeleted pathway
     79  let batchDeletedPromise = SpecialPowers.spawn(
     80    browser,
     81    ["content-batch-deleted-cookies", COOKIE_NAMEVALUE_2],
     82    waitForNotificationPromise
     83  );
     84  await TestUtils.waitForTick(); // waiting helps --verify
     85  await SpecialPowers.spawn(
     86    browser,
     87    [COOKIE_STRING_2, MAX_AGE_NEW],
     88    (cookie, max_age) => {
     89      content.document.cookie = cookie + ";Max-Age=" + max_age;
     90    }
     91  );
     92  await batchDeletedPromise;
     93 
     94  // COOKIE_DELETED/RemoveCookie pathway
     95  let cookieRemovedPromise = SpecialPowers.spawn(
     96    browser,
     97    ["content-removed-cookie", ""],
     98    waitForNotificationPromise
     99  );
    100  let cookieRemovedPromise2 = SpecialPowers.spawn(
    101    browser2,
    102    ["content-removed-cookie", ""],
    103    waitForNotificationPromise
    104  );
    105  await TestUtils.waitForTick();
    106  Services.cookies.removeCookiesFromExactHost(
    107    "example.com",
    108    JSON.stringify({})
    109  );
    110  await cookieRemovedPromise;
    111  await cookieRemovedPromise2;
    112 
    113  // cleanup prep
    114  let anotherCookieAdded = SpecialPowers.spawn(
    115    browser2,
    116    ["content-added-cookie", COOKIE_NAMEVALUE],
    117    waitForNotificationPromise
    118  );
    119  await TestUtils.waitForTick(); // waiting helps --verify
    120  await SpecialPowers.spawn(
    121    browser,
    122    [COOKIE_STRING, MAX_AGE_NEW],
    123    (cookie, max_age) => {
    124      content.document.cookie = cookie + ";Max-Age=" + max_age;
    125    }
    126  );
    127  await anotherCookieAdded;
    128 
    129  // ALL_COOKIES_CLEARED/RemoveAll pathway
    130  let cleanup = SpecialPowers.spawn(
    131    browser2,
    132    ["content-removed-all-cookies", ""],
    133    waitForNotificationPromise
    134  );
    135  await TestUtils.waitForTick();
    136  Services.cookies.removeAll();
    137  await cleanup;
    138 
    139  BrowserTestUtils.removeTab(tab1);
    140  BrowserTestUtils.removeTab(tab2);
    141 });