tor-browser

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

test_ignore_unsafe_inline.html (4339B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Bug 1004703 - ignore 'unsafe-inline' if nonce- or hash-source specified</title>
      5  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
      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 <iframe style="width:100%;" id="testframe"></iframe>
     11 
     12 <script class="testbody" type="text/javascript">
     13 
     14 SimpleTest.waitForExplicitFinish();
     15 
     16 /* Description of the test:
     17 * We load a page that contains three scripts using different policies
     18 * and make sure 'unsafe-inline' is ignored within script-src if hash-source
     19 * or nonce-source is specified.
     20 *
     21 * The expected output of each test is a sequence of chars.
     22 * E.g. the default char we expect is 'a', depending on what inline scripts
     23 * are allowed to run we also expect 'b', 'c', 'd'.
     24 *
     25 * The test also covers the handling of multiple policies where the second
     26 * policy makes use of a directive that should *not* fallback to
     27 * default-src, see Bug 1198422.
     28 */
     29 
     30 const POLICY_PREFIX = "default-src 'none'; script-src ";
     31 
     32 var tests = [
     33  {
     34    policy1: POLICY_PREFIX + "'unsafe-inline'",
     35    policy2: "frame-ancestors 'self'",
     36    description: "'unsafe-inline' allows all scripts to execute",
     37    file: "file_ignore_unsafe_inline.html",
     38    result: "abcd",
     39  },
     40  {
     41    policy1: POLICY_PREFIX + "'unsafe-inline' 'sha256-uJXAPKP5NZxnVMZMUkDofh6a9P3UMRc1CRTevVPS/rI='",
     42    policy2: "base-uri http://mochi.test",
     43    description: "defining a hash should only allow one script to execute",
     44    file: "file_ignore_unsafe_inline.html",
     45    result: "ac",
     46  },
     47  {
     48    policy1: POLICY_PREFIX + "'unsafe-inline' 'nonce-FooNonce'",
     49    policy2: "form-action 'none'",
     50    description: "defining a nonce should only allow one script to execute",
     51    file: "file_ignore_unsafe_inline.html",
     52    result: "ad",
     53  },
     54  {
     55    policy1: POLICY_PREFIX + "'unsafe-inline' 'sha256-uJXAPKP5NZxnVMZMUkDofh6a9P3UMRc1CRTevVPS/rI=' 'nonce-FooNonce'",
     56    policy2: "upgrade-insecure-requests",
     57    description: "defining hash and nonce should allow two scripts to execute",
     58    file: "file_ignore_unsafe_inline.html",
     59    result: "acd",
     60  },
     61  {
     62    policy1: POLICY_PREFIX + "'unsafe-inline' 'sha256-uJXAPKP5NZxnVMZMUkDofh6a9P3UMRc1CRTevVPS/rI=' 'nonce-FooNonce' 'unsafe-inline'",
     63    policy2: "referrer origin",
     64    description: "defining hash, nonce and 'unsafe-inline' twice should still only allow two scripts to execute",
     65    file: "file_ignore_unsafe_inline.html",
     66    result: "acd",
     67  },
     68  {
     69    policy1: "default-src 'unsafe-inline' 'sha256-uJXAPKP5NZxnVMZMUkDofh6a9P3UMRc1CRTevVPS/rI=' 'nonce-FooNonce' ",
     70    policy2: "sandbox allow-scripts allow-same-origin",
     71    description: "unsafe-inline should be ignored within default-src when a hash or nonce is specified",
     72    file: "file_ignore_unsafe_inline.html",
     73    result: "acd",
     74  },
     75 ];
     76 
     77 var counter = 0;
     78 var curTest;
     79 
     80 function loadNextTest() {
     81  if (counter == tests.length) {
     82    document.getElementById("testframe").removeEventListener("load", test);
     83    SimpleTest.finish();
     84    return;
     85  }
     86 
     87  curTest = tests[counter++];
     88  var src = "file_ignore_unsafe_inline_multiple_policies_server.sjs?file=";
     89  // append the file that should be served
     90  src += escape("tests/dom/security/test/csp/" + curTest.file);
     91 
     92  // append the first CSP that should be used to serve the file
     93  src += "&csp1=" + escape(curTest.policy1);
     94  // append the second CSP that should be used to serve the file
     95  src += "&csp2=" + escape(curTest.policy2);
     96 
     97  document.getElementById("testframe").addEventListener("load", test);
     98  document.getElementById("testframe").src = src;
     99 }
    100 
    101 function test() {
    102  try {
    103    document.getElementById("testframe").removeEventListener('load', test);
    104    var testframe = document.getElementById("testframe");
    105    var divcontent = testframe.contentWindow.document.getElementById('testdiv').innerHTML;
    106    // sort the characters to make sure the result is in ascending order
    107    // in case handlers run out of order
    108    divcontent = divcontent.split('').sort().join('');
    109 
    110    is(divcontent, curTest.result, curTest.description);
    111  }
    112  catch (e) {
    113    ok(false, "error: could not access content for test " + curTest.description + "!");
    114  }
    115  loadNextTest();
    116 }
    117 
    118 loadNextTest();
    119 
    120 </script>
    121 </body>
    122 </html>