tor-browser

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

test_strict_dynamic_default_src.html (4732B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Bug 1299483 - CSP: Implement 'strict-dynamic'</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 scripts and images with a CSP of 'strict-dynamic' making sure
     18 * allowlists get ignored for scripts but not for images when strict-dynamic
     19 * appears in default-src.
     20 *
     21 * Please note that we do not support strict-dynamic within default-src yet,
     22 * see Bug 1313937. When updating this test please do not change the
     23 * csp policies, but only replace todo_is() with is().
     24 */
     25 
     26 var tests = [
     27  {
     28    script_desc: "(test1) script should be allowed because of valid nonce",
     29    img_desc: "(test1) img should be allowed because of 'self'",
     30    script_result: "allowed",
     31    img_result: "allowed",
     32    policy: "default-src 'strict-dynamic' 'self'; script-src 'nonce-foo'"
     33  },
     34  {
     35    script_desc: "(test 2) script should be blocked because of invalid nonce",
     36    img_desc: "(test 2) img should be allowed because of valid scheme-src",
     37    script_result: "blocked",
     38    img_result: "allowed",
     39    policy: "default-src 'strict-dynamic' http:; script-src 'nonce-bar' http:"
     40  },
     41  {
     42    script_desc: "(test 3) script should be blocked because of invalid nonce",
     43    img_desc: "(test 3) img should be allowed because of valid host-src",
     44    script_result: "blocked",
     45    script_enforced: "",
     46    img_result: "allowed",
     47    policy: "default-src 'strict-dynamic' mochi.test; script-src 'nonce-bar' http:"
     48  },
     49  {
     50    script_desc: "(test 4) script should be allowed because of valid nonce",
     51    img_desc: "(test 4) img should be blocked because of default-src 'strict-dynamic'",
     52    script_result: "allowed",
     53    img_result: "blocked",
     54    policy: "default-src 'strict-dynamic'; script-src 'nonce-foo'"
     55  },
     56  // some reverse order tests (have script-src appear before default-src)
     57  {
     58    script_desc: "(test 5) script should be allowed because of valid nonce",
     59    img_desc: "(test 5) img should be blocked because of default-src 'strict-dynamic'",
     60    script_result: "allowed",
     61    img_result: "blocked",
     62    policy: "script-src 'nonce-foo'; default-src 'strict-dynamic';"
     63  },
     64  {
     65    script_desc: "(test 6) script should be allowed because of valid nonce",
     66    img_desc: "(test 6) img should be blocked because of default-src http:",
     67    script_result: "blocked",
     68    img_result: "blocked",
     69    policy: "script-src 'nonce-bar' http:; default-src 'strict-dynamic' http:;"
     70  },
     71  {
     72    script_desc: "(test 7) script should be allowed because of invalid nonce",
     73    img_desc: "(test 7) img should be blocked because of image-src http:",
     74    script_result: "blocked",
     75    img_result: "blocked",
     76    policy: "script-src 'nonce-bar' http:; default-src 'strict-dynamic' http:; img-src http:"
     77  },
     78 ];
     79 
     80 var counter = 0;
     81 var curTest;
     82 
     83 function loadNextTest() {
     84  if (counter == tests.length) {
     85    SimpleTest.finish();
     86    return;
     87  }
     88 
     89  curTest = tests[counter++];
     90  var src = "file_testserver.sjs?file=";
     91  // append the file that should be served
     92  src += escape("tests/dom/security/test/csp/file_strict_dynamic_default_src.html");
     93  // append the CSP that should be used to serve the file
     94  src += "&csp=" + escape(curTest.policy);
     95 
     96  document.getElementById("testframe").addEventListener("load", checkResults);
     97  document.getElementById("testframe").src = src;
     98 }
     99 
    100 function checkResults() {
    101  try {
    102    var testframe = document.getElementById("testframe");
    103    testframe.removeEventListener('load', checkResults);
    104 
    105    // check if script loaded
    106    var divcontent = testframe.contentWindow.document.getElementById('testdiv').innerHTML;
    107    var imgcontent = testframe.contentWindow.document.getElementById('testimage').dataset.result;
    108    if (curTest.script_result === "blocked") {
    109      todo_is(divcontent, curTest.script_result, curTest.script_desc);
    110    }
    111    else {
    112      is(divcontent, curTest.script_result, curTest.script_desc);
    113    }
    114 
    115    // check if image loaded
    116    var testimg = testframe.contentWindow.document.getElementById("testimage");
    117    if (curTest.img_result === "allowed") {
    118      todo_is(imgcontent, curTest.img_result, curTest.img_desc);
    119    }
    120    else {
    121      is(imgcontent, curTest.img_result, curTest.img_desc);
    122    }
    123  }
    124  catch (e) {
    125    ok(false, "ERROR: could not access content for test: '" + curTest.script_desc + "'");
    126  }
    127 
    128  loadNextTest();
    129 }
    130 
    131 // start running the tests
    132 loadNextTest();
    133 
    134 </script>
    135 </body>
    136 </html>