test_iframe_sandbox_inheritance.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=341604
  5. Implement HTML5 sandbox attribute for IFRAMEs - inheritance 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. /** Inheritance Tests **/
  16. SimpleTest.waitForExplicitFinish();
  17. SimpleTest.requestFlakyTimeout("untriaged");
  18. // A postMessage handler that is used by sandboxed iframes without
  19. // 'allow-same-origin' to communicate pass/fail back to this main page.
  20. // It expects to be called with an object like {ok: true/false, desc:
  21. // <description of the test> which it then forwards to ok().
  22. window.addEventListener("message", receiveMessage, false);
  23. function receiveMessage(event) {
  24. switch (event.data.type) {
  25. case "attempted":
  26. testAttempted();
  27. break;
  28. case "ok":
  29. ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
  30. break;
  31. default:
  32. // allow for old style message
  33. if (event.data.ok != undefined) {
  34. ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
  35. }
  36. }
  37. }
  38. var attemptedTests = 0;
  39. var passedTests = 0;
  40. var totalTestsToPass = 15;
  41. var totalTestsToAttempt = 19;
  42. function ok_wrapper(result, desc, addToAttempted = true) {
  43. ok(result, desc);
  44. if (result) {
  45. passedTests++;
  46. }
  47. if (addToAttempted) {
  48. testAttempted();
  49. }
  50. }
  51. // Added so that tests that don't register unless they fail,
  52. // can at least notify that they've attempted to run.
  53. function testAttempted() {
  54. attemptedTests++;
  55. if (attemptedTests == totalTestsToAttempt) {
  56. // Make sure all tests have had a chance to complete.
  57. setTimeout(function() {finish();}, 1000);
  58. }
  59. }
  60. var finishCalled = false;
  61. function finish() {
  62. if (!finishCalled) {
  63. finishCalled = true;
  64. is(passedTests, totalTestsToPass, "There are " + totalTestsToPass + " inheritance tests that should pass");
  65. SimpleTest.finish();
  66. }
  67. }
  68. function doTest() {
  69. // fails if bad
  70. // 1) an iframe with no sandbox attribute inside an iframe that has sandbox = ""
  71. // should not be able to execute scripts (cannot ever loosen permissions)
  72. // (done by file_iframe_sandbox_a_if2.html contained within file_iframe_sandbox_a_if1.html)
  73. testAttempted();
  74. // fails if bad
  75. // 2) an iframe with sandbox = "allow-scripts" inside an iframe that has sandbox = ""
  76. // should not be able to execute scripts (cannot ever loosen permissions)
  77. // (done by file_iframe_sandbox_a_if2.html contained within file_iframe_sandbox_a_if1.html)
  78. testAttempted();
  79. // passes if good and fails if bad
  80. // 3) an iframe with no sandbox attribute inside an iframe that has sandbox = "allow-scripts"
  81. // should not be same origin with the top window
  82. // (done by file_iframe_sandbox_a_if4.html contained within file_iframe_sandbox_a_if3.html)
  83. // passes if good and fails if bad
  84. // 4) an iframe with no sandbox attribute inside an iframe that has sandbox = "allow-scripts"
  85. // should not be same origin with its parent
  86. // (done by file_iframe_sandbox_a_if4.html contained within file_iframe_sandbox_a_if3.html)
  87. // passes if good
  88. // 5) an iframe with 'allow-same-origin' and 'allow-scripts' inside an iframe with 'allow-same-origin'
  89. // and 'allow-scripts' should be same origin with the top window
  90. // (done by file_iframe_sandbox_a_if6.html contained within file_iframe_sandbox_a_if5.html)
  91. // passes if good
  92. // 6) an iframe with 'allow-same-origin' and 'allow-scripts' inside an iframe with 'allow-same-origin'
  93. // and 'allow-scripts' should be same origin with its parent
  94. // (done by file_iframe_sandbox_a_if6.html contained within file_iframe_sandbox_a_if5.html)
  95. // passes if good
  96. // 7) an iframe with no sandbox attribute inside an iframe that has sandbox = "allow-scripts"
  97. // should be able to execute scripts
  98. // (done by file_iframe_sandbox_a_if7.html contained within file_iframe_sandbox_a_if3.html)
  99. // fails if bad
  100. // 8) an iframe with sandbox="" inside an iframe that has allow-scripts should not be able
  101. // to execute scripts
  102. // (done by file_iframe_sandbox_a_if2.html contained within file_iframe_sandbox_a_if3.html)
  103. testAttempted();
  104. // passes if good
  105. // 9) make sure that changing the sandbox flags on an iframe (if_8) doesn't affect
  106. // the sandboxing of subloads of content within that iframe
  107. var if_8 = document.getElementById('if_8');
  108. if_8.sandbox = 'allow-scripts';
  109. if_8.contentWindow.doSubload();
  110. // passes if good
  111. // 10) a <frame> inside an <iframe> sandboxed with 'allow-scripts' should not be same
  112. // origin with this document
  113. // done by file_iframe_sandbox_a_if11.html which is contained with file_iframe_sandbox_a_if10.html
  114. // passes if good
  115. // 11) a <frame> inside a <frame> inside an <iframe> sandboxed with 'allow-scripts' should not be same
  116. // origin with its parent frame or this document
  117. // done by file_iframe_sandbox_a_if12.html which is contained with file_iframe_sandbox_a_if11.html
  118. // passes if good, fails if bad
  119. // 12) An <object> inside an <iframe> sandboxed with 'allow-scripts' should not be same
  120. // origin with this document
  121. // Done by file_iframe_sandbox_a_if14.html which is contained within file_iframe_sandbox_a_if13.html
  122. // passes if good, fails if bad
  123. // 13) An <object> inside an <object> inside an <iframe> sandboxed with 'allow-scripts' should not be same
  124. // origin with its parent frame or this document
  125. // Done by file_iframe_sandbox_a_if15.html which is contained within file_iframe_sandbox_a_if14.html
  126. // passes if good, fails if bad
  127. // 14) An <object> inside a <frame> inside an <iframe> sandboxed with 'allow-scripts' should not be same
  128. // origin with its parent frame or this document
  129. // Done by file_iframe_sandbox_a_if15.html which is contained within file_iframe_sandbox_a_if16.html
  130. // which is contained within file_iframe_sandbox_a_if10.html
  131. // passes if good
  132. // 15) An <object> inside an <object> inside an <iframe> sandboxed with 'allow-scripts allow-forms'
  133. // should be able to submit forms.
  134. // Done by file_iframe_sandbox_a_if15.html which is contained within file_iframe_sandbox_a_if14.html
  135. // passes if good
  136. // 16) An <object> inside a <frame> inside an <iframe> sandboxed with 'allow-scripts allow-forms'
  137. // should be able to submit forms.
  138. // Done by file_iframe_sandbox_a_if15.html which is contained within file_iframe_sandbox_a_if16.html
  139. // which is contained within file_iframe_sandbox_a_if10.html
  140. // fails if bad
  141. // 17) An <object> inside an <iframe> sandboxed with 'allow-same-origin'
  142. // should not be able to run scripts.
  143. // Done by iframe "if_no_scripts" using a data: load.
  144. testAttempted();
  145. // passes if good
  146. // 18) An <object> inside an <iframe> sandboxed with 'allow-scripts allow-same-origin'
  147. // should be able to run scripts and be same origin with this document.
  148. // Done by iframe "if_scripts" using a data: load.
  149. // passes if good, fails if bad
  150. // 19) Make sure that the parent's document's sandboxing flags are copied when
  151. // changing the sandbox flags on an iframe inside an iframe.
  152. // Done in file_iframe_sandbox_a_if17.html and file_iframe_sandbox_a_if18.html
  153. }
  154. addLoadEvent(doTest);
  155. </script>
  156. <body>
  157. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
  158. <p id="display"></p>
  159. <div id="content">
  160. <iframe sandbox="" id="if_1" src="file_iframe_sandbox_a_if1.html" height="10" width="10"></iframe>
  161. <iframe sandbox="allow-scripts" id="if_3" src="file_iframe_sandbox_a_if3.html" height="10" width="10"></iframe>
  162. <iframe sandbox="allow-scripts allow-same-origin" id="if_5" src="file_iframe_sandbox_a_if5.html" height="10" width="10"></iframe>
  163. <iframe sandbox="allow-scripts allow-same-origin" id="if_8" src="file_iframe_sandbox_a_if8.html" height="10" width="10"></iframe>
  164. <iframe sandbox="allow-scripts allow-forms" id="if_10" src="file_iframe_sandbox_a_if10.html" height="10" width="10"></iframe>
  165. <iframe sandbox="allow-scripts allow-forms" id="if_13" src="file_iframe_sandbox_a_if13.html" height="10" width="10"></iframe>
  166. <iframe sandbox="allow-same-origin" id="if_no_scripts" src="data:text/html,<object%20data='data:text/html,<script>parent.parent.ok_wrapper(false, &quot;an object inside an iframe sandboxed with only allow-same-origin should not be able to run scripts&quot;)</script>'></object>" height="10" width="10"></iframe>
  167. <iframe sandbox="allow-scripts allow-same-origin" id="if_scripts" src="data:text/html,<object%20data='data:text/html,<script>parent.parent.ok_wrapper(true, &quot;an object inside an iframe sandboxed with allow-scripts allow-same-origin should be able to run scripts and call functions in the parent of the iframe&quot;)</script>'></object>" height="10" width="10"></iframe>
  168. <iframe sandbox="allow-same-origin" id="if_19" src="data:text/html,<iframe%20data='data:text/html,<script>parent.parent.ok_wrapper(true, &quot;an object inside an iframe sandboxed with allow-scripts allow-same-origin should be able to run scripts and call functions in the parent of the iframe&quot;)</script>'></object>" height="10" width="10"></iframe>
  169. <iframe sandbox="allow-scripts" id="if_17" src="file_iframe_sandbox_a_if17.html" height="10" width="10"></iframe>
  170. </div>
  171. </body>
  172. </html>