summaryrefslogtreecommitdiffstats
path: root/docshell/test/chrome/test_viewsource_forbidden_in_iframe.xhtml
blob: 0cc45c7821c6185fb6888eb352afc73644c9af46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/global.css"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=624883
-->
<window title="Mozilla Bug 624883"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=624883"
     target="_blank">Mozilla Bug 624883</a>
  </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, aRequest, aStateFlags, aStatus) {
          // 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);
      });

    refIframe.src = "wibble://example.com";
  }

  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>