tor-browser

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

browser_originattributes.js (3589B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 const USER_CONTEXTS = ["default", "personal", "work"];
      6 
      7 const COOKIE_NAMES = ["cookie0", "cookie1", "cookie2"];
      8 
      9 const TEST_URL =
     10  "http://example.com/browser/netwerk/cookie/test/browser/file_empty.html";
     11 
     12 // opens `uri' in a new tab with the provided userContextId and focuses it.
     13 // returns the newly opened tab
     14 async function openTabInUserContext(uri, userContextId) {
     15  // open the tab in the correct userContextId
     16  let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });
     17 
     18  // select tab and make sure its browser is focused
     19  gBrowser.selectedTab = tab;
     20  tab.ownerGlobal.focus();
     21 
     22  let browser = gBrowser.getBrowserForTab(tab);
     23  // wait for tab load
     24  await BrowserTestUtils.browserLoaded(browser);
     25 
     26  return { tab, browser };
     27 }
     28 
     29 add_setup(async function () {
     30  // make sure userContext is enabled.
     31  await new Promise(resolve => {
     32    SpecialPowers.pushPrefEnv(
     33      { set: [["privacy.userContext.enabled", true]] },
     34      resolve
     35    );
     36  });
     37 });
     38 
     39 add_task(async function test() {
     40  // load the page in 3 different contexts and set a cookie
     41  // which should only be visible in that context
     42  for (let userContextId of Object.keys(USER_CONTEXTS)) {
     43    // open our tab in the given user context
     44    let { tab, browser } = await openTabInUserContext(TEST_URL, userContextId);
     45 
     46    await SpecialPowers.spawn(
     47      browser,
     48      [{ names: COOKIE_NAMES, value: USER_CONTEXTS[userContextId] }],
     49      function (opts) {
     50        for (let name of opts.names) {
     51          content.document.cookie = name + "=" + opts.value;
     52        }
     53      }
     54    );
     55 
     56    // remove the tab
     57    gBrowser.removeTab(tab);
     58  }
     59 
     60  let expectedValues = USER_CONTEXTS.slice(0);
     61  await checkCookies(expectedValues, "before removal");
     62 
     63  // remove cookies that belongs to user context id #1
     64  Services.cookies.removeCookiesWithOriginAttributes(
     65    JSON.stringify({ userContextId: 1 })
     66  );
     67 
     68  expectedValues[1] = undefined;
     69  await checkCookies(expectedValues, "after removal");
     70 });
     71 
     72 async function checkCookies(expectedValues, time) {
     73  for (let userContextId of Object.keys(expectedValues)) {
     74    let cookiesFromTitle = await getCookiesFromJS(userContextId);
     75    let cookiesFromManager = getCookiesFromManager(userContextId);
     76 
     77    let expectedValue = expectedValues[userContextId];
     78    for (let name of COOKIE_NAMES) {
     79      is(
     80        cookiesFromTitle[name],
     81        expectedValue,
     82        `User context ${userContextId}: ${name} should be correct from title ${time}`
     83      );
     84      is(
     85        cookiesFromManager[name],
     86        expectedValue,
     87        `User context ${userContextId}: ${name} should be correct from manager ${time}`
     88      );
     89    }
     90  }
     91 }
     92 
     93 function getCookiesFromManager(userContextId) {
     94  let cookies = {};
     95  let allCookies = Services.cookies.getCookiesWithOriginAttributes(
     96    JSON.stringify({ userContextId })
     97  );
     98  for (let cookie of allCookies) {
     99    cookies[cookie.name] = cookie.value;
    100  }
    101  return cookies;
    102 }
    103 
    104 async function getCookiesFromJS(userContextId) {
    105  let { tab, browser } = await openTabInUserContext(TEST_URL, userContextId);
    106 
    107  // get the cookies
    108  let cookieString = await SpecialPowers.spawn(browser, [], function () {
    109    return content.document.cookie;
    110  });
    111 
    112  // check each item in the title and validate it meets expectatations
    113  let cookies = {};
    114  for (let cookie of cookieString.split(";")) {
    115    let [name, value] = cookie.trim().split("=");
    116    cookies[name] = value;
    117  }
    118 
    119  gBrowser.removeTab(tab);
    120  return cookies;
    121 }