tor-browser

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

browser_strip_on_share_nested_link.js (5342B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 let listService;
      7 
      8 const TEST_URL =
      9  "https://example.com/browser/browser/base/content/test/general/dummy_page.html";
     10 
     11 add_setup(async function () {
     12  await SpecialPowers.pushPrefEnv({
     13    set: [
     14      ["test.wait300msAfterTabSwitch", true],
     15      ["privacy.query_stripping.strip_list", "stripParam"],
     16    ],
     17  });
     18 
     19  // Get the list service so we can wait for it to be fully initialized before running tests.
     20  listService = Cc["@mozilla.org/query-stripping-list-service;1"].getService(
     21    Ci.nsIURLQueryStrippingListService
     22  );
     23 
     24  await listService.testWaitForInit();
     25 });
     26 
     27 /*
     28 Tests the strip-on-share feature for in-content links with nested urls
     29 */
     30 
     31 // Testing nested stripping with global params
     32 add_task(async function testNestedStrippingGlobalParam() {
     33  let validUrl =
     34    "https://www.example.com/?test=https%3A%2F%2Fwww.example.net%2F%3Futm_ad%3D1234";
     35  let shortenedUrl =
     36    "https://www.example.com/?test=https%3A%2F%2Fwww.example.net%2F";
     37  await testStripOnShare({
     38    originalURI: validUrl,
     39    strippedURI: shortenedUrl,
     40  });
     41 });
     42 
     43 // Testing nested stripping with site specific params
     44 add_task(async function testNestedStrippingSiteSpecific() {
     45  let validUrl =
     46    "https://www.example.com/?test=https%3A%2F%2Fwww.example.net%2F%3Ftest_3%3D1234";
     47  let shortenedUrl =
     48    "https://www.example.com/?test=https%3A%2F%2Fwww.example.net%2F";
     49  await testStripOnShare({
     50    originalURI: validUrl,
     51    strippedURI: shortenedUrl,
     52  });
     53 });
     54 
     55 // Testing nested stripping with incorrect site specific params
     56 add_task(async function testNoStrippedNestedParam() {
     57  let validUrl =
     58    "https://www.example.com/?test=https%3A%2F%2Fwww.example.com%2F%3Ftest_3%3D1234";
     59  let shortenedUrl =
     60    "https://www.example.com/?test=https%3A%2F%2Fwww.example.com%2F%3Ftest_3%3D1234";
     61  await testStripOnShare({
     62    originalURI: validUrl,
     63    strippedURI: shortenedUrl,
     64  });
     65 });
     66 
     67 // Testing order of stripping for nested stripping
     68 add_task(async function testOrderOfStripping() {
     69  let validUrl =
     70    "https://www.example.com/?test_1=https%3A%2F%2Fwww.example.net%2F%3Ftest_3%3D1234";
     71  let shortenedUrl = "https://www.example.com/";
     72  await testStripOnShare({
     73    originalURI: validUrl,
     74    strippedURI: shortenedUrl,
     75  });
     76 });
     77 
     78 // Testing correct scoping of site specific params for nested stripping
     79 add_task(async function testMultipleQueryParamsWithNestedStripping() {
     80  let validUrl =
     81    "https://www.example.com/?test_3=1234&test=https%3A%2F%2Fwww.example.net%2F%3Ftest_3%3D1234";
     82  let shortenedUrl =
     83    "https://www.example.com/?test_3=1234&test=https%3A%2F%2Fwww.example.net%2F";
     84  await testStripOnShare({
     85    originalURI: validUrl,
     86    strippedURI: shortenedUrl,
     87  });
     88 });
     89 
     90 // Testing functionality with no https pages
     91 add_task(async function testNonHTTPsPages() {
     92  let validUrl = "https://www.example.com/?test_2=1234&test=about%3A%3Aconfig";
     93  let shortenedUrl = "https://www.example.com/?test=about%3A%3Aconfig";
     94  await testStripOnShare({
     95    originalURI: validUrl,
     96    strippedURI: shortenedUrl,
     97  });
     98 });
     99 
    100 /**
    101 * Opens a new tab, opens the context menu and checks that the strip-on-share menu item is visible.
    102 * Checks that the stripped version of the url is copied to the clipboard.
    103 *
    104 * @param {string} originalURI - The orginal url before the stripping occurs
    105 * @param {string} strippedURI - The expected url after stripping occurs
    106 */
    107 async function testStripOnShare({ originalURI, strippedURI }) {
    108  await SpecialPowers.pushPrefEnv({
    109    set: [
    110      ["privacy.query_stripping.strip_on_share.enabled", true],
    111      ["privacy.query_stripping.strip_on_share.enableTestMode", true],
    112    ],
    113  });
    114 
    115  let testJson = {
    116    global: {
    117      queryParams: ["utm_ad"],
    118      topLevelSites: ["*"],
    119    },
    120    example: {
    121      queryParams: ["test_2", "test_1"],
    122      topLevelSites: ["www.example.com"],
    123    },
    124    exampleNet: {
    125      queryParams: ["test_3", "test_4"],
    126      topLevelSites: ["www.example.net"],
    127    },
    128  };
    129 
    130  await listService.testSetList(testJson);
    131 
    132  await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) {
    133    // Prepare a link
    134    await SpecialPowers.spawn(browser, [originalURI], function (startingURI) {
    135      let link = content.document.createElement("a");
    136      link.href = startingURI;
    137      link.textContent = "link with query param";
    138      link.id = "link";
    139      content.document.body.appendChild(link);
    140    });
    141    let contextMenu = document.getElementById("contentAreaContextMenu");
    142    // Open the context menu
    143    let awaitPopupShown = BrowserTestUtils.waitForEvent(
    144      contextMenu,
    145      "popupshown"
    146    );
    147    await BrowserTestUtils.synthesizeMouseAtCenter(
    148      "#link",
    149      { type: "contextmenu", button: 2 },
    150      browser
    151    );
    152    await awaitPopupShown;
    153    let awaitPopupHidden = BrowserTestUtils.waitForEvent(
    154      contextMenu,
    155      "popuphidden"
    156    );
    157    let stripOnShare = contextMenu.querySelector("#context-stripOnShareLink");
    158    Assert.ok(BrowserTestUtils.isVisible(stripOnShare), "Menu item is visible");
    159    // Make sure the stripped link will be copied to the clipboard
    160    await SimpleTest.promiseClipboardChange(strippedURI, () => {
    161      contextMenu.activateItem(stripOnShare);
    162    });
    163    await awaitPopupHidden;
    164  });
    165 }