tor-browser

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

timing-attack.js (1402B)


      1 function run_test(isolated) {
      2  let resolution = 100;
      3  if (isolated) {
      4    resolution = 5;
      5  }
      6  test(function() {
      7    function check_resolutions(times, length) {
      8      const end = length - 2;
      9 
     10      // we compare each value with the following ones
     11      for (let i = 0; i < end; i++) {
     12        const h1 = times[i];
     13        for (let j = i+1; j < end; j++) {
     14          const h2 = times[j];
     15          const diff = h2 - h1;
     16          assert_true((diff === 0) || ((diff * 1000) >= resolution),
     17            "Differences smaller than ' + resolution + ' microseconds: " + diff);
     18        }
     19      }
     20      return true;
     21    }
     22 
     23    const times = new Array(10);
     24    let index = 0;
     25    let hrt1, hrt2, hrt;
     26    assert_equals(self.crossOriginIsolated, isolated, "Document cross-origin isolated value matches");
     27 
     28    // rapid firing of performance.now
     29    hrt1 = performance.now();
     30    hrt2 = performance.now();
     31    times[index++] = hrt1;
     32    times[index++] = hrt2;
     33 
     34    // ensure that we get performance.now() to return a different value
     35    do {
     36      hrt = performance.now();
     37      times[index++] = hrt;
     38    } while ((hrt - hrt1) === 0);
     39 
     40    assert_true(check_resolutions(times, index), 'Difference should be at least ' + resolution + ' microseconds.');
     41  }, 'The recommended minimum resolution of the Performance interface has been set to ' + resolution + ' microseconds for cross-origin isolated contexts.');
     42 }