test_CSP.html (4173B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for Content Security Policy Connections</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 <p id="display"></p> 10 <div id="content" style="display: none"> 11 </div> 12 <iframe style="width:200px;height:200px;" id='cspframe'></iframe> 13 <script class="testbody" type="text/javascript"> 14 15 // These are test results: -1 means it hasn't run, 16 // true/false is the pass/fail result. 17 window.tests = { 18 img_good: -1, 19 img_bad: -1, 20 style_good: -1, 21 style_bad: -1, 22 frame_good: -1, 23 frame_bad: -1, 24 script_good: -1, 25 script_bad: -1, 26 xhr_good: -1, 27 xhr_bad: -1, 28 fetch_good: -1, 29 fetch_bad: -1, 30 beacon_good: -1, 31 beacon_bad: -1, 32 media_good: -1, 33 media_bad: -1, 34 font_good: -1, 35 font_bad: -1, 36 object_good: -1, 37 object_bad: -1, 38 }; 39 40 SpecialPowers.registerObservers("csp-on-violate-policy"); 41 42 // This is used to watch the blocked data bounce off CSP and allowed data 43 // get sent out to the wire. 44 function examiner() { 45 SpecialPowers.addObserver(this, "csp-on-violate-policy"); 46 SpecialPowers.addObserver(this, "specialpowers-csp-on-violate-policy"); 47 SpecialPowers.addObserver(this, "specialpowers-http-notify-request"); 48 } 49 examiner.prototype = { 50 observe(subject, topic, data) { 51 var testpat = new RegExp("testid=([a-z0-9_]+)"); 52 53 //_good things better be allowed! 54 //_bad things better be stopped! 55 56 // This is a special observer topic that is proxied from 57 // http-on-modify-request in the parent process to inform us when a URI is 58 // loaded 59 if (topic === "specialpowers-http-notify-request") { 60 var uri = data; 61 if (!testpat.test(uri)) return; 62 var testid = testpat.exec(uri)[1]; 63 64 window.testResult(testid, 65 /_good/.test(testid), 66 uri + " allowed by csp"); 67 } 68 69 if (topic === "csp-on-violate-policy" || 70 topic === "specialpowers-csp-on-violate-policy") { 71 // these were blocked... record that they were blocked 72 var asciiSpec = SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(subject, "nsIURI"), "asciiSpec"); 73 if (!testpat.test(asciiSpec)) return; 74 var testid = testpat.exec(asciiSpec)[1]; 75 window.testResult(testid, 76 /_bad/.test(testid), 77 asciiSpec + " blocked by \"" + data + "\""); 78 } 79 }, 80 81 // must eventually call this to remove the listener, 82 // or mochitests might get borked. 83 remove() { 84 SpecialPowers.removeObserver(this, "csp-on-violate-policy"); 85 SpecialPowers.removeObserver(this, "specialpowers-csp-on-violate-policy"); 86 SpecialPowers.removeObserver(this, "specialpowers-http-notify-request"); 87 } 88 } 89 90 window.examiner = new examiner(); 91 92 window.testResult = function(testname, result, msg) { 93 // test already complete.... forget it... remember the first result. 94 if (window.tests[testname] != -1) 95 return; 96 97 ok(testname in window.tests, "It's a real test"); 98 window.tests[testname] = result; 99 is(result, true, testname + ' test: ' + msg); 100 101 // if any test is incomplete, keep waiting 102 for (var v in window.tests) 103 if(tests[v] == -1) 104 return; 105 106 // ... otherwise, finish 107 window.examiner.remove(); 108 SimpleTest.finish(); 109 } 110 111 SimpleTest.waitForExplicitFinish(); 112 113 SpecialPowers.pushPrefEnv( 114 {'set':[// On a cellular connection the default preload value is 0 ("preload 115 // none"). Our Android emulators emulate a cellular connection, and 116 // so by default preload no media data. This causes the media_* tests 117 // to timeout. We set the default used by cellular connections to the 118 // same as used by non-cellular connections in order to get 119 // consistent behavior across platforms/devices. 120 ["media.preload.default", 2], 121 ["media.preload.default.cellular", 2]]}, 122 function() { 123 // save this for last so that our listeners are registered. 124 // ... this loads the testbed of good and bad requests. 125 document.getElementById('cspframe').src = 'file_main.html'; 126 }); 127 </script> 128 </pre> 129 </body> 130 </html>