tor-browser

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

browser_cookies_exceptions.js (14249B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 requestLongerTimeout(3);
      5 
      6 add_task(async function testAllow() {
      7  await runTest(
      8    async (params, observeAllPromise, apply) => {
      9      assertListContents(params, []);
     10 
     11      params.url.value = "test.com";
     12      params.btnAllow.doCommand();
     13 
     14      assertListContents(params, [
     15        ["http://test.com", params.allowL10nId],
     16        ["https://test.com", params.allowL10nId],
     17      ]);
     18 
     19      apply();
     20      await observeAllPromise;
     21    },
     22    () => {
     23      return [
     24        {
     25          type: "cookie",
     26          origin: "http://test.com",
     27          data: "added",
     28          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
     29        },
     30        {
     31          type: "cookie",
     32          origin: "https://test.com",
     33          data: "added",
     34          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
     35        },
     36      ];
     37    }
     38  );
     39 });
     40 
     41 add_task(async function testBlock() {
     42  await runTest(
     43    async (params, observeAllPromise, apply) => {
     44      params.url.value = "test.com";
     45      params.btnBlock.doCommand();
     46 
     47      assertListContents(params, [
     48        ["http://test.com", params.denyL10nId],
     49        ["https://test.com", params.denyL10nId],
     50      ]);
     51 
     52      apply();
     53      await observeAllPromise;
     54    },
     55    () => {
     56      return [
     57        {
     58          type: "cookie",
     59          origin: "http://test.com",
     60          data: "changed",
     61          capability: Ci.nsIPermissionManager.DENY_ACTION,
     62        },
     63        {
     64          type: "cookie",
     65          origin: "https://test.com",
     66          data: "changed",
     67          capability: Ci.nsIPermissionManager.DENY_ACTION,
     68        },
     69      ];
     70    }
     71  );
     72 });
     73 
     74 add_task(async function testAllowAgain() {
     75  await runTest(
     76    async (params, observeAllPromise, apply) => {
     77      params.url.value = "test.com";
     78      params.btnAllow.doCommand();
     79 
     80      assertListContents(params, [
     81        ["http://test.com", params.allowL10nId],
     82        ["https://test.com", params.allowL10nId],
     83      ]);
     84 
     85      apply();
     86      await observeAllPromise;
     87    },
     88    () => {
     89      return [
     90        {
     91          type: "cookie",
     92          origin: "http://test.com",
     93          data: "changed",
     94          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
     95        },
     96        {
     97          type: "cookie",
     98          origin: "https://test.com",
     99          data: "changed",
    100          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    101        },
    102      ];
    103    }
    104  );
    105 });
    106 
    107 add_task(async function testRemove() {
    108  await runTest(
    109    async (params, observeAllPromise, apply) => {
    110      while (params.richlistbox.itemCount) {
    111        params.richlistbox.selectedIndex = 0;
    112        params.btnRemove.doCommand();
    113      }
    114      assertListContents(params, []);
    115 
    116      apply();
    117      await observeAllPromise;
    118    },
    119    params => {
    120      let richlistItems = params.richlistbox.getElementsByAttribute(
    121        "origin",
    122        "*"
    123      );
    124      let observances = [];
    125      for (let item of richlistItems) {
    126        observances.push({
    127          type: "cookie",
    128          origin: item.getAttribute("origin"),
    129          data: "deleted",
    130        });
    131      }
    132      return observances;
    133    }
    134  );
    135 });
    136 
    137 add_task(async function testAdd() {
    138  await runTest(
    139    async (params, observeAllPromise, apply) => {
    140      let uri = Services.io.newURI("http://test.com");
    141      PermissionTestUtils.add(
    142        uri,
    143        "popup",
    144        Ci.nsIPermissionManager.DENY_ACTION
    145      );
    146 
    147      info("Adding unrelated permission should not change display.");
    148      assertListContents(params, []);
    149 
    150      apply();
    151      await observeAllPromise;
    152 
    153      PermissionTestUtils.remove(uri, "popup");
    154    },
    155    () => {
    156      return [
    157        {
    158          type: "popup",
    159          origin: "http://test.com",
    160          data: "added",
    161          capability: Ci.nsIPermissionManager.DENY_ACTION,
    162        },
    163      ];
    164    }
    165  );
    166 });
    167 
    168 add_task(async function testAllowHTTPSWithPort() {
    169  await runTest(
    170    async (params, observeAllPromise, apply) => {
    171      params.url.value = "https://test.com:12345";
    172      params.btnAllow.doCommand();
    173 
    174      assertListContents(params, [
    175        ["https://test.com:12345", params.allowL10nId],
    176      ]);
    177 
    178      apply();
    179      await observeAllPromise;
    180    },
    181    () => {
    182      return [
    183        {
    184          type: "cookie",
    185          origin: "https://test.com:12345",
    186          data: "added",
    187          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    188        },
    189      ];
    190    }
    191  );
    192 });
    193 
    194 add_task(async function testBlockHTTPSWithPort() {
    195  await runTest(
    196    async (params, observeAllPromise, apply) => {
    197      params.url.value = "https://test.com:12345";
    198      params.btnBlock.doCommand();
    199 
    200      assertListContents(params, [
    201        ["https://test.com:12345", params.denyL10nId],
    202      ]);
    203 
    204      apply();
    205      await observeAllPromise;
    206    },
    207    () => {
    208      return [
    209        {
    210          type: "cookie",
    211          origin: "https://test.com:12345",
    212          data: "changed",
    213          capability: Ci.nsIPermissionManager.DENY_ACTION,
    214        },
    215      ];
    216    }
    217  );
    218 });
    219 
    220 add_task(async function testAllowAgainHTTPSWithPort() {
    221  await runTest(
    222    async (params, observeAllPromise, apply) => {
    223      params.url.value = "https://test.com:12345";
    224      params.btnAllow.doCommand();
    225 
    226      assertListContents(params, [
    227        ["https://test.com:12345", params.allowL10nId],
    228      ]);
    229 
    230      apply();
    231      await observeAllPromise;
    232    },
    233    () => {
    234      return [
    235        {
    236          type: "cookie",
    237          origin: "https://test.com:12345",
    238          data: "changed",
    239          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    240        },
    241      ];
    242    }
    243  );
    244 });
    245 
    246 add_task(async function testRemoveHTTPSWithPort() {
    247  await runTest(
    248    async (params, observeAllPromise, apply) => {
    249      while (params.richlistbox.itemCount) {
    250        params.richlistbox.selectedIndex = 0;
    251        params.btnRemove.doCommand();
    252      }
    253 
    254      assertListContents(params, []);
    255 
    256      apply();
    257      await observeAllPromise;
    258    },
    259    params => {
    260      let richlistItems = params.richlistbox.getElementsByAttribute(
    261        "origin",
    262        "*"
    263      );
    264      let observances = [];
    265      for (let item of richlistItems) {
    266        observances.push({
    267          type: "cookie",
    268          origin: item.getAttribute("origin"),
    269          data: "deleted",
    270        });
    271      }
    272      return observances;
    273    }
    274  );
    275 });
    276 
    277 add_task(async function testAllowPort() {
    278  await runTest(
    279    async (params, observeAllPromise, apply) => {
    280      params.url.value = "localhost:12345";
    281      params.btnAllow.doCommand();
    282 
    283      assertListContents(params, [
    284        ["http://localhost:12345", params.allowL10nId],
    285        ["https://localhost:12345", params.allowL10nId],
    286      ]);
    287 
    288      apply();
    289      await observeAllPromise;
    290    },
    291    () => {
    292      return [
    293        {
    294          type: "cookie",
    295          origin: "http://localhost:12345",
    296          data: "added",
    297          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    298        },
    299        {
    300          type: "cookie",
    301          origin: "https://localhost:12345",
    302          data: "added",
    303          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    304        },
    305      ];
    306    }
    307  );
    308 });
    309 
    310 add_task(async function testBlockPort() {
    311  await runTest(
    312    async (params, observeAllPromise, apply) => {
    313      params.url.value = "localhost:12345";
    314      params.btnBlock.doCommand();
    315 
    316      assertListContents(params, [
    317        ["http://localhost:12345", params.denyL10nId],
    318        ["https://localhost:12345", params.denyL10nId],
    319      ]);
    320 
    321      apply();
    322      await observeAllPromise;
    323    },
    324    () => {
    325      return [
    326        {
    327          type: "cookie",
    328          origin: "http://localhost:12345",
    329          data: "changed",
    330          capability: Ci.nsIPermissionManager.DENY_ACTION,
    331        },
    332        {
    333          type: "cookie",
    334          origin: "https://localhost:12345",
    335          data: "changed",
    336          capability: Ci.nsIPermissionManager.DENY_ACTION,
    337        },
    338      ];
    339    }
    340  );
    341 });
    342 
    343 add_task(async function testAllowAgainPort() {
    344  await runTest(
    345    async (params, observeAllPromise, apply) => {
    346      params.url.value = "localhost:12345";
    347      params.btnAllow.doCommand();
    348 
    349      assertListContents(params, [
    350        ["http://localhost:12345", params.allowL10nId],
    351        ["https://localhost:12345", params.allowL10nId],
    352      ]);
    353 
    354      apply();
    355      await observeAllPromise;
    356    },
    357    () => {
    358      return [
    359        {
    360          type: "cookie",
    361          origin: "http://localhost:12345",
    362          data: "changed",
    363          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    364        },
    365        {
    366          type: "cookie",
    367          origin: "https://localhost:12345",
    368          data: "changed",
    369          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    370        },
    371      ];
    372    }
    373  );
    374 });
    375 
    376 add_task(async function testRemovePort() {
    377  await runTest(
    378    async (params, observeAllPromise, apply) => {
    379      while (params.richlistbox.itemCount) {
    380        params.richlistbox.selectedIndex = 0;
    381        params.btnRemove.doCommand();
    382      }
    383 
    384      assertListContents(params, []);
    385 
    386      apply();
    387      await observeAllPromise;
    388    },
    389    params => {
    390      let richlistItems = params.richlistbox.getElementsByAttribute(
    391        "origin",
    392        "*"
    393      );
    394      let observances = [];
    395      for (let item of richlistItems) {
    396        observances.push({
    397          type: "cookie",
    398          origin: item.getAttribute("origin"),
    399          data: "deleted",
    400        });
    401      }
    402      return observances;
    403    }
    404  );
    405 });
    406 
    407 add_task(async function testSort() {
    408  await runTest(
    409    async (params, observeAllPromise, apply) => {
    410      // Sort by site name.
    411      EventUtils.synthesizeMouseAtCenter(
    412        params.doc.getElementById("siteCol"),
    413        {},
    414        params.doc.defaultView
    415      );
    416 
    417      for (let URL of ["http://a", "http://z", "http://b"]) {
    418        let URI = Services.io.newURI(URL);
    419        PermissionTestUtils.add(
    420          URI,
    421          "cookie",
    422          Ci.nsIPermissionManager.ALLOW_ACTION
    423        );
    424      }
    425 
    426      assertListContents(params, [
    427        ["http://a", params.allowL10nId],
    428        ["http://b", params.allowL10nId],
    429        ["http://z", params.allowL10nId],
    430      ]);
    431 
    432      // Sort by site name in descending order.
    433      EventUtils.synthesizeMouseAtCenter(
    434        params.doc.getElementById("siteCol"),
    435        {},
    436        params.doc.defaultView
    437      );
    438 
    439      assertListContents(params, [
    440        ["http://z", params.allowL10nId],
    441        ["http://b", params.allowL10nId],
    442        ["http://a", params.allowL10nId],
    443      ]);
    444 
    445      apply();
    446      await observeAllPromise;
    447 
    448      for (let URL of ["http://a", "http://z", "http://b"]) {
    449        let uri = Services.io.newURI(URL);
    450        PermissionTestUtils.remove(uri, "cookie");
    451      }
    452    },
    453    () => {
    454      return [
    455        {
    456          type: "cookie",
    457          origin: "http://a",
    458          data: "added",
    459          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    460        },
    461        {
    462          type: "cookie",
    463          origin: "http://z",
    464          data: "added",
    465          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    466        },
    467        {
    468          type: "cookie",
    469          origin: "http://b",
    470          data: "added",
    471          capability: Ci.nsIPermissionManager.ALLOW_ACTION,
    472        },
    473      ];
    474    }
    475  );
    476 });
    477 
    478 add_task(async function testPrivateBrowsingSessionPermissionsAreHidden() {
    479  await runTest(
    480    async params => {
    481      assertListContents(params, []);
    482 
    483      let uri = Services.io.newURI("http://test.com");
    484      let privateBrowsingPrincipal =
    485        Services.scriptSecurityManager.createContentPrincipal(uri, {
    486          privateBrowsingId: 1,
    487        });
    488 
    489      // Add a session permission for private browsing.
    490      PermissionTestUtils.add(
    491        privateBrowsingPrincipal,
    492        "cookie",
    493        Services.perms.ALLOW_ACTION,
    494        Services.perms.EXPIRE_SESSION
    495      );
    496 
    497      assertListContents(params, []);
    498 
    499      PermissionTestUtils.remove(uri, "cookie");
    500    },
    501    () => {
    502      return [];
    503    }
    504  );
    505 });
    506 
    507 function assertListContents(params, expected) {
    508  Assert.equal(params.richlistbox.itemCount, expected.length);
    509 
    510  for (let i = 0; i < expected.length; i++) {
    511    let website = expected[i][0];
    512    let elements = params.richlistbox.getElementsByAttribute("origin", website);
    513    Assert.equal(elements.length, 1); // "It should find only one coincidence"
    514    Assert.equal(
    515      elements[0]
    516        .querySelector(".website-capability-value")
    517        .getAttribute("data-l10n-id"),
    518      expected[i][1]
    519    );
    520  }
    521 }
    522 
    523 async function runTest(test, getObservances) {
    524  registerCleanupFunction(function () {
    525    Services.prefs.clearUserPref("privacy.history.custom");
    526  });
    527 
    528  await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
    529    leaveOpen: true,
    530  });
    531 
    532  let doc = gBrowser.contentDocument;
    533 
    534  await selectHistoryMode(gBrowser.contentWindow, "custom");
    535 
    536  let promiseSubDialogLoaded = promiseLoadSubDialog(
    537    "chrome://browser/content/preferences/dialogs/permissions.xhtml"
    538  );
    539 
    540  let cookieExceptionsButton = doc.getElementById("cookieExceptions");
    541  cookieExceptionsButton.scrollIntoView();
    542  await EventUtils.synthesizeMouseAtCenter(
    543    cookieExceptionsButton,
    544    {},
    545    doc.ownerGlobal
    546  );
    547 
    548  let win = await promiseSubDialogLoaded;
    549 
    550  doc = win.document;
    551  let params = {
    552    doc,
    553    richlistbox: doc.getElementById("permissionsBox"),
    554    url: doc.getElementById("url"),
    555    btnAllow: doc.getElementById("btnAllow"),
    556    btnBlock: doc.getElementById("btnBlock"),
    557    btnRemove: doc.getElementById("removePermission"),
    558    allowL10nId: win.gPermissionManager._getCapabilityL10nId(
    559      Ci.nsIPermissionManager.ALLOW_ACTION
    560    ),
    561    denyL10nId: win.gPermissionManager._getCapabilityL10nId(
    562      Ci.nsIPermissionManager.DENY_ACTION
    563    ),
    564    allow: Ci.nsIPermissionManager.ALLOW_ACTION,
    565    deny: Ci.nsIPermissionManager.DENY_ACTION,
    566  };
    567  let btnApplyChanges = doc.querySelector("dialog").getButton("accept");
    568  let observances = getObservances(params);
    569  let observeAllPromise = createObserveAllPromise(observances);
    570 
    571  await test(params, observeAllPromise, () => btnApplyChanges.doCommand());
    572 
    573  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    574 }