Attachment #603903: iframe sandbox tests - general for bug #341604

View | Details | Raw Unified | Return to bug 341604
Collapse All | Expand All

(-)a/content/html/content/test/Makefile.in (+8 lines)
Line     Link Here 
 Lines 292-307   _TEST_FILES = \ Link Here 
292
		file_fullscreen-esc-exit.html \
292
		file_fullscreen-esc-exit.html \
293
		file_fullscreen-esc-exit-inner.html \
293
		file_fullscreen-esc-exit-inner.html \
294
		file_fullscreen-rollback.html \
294
		file_fullscreen-rollback.html \
295
		test_li_attributes_reflection.html \
295
		test_li_attributes_reflection.html \
296
		test_ol_attributes_reflection.html \
296
		test_ol_attributes_reflection.html \
297
		test_bug651956.html \
297
		test_bug651956.html \
298
		test_bug694503.html \
298
		test_bug694503.html \
299
		test_object_plugin_nav.html \
299
		test_object_plugin_nav.html \
300
		test_iframe_sandbox_general.html \
301
		file_iframe_sandbox_c_if1.html \
302
		file_iframe_sandbox_c_if2.html \
303
		file_iframe_sandbox_c_if3.html \
304
		file_iframe_sandbox_c_if4.html \
305
		file_iframe_sandbox_form_fail.html \
306
		file_iframe_sandbox_form_pass.html \
307
		file_iframe_sandbox_open_window_fail.html \
300
		$(NULL)
308
		$(NULL)
301
309
302
_BROWSER_TEST_FILES = \
310
_BROWSER_TEST_FILES = \
303
		browser_bug649778.js \
311
		browser_bug649778.js \
304
		file_bug649778.html \
312
		file_bug649778.html \
305
		file_bug649778.html^headers^ \
313
		file_bug649778.html^headers^ \
306
		$(NULL)
314
		$(NULL)
307
315
(-)a/content/html/content/test/file_iframe_sandbox_c_if1.html (+36 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>     
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
  
9
</head>
10
<script type="text/javascript">
11
  function ok(result, desc) { 
12
    window.top.postMessage({ok: result, desc: desc}, "*");
13
  }
14
  
15
  function doSubmit() {
16
    document.getElementById('a_form').submit();
17
  }
18
  
19
  function doStuff() {
20
    ok(true, "documents sandboxed with allow-scripts should be able to run scripts"); 
21
    
22
    // should not be allowed - don't have allow-forms
23
    sendMouseEvent({type:'click'}, 'a_button');
24
  }
25
</script>
26
<body onLoad="doStuff()">
27
  I am sandboxed but with "allow-scripts"
28
  
29
  <form method="get" action="file_iframe_sandbox_form_pass.html" id="a_form">
30
    First name: <input type="text" name="firstname">
31
    Last name: <input type="text" name="lastname">
32
    <input type="submit" onclick="doSubmit()" id="a_button">
33
  </form>
34
  
35
</body>
36
</html>
(-)a/content/html/content/test/file_iframe_sandbox_c_if2.html (+21 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>   
6
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
7
  
8
</head>
9
<script type="text/javascript">
10
  function ok(result, desc) { 
11
    window.top.postMessage({ok: result, desc: message}, "*");
12
  }
13
  
14
  function doStuff() {
15
    ok(false, "documents sandboxed WITHOUT allow-scripts should NOT be able to run scripts"); 
16
  }
17
</script>
18
<body onLoad="doStuff()">
19
  I am sandboxed with no permissions
20
</body>
21
</html>
(-)a/content/html/content/test/file_iframe_sandbox_c_if3.html (+29 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>     
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
  
9
</head>
10
<script type="text/javascript">
11
  function doSubmit() {
12
    document.getElementById('a_form').submit();
13
  }
14
  
15
  function doStuff() {    
16
    // try and submit the form - this should succeed
17
    sendMouseEvent({type:'click'}, 'a_button');
18
  }
19
</script>
20
<body onLoad="doStuff()">
21
  I am sandboxed but with "allow-scripts allow-forms"
22
  
23
  <form method="get" action="file_iframe_sandbox_form_pass.html" id="a_form">
24
    First name: <input type="text" name="firstname">
25
    Last name: <input type="text" name="lastname">
26
    <input type="submit" onclick="doSubmit()" id="a_button">
27
  </form>
28
</body>
29
</html>
(-)a/content/html/content/test/file_iframe_sandbox_c_if4.html (+42 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>     
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
  
9
</head>
10
<script type="text/javascript">
11
  function ok(result, desc) { 
12
    window.parent.ok_wrapper(result, desc);
13
  }
14
  
15
  function doStuff() {
16
    // try to open a new window via target="_blank", window.open(), and showModalDialog()
17
    // the window we try to open closes itself once it opens
18
    sendMouseEvent({type:'click'}, 'target_blank');
19
20
    // REVIEW : window.open in a sandboxed document throws an exception : 
21
    // should it ?
22
    try { 
23
      window.open("file_iframe_sandbox_open_window_fail.html");
24
    } catch (error) {
25
      ok(true, "window.open threw a JS exception and was not allowed");
26
    }
27
28
    // REVIEW : window.showModalDialog in a sandboxed document throws an exception : 
29
    // should it ?
30
    try {
31
      window.showModalDialog("file_iframe_sandbox_open_window_fail.html"); 
32
    } catch(error) {
33
      ok(true, "window.showModalDialog threw a JS exception and was not allowed");
34
    }
35
  }    
36
</script>
37
<body onLoad="doStuff()">
38
  I am sandboxed but with "allow-scripts allow-same-origin"
39
  
40
  <a href="file_iframe_sandbox_open_window_fail.html" target="_blank" id="target_blank">open window</a>
41
</body>
42
</html>
(-)a/content/html/content/test/file_iframe_sandbox_form_fail.html (+19 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
</head>
9
10
<body onLoad="doStuff()">
11
  I should NOT be loaded by a form submit from a sandbox without 'allow-forms'
12
</body>
13
</html>
14
15
<script>
16
  function doStuff() {
17
    window.top.postMessage({ok: false, desc: "documents sandboxed without allow-forms should NOT be able to submit forms"}, "*");
18
  }
19
</script>
(-)a/content/html/content/test/file_iframe_sandbox_form_pass.html (+19 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
</head>
9
10
<body onLoad="doStuff()">
11
  I should be loaded by a form submit from a sandbox with 'allow-forms'
12
</body>
13
</html>
14
15
<script>
16
  function doStuff() {
17
    window.top.postMessage({ok: true, desc: "documents sandboxed with allow-forms should be able to submit forms"}, "*");
18
  }
19
</script>
(-)a/content/html/content/test/file_iframe_sandbox_open_window_fail.html (+21 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>Test for Bug 341604</title>
6
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
7
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8
</head>
9
10
<body onLoad="doStuff()">
11
  I should NOT be opened by a sandboxed iframe via any method
12
</body>
13
</html>
14
15
<script>
16
  function doStuff() {
17
    console.log("file_iframe_sandbox_window_open_fail.html");
18
    window.opener.ok(false, "sandboxed documents should NOT be able to open windows");
19
    self.close();
20
  }
21
</script>
(-)a/content/html/content/test/test_iframe_sandbox_general.html (+108 lines)
Line     Link Here 
Line 0    Link Here 
1
<!DOCTYPE HTML>
2
<html>
3
<!--
4
https://bugzilla.mozilla.org/show_bug.cgi?id=341604
5
Implement HTML5 sandbox attribute for IFRAMEs - general tests
6
-->
7
<head>
8
  <meta charset="utf-8">
9
  <title>Test for Bug 341604</title>
10
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
11
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
12
</head>
13
<script type="application/javascript">
14
/** Test for Bug 341604 - Implement HTML5 sandbox attribute for IFRAMEs - general tests **/
15
16
// a postMessage handler that is used by sandboxed iframes without
17
// 'allow-same-origin' to communicate pass/fail back to this main page.
18
// it expects to be called with an object like {ok: true/false, desc: 
19
// <description of the test> which it then forwards to ok()
20
window.addEventListener("message", receiveMessage, false);  
21
      
22
function receiveMessage(event)  
23
{ 
24
  ok_wrapper(event.data.ok, event.data.desc);
25
}  
26
27
var completedTests = 0;
28
var passedTests = 0;
29
30
function ok_wrapper(result, desc) {
31
  ok(result, desc);
32
  
33
  completedTests++;
34
  
35
  if (result) {
36
    passedTests++;
37
  }
38
  
39
  // REVIEW : if one of these tests fails and this isn't called
40
  // this means this test will hang until the time out happens :(
41
  // advice on better ways to synchronize things appreciated !
42
  if (completedTests == 6) {
43
    ok(passedTests == 6, "There are 6 general tests that should pass");
44
    SimpleTest.finish();
45
  }
46
}
47
48
function doTest() { 
49
  SimpleTest.waitForExplicitFinish();
50
  
51
  // passes if good
52
  // 1) test that scripts can run in an iframe sandboxed with "allow-scripts"
53
  // (done in file_iframe_sandbox_c_if1.html which has 'allow-scripts')
54
  
55
  // fails if bad 
56
  // 2) test that allow scripts cannot run in an iframe sandboxed without "allow-scripts"
57
  // (done in file_iframe_sandbox_c_if2.html which has sandbox='')
58
  
59
  // passes if good
60
  // 3) test that a new iframe has sandbox attribute
61
  var ifr = document.createElement("iframe");
62
  ok_wrapper("sandbox" in ifr, "a new iframe should have a sandbox attribute");
63
64
  // passes if good
65
  // 4) test that the sandbox attribute is a DOMSettableTokenList
66
  ok_wrapper(ifr.sandbox instanceof DOMSettableTokenList, "sandbox attribute should be a DOMSettable Token List");
67
  
68
  // passes if good
69
  // 5) test that a sandboxed iframe with 'allow-forms' can submit forms
70
  // (done in file_iframe_sandbox_c_if3.html which has 'allow-forms' and 'allow-scripts')
71
  
72
  // fails if bad
73
  // 6) test that a sandboxed iframe without 'allow-forms' can NOT submit forms 
74
  // (done in file_iframe_sandbox_c_if1.html which only has 'allow-scripts')
75
 
76
  // REVIEW : are there other ways of opening new windows that the 3 below that are mentioned in the HTML5
77
  // spec ? 
78
  
79
  // fails if bad
80
  // 7) test that a sandboxed iframe can't open a new window using the target.attribute
81
  // this is done via file_iframe_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
82
  // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
83
  // function that calls window.parent.ok_wrapper 
84
  
85
  // passes if good
86
  // 8) test that a sandboxed iframe can't open a new window using window.open
87
  // this is done via file_iframe_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
88
  // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
89
  // function that calls window.parent.ok_wrapper
90
  
91
  // passes if good
92
  // 9) test that a sandboxed iframe can't open a new window using window.ShowModalDialog
93
  // this is done via file_iframe_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
94
  // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
95
  // function that calls window.parent.ok_wrapper
96
}
97
</script>
98
<body onload="doTest()">
99
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
100
<p id="display"></p>
101
<div id="content">
102
<iframe sandbox="allow-scripts" id="if_1" src="file_iframe_sandbox_c_if1.html" height="10" width="10"></iframe>
103
<iframe sandbox="" id="if_2" src="file_iframe_sandbox_c_if2.html" height="10" width="10"></iframe>
104
<iframe sandbox="allow-forms allow-scripts" id="if_3" src="file_iframe_sandbox_c_if3.html" height="10" width="10"></iframe>
105
<iframe sandbox="allow-same-origin allow-scripts" id="if_4" src="file_iframe_sandbox_c_if4.html" height="10" width="10"></iframe>
106
</div>
107
</body>
108
</html>

Return to bug 341604