tor-browser

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

cors-preflight.any.js (2002B)


      1 // META: script=/common/utils.js
      2 // META: script=/common/get-host-info.sub.js
      3 
      4 // Because apache decrements the Keep-Alive max value on each request, the
      5 // transferSize will vary slightly between requests for the same resource.
      6 const fuzzFactor = 3;  // bytes
      7 
      8 const {HTTP_REMOTE_ORIGIN} = get_host_info();
      9 const url = new URL('/resource-timing/resources/preflight.py',
     10    HTTP_REMOTE_ORIGIN).href;
     11 
     12 // The header bytes are expected to be > |minHeaderSize| and
     13 // < |maxHeaderSize|. If they are outside this range the test will fail.
     14 const minHeaderSize = 100;
     15 const maxHeaderSize = 1024;
     16 
     17 promise_test(async () => {
     18  const checkCorsAllowed = response => response.arrayBuffer();
     19  const requirePreflight = {headers: {'X-Require-Preflight' : '1'}};
     20  const collectEntries = new Promise(resolve => {
     21    let entriesSeen = [];
     22    new PerformanceObserver(entryList => {
     23      entriesSeen = entriesSeen.concat(entryList.getEntries());
     24      if (entriesSeen.length > 2) {
     25        throw new Error(`Saw too many PerformanceResourceTiming entries ` +
     26            `(${entriesSeen.length})`);
     27      }
     28      if (entriesSeen.length == 2) {
     29        resolve(entriesSeen);
     30      }
     31    }).observe({"type": "resource"});
     32  });
     33 
     34  // Although this fetch doesn't send a pre-flight request, the server response
     35  // will allow cross-origin requests explicitly with the
     36  // Access-Control-Allow-Origin header.
     37  await fetch(url).then(checkCorsAllowed);
     38 
     39  // This fetch will send a pre-flight request to do the CORS handshake
     40  // explicitly.
     41  await fetch(url, requirePreflight).then(checkCorsAllowed);
     42 
     43  const entries = await collectEntries;
     44  assert_greater_than(entries[0].transferSize, 0, 'No-preflight transferSize');
     45  const lowerBound = entries[0].transferSize - fuzzFactor;
     46  const upperBound = entries[0].transferSize + fuzzFactor;
     47  assert_between_exclusive(entries[1].transferSize, lowerBound, upperBound,
     48      'Preflighted transferSize');
     49 }, 'PerformanceResourceTiming sizes fetch with preflight test');