Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: true
 - Manifest: docshell/test/chrome/chrome.toml
 
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/global.css"?>
<!--
-->
  <!-- test results are displayed in the html:body -->
  </body>
  <!-- test code goes here -->
  <iframe type="content" onload="startTest()" src="file_viewsource_forbidden_in_iframe.html"></iframe>
  <script type="application/javascript">
  <![CDATA[
  SimpleTest.waitForExplicitFinish();
  // We create a promise that will resolve with the error message
  // on a network error page load and reject on any other load.
  function createNetworkErrorMessagePromise(frame) {
    return new Promise(function(resolve, reject) {
      // Error pages do not fire "load" events, so use a progressListener.
      var originalDocumentURI = frame.contentDocument.documentURI;
      var progressListener = {
        onLocationChange(aWebProgress, aRequest, aLocation, aFlags) {
          // Make sure nothing other than an error page is loaded.
          if (!(aFlags & Ci.nsIWebProgressListener.LOCATION_CHANGE_ERROR_PAGE)) {
            reject("location change was not to an error page");
          }
        },
        onStateChange(aWebProgress) {
          // Wait until the documentURI changes (from about:blank) this should
          // be the error page URI.
          var documentURI = frame.contentDocument.documentURI;
          if (documentURI == originalDocumentURI) {
            return;
          }
          aWebProgress.removeProgressListener(progressListener,
                                              Ci.nsIWebProgress.NOTIFY_ALL);
          var matchArray = /about:neterror\?.*&d=([^&]*)/.exec(documentURI);
          if (!matchArray) {
            reject("no network error message found in URI")
            return;
          }
          var errorMsg = matchArray[1];
          resolve(decodeURIComponent(errorMsg));
        },
        QueryInterface: ChromeUtils.generateQI(["nsIWebProgressListener",
                                                "nsISupportsWeakReference"])
      };
      frame.contentWindow.docShell
                         .QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIWebProgress)
                         .addProgressListener(progressListener,
                                              Ci.nsIWebProgress.NOTIFY_LOCATION |
                                              Ci.nsIWebProgress.NOTIFY_STATE_REQUEST);
    });
  }
  function startTest() {
    // Get a reference message that we know will be an unknown protocol message,
    // so we can use it for comparisons in the test cases.
    var refIframe = window[0].document.getElementById("refIframe");
    var refErrorPromise = createNetworkErrorMessagePromise(refIframe);
    refErrorPromise.then(
      function(msg) {
        window.refErrorMsg = msg;
        var testIframe = window[0].document.getElementById("testIframe");
        // Run test cases on load of "about:blank", so that the URI always changes
        // and we can detect this in our Promise.
        testIframe.onload = runNextTestCase;
        testIframe.src = "about:blank";
      },
      function(reason) {
        ok(false, "Could not get reference error message", reason);
        SimpleTest.finish();
      })
      .catch(function(e) {
        ok(false, "Unexpected exception thrown getting reference error message", e);
      });
  }
  function runTestCase(testCase) {
    var testIframe = window[0].document.getElementById("testIframe");
    var expectedErrorMsg = window.refErrorMsg.replace("wibble", testCase.expectedProtocolList);
    var testErrorPromise = createNetworkErrorMessagePromise(testIframe);
    testErrorPromise.then(
      function(actualErrorMsg) {
        is(actualErrorMsg, expectedErrorMsg, testCase.desc);
        testIframe.src = "about:blank";
      },
      function(reason) {
        ok(false, testCase.desc, reason);
        testIframe.src = "about:blank";
      })
      .catch(function(e) {
        ok(false, testCase.desc + " - unexpected exception thrown", e);
      });
    testIframe.src = testCase.protocols + "://example.com/!/";
  }
  var testCaseIndex = -1;
  let testCases = [
    {
      desc: "Test 1: view-source should not be allowed in an iframe",
      protocols: "view-source:http",
      expectedProtocolList: "view-source, http"
    },
    {
      desc: "Test 2: jar:view-source should not be allowed in an iframe",
      protocols: "jar:view-source:http",
      expectedProtocolList: "jar, view-source, http"
    },
    {
      desc: "Test 3: if invalid protocol first should report before view-source",
      protocols: "wibble:view-source:http",
      // Nothing after the invalid protocol gets set as a proper nested URI,
      // so the list stops there.
      expectedProtocolList: "wibble"
    },
    {
      desc: "Test 4: if view-source first should report before invalid protocol",
      protocols: "view-source:wibble:http",
      expectedProtocolList: "view-source, wibble"
    }
  ];
  function runNextTestCase() {
    ++testCaseIndex;
    if (testCaseIndex == testCases.length) {
      SimpleTest.finish();
      return;
    }
    runTestCase(testCases[testCaseIndex]);
  }
  ]]>
  </script>
</window>