tor-browser

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

test_clear_blocked_cookies.js (2755B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 const HOSTNAME_DOMAIN = "browser_policy_clear_blocked_cookies.com";
      6 const ORIGIN_DOMAIN = "browser_policy_clear_blocked_cookies.org";
      7 
      8 add_task(async function setup() {
      9  const expiry = Date.now() + 24 * 60 * 60;
     10  let cv = Services.cookies.add(
     11    HOSTNAME_DOMAIN,
     12    "/",
     13    "secure",
     14    "true",
     15    true,
     16    false,
     17    false,
     18    expiry,
     19    {},
     20    Ci.nsICookie.SAMESITE_UNSET,
     21    Ci.nsICookie.SCHEME_HTTPS
     22  );
     23  Assert.equal(cv.result, Ci.nsICookieValidation.eOK);
     24 
     25  cv = Services.cookies.add(
     26    HOSTNAME_DOMAIN,
     27    "/",
     28    "insecure",
     29    "true",
     30    false,
     31    false,
     32    false,
     33    expiry,
     34    {},
     35    Ci.nsICookie.SAMESITE_UNSET,
     36    Ci.nsICookie.SCHEME_HTTP
     37  );
     38  Assert.equal(cv.result, Ci.nsICookieValidation.eOK);
     39 
     40  cv = Services.cookies.add(
     41    ORIGIN_DOMAIN,
     42    "/",
     43    "secure",
     44    "true",
     45    true,
     46    false,
     47    false,
     48    expiry,
     49    {},
     50    Ci.nsICookie.SAMESITE_UNSET,
     51    Ci.nsICookie.SCHEME_HTTPS
     52  );
     53  Assert.equal(cv.result, Ci.nsICookieValidation.eOK);
     54 
     55  cv = Services.cookies.add(
     56    ORIGIN_DOMAIN,
     57    "/",
     58    "insecure",
     59    "true",
     60    false,
     61    false,
     62    false,
     63    expiry,
     64    {},
     65    Ci.nsICookie.SAMESITE_UNSET,
     66    Ci.nsICookie.SCHEME_HTTP
     67  );
     68  Assert.equal(cv.result, Ci.nsICookieValidation.eOK);
     69 
     70  cv = Services.cookies.add(
     71    "example.net",
     72    "/",
     73    "secure",
     74    "true",
     75    true,
     76    false,
     77    false,
     78    expiry,
     79    {},
     80    Ci.nsICookie.SAMESITE_UNSET,
     81    Ci.nsICookie.SCHEME_HTTPS
     82  );
     83  Assert.equal(cv.result, Ci.nsICookieValidation.eOK);
     84 
     85  await setupPolicyEngineWithJson({
     86    policies: {
     87      Cookies: {
     88        Block: [`http://${HOSTNAME_DOMAIN}`, `https://${ORIGIN_DOMAIN}:8080`],
     89      },
     90    },
     91  });
     92 });
     93 
     94 function retrieve_all_cookies(host) {
     95  const values = [];
     96  for (let cookie of Services.cookies.getCookiesFromHost(host, {})) {
     97    values.push({
     98      host: cookie.host,
     99      name: cookie.name,
    100      path: cookie.path,
    101    });
    102  }
    103  return values;
    104 }
    105 
    106 add_task(async function test_cookies_for_blocked_sites_cleared() {
    107  const cookies = {
    108    hostname: retrieve_all_cookies(HOSTNAME_DOMAIN),
    109    origin: retrieve_all_cookies(ORIGIN_DOMAIN),
    110    keep: retrieve_all_cookies("example.net"),
    111  };
    112  const expected = {
    113    hostname: [],
    114    origin: [],
    115    keep: [{ host: "example.net", name: "secure", path: "/" }],
    116  };
    117  equal(
    118    JSON.stringify(cookies),
    119    JSON.stringify(expected),
    120    "All stored cookies for blocked origins should be cleared"
    121  );
    122 });
    123 
    124 add_task(function teardown() {
    125  for (let host of [HOSTNAME_DOMAIN, ORIGIN_DOMAIN, "example.net"]) {
    126    Services.cookies.removeCookiesWithOriginAttributes("{}", host);
    127  }
    128 });