content/base/test/csp/test_CSP_frameancestors.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test for Content Security Policy Frame Ancestors directive</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 7 </head>
michael@0 8 <body>
michael@0 9 <p id="display"></p>
michael@0 10 <div id="content" style="display: none">
michael@0 11 </div>
michael@0 12 <iframe style="width:100%;height:300px;" id='cspframe'></iframe>
michael@0 13 <iframe style="width:100%;height:300px;" id='cspframe2'></iframe>
michael@0 14 <script class="testbody" type="text/javascript">
michael@0 15
michael@0 16 var path = "/tests/content/base/test/csp/";
michael@0 17
michael@0 18 // These are test results: -1 means it hasn't run,
michael@0 19 // true/false is the pass/fail result.
michael@0 20 var framesThatShouldLoad = {
michael@0 21 aa_allow: -1, /* innermost frame allows a */
michael@0 22 //aa_block: -1, /* innermost frame denies a */
michael@0 23 ab_allow: -1, /* innermost frame allows a */
michael@0 24 //ab_block: -1, /* innermost frame denies a */
michael@0 25 aba_allow: -1, /* innermost frame allows b,a */
michael@0 26 //aba_block: -1, /* innermost frame denies b */
michael@0 27 //aba2_block: -1, /* innermost frame denies a */
michael@0 28 abb_allow: -1, /* innermost frame allows b,a */
michael@0 29 //abb_block: -1, /* innermost frame denies b */
michael@0 30 //abb2_block: -1, /* innermost frame denies a */
michael@0 31 aa_allow_spec_compliant: -1, /* innermost frame allows a *
michael@0 32 //aa_block_spec_compliant: -1, /* innermost frame denies a */
michael@0 33 ab_allow_spec_compliant: -1, /* innermost frame allows a */
michael@0 34 //ab_block_spec_compliant: -1, /* innermost frame denies a */
michael@0 35 aba_allow_spec_compliant: -1, /* innermost frame allows b,a */
michael@0 36 //aba_block_spec_compliant: -1, /* innermost frame denies b */
michael@0 37 //aba2_block_spec_compliant: -1, /* innermost frame denies a */
michael@0 38 abb_allow_spec_compliant: -1, /* innermost frame allows b,a */
michael@0 39 //abb_block_spec_compliant: -1, /* innermost frame denies b */
michael@0 40 //abb2_block_spec_compliant: -1, /* innermost frame denies a */
michael@0 41 };
michael@0 42
michael@0 43 var expectedViolationsLeft = 12;
michael@0 44
michael@0 45 // This is used to watch the blocked data bounce off CSP and allowed data
michael@0 46 // get sent out to the wire.
michael@0 47 function examiner() {
michael@0 48 SpecialPowers.addObserver(this, "csp-on-violate-policy", false);
michael@0 49 }
michael@0 50 examiner.prototype = {
michael@0 51 observe: function(subject, topic, data) {
michael@0 52 // subject should be an nsURI, and should be either allowed or blocked.
michael@0 53 if (!SpecialPowers.can_QI(subject))
michael@0 54 return;
michael@0 55
michael@0 56 if (topic === "csp-on-violate-policy") {
michael@0 57 //these were blocked... record that they were blocked
michael@0 58 var asciiSpec = SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(subject, "nsIURI"), "asciiSpec");
michael@0 59 window.frameBlocked(asciiSpec, data);
michael@0 60 }
michael@0 61 },
michael@0 62
michael@0 63 // must eventually call this to remove the listener,
michael@0 64 // or mochitests might get borked.
michael@0 65 remove: function() {
michael@0 66 SpecialPowers.removeObserver(this, "csp-on-violate-policy");
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 // called when a frame is loaded
michael@0 71 // -- if it's not enumerated above, it should not load!
michael@0 72 var frameLoaded = function(testname, uri) {
michael@0 73 //test already complete.... forget it... remember the first result.
michael@0 74 if (window.framesThatShouldLoad[testname] != -1)
michael@0 75 return;
michael@0 76
michael@0 77 if (typeof window.framesThatShouldLoad[testname] === 'undefined') {
michael@0 78 // uh-oh, we're not expecting this frame to load!
michael@0 79 ok(false, testname + ' framed site should not have loaded: ' + uri);
michael@0 80 } else {
michael@0 81 framesThatShouldLoad[testname] = true;
michael@0 82 ok(true, testname + ' framed site when allowed by csp (' + uri + ')');
michael@0 83 }
michael@0 84 checkTestResults();
michael@0 85 }
michael@0 86
michael@0 87 // called when a frame is blocked
michael@0 88 // -- we can't determine *which* frame was blocked, but at least we can count them
michael@0 89 var frameBlocked = function(uri, policy) {
michael@0 90 ok(true, 'a CSP policy blocked frame from being loaded: ' + policy);
michael@0 91 expectedViolationsLeft--;
michael@0 92 checkTestResults();
michael@0 93 }
michael@0 94
michael@0 95
michael@0 96 // Check to see if all the tests have run
michael@0 97 var checkTestResults = function() {
michael@0 98 // if any test is incomplete, keep waiting
michael@0 99 for (var v in framesThatShouldLoad)
michael@0 100 if(window.framesThatShouldLoad[v] == -1)
michael@0 101 return;
michael@0 102
michael@0 103 if (window.expectedViolationsLeft > 0)
michael@0 104 return;
michael@0 105
michael@0 106 // ... otherwise, finish
michael@0 107 window.examiner.remove();
michael@0 108 SimpleTest.finish();
michael@0 109 }
michael@0 110
michael@0 111 window.addEventListener("message", receiveMessage, false);
michael@0 112
michael@0 113 function receiveMessage(event) {
michael@0 114 if (event.data.call && event.data.call == 'frameLoaded')
michael@0 115 frameLoaded(event.data.testname, event.data.uri);
michael@0 116 }
michael@0 117
michael@0 118 //////////////////////////////////////////////////////////////////////
michael@0 119 // set up and go
michael@0 120 window.examiner = new examiner();
michael@0 121 SimpleTest.waitForExplicitFinish();
michael@0 122
michael@0 123 // added this so the tests run even if we don't flip the pref on by default.
michael@0 124 SpecialPowers.pushPrefEnv(
michael@0 125 {'set':[["security.csp.speccompliant", true]]},
michael@0 126 function() {
michael@0 127 // save this for last so that our listeners are registered.
michael@0 128 // ... this loads the testbed of good and bad requests.
michael@0 129 document.getElementById('cspframe').src = 'file_CSP_frameancestors_main.html';
michael@0 130 document.getElementById('cspframe2').src = 'file_CSP_frameancestors_main_spec_compliant.html';
michael@0 131 });
michael@0 132
michael@0 133 </script>
michael@0 134 </pre>
michael@0 135 </body>
michael@0 136 </html>

mercurial