tor-browser

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

test_meta_csp_self.html (2230B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Bug 1387871 - CSP: Test 'self' within meta csp in  data: URI iframe</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 </head>
      8 <body>
      9 <iframe style="width:100%;" id="testframe"></iframe>
     10 
     11 <script class="testbody" type="text/javascript">
     12 
     13 SimpleTest.waitForExplicitFinish();
     14 
     15 /* Description of the test:
     16 * We load a data: URI into an iframe which provides a meta-csp
     17 * including the keyword 'self'. We make sure 'self' does not
     18 * allow a data: image to load.
     19 */
     20 
     21 window.addEventListener("message", receiveMessage);
     22 function receiveMessage(event) {
     23  window.removeEventListener("message", receiveMessage);
     24  is(event.data.result, "dataFrameReady", "sanity: received msg from loaded frame");
     25 
     26  var frame = document.getElementById("testframe");
     27 
     28  // make sure the img was blocked
     29  var img = SpecialPowers.wrap(frame).contentDocument.getElementById("testimg");
     30  is(img.naturalWidth, 0, "img should be blocked -  width should be 0");
     31  is(img.naturalHeight, 0, "img should be blocked - height should be 0");
     32 
     33  // sanity check, make sure 'self' translates into data
     34  var contentDoc = SpecialPowers.wrap(frame).contentDocument;
     35  // parse the cspJSON in a csp-object
     36  var cspOBJ = JSON.parse(contentDoc.cspJSON);
     37  ok(cspOBJ, "sanity: was able to parse the CSP JSON");
     38 
     39  // make sure we only got one policy
     40  var policies = cspOBJ["csp-policies"];
     41  is(policies.length, 1, "sanity: received one CSP policy");
     42 
     43  var policy = policies[0];
     44  var val = policy['img-src'];
     45  is(val.toString(), "'self'", "'self' should translate into data");
     46  SimpleTest.finish();
     47 }
     48 
     49 let DATA_URI = `data:text/html,
     50  <html>
     51  <head>
     52    <meta http-equiv="Content-Security-Policy" content="img-src 'self'">
     53  </head>
     54  <body onload="parent.postMessage({result:'dataFrameReady'},'*');">
     55    data: URI frame with meta-csp including 'self'<br/>
     56    <img id="testimg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" />
     57  </body>
     58  </html>`;
     59 document.getElementById("testframe").src = DATA_URI;
     60 
     61 </script>
     62 </body>
     63 </html>