test_iframe_sandbox_general.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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>Tests for Bug 341604 and Bug 766282</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 - general tests **/
  16. SimpleTest.expectAssertions(0, 1);
  17. SimpleTest.waitForExplicitFinish();
  18. SimpleTest.requestCompleteLog();
  19. // a postMessage handler that is used by sandboxed iframes without
  20. // 'allow-same-origin' to communicate pass/fail back to this main page.
  21. // it expects to be called with an object like {ok: true/false, desc:
  22. // <description of the test> which it then forwards to ok()
  23. window.addEventListener("message", receiveMessage, false);
  24. function receiveMessage(event)
  25. {
  26. ok_wrapper(event.data.ok, event.data.desc);
  27. }
  28. var completedTests = 0;
  29. var passedTests = 0;
  30. function ok_wrapper(result, desc) {
  31. ok(result, desc);
  32. completedTests++;
  33. if (result) {
  34. passedTests++;
  35. }
  36. if (completedTests == 32) {
  37. is(passedTests, completedTests, "There are " + completedTests + " general tests that should pass");
  38. SimpleTest.finish();
  39. }
  40. }
  41. function doTest() {
  42. // passes twice if good
  43. // 1) test that inline scripts (<script>) can run in an iframe sandboxed with "allow-scripts"
  44. // (done in file_iframe_sandbox_c_if1.html which has 'allow-scripts')
  45. // passes twice if good
  46. // 2) test that <script src=...> can run in an iframe sandboxed with "allow-scripts"
  47. // (done in file_iframe_sandbox_c_if1.html which has 'allow-scripts')
  48. // passes twice if good
  49. // 3) test that script in an event listener (body onload) can run in an iframe sandboxed with "allow-scripts"
  50. // (done in file_iframe_sandbox_c_if1.html which has 'allow-scripts')
  51. // passes twice if good
  52. // 4) test that script in an javascript:url can run in an iframe sandboxed with "allow-scripts"
  53. // (done in file_iframe_sandbox_c_if1.html which has 'allow-scripts')
  54. // fails if bad
  55. // 5) test that inline scripts cannot run in an iframe sandboxed without "allow-scripts"
  56. // (done in file_iframe_sandbox_c_if2.html which has sandbox='')
  57. // fails if bad
  58. // 6) test that <script src=...> cannot run in an iframe sandboxed without "allow-scripts"
  59. // (done in file_iframe_sandbox_c_if2.html which has sandbox='')
  60. // fails if bad
  61. // 7) test that script in an event listener (body onload) cannot run in an iframe sandboxed without "allow-scripts"
  62. // (done in file_iframe_sandbox_c_if2.html which has sandbox='')
  63. // fails if bad
  64. // 8) test that script in an event listener (img onerror) cannot run in an iframe sandboxed without "allow-scripts"
  65. // (done in file_iframe_sandbox_c_if2.html which has sandbox='')
  66. // fails if bad
  67. // 9) test that script in an javascript:url cannot run in an iframe sandboxed without "allow-scripts"
  68. // (done in file_iframe_sandbox_c_if_5.html which has sandbox='allow-same-origin')
  69. var if_w = document.getElementById('if_5').contentWindow;
  70. sendMouseEvent({type:'click'}, 'a_link', if_w);
  71. // passes if good
  72. // 10) test that a new iframe has sandbox attribute
  73. var ifr = document.createElement("iframe");
  74. ok_wrapper("sandbox" in ifr, "a new iframe should have a sandbox attribute");
  75. // passes if good
  76. // 11) test that the sandbox attribute's default stringyfied value is an empty string
  77. ok_wrapper(ifr.sandbox.length === 0 && ifr.sandbox == "", "default sandbox attribute should be an empty string");
  78. // passes if good
  79. // 12) test that a sandboxed iframe with 'allow-forms' can submit forms
  80. // (done in file_iframe_sandbox_c_if3.html which has 'allow-forms' and 'allow-scripts')
  81. // fails if bad
  82. // 13) test that a sandboxed iframe without 'allow-forms' can NOT submit forms
  83. // (done in file_iframe_sandbox_c_if1.html which only has 'allow-scripts')
  84. // fails if bad
  85. // 14) test that a sandboxed iframe can't open a new window using the target.attribute
  86. // this is done via file_iframe_sandbox_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
  87. // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
  88. // function that calls window.parent.ok_wrapper
  89. // passes if good
  90. // 15) test that a sandboxed iframe can't open a new window using window.open
  91. // this is done via file_iframe_sandbox_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
  92. // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
  93. // function that calls window.parent.ok_wrapper
  94. // passes if good
  95. // 16) test that a sandboxed iframe can't open a new window using window.ShowModalDialog
  96. // this is done via file_iframe_sandbox_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
  97. // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
  98. // function that calls window.parent.ok_wrapper
  99. // passes twice if good
  100. // 17) test that a sandboxed iframe can access same-origin documents and run scripts when its sandbox attribute
  101. // is separated with two spaces
  102. // done via file_iframe_sandbox_c_if6.html which is sandboxed with " allow-scripts allow-same-origin "
  103. // passes twice if good
  104. // 18) test that a sandboxed iframe can access same-origin documents and run scripts when its sandbox attribute
  105. // is separated with tabs
  106. // done via file_iframe_sandbox_c_if6.html which is sandboxed with "&#x09;allow-scripts&#x09;allow-same-origin&#x09;"
  107. // passes twice if good
  108. // 19) test that a sandboxed iframe can access same-origin documents and run scripts when its sandbox attribute
  109. // is separated with line feeds
  110. // done via file_iframe_sandbox_c_if6.html which is sandboxed with "&#x0a;allow-scripts&#x0a;allow-same-origin&#x0a;"
  111. // passes twice if good
  112. // 20) test that a sandboxed iframe can access same-origin documents and run scripts when its sandbox attribute
  113. // is separated with form feeds
  114. // done via file_iframe_sandbox_c_if6.html which is sandboxed with "&#x0c;allow-scripts&#x0c;allow-same-origin&#x0c;"
  115. // passes twice if good
  116. // 21) test that a sandboxed iframe can access same-origin documents and run scripts when its sandbox attribute
  117. // is separated with carriage returns
  118. // done via file_iframe_sandbox_c_if6.html which is sandboxed with "&#x0d;allow-scripts&#x0d;allow-same-origin&#x0d;"
  119. // fails if bad
  120. // 22) test that an iframe with sandbox="" does NOT have script in a src attribute created by a javascript:
  121. // URL executed
  122. // done by this page, see if_7
  123. // passes if good
  124. // 23) test that an iframe with sandbox="allow-scripts" DOES have script in a src attribute created by a javascript:
  125. // URL executed
  126. // done by this page, see if_8
  127. // fails if bad
  128. // 24) test that an iframe with sandbox="", starting out with a document already loaded, does NOT have script in a newly
  129. // set src attribute created by a javascript: URL executed
  130. // done by this page, see if_9
  131. // passes if good
  132. // 25) test that an iframe with sandbox="allow-scripts", starting out with a document already loaded, DOES have script
  133. // in a newly set src attribute created by a javascript: URL executed
  134. // done by this page, see if_10
  135. // passes if good or fails if bad
  136. // 26) test that an sandboxed document without 'allow-same-origin' can NOT access indexedDB
  137. // done via file_iframe_sandbox_c_if7.html, which has sandbox='allow-scripts'
  138. // passes if good or fails if bad
  139. // 27) test that an sandboxed document with 'allow-same-origin' can access indexedDB
  140. // done via file_iframe_sandbox_c_if8.html, which has sandbox='allow-scripts allow-same-origin'
  141. // fails if bad
  142. // 28) Test that a sandboxed iframe can't open a new window using the target.attribute for a
  143. // non-existing browsing context (BC341604).
  144. // This is done via file_iframe_sandbox_c_if4.html which is sandboxed with "allow-scripts" and "allow-same-origin"
  145. // the window it attempts to open calls window.opener.ok(false, ...) and file_iframe_c_if4.html has an ok()
  146. // function that calls window.parent.ok_wrapper.
  147. // passes twice if good
  148. // 29-32) Test that sandboxFlagsAsString returns the set flags.
  149. // see if_14 and if_15
  150. // passes once if good
  151. // 33) Test that sandboxFlagsAsString returns null if iframe does not have sandbox flag set.
  152. // see if_16
  153. }
  154. addLoadEvent(doTest);
  155. var started_if_9 = false;
  156. var started_if_10 = false;
  157. function start_if_9() {
  158. if (started_if_9)
  159. return;
  160. started_if_9 = true;
  161. sendMouseEvent({type:'click'}, 'a_button');
  162. }
  163. function start_if_10() {
  164. if (started_if_10)
  165. return;
  166. started_if_10 = true;
  167. sendMouseEvent({type:'click'}, 'a_button2');
  168. }
  169. function do_if_9() {
  170. var if_9 = document.getElementById('if_9');
  171. if_9.src = 'javascript:"<html><script>window.parent.ok_wrapper(false, \'an iframe sandboxed without allow-scripts should not execute script in a javascript URL in a newly set src attribute\');<\/script><\/html>"';
  172. }
  173. function do_if_10() {
  174. var if_10 = document.getElementById('if_10');
  175. if_10.src = 'javascript:"<html><script>window.parent.ok_wrapper(true, \'an iframe sandboxed with allow-scripts should execute script in a javascript URL in a newly set src attribute\');<\/script><\/html>"';
  176. }
  177. function eqFlags(a, b) {
  178. // both a and b should be either null or have the array same flags
  179. if (a === null && b === null) { return true; }
  180. if (a === null || b === null) { return false; }
  181. if (a.length !== b.length) { return false; }
  182. var a_sorted = a.sort();
  183. var b_sorted = b.sort();
  184. for (var i in a_sorted) {
  185. if (a_sorted[i] !== b_sorted[i]) { return false; }
  186. }
  187. return true;
  188. }
  189. function getSandboxFlags(doc) {
  190. var flags = doc.sandboxFlagsAsString;
  191. if (flags === null) { return null; }
  192. return flags? flags.split(" "):[];
  193. }
  194. function test_sandboxFlagsAsString(name, expected) {
  195. var ifr = document.getElementById(name);
  196. try {
  197. var flags = getSandboxFlags(SpecialPowers.wrap(ifr).contentDocument);
  198. ok_wrapper(eqFlags(flags, expected), name + ' expected: "' + expected + '", got: "' + flags + '"');
  199. } catch (e) {
  200. ok_wrapper(false, name + ' expected "' + expected + ', but failed with ' + e);
  201. }
  202. }
  203. </script>
  204. <body>
  205. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=341604">Mozilla Bug 341604</a> - Implement HTML5 sandbox attribute for IFRAMEs
  206. <p id="display"></p>
  207. <div id="content">
  208. <iframe sandbox="allow-same-origin allow-scripts" id="if_1" src="file_iframe_sandbox_c_if1.html" height="10" width="10"></iframe>
  209. <iframe sandbox="aLlOw-SAME-oRiGin ALLOW-sCrIpTs" id="if_1_case_insensitive" src="file_iframe_sandbox_c_if1.html" height="10" width="10"></iframe>
  210. <iframe sandbox="" id="if_2" src="file_iframe_sandbox_c_if2.html" height="10" width="10"></iframe>
  211. <iframe sandbox="allow-forms allow-scripts" id="if_3" src="file_iframe_sandbox_c_if3.html" height="10" width="10"></iframe>
  212. <iframe sandbox="allow-same-origin allow-scripts" id="if_4" src="file_iframe_sandbox_c_if4.html" height="10" width="10"></iframe>
  213. <iframe sandbox="allow-same-origin" id="if_5" src="file_iframe_sandbox_c_if5.html" height="10" width="10"></iframe>
  214. <iframe sandbox=" allow-same-origin allow-scripts " id="if_6_a" src="file_iframe_sandbox_c_if6.html" height="10" width="10"></iframe>
  215. <iframe sandbox="&#x09;allow-same-origin&#x09;allow-scripts&#x09;" id="if_6_b" src="file_iframe_sandbox_c_if6.html" height="10" width="10"></iframe>
  216. <iframe sandbox="&#x0a;allow-same-origin&#x0a;allow-scripts&#x0a;" id="if_6_c" src="file_iframe_sandbox_c_if6.html" height="10" width="10"></iframe>
  217. <iframe sandbox="&#x0c;allow-same-origin&#x0c;allow-scripts&#x0c;" id="if_6_d" src="file_iframe_sandbox_c_if6.html" height="10" width="10"></iframe>
  218. <iframe sandbox="&#x0d;allow-same-origin&#x0d;allow-scripts&#x0d;" id="if_6_e" src="file_iframe_sandbox_c_if6.html" height="10" width="10"></iframe>
  219. <iframe sandbox="allow-same-origin" id='if_7' src="javascript:'<html><script>window.parent.ok_wrapper(false, \'an iframe sandboxed without allow-scripts should not execute script in a javascript URL in its src attribute\');<\/script><\/html>';" height="10" width="10"></iframe>
  220. <iframe sandbox="allow-same-origin allow-scripts" id='if_8' src="javascript:'<html><script>window.parent.ok_wrapper(true, \'an iframe sandboxed without allow-scripts should execute script in a javascript URL in its src attribute\');<\/script><\/html>';" height="10" width="10"></iframe>
  221. <iframe sandbox="allow-same-origin" onload='start_if_9()' id='if_9' src="about:blank" height="10" width="10"></iframe>
  222. <iframe sandbox="allow-same-origin allow-scripts" onload='start_if_10()' id='if_10' src="about:blank" height="10" width="10"></iframe>
  223. <iframe sandbox="allow-scripts" id='if_11' src="file_iframe_sandbox_c_if7.html" height="10" width="10"></iframe>
  224. <iframe sandbox="allow-same-origin allow-scripts" id='if_12' src="file_iframe_sandbox_c_if8.html" height="10" width="10"></iframe>
  225. <iframe sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-top-navigation " id='if_13' src="file_iframe_sandbox_c_if9.html" height="10" width="10" onload='test_sandboxFlagsAsString("if_13",["allow-forms", "allow-pointer-lock", "allow-popups", "allow-same-origin", "allow-scripts", "allow-top-navigation"])'></iframe>
  226. <iframe sandbox="&#x09;allow-same-origin&#x09;allow-scripts&#x09;" id="if_14" src="file_iframe_sandbox_c_if6.html" height="10" width="10" onload='test_sandboxFlagsAsString("if_14",["allow-same-origin","allow-scripts"])'></iframe>
  227. <iframe sandbox="" id="if_15" src="file_iframe_sandbox_c_if9.html" height="10" width="10" onload='test_sandboxFlagsAsString("if_15",[])'></iframe>
  228. <iframe id="if_16" src="file_iframe_sandbox_c_if9.html" height="10" width="10" onload='test_sandboxFlagsAsString("if_16",null)'></iframe>
  229. <input type='button' id="a_button" onclick='do_if_9()'>
  230. <input type='button' id="a_button2" onclick='do_if_10()'>
  231. </div>
  232. </body>
  233. </html>