tor-browser

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

new-window.https.window.js (3123B)


      1 // META: script=/common/utils.js
      2 // META: script=/common/get-host-info.sub.js
      3 // META: script=/fetch/fetch-later/resources/fetch-later-helper.js
      4 
      5 'use strict';
      6 
      7 const {
      8  HTTPS_ORIGIN,
      9  HTTPS_NOTSAMESITE_ORIGIN,
     10 } = get_host_info();
     11 
     12 function fetchLaterPopupUrl(host, targetUrl) {
     13  return `${host}/fetch/fetch-later/resources/fetch-later.html?url=${
     14      encodeURIComponent(targetUrl)}&activateAfter=0`;
     15 }
     16 
     17 async function receiveMessageFromPopup(url) {
     18  const expect =
     19      new FetchLaterIframeExpectation(FetchLaterExpectationType.DONE);
     20  const messageType = await new Promise((resolve, reject) => {
     21    window.addEventListener('message', function handler(e) {
     22      try {
     23        if (expect.run(e, url)) {
     24          window.removeEventListener('message', handler);
     25          resolve(e.data.type);
     26        }
     27      } catch (err) {
     28        reject(err);
     29      }
     30    });
     31  });
     32 
     33  assert_equals(messageType, FetchLaterIframeMessageType.DONE);
     34 }
     35 
     36 for (const target of ['', '_blank']) {
     37  // NOTE: noopener popup window cannot communicate back. It will be too
     38  // unreliable to only use `expectBeacon()` to test such window.
     39  for (const features of ['', 'popup']) {
     40    parallelPromiseTest(
     41        async t => {
     42          const uuid = token();
     43          const url =
     44              generateSetBeaconURL(uuid, {host: HTTPS_NOTSAMESITE_ORIGIN});
     45 
     46          // Opens a blank popup window that fires a fetchLater request.
     47          const w = window.open(
     48              `javascript: fetchLater("${url}", {activateAfter: 0})`, target,
     49              features);
     50          await new Promise(resolve => w.addEventListener('load', resolve));
     51 
     52          // The popup should have sent the request.
     53          await expectBeacon(uuid, {count: 1});
     54          w.close();
     55        },
     56        `A blank window[target='${target}'][features='${
     57            features}'] can trigger fetchLater.`);
     58 
     59    parallelPromiseTest(
     60        async t => {
     61          const uuid = token();
     62          const popupUrl =
     63              fetchLaterPopupUrl(HTTPS_ORIGIN, generateSetBeaconURL(uuid));
     64 
     65          // Opens a same-origin popup that fires a fetchLater request.
     66          const w = window.open(popupUrl, target, features);
     67          await receiveMessageFromPopup(popupUrl);
     68 
     69          // The popup should have sent the request.
     70          await expectBeacon(uuid, {count: 1});
     71          w.close();
     72        },
     73        `A same-origin window[target='${target}'][features='${
     74            features}'] can trigger fetchLater.`);
     75 
     76    parallelPromiseTest(
     77        async t => {
     78          const uuid = token();
     79          const popupUrl = fetchLaterPopupUrl(
     80              HTTPS_NOTSAMESITE_ORIGIN, generateSetBeaconURL(uuid));
     81 
     82          // Opens a cross-origin popup that fires a fetchLater request.
     83          const w = window.open(popupUrl, target, features);
     84          await receiveMessageFromPopup(popupUrl);
     85 
     86          // The popup should have sent the request.
     87          await expectBeacon(uuid, {count: 1});
     88          w.close();
     89        },
     90        `A cross-origin window[target='${target}'][features='${
     91            features}'] can trigger fetchLater.`);
     92  }
     93 }