Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 type="text/javascript" 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">
14 </div>
16 <iframe style="width:200px;height:200px;" id='cspframe'></iframe>
17 <script class="testbody" type="text/javascript">
20 var path = "/tests/content/base/test/csp/";
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 };
30 //csp related
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", false);
36 SpecialPowers.addObserver(this, "specialpowers-http-notify-request", false);
37 }
39 examiner.prototype = {
40 observe: function(subject, topic, data) {
41 var testpat = new RegExp("testid=([a-z0-9_]+)");
43 //_good things better be allowed!
44 //_bad things better be stopped!
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];
52 window.testResult(testid,
53 /_good/.test(testid),
54 uri + " allowed by csp");
55 }
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 },
68 // must eventually call this to remove the listener,
69 // or mochitests might get borked.
70 remove: function() {
71 SpecialPowers.removeObserver(this, "csp-on-violate-policy");
72 SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
73 }
74 }
76 window.examiner = new examiner();
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;
83 window.tests[testname] = result;
84 is(result, true, testname + ' test: ' + msg);
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 }
93 // ... otherwise, finish
94 window.examiner.remove();
95 SimpleTest.finish();
96 }
98 SimpleTest.waitForExplicitFinish();
100 SpecialPowers.pushPrefEnv(
101 {'set':[["security.csp.speccompliant", true]]},
102 function() {
103 // save this for last so that our listeners are registered.
104 // ... this loads the testbed of good and bad requests.
105 document.getElementById('cspframe').src = 'file_CSP_bug941404.html';
106 });
108 </script>
109 </pre>
110 </body>
111 </html>