tor-browser

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

fetch-frame-resource.https.html (8200B)


      1 <!DOCTYPE html>
      2 <title>Service Worker: Fetch for the frame loading.</title>
      3 <meta name=timeout content=long>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="/common/get-host-info.sub.js"></script>
      7 <script src="resources/test-helpers.sub.js"></script>
      8 <body>
      9 <script>
     10 var worker = 'resources/fetch-rewrite-worker.js';
     11 var path = base_path() + 'resources/fetch-access-control.py';
     12 var host_info = get_host_info();
     13 
     14 function getLoadedObject(win, contentFunc, closeFunc) {
     15  return new Promise(function(resolve) {
     16      function done(contentString) {
     17        var result = null;
     18        // fetch-access-control.py returns a string like "report( <json> )".
     19        // Eval the returned string with a report functionto get the json
     20        // object.
     21        try {
     22          function report(obj) { result = obj };
     23          eval(contentString);
     24        } catch(e) {
     25          // just resolve null if we get unexpected page content
     26        }
     27        closeFunc(win);
     28        resolve(result);
     29      }
     30 
     31      // We can't catch the network error on window. So we use the timer.
     32      var timeout = setTimeout(function() {
     33          // Failure pages are considered cross-origin in some browsers.  This
     34          // means you cannot even .resolve() the window because the check for
     35          // the .then property will throw.  Instead, treat cross-origin
     36          // failure pages as the empty string which will fail to parse as the
     37          // expected json result.
     38          var content = '';
     39          try {
     40            content = contentFunc(win);
     41          } catch(e) {
     42            // use default empty string for cross-domain window
     43          }
     44          done(content);
     45        }, 10000);
     46 
     47      win.onload = function() {
     48          clearTimeout(timeout);
     49          let content = '';
     50          try {
     51            content = contentFunc(win);
     52          } catch(e) {
     53            // use default empty string for cross-domain window (see above)
     54          }
     55          done(content);
     56        };
     57    });
     58 }
     59 
     60 function getLoadedFrameAsObject(frame) {
     61  return getLoadedObject(frame, function(f) {
     62      return f.contentDocument.body.textContent;
     63    }, function(f) {
     64      f.parentNode.removeChild(f);
     65    });
     66 }
     67 
     68 function getLoadedWindowAsObject(win) {
     69  return getLoadedObject(win, function(w) {
     70      return w.document.body.textContent
     71    }, function(w) {
     72      w.close();
     73    });
     74 }
     75 
     76 promise_test(function(t) {
     77    var scope = 'resources/fetch-frame-resource/frame-basic';
     78    var frame;
     79    return service_worker_unregister_and_register(t, worker, scope)
     80      .then(function(reg) {
     81          t.add_cleanup(function() {
     82              return service_worker_unregister(t, scope);
     83            });
     84 
     85          return wait_for_state(t, reg.installing, 'activated');
     86        })
     87      .then(function() {
     88          frame = document.createElement('iframe');
     89          frame.src =
     90            scope + '?url=' +
     91            encodeURIComponent(host_info['HTTPS_ORIGIN'] + path);
     92          document.body.appendChild(frame);
     93          return getLoadedFrameAsObject(frame);
     94        })
     95      .then(function(result) {
     96          assert_equals(
     97            result.jsonpResult,
     98            'success',
     99            'Basic type response could be loaded in the iframe.');
    100          frame.remove();
    101        });
    102  }, 'Basic type response could be loaded in the iframe.');
    103 
    104 promise_test(function(t) {
    105    var scope = 'resources/fetch-frame-resource/frame-cors';
    106    var frame;
    107    return service_worker_unregister_and_register(t, worker, scope)
    108      .then(function(reg) {
    109          t.add_cleanup(function() {
    110              return service_worker_unregister(t, scope);
    111            });
    112 
    113          return wait_for_state(t, reg.installing, 'activated');
    114        })
    115      .then(function() {
    116          frame = document.createElement('iframe');
    117          frame.src =
    118            scope + '?mode=cors&url=' +
    119            encodeURIComponent(host_info['HTTPS_REMOTE_ORIGIN'] + path +
    120                               '?ACAOrigin=' + host_info['HTTPS_ORIGIN'] +
    121                               '&ACACredentials=true');
    122          document.body.appendChild(frame);
    123          return getLoadedFrameAsObject(frame);
    124        })
    125      .then(function(result) {
    126          assert_equals(
    127            result.jsonpResult,
    128            'success',
    129            'CORS type response could be loaded in the iframe.');
    130          frame.remove();
    131        });
    132  }, 'CORS type response could be loaded in the iframe.');
    133 
    134 promise_test(function(t) {
    135    var scope = 'resources/fetch-frame-resource/frame-opaque';
    136    var frame;
    137    return service_worker_unregister_and_register(t, worker, scope)
    138      .then(function(reg) {
    139          t.add_cleanup(function() {
    140              return service_worker_unregister(t, scope);
    141            });
    142 
    143          return wait_for_state(t, reg.installing, 'activated');
    144        })
    145      .then(function() {
    146          frame = document.createElement('iframe');
    147          frame.src =
    148            scope + '?mode=no-cors&url=' +
    149            encodeURIComponent(host_info['HTTPS_REMOTE_ORIGIN'] + path);
    150          document.body.appendChild(frame);
    151          return getLoadedFrameAsObject(frame);
    152        })
    153      .then(function(result) {
    154          assert_equals(
    155            result,
    156            null,
    157            'Opaque type response could not be loaded in the iframe.');
    158          frame.remove();
    159        });
    160  }, 'Opaque type response could not be loaded in the iframe.');
    161 
    162 promise_test(function(t) {
    163    var scope = 'resources/fetch-frame-resource/window-basic';
    164    return service_worker_unregister_and_register(t, worker, scope)
    165      .then(function(reg) {
    166          t.add_cleanup(function() {
    167              return service_worker_unregister(t, scope);
    168            });
    169 
    170          return wait_for_state(t, reg.installing, 'activated');
    171        })
    172      .then(function() {
    173          var win = window.open(
    174            scope + '?url=' +
    175            encodeURIComponent(host_info['HTTPS_ORIGIN'] + path));
    176          return getLoadedWindowAsObject(win);
    177        })
    178      .then(function(result) {
    179          assert_equals(
    180            result.jsonpResult,
    181            'success',
    182            'Basic type response could be loaded in the new window.');
    183        });
    184  }, 'Basic type response could be loaded in the new window.');
    185 
    186 promise_test(function(t) {
    187    var scope = 'resources/fetch-frame-resource/window-cors';
    188    return service_worker_unregister_and_register(t, worker, scope)
    189      .then(function(reg) {
    190          t.add_cleanup(function() {
    191              return service_worker_unregister(t, scope);
    192            });
    193 
    194          return wait_for_state(t, reg.installing, 'activated');
    195        })
    196      .then(function() {
    197          var win = window.open(
    198            scope + '?mode=cors&url=' +
    199            encodeURIComponent(host_info['HTTPS_REMOTE_ORIGIN'] + path +
    200                               '?ACAOrigin=' + host_info['HTTPS_ORIGIN'] +
    201                               '&ACACredentials=true'));
    202          return getLoadedWindowAsObject(win);
    203        })
    204      .then(function(result) {
    205          assert_equals(
    206            result.jsonpResult,
    207            'success',
    208            'CORS type response could be loaded in the new window.');
    209        });
    210  }, 'CORS type response could be loaded in the new window.');
    211 
    212 promise_test(function(t) {
    213    var scope = 'resources/fetch-frame-resource/window-opaque';
    214    return service_worker_unregister_and_register(t, worker, scope)
    215      .then(function(reg) {
    216          t.add_cleanup(function() {
    217              return service_worker_unregister(t, scope);
    218            });
    219 
    220          return wait_for_state(t, reg.installing, 'activated');
    221        })
    222      .then(function() {
    223          var win = window.open(
    224            scope + '?mode=no-cors&url=' +
    225            encodeURIComponent(host_info['HTTPS_REMOTE_ORIGIN'] + path));
    226          return getLoadedWindowAsObject(win);
    227        })
    228      .then(function(result) {
    229          assert_equals(
    230            result,
    231            null,
    232            'Opaque type response could not be loaded in the new window.');
    233        });
    234  }, 'Opaque type response could not be loaded in the new window.');
    235 </script>
    236 </body>