tor-browser

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

browser_csp_uir.js (2911B)


      1 "use strict";
      2 
      3 const TEST_PATH = getRootDirectory(gTestPath).replace(
      4  "chrome://mochitests/content",
      5  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      6  "http://example.com"
      7 );
      8 const TEST_URI = TEST_PATH + "file_csp_uir.html"; // important to be http: to test upgrade-insecure-requests
      9 const RESULT_URI =
     10  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     11  TEST_PATH.replace("http://", "https://") + "file_csp_uir_dummy.html";
     12 
     13 function verifyCSP(aTestName, aBrowser, aResultURI) {
     14  return SpecialPowers.spawn(
     15    aBrowser,
     16    [{ aTestName, aResultURI }],
     17    async function ({ aTestName, aResultURI }) {
     18      let channel = content.docShell.currentDocumentChannel;
     19      is(channel.URI.asciiSpec, aResultURI, "testing CSP for " + aTestName);
     20    }
     21  );
     22 }
     23 
     24 add_task(async function test_csp_inheritance_regular_click() {
     25  await BrowserTestUtils.withNewTab(TEST_URI, async function (browser) {
     26    let loadPromise = BrowserTestUtils.browserLoaded(
     27      browser,
     28      false,
     29      RESULT_URI
     30    );
     31    // set the data href + simulate click
     32    BrowserTestUtils.synthesizeMouseAtCenter(
     33      "#testlink",
     34      {},
     35      gBrowser.selectedBrowser
     36    );
     37    await loadPromise;
     38    await verifyCSP("click()", gBrowser.selectedBrowser, RESULT_URI);
     39  });
     40 });
     41 
     42 add_task(async function test_csp_inheritance_ctrl_click() {
     43  await BrowserTestUtils.withNewTab(TEST_URI, async function () {
     44    let loadPromise = BrowserTestUtils.waitForNewTab(
     45      gBrowser,
     46      RESULT_URI,
     47      true
     48    );
     49    // set the data href + simulate ctrl+click
     50    BrowserTestUtils.synthesizeMouseAtCenter(
     51      "#testlink",
     52      { ctrlKey: true, metaKey: true },
     53      gBrowser.selectedBrowser
     54    );
     55    let tab = await loadPromise;
     56    gBrowser.selectTabAtIndex(2);
     57    await verifyCSP("ctrl-click()", gBrowser.selectedBrowser, RESULT_URI);
     58    await BrowserTestUtils.removeTab(tab);
     59  });
     60 });
     61 
     62 add_task(
     63  async function test_csp_inheritance_right_click_open_link_in_new_tab() {
     64    await BrowserTestUtils.withNewTab(TEST_URI, async function () {
     65      let loadPromise = BrowserTestUtils.waitForNewTab(gBrowser, RESULT_URI);
     66      // set the data href + simulate right-click open link in tab
     67      BrowserTestUtils.waitForEvent(document, "popupshown", false, event => {
     68        // These are operations that must be executed synchronously with the event.
     69        document.getElementById("context-openlinkintab").doCommand();
     70        event.target.hidePopup();
     71        return true;
     72      });
     73      BrowserTestUtils.synthesizeMouseAtCenter(
     74        "#testlink",
     75        { type: "contextmenu", button: 2 },
     76        gBrowser.selectedBrowser
     77      );
     78 
     79      let tab = await loadPromise;
     80      gBrowser.selectTabAtIndex(2);
     81      await verifyCSP(
     82        "right-click-open-in-new-tab()",
     83        gBrowser.selectedBrowser,
     84        RESULT_URI
     85      );
     86      await BrowserTestUtils.removeTab(tab);
     87    });
     88  }
     89 );