content/base/test/csp/test_bug886164.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <meta charset="utf-8">
michael@0 5 <title>Bug 886164 - Enforce CSP in sandboxed iframe</title>
michael@0 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body>
michael@0 10 <p id="display"></p>
michael@0 11 <div id="content" style="display: none">
michael@0 12 </div>
michael@0 13 <iframe style="width:200px;height:200px;" id='cspframe' sandbox="allow-same-origin"></iframe>
michael@0 14 <iframe style="width:200px;height:200px;" id='cspframe2' sandbox></iframe>
michael@0 15 <iframe style="width:200px;height:200px;" id='cspframe3' sandbox="allow-same-origin"></iframe>
michael@0 16 <iframe style="width:200px;height:200px;" id='cspframe4' sandbox></iframe>
michael@0 17 <iframe style="width:200px;height:200px;" id='cspframe5' sandbox="allow-scripts"></iframe>
michael@0 18 <iframe style="width:200px;height:200px;" id='cspframe6' sandbox="allow-same-origin allow-scripts"></iframe>
michael@0 19 <script class="testbody" type="text/javascript">
michael@0 20
michael@0 21
michael@0 22 var path = "/tests/content/base/test/csp/";
michael@0 23
michael@0 24 // These are test results: -1 means it hasn't run,
michael@0 25 // true/false is the pass/fail result.
michael@0 26 window.tests = {
michael@0 27 // sandbox allow-same-origin; 'self'
michael@0 28 img_good: -1, // same origin
michael@0 29 img_bad: -1, //example.com
michael@0 30
michael@0 31 // sandbox; 'self'
michael@0 32 img2_bad: -1, //example.com
michael@0 33 img2a_good: -1, // same origin & is image
michael@0 34
michael@0 35 // sandbox allow-same-origin; 'none'
michael@0 36 img3_bad: -1,
michael@0 37 img3a_bad: -1,
michael@0 38
michael@0 39 // sandbox; 'none'
michael@0 40 img4_bad: -1,
michael@0 41 img4a_bad: -1,
michael@0 42
michael@0 43 // sandbox allow-scripts; 'none' 'unsafe-inline'
michael@0 44 img5_bad: -1,
michael@0 45 img5a_bad: -1,
michael@0 46 script5_bad: -1,
michael@0 47 script5a_bad: -1,
michael@0 48
michael@0 49 // sandbox allow-same-origin allow-scripts; 'self' 'unsafe-inline'
michael@0 50 img6_bad: -1,
michael@0 51 script6_bad: -1,
michael@0 52 };
michael@0 53
michael@0 54 // a postMessage handler that is used by sandboxed iframes without
michael@0 55 // 'allow-same-origin' to communicate pass/fail back to this main page.
michael@0 56 // it expects to be called with an object like {ok: true/false, desc:
michael@0 57 // <description of the test> which it then forwards to ok()
michael@0 58 window.addEventListener("message", receiveMessage, false);
michael@0 59
michael@0 60 function receiveMessage(event)
michael@0 61 {
michael@0 62 ok_wrapper(event.data.ok, event.data.desc);
michael@0 63 }
michael@0 64
michael@0 65 var cspTestsDone = false;
michael@0 66 var iframeSandboxTestsDone = false;
michael@0 67
michael@0 68 // iframe related
michael@0 69 var completedTests = 0;
michael@0 70 var passedTests = 0;
michael@0 71
michael@0 72 function ok_wrapper(result, desc) {
michael@0 73 ok(result, desc);
michael@0 74
michael@0 75 completedTests++;
michael@0 76
michael@0 77 if (result) {
michael@0 78 passedTests++;
michael@0 79 }
michael@0 80
michael@0 81 if (completedTests === 5) {
michael@0 82 iframeSandboxTestsDone = true;
michael@0 83 if (cspTestsDone) {
michael@0 84 SimpleTest.finish();
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89
michael@0 90 //csp related
michael@0 91
michael@0 92 // This is used to watch the blocked data bounce off CSP and allowed data
michael@0 93 // get sent out to the wire.
michael@0 94 function examiner() {
michael@0 95 SpecialPowers.addObserver(this, "csp-on-violate-policy", false);
michael@0 96 SpecialPowers.addObserver(this, "specialpowers-http-notify-request", false);
michael@0 97 }
michael@0 98 examiner.prototype = {
michael@0 99 observe: function(subject, topic, data) {
michael@0 100 var testpat = new RegExp("testid=([a-z0-9_]+)");
michael@0 101
michael@0 102 //_good things better be allowed!
michael@0 103 //_bad things better be stopped!
michael@0 104
michael@0 105 if (topic === "specialpowers-http-notify-request") {
michael@0 106 //these things were allowed by CSP
michael@0 107 var uri = data;
michael@0 108 if (!testpat.test(uri)) return;
michael@0 109 var testid = testpat.exec(uri)[1];
michael@0 110
michael@0 111 window.testResult(testid,
michael@0 112 /_good/.test(testid),
michael@0 113 uri + " allowed by csp");
michael@0 114 }
michael@0 115
michael@0 116 if(topic === "csp-on-violate-policy") {
michael@0 117 //these were blocked... record that they were blocked
michael@0 118 var asciiSpec = SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(subject, "nsIURI"), "asciiSpec");
michael@0 119 if (!testpat.test(asciiSpec)) return;
michael@0 120 var testid = testpat.exec(asciiSpec)[1];
michael@0 121 window.testResult(testid,
michael@0 122 /_bad/.test(testid),
michael@0 123 asciiSpec + " blocked by \"" + data + "\"");
michael@0 124 }
michael@0 125 },
michael@0 126
michael@0 127 // must eventually call this to remove the listener,
michael@0 128 // or mochitests might get borked.
michael@0 129 remove: function() {
michael@0 130 SpecialPowers.removeObserver(this, "csp-on-violate-policy");
michael@0 131 SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
michael@0 132 }
michael@0 133 }
michael@0 134
michael@0 135 window.examiner = new examiner();
michael@0 136
michael@0 137 window.testResult = function(testname, result, msg) {
michael@0 138 //test already complete.... forget it... remember the first result.
michael@0 139 if (window.tests[testname] != -1)
michael@0 140 return;
michael@0 141
michael@0 142 window.tests[testname] = result;
michael@0 143 ok(result, testname + ' test: ' + msg);
michael@0 144
michael@0 145 // if any test is incomplete, keep waiting
michael@0 146 for (var v in window.tests)
michael@0 147 if(tests[v] == -1)
michael@0 148 return;
michael@0 149
michael@0 150 // ... otherwise, finish
michael@0 151 window.examiner.remove();
michael@0 152 cspTestsDone = true;
michael@0 153 if (iframeSandboxTestsDone) {
michael@0 154 SimpleTest.finish();
michael@0 155 }
michael@0 156 }
michael@0 157
michael@0 158 SimpleTest.waitForExplicitFinish();
michael@0 159
michael@0 160 SpecialPowers.pushPrefEnv(
michael@0 161 {'set':[["security.csp.speccompliant", true]]},
michael@0 162 function() {
michael@0 163 // save this for last so that our listeners are registered.
michael@0 164 // ... this loads the testbed of good and bad requests.
michael@0 165 document.getElementById('cspframe').src = 'file_bug886164.html';
michael@0 166 document.getElementById('cspframe2').src = 'file_bug886164_2.html';
michael@0 167 document.getElementById('cspframe3').src = 'file_bug886164_3.html';
michael@0 168 document.getElementById('cspframe4').src = 'file_bug886164_4.html';
michael@0 169 document.getElementById('cspframe5').src = 'file_bug886164_5.html';
michael@0 170 document.getElementById('cspframe6').src = 'file_bug886164_6.html';
michael@0 171 });
michael@0 172
michael@0 173 </script>
michael@0 174 </pre>
michael@0 175 </body>
michael@0 176 </html>

mercurial