Attachment #603514: iframe sandbox tests - same origin for bug #341604

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

(-)a/content/html/content/test/Makefile.in (+4 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_same_origin.html \
301
		file_iframe_sandbox_b_if1.html \
302
		file_iframe_sandbox_b_if2.html \
303
		file_iframe_sandbox_b_if3.html \
300
		$(NULL)
304
		$(NULL)
301
305
302
_BROWSER_TEST_FILES = \
306
_BROWSER_TEST_FILES = \
303
		browser_bug649778.js \
307
		browser_bug649778.js \
304
		file_bug649778.html \
308
		file_bug649778.html \
305
		file_bug649778.html^headers^ \
309
		file_bug649778.html^headers^ \
306
		$(NULL)
310
		$(NULL)
307
311
(-)a/content/html/content/test/file_iframe_sandbox_b_if1.html (+11 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
</head>
8
<body>
9
  I am sandboxed without any permissions
10
</body>
11
</html>
(-)a/content/html/content/test/file_iframe_sandbox_b_if2.html (+49 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
</head>
8
<script>
9
  function ok(condition, msg) {
10
    window.parent.ok_wrapper(condition, msg);
11
  }
12
13
  function testXHR() {
14
    var xhr = new XMLHttpRequest();
15
16
    xhr.open("GET", "file_iframe_sandbox_if1.html");
17
18
    xhr.onreadystatechange = function (oEvent) {
19
      var result = false;
20
      if (xhr.readyState == 4) {
21
        if (xhr.status == 200) {
22
          result = true;
23
        }
24
        ok(result, "XHR should work normally in an iframe sandboxed with 'allow-same-origin'");
25
      }
26
    }
27
    
28
    xhr.send(null);
29
  }
30
31
  function doStuff() {
32
    ok(true, "documents sandboxed with 'allow-same-origin' should be able to access their parent");
33
    
34
    // should be able to access document.cookie since we have 'allow-same-origin'
35
    ok(document.cookie == "", "a document sandboxed with allow-same-origin should be able to access document.cookie");
36
    
37
    // should be able to access localStorage since we have 'allow-same-origin'
38
    ok(window.localStorage, "a document sandboxed with allow-same-origin should be able to access localStorage");
39
    
40
    // should be able to access sessionStorage since we have 'allow-same-origin'
41
    ok(window.sessionStorage, "a document sandboxed with allow-same-origin should be able to access sessionStorage");
42
  
43
    testXHR();
44
  }
45
</script>
46
<body onLoad="doStuff()">
47
  I am sandboxed but with "allow-same-origin" and "allow-scripts"
48
</body>
49
</html>
(-)a/content/html/content/test/file_iframe_sandbox_b_if3.html (+65 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
</head>
8
<script>
9
  function ok(result, message) {
10
    window.top.postMessage({ok: result, desc: message}, "*");
11
  }
12
13
  function testXHR() {
14
  var xhr = new XMLHttpRequest();
15
16
  xhr.open("GET", "file_iframe_sandbox_if1.html");
17
18
  xhr.onreadystatechange = function (oEvent) {
19
    var result = false;
20
    if (xhr.readyState == 4) {
21
      if (xhr.status == 0) {
22
        result = true;
23
      }
24
      ok(result, "XHR should be blocked in an iframe sandboxed WITHOUT 'allow-same-origin'");
25
    }
26
  }
27
  
28
  xhr.send(null);
29
}
30
  
31
  function doStuff() {
32
    try {
33
      window.parent.ok(false, "documents sandboxed without 'allow-same-origin' should NOT be able to access their parent");
34
    } catch (error) {
35
      ok(true, "documents sandboxed without 'allow-same-origin' should NOT be able to access their parent");
36
    }
37
    
38
    // should NOT be able to access document.cookie 
39
    try {
40
      var foo = document.cookie;
41
    } catch(error) {
42
      ok(true, "a document sandboxed without allow-same-origin should NOT be able to access document.cookie");
43
    }
44
    
45
    // should NOT be able to access localStorage
46
    try {
47
      var foo = window.localStorage;
48
    } catch(error) {
49
      ok(true, "a document sandboxed without allow-same-origin should NOT be able to access localStorage");
50
    }
51
52
    // should NOT be able to access sessionStorage 
53
    try {
54
      var foo = window.sessionStorage;
55
    } catch(error) {
56
      ok(true, "a document sandboxed without allow-same-origin should NOT be able to access sessionStorage");
57
    }
58
  
59
  testXHR();
60
  }
61
</script>
62
<body onLoad="doStuff()">
63
  I am sandboxed but with "allow-scripts"
64
</body>
65
</html>
(-)a/content/html/content/test/test_iframe_sandbox_same_origin.html (+110 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 - same origin 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 **/
15
/** Same Origin Tests **/
16
17
var completedTests = 0;
18
var passedTests = 0;
19
20
function ok_wrapper(result, desc) {
21
  ok(result, desc);
22
  
23
  completedTests++;
24
25
  if (result) {
26
    passedTests++;
27
  }
28
  
29
  // REVIEW : if one of these tests fails and this isn't called
30
  // this means this test will hang until the time out happens :(
31
  // advice on better ways to synchronize things appreciated !
32
  if (completedTests == 12) {
33
    ok(passedTests == 12, "There are 12 same-origin tests that should pass");
34
  
35
    SimpleTest.finish();
36
  }
37
}
38
39
function receiveMessage(event)  
40
{ 
41
  ok_wrapper(event.data.ok, event.data.desc);
42
}  
43
44
// a postMessage handler that is used by sandboxed iframes without
45
// 'allow-same-origin' to communicate pass/fail back to this main page.
46
// it expects to be called with an object like {ok: true/false, desc: 
47
// <description of the test> which it then forwards to ok()
48
window.addEventListener("message", receiveMessage, false);  
49
50
function doTest() { 
51
  SimpleTest.waitForExplicitFinish();
52
53
  // 1) test that we can't access an iframe sandboxed without "allow-same-origin"
54
  var if_1 = document.getElementById("if_1");
55
  try {
56
    var b = if_1.contentDocument.body;
57
    ok_wrapper(false, "accessing body of a sandboxed document should not be allowed");
58
  } catch (err){
59
    ok_wrapper(true, "accessing body of a sandboxed document should not be allowed");
60
  }
61
  
62
  // 2) test that we can access an iframe sandboxed with "allow-same-origin"
63
  var if_2 = document.getElementById("if_2");
64
  
65
  try {
66
    var b = if_2.contentDocument.body;
67
    ok_wrapper(true, "accessing body of a sandboxed document with allow-same-origin should be allowed");
68
  } catch (err) {
69
    ok_wrapper(false, "accessing body of a sandboxed document with allow-same-origin should be allowed");
70
  }
71
  
72
  // 3) test that a sandboxed iframe without 'allow-same-origin' cannot access its parent
73
  // this is done by file_iframe_b_if3.html which has 'allow-scripts' but not 'allow-same-origin'
74
  
75
  // 4) test that a sandboxed iframe with 'allow-same-origin' can access its parent
76
  // this is done by file_iframe_b_if2.html which has 'allow-same-origin' and 'allow-scripts'
77
 
78
  // 5) check that a sandboxed iframe with "allow-same-origin" can access document.cookie
79
  // this is done by file_iframe_b_if2.html which has 'allow-same-origin' and 'allow-scripts'
80
 
81
  // 6) check that a sandboxed iframe with "allow-same-origin" can access window.localStorage
82
  // this is done by file_iframe_b_if2.html which has 'allow-same-origin' and 'allow-scripts'
83
84
  // 7) check that a sandboxed iframe with "allow-same-origin" can access window.sessionStorage
85
  // this is done by file_iframe_b_if2.html which has 'allow-same-origin' and 'allow-scripts'
86
 
87
  // 8) check that a sandboxed iframe WITHOUT "allow-same-origin" can NOT access document.cookie
88
  // this is done by file_iframe_b_if3.html which has 'allow-scripts' but not 'allow-same-origin'
89
  
90
  // 9) check that a sandboxed iframe WITHOUT "allow-same-origin" can NOT access window.localStorage
91
  // this is done by file_iframe_b_if3.html which has 'allow-scripts' but not 'allow-same-origin'
92
  
93
  // 10) check that a sandboxed iframe WITHOUT "allow-same-origin" can NOT access window.sessionStorage
94
  // this is done by file_iframe_b_if3.html which has 'allow-scripts' but not 'allow-same-origin'
95
  
96
  // 11) check that XHR works normally in a sandboxed iframe with "allow-same-origin" and "allow-scripts"
97
  // this is done by file_iframe_b_if2.html which has 'allow-same-origin' and 'allow-scripts'
98
  
99
  // 12) check that XHR is blocked in a sandboxed iframe with "allow-scripts" but WITHOUT "allow-same-origin" 
100
  // this is done by file_iframe_b_if3.html which has 'allow-scripts' but not 'allow-same-origin'
101
}
102
</script>
103
<body onload="doTest()">
104
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
105
<p id="display"></p>
106
<div id="content">
107
<iframe sandbox="" id="if_1" src="file_iframe_sandbox_b_if1.html" height="10" width="10"></iframe>
108
<iframe sandbox="allow-same-origin allow-scripts" id="if_2" src="file_iframe_sandbox_b_if2.html" height="10" width="10"></iframe>
109
<iframe sandbox="allow-scripts" id="if_3" src="file_iframe_sandbox_b_if3.html" height="10" width="10"></iframe>
110
</div>

Return to bug 341604