tor-browser

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

browser_cookies_sameSite.js (2454B)


      1 "use strict";
      2 
      3 const TEST_HTTP_URL = "http://example.com";
      4 const TEST_HTTPS_URL = "https://example.com";
      5 const MAX_EXPIRY = Math.pow(2, 62);
      6 
      7 function getSingleCookie() {
      8  let cookies = Array.from(Services.cookies.cookies);
      9  Assert.equal(cookies.length, 1, "expected one cookie");
     10  return cookies[0];
     11 }
     12 
     13 async function verifyRestore(url, sameSiteSetting) {
     14  Services.cookies.removeAll();
     15 
     16  // Make sure that sessionstore.js can be forced to be created by setting
     17  // the interval pref to 0.
     18  await SpecialPowers.pushPrefEnv({
     19    set: [["browser.sessionstore.interval", 0]],
     20  });
     21 
     22  let tab = BrowserTestUtils.addTab(gBrowser, url);
     23  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
     24 
     25  // Add a cookie with specific same-site setting.
     26  let r = Math.floor(Math.random() * MAX_EXPIRY);
     27  const cv = Services.cookies.add(
     28    url,
     29    "/",
     30    "name" + r,
     31    "value" + r,
     32    false,
     33    false,
     34    true,
     35    MAX_EXPIRY,
     36    {},
     37    sameSiteSetting,
     38    url.startsWith("https:")
     39      ? Ci.nsICookie.SCHEME_HTTPS
     40      : Ci.nsICookie.SCHEME_HTTP
     41  );
     42  is(cv.result, Ci.nsICookieValidation.eOK, "Valid cookie");
     43 
     44  await TabStateFlusher.flush(tab.linkedBrowser);
     45 
     46  // Get the sessionstore state for the window.
     47  let state = ss.getBrowserState();
     48 
     49  // Verify our cookie got set.
     50  let cookie = getSingleCookie();
     51 
     52  // Remove the cookie.
     53  Services.cookies.removeAll();
     54 
     55  // Restore the window state.
     56  await setBrowserState(state);
     57 
     58  // At this point, the cookie should be restored.
     59  let cookie2 = getSingleCookie();
     60 
     61  is(
     62    cookie2.sameSite,
     63    cookie.sameSite,
     64    "cookie same-site flag successfully restored"
     65  );
     66 
     67  is(
     68    cookie2.schemeMap,
     69    cookie.schemeMap,
     70    "cookie schemeMap flag successfully restored"
     71  );
     72 
     73  // Clean up.
     74  Services.cookies.removeAll();
     75  BrowserTestUtils.removeTab(gBrowser.tabs[1]);
     76 }
     77 
     78 /**
     79 * Tests that cookie.sameSite flag is stored and restored correctly by
     80 * sessionstore.
     81 */
     82 add_task(async function () {
     83  // Test for various possible values of cookie.sameSite and schemeMap.
     84  await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_UNSET);
     85  await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_LAX);
     86  await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_STRICT);
     87 
     88  await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_UNSET);
     89  await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_LAX);
     90  await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_STRICT);
     91 });