tor-browser

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

test_bug941404.html (2964B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Bug 941404 - Data documents should not set CSP</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <p id="display"></p>
     11 <div id="content" style="display: none">
     12 
     13 
     14 </div>
     15 
     16 <iframe style="width:200px;height:200px;" id='cspframe'></iframe>
     17 <script class="testbody" type="text/javascript">
     18 
     19 
     20 var path = "/tests/dom/security/test/csp/";
     21 
     22 // These are test results: -1 means it hasn't run,
     23 // true/false is the pass/fail result.
     24 window.tests = {
     25  img_good: -1,
     26  img2_good: -1,
     27 };
     28 
     29 
     30 //csp related
     31 
     32 // This is used to watch the blocked data bounce off CSP and allowed data
     33 // get sent out to the wire.
     34 function examiner() {
     35  SpecialPowers.addObserver(this, "csp-on-violate-policy");
     36  SpecialPowers.addObserver(this, "specialpowers-http-notify-request");
     37 }
     38 
     39 examiner.prototype  = {
     40  observe(subject, topic, data) {
     41    var testpat = new RegExp("testid=([a-z0-9_]+)");
     42 
     43    //_good things better be allowed!
     44    //_bad things better be stopped!
     45 
     46    if (topic === "specialpowers-http-notify-request") {
     47      //these things were allowed by CSP
     48      var uri = data;
     49      if (!testpat.test(uri)) return;
     50      var testid = testpat.exec(uri)[1];
     51 
     52      window.testResult(testid,
     53                        /_good/.test(testid),
     54                        uri + " allowed by csp");
     55    }
     56 
     57    if(topic === "csp-on-violate-policy") {
     58      //these were blocked... record that they were blocked
     59      var asciiSpec = SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(subject, "nsIURI"), "asciiSpec");
     60      if (!testpat.test(asciiSpec)) return;
     61      var testid = testpat.exec(asciiSpec)[1];
     62      window.testResult(testid,
     63                        /_bad/.test(testid),
     64                        asciiSpec + " blocked by \"" + data + "\"");
     65    }
     66  },
     67 
     68  // must eventually call this to remove the listener,
     69  // or mochitests might get borked.
     70  remove() {
     71    SpecialPowers.removeObserver(this, "csp-on-violate-policy");
     72    SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
     73  }
     74 }
     75 
     76 window.examiner = new examiner();
     77 
     78 window.testResult = function(testname, result, msg) {
     79  //test already complete.... forget it... remember the first result.
     80  if (window.tests[testname] != -1)
     81    return;
     82 
     83  window.tests[testname] = result;
     84  is(result, true, testname + ' test: ' + msg);
     85 
     86  // if any test is incomplete, keep waiting
     87  for (var v in window.tests)
     88    if(tests[v] == -1) {
     89      console.log(v + " is not complete");
     90      return;
     91    }
     92 
     93  // ... otherwise, finish
     94  window.examiner.remove();
     95  SimpleTest.finish();
     96 }
     97 
     98 SimpleTest.waitForExplicitFinish();
     99 
    100 // save this for last so that our listeners are registered.
    101 // ... this loads the testbed of good and bad requests.
    102 document.getElementById('cspframe').src = 'file_bug941404.html';
    103 
    104 </script>
    105 </pre>
    106 </body>
    107 </html>