Attachment #616395: iframe sandbox test - workers for bug #341604

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

(-)a/content/html/content/test/Makefile.in (+3 lines)
Line     Link Here 
 Lines 296-311   _TEST_FILES = \ Link Here 
296
		file_fullscreen-svg-element.html \
296
		file_fullscreen-svg-element.html \
297
		test_li_attributes_reflection.html \
297
		test_li_attributes_reflection.html \
298
		test_ol_attributes_reflection.html \
298
		test_ol_attributes_reflection.html \
299
		test_bug651956.html \
299
		test_bug651956.html \
300
		test_bug694503.html \
300
		test_bug694503.html \
301
		test_object_plugin_nav.html \
301
		test_object_plugin_nav.html \
302
		test_bug742030.html \
302
		test_bug742030.html \
303
		test_bug742549.html \
303
		test_bug742549.html \
304
		test_iframe_sandbox_workers.html \
305
		file_iframe_sandbox_g_if1.html \
306
		file_iframe_sandbox_worker.js \
304
		$(NULL)
307
		$(NULL)
305
308
306
_BROWSER_TEST_FILES = \
309
_BROWSER_TEST_FILES = \
307
		browser_bug649778.js \
310
		browser_bug649778.js \
308
		file_bug649778.html \
311
		file_bug649778.html \
309
		file_bug649778.html^headers^ \
312
		file_bug649778.html^headers^ \
310
		$(NULL)
313
		$(NULL)
311
314
(-)a/content/html/content/test/file_iframe_sandbox_g_if1.html (+61 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
</head>
9
<script type="text/javascript">
10
  function ok(result, desc) {
11
    window.parent.postMessage({ok: result, desc: desc}, "*");
12
  }
13
14
  function doStuff() {
15
    // test data URI
16
17
    // self.onmessage = function(event) {
18
    //   self.postMessage('make it so');
19
    // };
20
    var data_url = "data:text/plain;charset=utf-8;base64,c2VsZi5vbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkgeyAgDQogICAgc2VsZi5wb3N0TWVzc2FnZSgnbWFrZSBpdCBzbycpOyAgDQp9Ow==";
21
    var worker_data = new Worker(data_url);
22
    worker_data.addEventListener('message', function(event) {
23
      ok(true, "a worker in a sandboxed document should be able to be loaded from a data: URI");
24
    }, false);
25
26
    worker_data.postMessage("engage!");
27
28
    // test a blob URI we created (will have the same null principal
29
    // as us
30
    var b = new Blob(["onmessage = function(event) { self.postMessage('make it so');};"]);
31
32
    var blobURL = URL.createObjectURL(b);
33
34
    var worker_blob = new Worker(blobURL);
35
36
    worker_blob.addEventListener('message', function(event) {
37
      ok(true, "a worker in a sandboxed document should be able to be loaded from a blob URI " +
38
         "created by that sandboxed document");
39
    }, false);
40
41
    worker_blob.postMessage("engage!");
42
43
    // test loading with relative url - this should fail since we are
44
    // sandboxed and have a null principal
45
    var worker_js = new Worker('file_iframe_sandbox_worker.js');
46
47
    worker_js.onerror = function(error) {
48
      ok(true, "a worker in a sandboxed document should not be able to load from a relative URI");
49
    }
50
51
    worker_js.addEventListener('message', function(event) {
52
      ok(false, "a worker in a sandboxed document should not be able to load from a relative URI");
53
    }, false);
54
55
    worker_js.postMessage('engage');
56
  }
57
</script>
58
<body onload='doStuff();'>
59
  I am sandboxed but with "allow-scripts"
60
</body>
61
</html>
(-)a/content/html/content/test/file_iframe_sandbox_worker.js (+3 lines)
Line     Link Here 
Line 0    Link Here 
1
self.onmessage = function(event) {
2
  self.postMessage('make it so');
3
};
(-)a/content/html/content/test/test_iframe_sandbox_workers.html (+80 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 - tests for workers
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
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
12
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
13
</head>
14
<script type="application/javascript">
15
/** Test for Bug 341604 - Implement HTML5 sandbox attribute for IFRAMEs - test for workers **/
16
17
// a postMessage handler that is used by sandboxed iframes without
18
// 'allow-same-origin' to communicate pass/fail back to this main page.
19
// it expects to be called with an object like {ok: true/false, desc:
20
// <description of the test> which it then forwards to ok()
21
window.addEventListener("message", receiveMessage, false);
22
23
function receiveMessage(event)
24
{
25
  ok_wrapper(event.data.ok, event.data.desc);
26
}
27
28
var completedTests = 0;
29
var passedTests = 0;
30
31
function ok_wrapper(result, desc) {
32
  ok(result, desc);
33
34
  completedTests++;
35
36
  if (result) {
37
    passedTests++;
38
  }
39
40
  // REVIEW : if one of these tests fails and this isn't called
41
  // this means this test will hang until the time out happens :(
42
  // advice on better ways to synchronize things appreciated !
43
  if (completedTests == 3) {
44
    ok(passedTests == 3, "There are 2 worker tests that should pass");
45
    SimpleTest.finish();
46
  }
47
}
48
49
function doTest() {
50
  SimpleTest.waitForExplicitFinish();
51
52
  // passes if good
53
  // 1) test that a worker in a sandboxed iframe with 'allow-scripts' can be loaded
54
  // from a data: URI
55
  // (done by file_iframe_sandbox_g_if1.html)
56
57
  // passes if good
58
  // 2) test that a worker in a sandboxed iframe with 'allow-scripts' can be loaded
59
  // from a blob URI created by the sandboxed document itself
60
  // (done by file_iframe_sandbox_g_if1.html)
61
62
  // passes if good
63
  // 3) test that a worker in a sandboxed iframe with 'allow-scripts' without
64
  // 'allow-same-origin' cannot load a script via a relative URI
65
  // (done by file_iframe_sandbox_g_if1.html)
66
67
  // TODO: implement this test when bug 722126 "Can't transfer File objects
68
  // with postMessage cross domain" is fixed
69
  // test that a worker in a sandboxed iframe with 'allow-scripts' cannot be loaded
70
  // from a blob URI not created by the sandboxed document itself
71
}
72
</script>
73
<body onload="doTest()">
74
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
75
<p id="display"></p>
76
<div id="content">
77
<iframe sandbox="allow-scripts" id="if_1" src="file_iframe_sandbox_g_if1.html" height="10" width="10"></iframe>
78
</div>
79
</body>
80
</html>

Return to bug 341604