tor-browser

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

notify-event-iframe.https.html (4007B)


      1 <!DOCTYPE html>
      2 <meta name="timeout" content="long">
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="/resources/testdriver.js"></script>
      6 <script src="/resources/testdriver-actions.js"></script>
      7 <script src="/resources/testdriver-vendor.js"></script>
      8 <script src="/common/utils.js"></script>
      9 <script src="/common/dispatcher/dispatcher.js"></script>
     10 <script src="/common/get-host-info.sub.js"></script>
     11 <script src="resources/utils.js"></script>
     12 <title>Test fenced frame notifyEvent() functionality with iframes</title>
     13 
     14 <body>
     15  <script>
     16    async function runNestedIFrameTest(frame_type) {
     17      // Create a fenced frame that will respond to window.fence.notifyEvent().
     18      const fencedframe = await attachFencedFrameContext(
     19                  {generator_api: 'fledge'});
     20      let notified = false;
     21      fencedframe.element.addEventListener('fencedtreeclick', () => notified = true);
     22 
     23      await fencedframe.execute(async (frame_type) => {
     24        window.click_error = new Promise((resolve, reject) => {
     25          window.addEventListener('message', (event) => {
     26            resolve(event.data);
     27          });
     28        });
     29 
     30        let iframe = null;
     31        if (frame_type === 'same-origin') {
     32          iframe = await attachIFrameContext({
     33            origin: get_host_info().HTTPS_ORIGIN
     34          });
     35        } else if (frame_type === 'cross-origin') {
     36          iframe = await attachIFrameContext({
     37            origin: get_host_info().HTTPS_REMOTE_ORIGIN
     38          });
     39        }
     40 
     41        // Calling notifyEvent() on click in the iframe should fail, but we need
     42        // to move the exception out of the click handler to assert on it.
     43        await iframe.execute(() => {
     44          document.addEventListener('click', (e) => {
     45            try {
     46              window.fence.notifyEvent(e);
     47            } catch (err) {
     48              window.parent.postMessage(err, '*');
     49              return;
     50            }
     51            window.parent.postMessage(new TypeError('No exception'), '*');
     52          });
     53        });
     54      }, [frame_type]);
     55 
     56      await multiClick(10, 10, fencedframe.element);
     57 
     58      // Ensure the correct exception was thrown.
     59      await fencedframe.execute(async () => {
     60        let err = await window.click_error;
     61        assert_equals(err.name, 'SecurityError');
     62        assert_equals(err.message,
     63          "Failed to execute 'notifyEvent' on 'Fence': notifyEvent is only available in fenced frame roots.");
     64      });
     65 
     66      // Because the notifyEvent() call failed, no event was sent to the
     67      // top-level fenced frame.
     68      assert_false(notified);
     69    }
     70 
     71    promise_test(async (t) => {
     72      return runNestedIFrameTest('same-origin');
     73    }, "Test that fenced frame notifyEvent() fails in a nested same-origin iframe.");
     74 
     75    promise_test(async (t) => {
     76      return runNestedIFrameTest('cross-origin');
     77    }, "Test that fenced frame notifyEvent() fails in a nested cross-origin iframe.");
     78 
     79    promise_test(async (t) => {
     80      window.click_error = new Promise((resolve, reject) => {
     81        window.addEventListener('message', (event) => {
     82           resolve(event.data);
     83        });
     84      });
     85 
     86      const urn_iframe = await attachIFrameContext(
     87                  {generator_api: 'fledge'});
     88 
     89      await urn_iframe.execute(() => {
     90        document.addEventListener('click', (e) => {
     91          try {
     92            window.fence.notifyEvent(e);
     93          } catch (err) {
     94            window.parent.postMessage(err, '*');
     95            return;
     96          }
     97          window.parent.postMessage(new TypeError('No exception'), '*');
     98        });
     99      });
    100 
    101      await multiClick(10, 10, urn_iframe.element);
    102 
    103      let err = await window.click_error
    104      assert_equals(err.name, 'SecurityError');
    105      assert_equals(err.message,
    106        "Failed to execute 'notifyEvent' on 'Fence': notifyEvent is only available in fenced frame roots.");
    107    }, "Test that notifyEvent() fails in a URN iframe.");
    108  </script>
    109 </body>