tor-browser

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

referrer_worker.html (4973B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 </head>
      5 <body onload="tests.next();">
      6 <script type="text/javascript">
      7 const SJS = "referrer_test_server.sjs?";
      8 const BASE_URL = "https://example.com/tests/dom/workers/test/" + SJS;
      9 const GET_RESULT = BASE_URL + 'ACTION=get-test-results';
     10 const RESET_STATE = BASE_URL + 'ACTION=resetState';
     11 
     12 function ok(val, message) {
     13  val = val ? "true" : "false";
     14  window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message + "');", "*");
     15 }
     16 
     17 function info(val) {
     18  window.parent.postMessage("SimpleTest.info(" + val + ");", "*");
     19 }
     20 
     21 function is(a, b, message) {
     22  ok(a == b, message);
     23 }
     24 
     25 function finish() {
     26  // Let window.onerror have a chance to fire
     27  setTimeout(function() {
     28    setTimeout(function() {
     29      tests.return();
     30      window.parent.postMessage("SimpleTest.finish();", "*");
     31    }, 0);
     32  }, 0);
     33 }
     34 
     35 var testCases = {
     36  'same-origin':  { 'Referrer-Policy' : { 'default' : 'full',
     37                                          'origin' : 'origin',
     38                                          'origin-when-cross-origin' : 'full',
     39                                          'unsafe-url' : 'full',
     40                                          'same-origin' : 'full',
     41                                          'strict-origin' : 'origin',
     42                                          'strict-origin-when-cross-origin' : 'full',
     43                                          'no-referrer' : 'none',
     44                                          'unsafe-url, no-referrer' : 'none',
     45                                          'invalid' : 'full' }},
     46 
     47  'cross-origin':  { 'Referrer-Policy' : { 'default' : 'origin',
     48                                           'origin' : 'origin',
     49                                           'origin-when-cross-origin' : 'origin',
     50                                           'unsafe-url' : 'full',
     51                                           'same-origin' : 'none',
     52                                           'strict-origin' : 'origin',
     53                                           'strict-origin-when-cross-origin' : 'origin',
     54                                           'no-referrer' : 'none',
     55                                           'unsafe-url, no-referrer' : 'none',
     56                                           'invalid' : 'origin' }},
     57 
     58  // Downgrading in worker is blocked entirely without unblock option
     59  // https://bugzilla.mozilla.org/show_bug.cgi?id=1198078#c17
     60  // Skip the downgrading test
     61  /* 'downgrade':  { 'Referrer-Policy' : { 'default' : 'full',
     62                                          'origin'  : 'full',
     63                                          'origin-when-cross-origin"'  : 'full',
     64                                          'unsafe-url'  : 'full',
     65                                          'same-origin' : 'none',
     66                                          'strict-origin' : 'none',
     67                                          'strict-origin-when-cross-origin' : 'none',
     68                                          'no-referrer'  : 'full',
     69                                          'unsafe-url, no-referrer' : 'none',
     70                                          'invalid'  : 'full' }}, */
     71 
     72 
     73 };
     74 
     75 var advance = function() { tests.next(); };
     76 
     77 /**
     78 * helper to perform an XHR
     79 * to do checkIndividualResults and resetState
     80 */
     81 function doXHR(aUrl, onSuccess, onFail) {
     82  var xhr = new XMLHttpRequest({mozSystem: true});
     83  xhr.responseType = "json";
     84  xhr.onload = function () {
     85    onSuccess(xhr);
     86  };
     87  xhr.onerror = function () {
     88    onFail(xhr);
     89  };
     90  xhr.open('GET', aUrl, true);
     91  xhr.send(null);
     92 }
     93 
     94 
     95 function resetState() {
     96  doXHR(RESET_STATE,
     97    advance,
     98    function(xhr) {
     99      ok(false, "error in reset state");
    100      finish();
    101    });
    102 }
    103 
    104 function checkIndividualResults(aType, aPolicy, aExpected) {
    105  var onload = xhr => {
    106    var results = xhr.response;
    107    dump(JSON.stringify(xhr.response));
    108    // test id equals type + "-" + policy
    109    // Ex: same-origin-default
    110    var id = aType + "-" + aPolicy;
    111    ok(id in results, id + " tests have to be performed.");
    112    is(results[id].policy, aExpected, id + ' --- ' + results[id].policy + ' (' + results[id].referrer + ')');
    113    advance();
    114  };
    115  var onerror = xhr => {
    116    ok(false, "Can't get results from the counter server.");
    117    finish();
    118  };
    119  doXHR(GET_RESULT, onload, onerror);
    120 }
    121 
    122 var tests = (function*() {
    123 
    124  for (var type in testCases) {
    125    for (var policy in testCases[type]['Referrer-Policy']) {
    126      yield resetState();
    127      var searchParams = new URLSearchParams();
    128      searchParams.append("TYPE", type);
    129      searchParams.append("ACTION", "test");
    130      searchParams.append("Referrer-Policy", policy);
    131      var worker = new Worker(BASE_URL + searchParams.toString());
    132      worker.onmessage = function () {
    133        advance();
    134      };
    135      yield worker.postMessage(42);
    136      yield checkIndividualResults(type, policy, escape(testCases[type]['Referrer-Policy'][policy]));
    137    }
    138  }
    139 
    140  finish();
    141 })();
    142 </script>
    143 </body>
    144 </html>