test_iframe_sandbox_popups_inheritance.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=766282
  5. Implement HTML5 sandbox allow-popuos directive for IFRAMEs - inheritance tests
  6. -->
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Tests for Bug 766282</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. SimpleTest.expectAssertions(0, 5);
  15. SimpleTest.waitForExplicitFinish();
  16. SimpleTest.requestFlakyTimeout("untriaged");
  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. window.addEventListener("message", receiveMessage, false);
  20. function receiveMessage(event) {
  21. switch (event.data.type) {
  22. case "attempted":
  23. testAttempted();
  24. break;
  25. case "ok":
  26. ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
  27. break;
  28. default:
  29. // allow for old style message
  30. if (event.data.ok != undefined) {
  31. ok_wrapper(event.data.ok, event.data.desc, event.data.addToAttempted);
  32. }
  33. }
  34. }
  35. var iframesWithWindowsToClose = new Array();
  36. var attemptedTests = 0;
  37. var passedTests = 0;
  38. var totalTestsToPass = 15;
  39. var totalTestsToAttempt = 21;
  40. function ok_wrapper(result, desc, addToAttempted = true) {
  41. ok(result, desc);
  42. if (result) {
  43. passedTests++;
  44. }
  45. if (addToAttempted) {
  46. testAttempted();
  47. }
  48. }
  49. // Added so that tests that don't register unless they fail,
  50. // can at least notify that they've attempted to run.
  51. function testAttempted() {
  52. attemptedTests++;
  53. if (attemptedTests == totalTestsToAttempt) {
  54. // Make sure all tests have had a chance to complete.
  55. setTimeout(function() {finish();}, 1000);
  56. }
  57. }
  58. var finishCalled = false;
  59. function finish() {
  60. if (!finishCalled) {
  61. finishCalled = true;
  62. is(passedTests, totalTestsToPass, "There are " + totalTestsToPass + " inheritance tests that should pass");
  63. closeWindows();
  64. SimpleTest.finish();
  65. }
  66. }
  67. function checkTestsFinished() {
  68. // If our own finish() has not been called, probably failed due to a timeout, so close remaining windows.
  69. if (!finishCalled) {
  70. closeWindows();
  71. }
  72. }
  73. function closeWindows() {
  74. for (var i = 0; i < iframesWithWindowsToClose.length; i++) {
  75. document.getElementById(iframesWithWindowsToClose[i]).contentWindow.postMessage({type: "closeWindows"}, "*");
  76. }
  77. }
  78. function doTest() {
  79. // passes if good and fails if bad
  80. // 1,2,3) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups
  81. // allow-same-origin" should not have its origin sandbox flag set and be able to access document.cookie.
  82. // (Done by file_iframe_sandbox_k_if5.html opened from file_iframe_sandbox_k_if4.html)
  83. // This is repeated for 3 different ways of opening the window,
  84. // see file_iframe_sandbox_k_if4.html for details.
  85. // passes if good
  86. // 4,5,6) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups
  87. // allow-top-navigation" should not have its top-level navigation sandbox flag set and be able to
  88. // navigate top. (Done by file_iframe_sandbox_k_if5.html (and if6) opened from
  89. // file_iframe_sandbox_k_if4.html). This is repeated for 3 different ways of opening the window,
  90. // see file_iframe_sandbox_k_if4.html for details.
  91. // passes if good
  92. // 7,8,9) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups
  93. // all-forms" should not have its forms sandbox flag set and be able to submit forms.
  94. // (Done by file_iframe_sandbox_k_if7.html opened from file_iframe_sandbox_k_if4.html)
  95. // This is repeated for 3 different ways of opening the window,
  96. // see file_iframe_sandbox_k_if4.html for details.
  97. // passes if good
  98. // 10,11,12) Make sure that the sandbox flags copied to a new browsing context are taken from the
  99. // current active document not the browsing context (iframe / docShell).
  100. // This is done by removing allow-same-origin and calling doSubOpens from file_iframe_sandbox_k_if8.html,
  101. // which opens file_iframe_sandbox_k_if9.html in 3 different ways.
  102. // It then navigates to file_iframe_sandbox_k_if1.html to run tests 13 - 21 below.
  103. var if_8_1 = document.getElementById('if_8_1');
  104. if_8_1.sandbox = 'allow-scripts allow-popups';
  105. if_8_1.contentWindow.doSubOpens();
  106. // passes if good and fails if bad
  107. // 13,14,15) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups"
  108. // should have its origin sandbox flag set and not be able to access document.cookie.
  109. // This is done by file_iframe_sandbox_k_if8.html navigating to file_iframe_sandbox_k_if1.html
  110. // after allow-same-origin has been removed from iframe if_8_1. file_iframe_sandbox_k_if1.html
  111. // opens file_iframe_sandbox_k_if2.html in 3 different ways to perform the tests.
  112. iframesWithWindowsToClose.push("if_8_1");
  113. // fails if bad
  114. // 16,17,18) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups"
  115. // should have its forms sandbox flag set and not be able to submit forms.
  116. // This is done by file_iframe_sandbox_k_if2.html, see test 10 for details of how this is opened.
  117. // fails if bad
  118. // 19,20,21) A window opened from inside an iframe that has sandbox = "allow-scripts allow-popups"
  119. // should have its top-level navigation sandbox flag set and not be able to navigate top.
  120. // This is done by file_iframe_sandbox_k_if2.html, see test 10 for details of how this is opened.
  121. }
  122. addLoadEvent(doTest);
  123. </script>
  124. <body onunload="checkTestsFinished()">
  125. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=766282">Mozilla Bug 766282</a> - Implement HTML5 sandbox allow-popups directive for IFRAMEs
  126. <p id="display"></p>
  127. <div id="content">
  128. <iframe sandbox="allow-scripts allow-popups allow-same-origin allow-forms allow-top-navigation" id="if_4" src="file_iframe_sandbox_k_if4.html" height="10" width="10"></iframe>
  129. <iframe sandbox="allow-scripts allow-popups allow-same-origin" id="if_8_1" src="file_iframe_sandbox_k_if8.html" height="10" width="10"></iframe>
  130. </div>
  131. </body>
  132. </html>