tor-browser

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

browser_partitionedConsole.js (5441B)


      1 "use strict";
      2 
      3 const DOMAIN = "https://example.com/";
      4 const PATH = "browser/netwerk/cookie/test/browser/";
      5 const TOP_PAGE = DOMAIN + PATH + "file_empty.html";
      6 
      7 // Run the test with CHIPS disabled, expecting a warning message
      8 add_task(async _ => {
      9  await SpecialPowers.pushPrefEnv({
     10    set: [["network.cookie.cookieBehavior.optInPartitioning", false]],
     11  });
     12 
     13  const expected = [];
     14 
     15  const consoleListener = {
     16    observe(what) {
     17      if (!(what instanceof Ci.nsIConsoleMessage)) {
     18        return;
     19      }
     20 
     21      info("Console Listener: " + what);
     22      for (let i = expected.length - 1; i >= 0; --i) {
     23        const e = expected[i];
     24 
     25        if (what.message.includes(e.match)) {
     26          ok(true, "Message received: " + e.match);
     27          expected.splice(i, 1);
     28          e.resolve();
     29        }
     30      }
     31    },
     32  };
     33 
     34  Services.console.registerListener(consoleListener);
     35 
     36  registerCleanupFunction(() =>
     37    Services.console.unregisterListener(consoleListener)
     38  );
     39 
     40  const netPromises = [
     41    new Promise(resolve => {
     42      expected.push({
     43        resolve,
     44        match:
     45          "Cookie “a” will soon be rejected because it is foreign and does not have the “Partitioned“ attribute.",
     46      });
     47    }),
     48  ];
     49 
     50  // Let's open our tab.
     51  const tab = BrowserTestUtils.addTab(gBrowser, TOP_PAGE);
     52  gBrowser.selectedTab = tab;
     53  const browser = gBrowser.getBrowserForTab(tab);
     54  await BrowserTestUtils.browserLoaded(browser);
     55 
     56  // Set cookies with cross-site HTTP
     57  await SpecialPowers.spawn(browser, [], async function () {
     58    await content.fetch(
     59      "https://example.org/browser/netwerk/cookie/test/browser/partitioned.sjs",
     60      { credentials: "include" }
     61    );
     62  });
     63 
     64  // Let's wait for the first set of console events.
     65  await Promise.all(netPromises);
     66 
     67  // Let's close the tab.
     68  BrowserTestUtils.removeTab(tab);
     69 });
     70 
     71 // Run the test with CHIPS enabled, expecting a different warning message
     72 add_task(async _ => {
     73  await SpecialPowers.pushPrefEnv({
     74    set: [["network.cookie.cookieBehavior.optInPartitioning", true]],
     75  });
     76 
     77  const expected = [];
     78 
     79  const consoleListener = {
     80    observe(what) {
     81      if (!(what instanceof Ci.nsIConsoleMessage)) {
     82        return;
     83      }
     84 
     85      info("Console Listener: " + what);
     86      for (let i = expected.length - 1; i >= 0; --i) {
     87        const e = expected[i];
     88 
     89        if (what.message.includes(e.match)) {
     90          ok(true, "Message received: " + e.match);
     91          expected.splice(i, 1);
     92          e.resolve();
     93        }
     94      }
     95    },
     96  };
     97 
     98  Services.console.registerListener(consoleListener);
     99 
    100  registerCleanupFunction(() =>
    101    Services.console.unregisterListener(consoleListener)
    102  );
    103 
    104  const netPromises = [
    105    new Promise(resolve => {
    106      expected.push({
    107        resolve,
    108        match:
    109          "Cookie “a” has been rejected because it is foreign and does not have the “Partitioned“ attribute.",
    110      });
    111    }),
    112  ];
    113 
    114  // Let's open our tab.
    115  const tab = BrowserTestUtils.addTab(gBrowser, TOP_PAGE);
    116  gBrowser.selectedTab = tab;
    117  const browser = gBrowser.getBrowserForTab(tab);
    118  await BrowserTestUtils.browserLoaded(browser);
    119 
    120  // Set cookies with cross-site HTTP
    121  await SpecialPowers.spawn(browser, [], async function () {
    122    await content.fetch(
    123      "https://example.org/browser/netwerk/cookie/test/browser/partitioned.sjs",
    124      { credentials: "include" }
    125    );
    126  });
    127 
    128  // Let's wait for the first set of console events.
    129  await Promise.all(netPromises);
    130 
    131  // Let's close the tab.
    132  BrowserTestUtils.removeTab(tab);
    133 });
    134 
    135 // Run the test with CHIPS enabled, ensuring the partitioned cookies require
    136 // secure context.
    137 add_task(async function partitionedAttrRequiresSecure() {
    138  await SpecialPowers.pushPrefEnv({
    139    set: [
    140      ["network.cookie.cookieBehavior.optInPartitioning", true],
    141      ["network.cookie.CHIPS.enabled", true],
    142    ],
    143  });
    144 
    145  // Clear all cookies before testing.
    146  Services.cookies.removeAll();
    147 
    148  const expected = [];
    149 
    150  const consoleListener = {
    151    observe(what) {
    152      if (!(what instanceof Ci.nsIConsoleMessage)) {
    153        return;
    154      }
    155 
    156      info("Console Listener: " + what);
    157      for (let i = expected.length - 1; i >= 0; --i) {
    158        const e = expected[i];
    159 
    160        if (what.message.includes(e.match)) {
    161          ok(true, "Message received: " + e.match);
    162          expected.splice(i, 1);
    163          e.resolve();
    164        }
    165      }
    166    },
    167  };
    168 
    169  Services.console.registerListener(consoleListener);
    170 
    171  registerCleanupFunction(() =>
    172    Services.console.unregisterListener(consoleListener)
    173  );
    174 
    175  const netPromises = [
    176    new Promise(resolve => {
    177      expected.push({
    178        resolve,
    179        match:
    180          "Cookie “c” has been rejected because it has the “Partitioned” attribute but is missing the “secure” attribute.",
    181      });
    182    }),
    183  ];
    184 
    185  // Let's open our tab.
    186  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TOP_PAGE);
    187 
    188  // Set cookies with cross-site HTTP
    189  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    190    await content.fetch(
    191      "https://example.org/browser/netwerk/cookie/test/browser/partitioned.sjs?nosecure",
    192      { credentials: "include" }
    193    );
    194  });
    195 
    196  // Let's wait for the first set of console events.
    197  await Promise.all(netPromises);
    198 
    199  // Ensure no cookie is set.
    200  is(Services.cookies.cookies.length, 0, "No cookie is set.");
    201 
    202  // Let's close the tab.
    203  BrowserTestUtils.removeTab(tab);
    204 });