tor-browser

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

browser_storage_cookies_partitioned.js (6276B)


      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 "use strict";
      6 
      7 // Test that the Partitioned cookies are displayed.
      8 
      9 SpecialPowers.pushPrefEnv({
     10  set: [["security.allow_eval_with_system_principal", true]],
     11 });
     12 
     13 const doctype = `<!DOCTYPE html>`;
     14 const cookieAttrs = `Secure;SameSite=None;Path=/`;
     15 
     16 const NESTED_EXAMPLE_COM_URI =
     17  "https://example.com/document-builder.sjs?" +
     18  new URLSearchParams({
     19    html: `${doctype}
     20      <h1>Example.com iframe<h1>
     21      <script>
     22        document.cookie = "foo=partitioned-nested; Partitioned; ${cookieAttrs}";
     23      </script>`,
     24  });
     25 
     26 const EXAMPLE_ORG_URI =
     27  "https://example.org/document-builder.sjs?" +
     28  new URLSearchParams({
     29    html: `${doctype}
     30      <h1>Example.org iframe<h1>
     31      <iframe src="${NESTED_EXAMPLE_COM_URI}"></iframe>
     32      <script>
     33        document.cookie = "fooThirdPartyPartitioned=partitioned-third-party; Partitioned; ${cookieAttrs}";
     34      </script>`,
     35  });
     36 
     37 const EXAMPLE_COM_URI =
     38  "https://example.com/document-builder.sjs?" +
     39  new URLSearchParams({
     40    html: `${doctype}<h1>Top example.com<h1><iframe src="${EXAMPLE_ORG_URI}"></iframe>
     41      <script>
     42        document.cookie = "foo=unpartitioned-top; ${cookieAttrs}";
     43      </script>`,
     44  });
     45 
     46 add_task(async function () {
     47  await openTabAndSetupStorage(EXAMPLE_COM_URI);
     48  gUI.tree.expandAll();
     49  showColumn("partitionKey", true);
     50 
     51  info("Check that we get both partitioned and unpartitioned cookies");
     52  await selectTreeItem(["cookies", "https://example.com"]);
     53  const topLevelExampleComFooId = getCookieId("foo", "example.com", "/");
     54  await selectTableItem(topLevelExampleComFooId);
     55  checkCell(topLevelExampleComFooId, "value", "unpartitioned-top");
     56  checkCell(topLevelExampleComFooId, "partitionKey", "");
     57  await findVariableViewProperties([
     58    { name: "foo.Partition Key", dontMatch: true },
     59  ]);
     60 
     61  const nestedExampleComFooId = getCookieId(
     62    "foo",
     63    "example.com",
     64    "/",
     65    "(https,example.com,f)"
     66  );
     67  await selectTableItem(nestedExampleComFooId);
     68  checkCell(nestedExampleComFooId, "value", "partitioned-nested");
     69  checkCell(nestedExampleComFooId, "partitionKey", "https://example.com");
     70  await findVariableViewProperties([
     71    { name: "foo.Partition Key", value: "https://example.com" },
     72  ]);
     73 
     74  await selectTreeItem(["cookies", "https://example.org"]);
     75  const thirdPartyPartitionedId = getCookieId(
     76    "fooThirdPartyPartitioned",
     77    "example.org",
     78    "/",
     79    "(https,example.com)"
     80  );
     81  await selectTableItem(thirdPartyPartitionedId);
     82  checkCell(thirdPartyPartitionedId, "value", "partitioned-third-party");
     83  checkCell(thirdPartyPartitionedId, "partitionKey", "https://example.com");
     84  await findVariableViewProperties([
     85    {
     86      name: "fooThirdPartyPartitioned.Partition Key",
     87      value: "https://example.com",
     88    },
     89  ]);
     90 
     91  info("Edit unpartitioned cookie value");
     92  await selectTreeItem(["cookies", "https://example.com"]);
     93 
     94  await editCell(topLevelExampleComFooId, "value", "unpartitioned-top-updated");
     95  is(
     96    await getTopLevelContentPageCookie(),
     97    "foo=unpartitioned-top-updated",
     98    "top-level, unpartitioned cookie value was updated as expected"
     99  );
    100  is(
    101    await getNestedExampleComContentPageCookie(),
    102    "foo=partitioned-nested",
    103    "partitioned cookie value was not updated"
    104  );
    105 
    106  info("Edit partitioned cookie value");
    107  await editCell(nestedExampleComFooId, "value", "partitioned-nested-updated");
    108  is(
    109    await getNestedExampleComContentPageCookie(),
    110    "foo=partitioned-nested-updated",
    111    "partitioned cookie value was updated as expected"
    112  );
    113 
    114  info("Remove unpartitioned cookie");
    115  await removeCookieWithKeyboard(topLevelExampleComFooId);
    116  is(
    117    await getTopLevelContentPageCookie(),
    118    "",
    119    "top-level, unpartitioned cookie was removed"
    120  );
    121  is(
    122    await getNestedExampleComContentPageCookie(),
    123    "foo=partitioned-nested-updated",
    124    "partitioned cookie was not removed"
    125  );
    126 
    127  info("Remove partitioned cookie");
    128  await removeCookieWithKeyboard(nestedExampleComFooId);
    129  is(
    130    await getNestedExampleComContentPageCookie(),
    131    "",
    132    "partitioned cookie value was updated as expected"
    133  );
    134 });
    135 
    136 add_task(async function checkRemoveAll() {
    137  await openTabAndSetupStorage(EXAMPLE_COM_URI);
    138  gUI.tree.expandAll();
    139 
    140  info(
    141    "Check that remove all does remove all partitioned and unpartitioned cookies"
    142  );
    143  await selectTreeItem(["cookies", "https://example.com"]);
    144 
    145  const contextMenu =
    146    gPanelWindow.document.getElementById("storage-tree-popup");
    147  const menuDeleteAllItem = contextMenu.querySelector(
    148    "#storage-tree-popup-delete-all"
    149  );
    150 
    151  const eventName = "store-objects-edit";
    152  const onRemoved = gUI.once(eventName);
    153 
    154  const selector = `[data-id='${JSON.stringify([
    155    "cookies",
    156    "https://example.com",
    157  ])}'] > .tree-widget-item`;
    158  const target = gPanelWindow.document.querySelector(selector);
    159  await waitForContextMenu(contextMenu, target, () => {
    160    contextMenu.activateItem(menuDeleteAllItem);
    161  });
    162  await onRemoved;
    163 
    164  is(
    165    await getTopLevelContentPageCookie(),
    166    "",
    167    "top-level, unpartitioned cookie was removed"
    168  );
    169  is(
    170    await getNestedExampleComContentPageCookie(),
    171    "",
    172    "partitioned cookie was removed as well"
    173  );
    174 });
    175 
    176 function getTopLevelContentPageCookie() {
    177  return SpecialPowers.spawn(
    178    gBrowser.selectedBrowser,
    179    [],
    180    () => content.window.document.cookie
    181  );
    182 }
    183 
    184 async function getNestedExampleComContentPageCookie() {
    185  const remoteIframeBc = await SpecialPowers.spawn(
    186    gBrowser.selectedBrowser,
    187    [],
    188    () => content.document.querySelector("iframe").browsingContext
    189  );
    190  return SpecialPowers.spawn(remoteIframeBc, [], async () => {
    191    const iframe = content.document.querySelector("iframe");
    192    // evalute code in nested remote frame
    193    const res = await SpecialPowers.spawn(
    194      iframe,
    195      [],
    196      () => content.document.cookie
    197    );
    198    return res;
    199  });
    200 }
    201 
    202 async function removeCookieWithKeyboard(cookieId) {
    203  await selectTableItem(cookieId);
    204  const onDelete = gUI.once("store-objects-edit");
    205  EventUtils.synthesizeKey("VK_BACK_SPACE", {}, gPanelWindow);
    206  await onDelete;
    207 }