summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_iframe_sandbox_navigation.html
blob: caaf4439b84ace62736d5d2c600fa0f7b85687e2 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=341604
Implement HTML5 sandbox attribute for IFRAMEs
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 341604 - navigation</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<script type="application/javascript">
/** Test for Bug 341604 - Implement HTML5 sandbox attribute for IFRAMEs **/
/** Navigation tests Part 1**/

SimpleTest.requestLongerTimeout(2); // slow on Android
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");
// a postMessage handler that is used by sandboxed iframes without
// 'allow-same-origin'/other windows to communicate pass/fail back to this main page.
// it expects to be called with an object like {ok: true/false, desc:
// <description of the test> which it then forwards to ok()
var bc = new BroadcastChannel("test_iframe_sandbox_navigation");
bc.addEventListener("message", receiveMessage);
window.addEventListener("message", receiveMessage);

var testPassesReceived = 0;

function receiveMessage(event) {
  switch (event.data.type) {
    case "attempted":
      testAttempted();
      break;
    case "ok":
      ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
      break;
    case "if_10":
      doIf10TestPart2();
      break;
    default:
      // allow for old style message
      if (event.data.ok != undefined) {
        ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
      }
  }
}

// Open windows for tests to attempt to navigate later.
var windowsToClose = new Array();

var attemptedTests = 0;
var passedTests = 0;
var totalTestsToPass = 7;
var totalTestsToAttempt = 13;

function ok_wrapper(result, desc, addToAttempted = true) {
  ok(result, desc);

  if (result) {
    passedTests++;
  }

  if (addToAttempted) {
    testAttempted();
  }
}

// Added so that tests that don't register unless they fail,
// can at least notify that they've attempted to run.
function testAttempted() {
  attemptedTests++;
  if (attemptedTests == totalTestsToAttempt) {
    // Make sure all tests have had a chance to complete.
    setTimeout(function() {finish();}, 1000);
  }
}

var finishCalled = false;

function finish() {
  if (!finishCalled) {
    finishCalled = true;
    is(passedTests, totalTestsToPass, "There are " + totalTestsToPass + " navigation tests that should pass");

    closeWindows();

    bc.close();

    SimpleTest.finish();
  }
}

function checkTestsFinished() {
  // If our own finish() has not been called, probably failed due to a timeout, so close remaining windows.
  if (!finishCalled) {
    closeWindows();
  }
}

function closeWindows() {
  for (var i = 0; i < windowsToClose.length; i++) {
    windowsToClose[i].close();
  }
}

function doTest() {
  // passes if good
  // 1) A sandboxed iframe is allowed to navigate itself
  // (done by file_iframe_sandbox_d_if1.html which has 'allow-scripts' and navigates to
  // file_iframe_sandbox_navigation_pass.html).

  // passes if good
  // 2) A sandboxed iframe is allowed to navigate its children, even if they are sandboxed
  // (done by file_iframe_sandbox_d_if2.html which has 'allow-scripts', it navigates a child
  // iframe containing file_iframe_sandbox_navigation_start.html to file_iframe_sandbox_navigation_pass.html).

  // fails if bad
  // 3) A sandboxed iframe is not allowed to navigate its ancestor
  // (done by file_iframe_sandbox_d_if4.html contained within file_iframe_sandbox_d_if3.html,
  // it attempts to navigate file_iframe_sandbox_d_if3.html to file_iframe_sandbox_navigation_fail.html).

  // fails if bad
  // 4) A sandboxed iframe is not allowed to navigate its sibling
  // (done by file_iframe_sandbox_d_if5.html which has 'allow scripts allow-same-origin'
  // and attempts to navigate file_iframe_navigation_start.html contained in if_sibling on this
  // page to file_iframe_sandbox_navigation_fail.html).

  // passes if good, fails if bad
  // 5) When a link is clicked in a sandboxed iframe, the document navigated to is sandboxed
  // the same as the original document and is not same origin with parent document
  // (done by file_iframe_sandbox_d_if6.html which simulates a link click and navigates
  // to file_iframe_sandbox_d_if7.html which attempts to call back into its parent).

  // fails if bad
  // 6) An iframe (if_8) has sandbox="allow-same-origin allow-scripts", the sandboxed document
  // (file_iframe_sandbox_d_if_8.html) that it contains accesses its parent (this file) and removes
  // 'allow-same-origin' and then triggers a reload.
  // The document should not be able to access its parent (this file).

  // fails if bad
  // 7) An iframe (if_9) has sandbox="allow-same-origin allow-scripts", the sandboxed document
  // (file_iframe_sandbox_d_if_9.html) that it contains accesses its parent (this file) and removes
  // 'allow-scripts' and then triggers a reload.
  // The document should not be able to run a script and access its parent (this file).

  // passes if good
  // 8) a document in an iframe with sandbox='allow-scripts' should have a different null
  // principal in its original document than a document to which it navigates itself
  // file_iframe_sandbox_d_if_10.html does this, co-ordinating with this page via postMessage

  // passes if good
  // 9) a document (file_iframe_sandbox_d_if11.html in an iframe (if_11) with sandbox='allow-scripts'
  // is navigated to file_iframe_sandbox_d_if12.html - when that document loads
  // a message is sent back to this document, which adds 'allow-same-origin' to if_11 and then
  // calls .back on it - file_iframe_sandbox_if12.html should be able to call back into this
  // document - this is all contained in file_iframe_sandbox_d_if13.html which is opened in another
  // tab so it has its own isolated session history
  window.open("file_iframe_sandbox_d_if13.html");

  // open up the top navigation tests

  // fails if bad
  // 10) iframe with sandbox='allow-scripts' can NOT navigate top
  // file_iframe_sandbox_e_if1.html contains file_iframe_sandbox_e_if6.html which
  // attempts to navigate top
  windowsToClose.push(window.open("file_iframe_sandbox_e_if1.html"));

  // fails if bad
  // 11) iframe with sandbox='allow-scripts' nested inside iframe with
  // 'allow-top-navigation allow-scripts' can NOT navigate top
  // file_iframe_sandbox_e_if2.html contains file_iframe_sandbox_e_if1.html which
  // contains file_iframe_sandbox_e_if6.html which attempts to navigate top
  windowsToClose.push(window.open("file_iframe_sandbox_e_if2.html"));

  // passes if good
  // 12) iframe with sandbox='allow-top-navigation allow-scripts' can navigate top
  // file_iframe_sandbox_e_if3.html contains file_iframe_sandbox_e_if5.html which navigates top
  window.open("file_iframe_sandbox_e_if3.html");

  // passes if good
  // 13) iframe with sandbox='allow-top-navigation allow-scripts' nested inside an iframe with
  // 'allow-top-navigation allow-scripts' can navigate top
  // file_iframe_sandbox_e_if4.html contains file_iframe_sandbox_e_if3.html which contains
  // file_iframe_sandbox_e_if5.html which navigates top
  window.open("file_iframe_sandbox_e_if4.html");
}

addLoadEvent(doTest);

window.modified_if_8 = false;

function reload_if_8() {
  var if_8 = document.getElementById('if_8');
  if_8.src = 'file_iframe_sandbox_d_if8.html';
}

function modify_if_8() {
  // If this is the second time this has been called
  // that's a failed test (allow-same-origin was removed
  // the first time).
  if (window.modified_if_8) {
    ok_wrapper(false, "a sandboxed iframe from which 'allow-same-origin' was removed should not be able to access its parent");

    // need to return here since we end up in an infinite loop otherwise
    return;
  }

  var if_8 = document.getElementById('if_8');
  window.modified_if_8 = true;

  if_8.sandbox = 'allow-scripts';
  testAttempted();
  sendMouseEvent({type:'click'}, 'a_button');
}

window.modified_if_9 = false;

function reload_if_9() {
  var if_9 = document.getElementById('if_9');
  if_9.src = 'file_iframe_sandbox_d_if9.html';
}

function modify_if_9() {
  // If this is the second time this has been called
  // that's a failed test (allow-scripts was removed
  // the first time).
  if (window.modified_if_9) {
    ok_wrapper(false, "an sandboxed iframe from which 'allow-scripts' should be removed should not be able to access its parent via a script", false);

    // need to return here since we end up in an infinite loop otherwise
    return;
  }

  var if_9 = document.getElementById('if_9');
  window.modified_if_9 = true;

  if_9.sandbox = 'allow-same-origin';

  testAttempted();
  sendMouseEvent({type:'click'}, 'a_button2');
}

var firstPrincipal = "";
var secondPrincipal;

function doIf10TestPart1() {
  if (firstPrincipal != "")
    return;

  // use SpecialPowers to get the principal of if_10.
  // NB: We stringify here and below because special-powers wrapping doesn't
  // preserve identity.
  var if_10 = document.getElementById('if_10');
  firstPrincipal = SpecialPowers.wrap(if_10).contentDocument.nodePrincipal.origin;
  if_10.src = 'file_iframe_sandbox_d_if10.html';
}

function doIf10TestPart2() {
  var if_10 = document.getElementById('if_10');
  // use SpecialPowers to get the principal of if_10
  secondPrincipal = SpecialPowers.wrap(if_10).contentDocument.nodePrincipal.origin;
  ok_wrapper(firstPrincipal != secondPrincipal, "documents should NOT have the same principal if they are sandboxed without" +
    " allow-same-origin and the first document is navigated to the second");
}
</script>
<body onunload="checkTestsFinished()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
<p id="display"></p>
<div id="content">
<iframe sandbox="allow-scripts" id="if_1" src="file_iframe_sandbox_d_if1.html" height="10" width="10"></iframe>
<iframe sandbox="allow-scripts" id="if_2" src="file_iframe_sandbox_d_if2.html" height="10" width="10"></iframe>
<iframe sandbox="allow-scripts" id="if_3" src="file_iframe_sandbox_d_if3.html" height="10" width="10"></iframe>
<iframe id="if_sibling" name="if_sibling" src="about:blank" height="10" width="10"></iframe>
<iframe sandbox="allow-scripts allow-same-origin" id="if_5" src="file_iframe_sandbox_d_if5.html" height="10" width="10"></iframe>
<iframe sandbox="allow-scripts" id="if_6" src="file_iframe_sandbox_d_if6.html" height="10" width="10"></iframe>
<iframe sandbox="allow-same-origin allow-scripts" id="if_8" src="file_iframe_sandbox_d_if8.html" height="10" width="10"></iframe>
<iframe sandbox="allow-same-origin allow-scripts" id="if_9" src="file_iframe_sandbox_d_if9.html" height="10" width="10"></iframe>
<iframe sandbox="allow-scripts" id="if_10" src="file_iframe_sandbox_navigation_start.html" onload='doIf10TestPart1()' height="10" width="10"></iframe>
</div>
<input type='button' id="a_button" onclick='reload_if_8()'>
<input type='button' id="a_button2" onclick='reload_if_9()'>
</body>
</html>