docshell/test/chrome/test_viewsource_forbidden_in_iframe.xul

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 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin/"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=624883
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug 624883"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
michael@0 10
michael@0 11 <!-- test results are displayed in the html:body -->
michael@0 12 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 13 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=624883"
michael@0 14 target="_blank">Mozilla Bug 624883</a>
michael@0 15 </body>
michael@0 16
michael@0 17 <!-- test code goes here -->
michael@0 18 <iframe type="content" onload="startTest()" src="file_viewsource_forbidden_in_iframe.html"></iframe>
michael@0 19
michael@0 20 <script type="application/javascript">
michael@0 21 <![CDATA[
michael@0 22
michael@0 23 const Ci = Components.interfaces;
michael@0 24 const Cu = Components.utils;
michael@0 25
michael@0 26 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 27
michael@0 28 SimpleTest.waitForExplicitFinish();
michael@0 29
michael@0 30 // We create a promise that will resolve with the error message
michael@0 31 // on a network error page load and reject on any other load.
michael@0 32 function createNetworkErrorMessagePromise(frame) {
michael@0 33 return new Promise(function(resolve, reject) {
michael@0 34
michael@0 35 // Error pages do not fire "load" events, so use a progressListener.
michael@0 36 var originalDocumentURI = frame.contentDocument.documentURI;
michael@0 37 var progressListener = {
michael@0 38 onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags) {
michael@0 39 // Make sure nothing other than an error page is loaded.
michael@0 40 if (!(aFlags & Ci.nsIWebProgressListener.LOCATION_CHANGE_ERROR_PAGE)) {
michael@0 41 reject("location change was not to an error page");
michael@0 42 }
michael@0 43 },
michael@0 44
michael@0 45 onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
michael@0 46 // Wait until the documentURI changes (from about:blank) this should
michael@0 47 // be the error page URI.
michael@0 48 var documentURI = frame.contentDocument.documentURI;
michael@0 49 if (documentURI == originalDocumentURI) {
michael@0 50 return;
michael@0 51 }
michael@0 52
michael@0 53 aWebProgress.removeProgressListener(progressListener,
michael@0 54 Ci.nsIWebProgress.NOTIFY_ALL);
michael@0 55 var matchArray = /about:neterror\?.*&d=([^&]*)/.exec(documentURI);
michael@0 56 if (!matchArray) {
michael@0 57 reject("no network error message found in URI")
michael@0 58 return;
michael@0 59 }
michael@0 60
michael@0 61 var errorMsg = matchArray[1];
michael@0 62 resolve(decodeURIComponent(errorMsg));
michael@0 63 },
michael@0 64
michael@0 65 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
michael@0 66 Ci.nsISupportsWeakReference])
michael@0 67 };
michael@0 68
michael@0 69 frame.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 70 .getInterface(Ci.nsIWebNavigation)
michael@0 71 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 72 .getInterface(Ci.nsIWebProgress)
michael@0 73 .addProgressListener(progressListener,
michael@0 74 Ci.nsIWebProgress.NOTIFY_LOCATION |
michael@0 75 Ci.nsIWebProgress.NOTIFY_STATE_REQUEST);
michael@0 76 });
michael@0 77 }
michael@0 78
michael@0 79 function startTest() {
michael@0 80 // Get a reference message that we know will be an unknown protocol message,
michael@0 81 // so we can use it for comparisons in the test cases.
michael@0 82 var refIframe = window[0].document.getElementById("refIframe");
michael@0 83 var refErrorPromise = createNetworkErrorMessagePromise(refIframe);
michael@0 84
michael@0 85 refErrorPromise.then(
michael@0 86 function(msg) {
michael@0 87 window.refErrorMsg = msg;
michael@0 88 var testIframe = window[0].document.getElementById("testIframe");
michael@0 89
michael@0 90 // Run test cases on load of "about:blank", so that the URI always changes
michael@0 91 // and we can detect this in our Promise.
michael@0 92 testIframe.onload = runNextTestCase;
michael@0 93 testIframe.src = "about:blank";
michael@0 94 },
michael@0 95 function(reason) {
michael@0 96 ok(false, "Could not get reference error message", reason);
michael@0 97 SimpleTest.finish();
michael@0 98 })
michael@0 99 .catch(function(e) {
michael@0 100 ok(false, "Unexpected exception thrown getting reference error message", exception);
michael@0 101 });
michael@0 102
michael@0 103 refIframe.src = "wibble://example.com";
michael@0 104 }
michael@0 105
michael@0 106 function runTestCase(testCase) {
michael@0 107 var testIframe = window[0].document.getElementById("testIframe");
michael@0 108 var expectedErrorMsg = window.refErrorMsg.replace("wibble", testCase.expectedProtocolList);
michael@0 109
michael@0 110 var testErrorPromise = createNetworkErrorMessagePromise(testIframe);
michael@0 111 testErrorPromise.then(
michael@0 112 function(actualErrorMsg) {
michael@0 113 is(actualErrorMsg, expectedErrorMsg, testCase.desc);
michael@0 114 testIframe.src = "about:blank";
michael@0 115 },
michael@0 116 function(reason) {
michael@0 117 ok(false, testCase.desc, reason);
michael@0 118 testIframe.src = "about:blank";
michael@0 119 })
michael@0 120 .catch(function(e) {
michael@0 121 ok(false, testCase.desc + " - unexpected exception thrown", exception);
michael@0 122 });
michael@0 123
michael@0 124 testIframe.src = testCase.protocols + "://example.com/!/";
michael@0 125 }
michael@0 126
michael@0 127 var testCaseIndex = -1;
michael@0 128 testCases = [
michael@0 129 {
michael@0 130 desc: "Test 1: view-source should not be allowed in an iframe",
michael@0 131 protocols: "view-source:http",
michael@0 132 expectedProtocolList: "view-source, http"
michael@0 133 },
michael@0 134 {
michael@0 135 desc: "Test 2: feed:view-source should not be allowed in an iframe",
michael@0 136 protocols: "feed:view-source:http",
michael@0 137 expectedProtocolList: "feed, view-source, http"
michael@0 138 },
michael@0 139 {
michael@0 140 desc: "Test 3: jar:view-source should not be allowed in an iframe",
michael@0 141 protocols: "jar:view-source:http",
michael@0 142 expectedProtocolList: "jar, view-source, http"
michael@0 143 },
michael@0 144 {
michael@0 145 desc: "Test 4: pcast:view-source should not be allowed in an iframe",
michael@0 146 protocols: "pcast:view-source:http",
michael@0 147 expectedProtocolList: "pcast, view-source, http"
michael@0 148 },
michael@0 149 {
michael@0 150 desc: "Test 5: pcast:feed:view-source should not be allowed in an iframe",
michael@0 151 protocols: "pcast:feed:view-source:http",
michael@0 152 expectedProtocolList: "pcast, feed, view-source, http"
michael@0 153 },
michael@0 154 {
michael@0 155 desc: "Test 6: if invalid protocol first should report before view-source",
michael@0 156 protocols: "wibble:view-source:http",
michael@0 157 // Nothing after the invalid protocol gets set as a proper nested URI,
michael@0 158 // so the list stops there.
michael@0 159 expectedProtocolList: "wibble"
michael@0 160 },
michael@0 161 {
michael@0 162 desc: "Test 7: if view-source first should report before invalid protocol",
michael@0 163 protocols: "view-source:wibble:http",
michael@0 164 expectedProtocolList: "view-source, wibble"
michael@0 165 }
michael@0 166 ];
michael@0 167
michael@0 168 function runNextTestCase() {
michael@0 169 ++testCaseIndex;
michael@0 170 if (testCaseIndex == testCases.length) {
michael@0 171 SimpleTest.finish();
michael@0 172 return;
michael@0 173 }
michael@0 174
michael@0 175 runTestCase(testCases[testCaseIndex]);
michael@0 176 }
michael@0 177
michael@0 178 ]]>
michael@0 179 </script>
michael@0 180 </window>

mercurial