tor-browser

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

sandbox-top-navigation-helper.sub.js (2873B)


      1 // To use this file, use the following imports:
      2 // // META: script=/common/dispatcher/dispatcher.js
      3 // // META: script=/common/get-host-info.sub.js
      4 // // META: script=/common/utils.js
      5 // // META: script=/resources/testdriver.js
      6 // // META: script=/resources/testdriver-vendor.js
      7 // // META: script=/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js
      8 // // META: script=./resources/sandbox-top-navigation-helper.sub.js
      9 
     10 // Helper file that provides various functions to test top-level navigation
     11 // with various frame and sandbox flag configurations.
     12 
     13 async function createNestedIframe(parent, origin, frame_sandbox, header_sandbox)
     14 {
     15  let headers = [];
     16  if (header_sandbox) {
     17    headers.push([
     18      "Content-Security-Policy",
     19      "sandbox allow-scripts " + header_sandbox
     20    ]);
     21  }
     22  let iframe_attributes = {};
     23  if (frame_sandbox) {
     24    iframe_attributes.sandbox = "allow-scripts " + frame_sandbox;
     25  }
     26  return parent.addIframe({
     27    origin: origin,
     28    scripts: [
     29      '/resources/testdriver.js',
     30      '/resources/testdriver-vendor.js',
     31    ],
     32    headers: headers,
     33  }, iframe_attributes);
     34 }
     35 
     36 async function navigateFrameTo(frame, origin) {
     37  return frame.navigateToNew({
     38    origin: origin,
     39    scripts: [
     40      '/resources/testdriver.js',
     41      '/resources/testdriver-vendor.js',
     42    ],
     43  });
     44 }
     45 
     46 async function attemptTopNavigation(iframe, should_succeed) {
     47  let did_succeed;
     48  try {
     49    await iframe.executeScript(() => {
     50      window.top.location.href =
     51          'http://{{hosts[alt][www2]}}:{{ports[http][0]}}';
     52    });
     53    did_succeed = true;
     54  } catch (e) {
     55    did_succeed = false;
     56  }
     57 
     58  assert_equals(did_succeed, should_succeed,
     59      should_succeed ?
     60          "The navigation should succeed." :
     61          "The navigation should fail.");
     62 }
     63 
     64 async function setupTest() {
     65  const rcHelper = new RemoteContextHelper();
     66  return rcHelper.addWindow(/*config=*/ null, /*options=*/ {});
     67 }
     68 
     69 async function activate(iframe) {
     70  return iframe.executeScript(async () => {
     71    let b = document.createElement("button");
     72    document.body.appendChild(b);
     73 
     74    // Since test_driver.bless() does not play nicely with the remote context
     75    // helper, this is a workaround to trigger user activation in the iframe.
     76    // This adds a button to the iframe and then simulates hitting the 'tab' key
     77    // twice. Once to focus on the button, and once to trigger user activation
     78    // in the iframe (user activation is given to the frame that has focus when
     79    // the tab key is pressed, not the frame that ends up getting focus). Note
     80    // that this will result in both the parent and this frame getting user
     81    // activation. Note that this currently only works for iframes nested 1
     82    // level deep.
     83    test_driver.set_test_context(window.top);
     84    return test_driver.send_keys(document.body, "\uE004\uE004");
     85  });
     86 }