tor-browser

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

storage-access-permission.sub.https.window.js (3976B)


      1 // META: script=helpers.js
      2 // META: script=/resources/testdriver.js
      3 // META: script=/resources/testdriver-vendor.js
      4 'use strict';
      5 
      6 (async function() {
      7  // These are cross-domain from the current document.
      8  const wwwAlt = "https://{{hosts[alt][www]}}:{{ports[https][0]}}";
      9  const www1Alt = "https://{{hosts[alt][www1]}}:{{ports[https][0]}}";
     10  const responder_html_load_ack = "/storage-access-api/resources/script-with-cookie-header.py?script=embedded_responder.js&should_ack_load=true";
     11 
     12  function permissionChangeEvent() {
     13    return new Promise(resolve => {
     14      window.onmessage = e => {
     15        if (e.data.tag == 'observed_permission_change') {
     16          resolve(e.data.state);
     17        }
     18      };
     19    });
     20  }
     21 
     22  // Test the interaction between two (same-origin) iframes.
     23  promise_test(async (t) => {
     24    // Note: the web platform doesn't guarantee that each iframe has finished
     25    // loading (and executing its script) by the time the CreateFrame promise
     26    // resolves. Therefore the script will signal the parent when it's loaded
     27    // and safe to proceed. Without this extra synchronization, frames can
     28    // miss messages that are essential to the test, and cause the test to
     29    // timeout.
     30    const frame1_loaded = new Promise(r => {
     31      onmessage = e => r(e.data);
     32    });
     33    const frame1 = await CreateFrame(wwwAlt + responder_html_load_ack);
     34    assert_equals(await frame1_loaded, "loaded");
     35 
     36    const frame2_loaded = new Promise(r => {
     37      onmessage = e => r(e.data);
     38    });
     39    const frame2 = await CreateFrame(www1Alt + responder_html_load_ack);
     40    assert_equals(await frame2_loaded, "loaded");
     41 
     42    t.add_cleanup(async () => {
     43      await SetPermissionInFrame(frame1, [{ name: 'storage-access' }, 'prompt']);
     44    });
     45 
     46    // Install observer on frame, and wait for acknowledgement that it is
     47    // installed.
     48    assert_equals(await ObservePermissionChange(frame2),
     49                  "permission_change_observer_installed");
     50 
     51    const observed_event = permissionChangeEvent();
     52    await SetPermissionInFrame(frame1, [{ name: 'storage-access' }, 'granted']);
     53    const state = await observed_event;
     54    assert_equals(state, "granted");
     55  }, 'Permissions grants are observable across same-origin iframes');
     56 
     57  promise_test(async (t) => {
     58    // Note: the web platform doesn't guarantee that each iframe has finished
     59    // loading (and executing its script) by the time the CreateFrame promise
     60    // resolves. Therefore the script will signal the parent when it's loaded
     61    // and safe to proceed. Without this extra synchronization, frames can
     62    // miss messages that are essential to the test, and cause the test to
     63    // timeout.
     64    const frame1_loaded = new Promise(r => {
     65      onmessage = e => r(e.data);
     66    });
     67    const frame1 = await CreateFrame(wwwAlt + responder_html_load_ack);
     68    assert_equals(await frame1_loaded, "loaded");
     69 
     70    const frame2_loaded = new Promise(r => {
     71      onmessage = e => r(e.data);
     72    });
     73    const frame2 = await CreateFrame(www1Alt + responder_html_load_ack);
     74    assert_equals(await frame2_loaded, "loaded");
     75 
     76    t.add_cleanup(async () => {
     77      await SetPermissionInFrame(frame1, [{ name: 'storage-access' }, 'prompt']);
     78    });
     79 
     80    // Install observer on frame, and wait for acknowledgement that it is
     81    // installed.
     82    assert_equals(await ObservePermissionChange(frame2),
     83                  "permission_change_observer_installed");
     84 
     85    const observed_event = permissionChangeEvent();
     86    await SetPermissionInFrame(frame1, [{ name: 'storage-access' }, 'granted']);
     87    const state = await observed_event;
     88    assert_equals(state, "granted");
     89  }, "Permissions grants are observable across same-site iframes");
     90 
     91  promise_test(async () => {
     92    // Finally run the simple tests below in a separate cross-origin iframe.
     93    await RunTestsInIFrame('https://{{domains[www]}}:{{ports[https][0]}}/storage-access-api/resources/permissions-iframe.https.html');
     94  }, "IFrame tests");
     95 
     96 })();