test_top_navigation_by_location.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=785310
  5. html5 sandboxed iframe should not be able to perform top navigation with scripts allowed
  6. -->
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Test for Bug 785310 - iframe sandbox top navigation by location tests</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. <script>
  13. SimpleTest.waitForExplicitFinish();
  14. var testWin;
  15. function runScriptNavigationTest(testCase) {
  16. window.onmessage = function(event) {
  17. if (event.data != "newTop") {
  18. ok(false, "event.data: got '" + event.data + "', expected 'newTop'");
  19. }
  20. ok(!testCase.shouldBeBlocked, testCase.desc, "top navigation was NOT blocked");
  21. runNextTest();
  22. };
  23. try {
  24. SpecialPowers.wrap(testWin[testCase.iframeName]).eval(testCase.script);
  25. } catch(e) {
  26. ok(testCase.shouldBeBlocked, testCase.desc, SpecialPowers.wrap(e).message);
  27. runNextTest();
  28. }
  29. }
  30. var testCaseIndex = -1;
  31. testCases = [
  32. {
  33. desc: "Test 1: top.location.replace should be blocked when sandboxed without allow-top-navigation",
  34. script: "top.location.replace('file_top_navigation_by_location.html')",
  35. iframeName: "if1",
  36. shouldBeBlocked: true
  37. },
  38. {
  39. desc: "Test 2: top.location.assign should be blocked when sandboxed without allow-top-navigation",
  40. script: "top.location.assign('file_top_navigation_by_location.html')",
  41. iframeName: "if1",
  42. shouldBeBlocked: true
  43. },
  44. {
  45. desc: "Test 3: top.location.href should be blocked when sandboxed without allow-top-navigation",
  46. script: "top.location.href = 'file_top_navigation_by_location.html'",
  47. iframeName: "if1",
  48. shouldBeBlocked: true
  49. },
  50. {
  51. desc: "Test 4: top.location.pathname should be blocked when sandboxed without allow-top-navigation",
  52. script: "top.location.pathname = top.location.pathname",
  53. iframeName: "if1",
  54. shouldBeBlocked: true
  55. },
  56. {
  57. desc: "Test 5: top.location should be blocked when sandboxed without allow-top-navigation",
  58. script: "top.location = 'file_top_navigation_by_location.html'",
  59. iframeName: "if1",
  60. shouldBeBlocked: true
  61. },
  62. {
  63. desc: "Test 6: top.location.hash should be blocked when sandboxed without allow-top-navigation",
  64. script: "top.location.hash = 'wibble'",
  65. iframeName: "if1",
  66. shouldBeBlocked: true
  67. },
  68. {
  69. desc: "Test 7: top.location.replace should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  70. script: "top.location.replace('file_top_navigation_by_location.html')",
  71. iframeName: "if2",
  72. shouldBeBlocked: false
  73. },
  74. {
  75. desc: "Test 8: top.location.assign should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  76. script: "top.location.assign('file_top_navigation_by_location.html')",
  77. iframeName: "if2",
  78. shouldBeBlocked: false
  79. },
  80. {
  81. desc: "Test 9: top.location.href should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  82. script: "top.location.href = 'file_top_navigation_by_location.html'",
  83. iframeName: "if2",
  84. shouldBeBlocked: false
  85. },
  86. {
  87. desc: "Test 10: top.location.pathname should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  88. script: "top.location.pathname = top.location.pathname",
  89. iframeName: "if2",
  90. shouldBeBlocked: false
  91. },
  92. {
  93. desc: "Test 11: top.location should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  94. script: "top.location = 'file_top_navigation_by_location.html'",
  95. iframeName: "if2",
  96. shouldBeBlocked: false
  97. },
  98. {
  99. desc: "Test 12: top.location.hash should NOT be blocked when sandboxed with allow-same-origin allow-top-navigation",
  100. script: "top.location.hash = 'wibble'",
  101. iframeName: "if2",
  102. shouldBeBlocked: false
  103. },
  104. {
  105. desc: "Test 13: top.location.replace should NOT be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  106. script: "top.location.replace('file_top_navigation_by_location.html')",
  107. iframeName: "if3",
  108. shouldBeBlocked: false
  109. },
  110. {
  111. desc: "Test 14: top.location.assign should be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  112. script: "top.location.assign('file_top_navigation_by_location.html')",
  113. iframeName: "if3",
  114. shouldBeBlocked: true
  115. },
  116. {
  117. desc: "Test 15: top.location.href should NOT be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  118. script: "top.location.href = 'file_top_navigation_by_location.html'",
  119. iframeName: "if3",
  120. shouldBeBlocked: false
  121. },
  122. {
  123. desc: "Test 16: top.location.pathname should be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  124. script: "top.location.pathname = top.location.pathname",
  125. iframeName: "if3",
  126. shouldBeBlocked: true
  127. },
  128. {
  129. desc: "Test 17: top.location should NOT be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  130. script: "top.location = 'file_top_navigation_by_location.html'",
  131. iframeName: "if3",
  132. shouldBeBlocked: false
  133. },
  134. {
  135. desc: "Test 18: top.location.hash should be blocked when sandboxed with allow-top-navigation, but without allow-same-origin",
  136. script: "top.location.hash = 'wibble'",
  137. iframeName: "if3",
  138. shouldBeBlocked: true
  139. }
  140. ];
  141. function runNextTest() {
  142. ++testCaseIndex;
  143. if (testCaseIndex == testCases.length) {
  144. testWin.close();
  145. SimpleTest.finish();
  146. return;
  147. }
  148. runScriptNavigationTest(testCases[testCaseIndex]);
  149. }
  150. window.onmessage = runNextTest;
  151. testWin = window.open("file_top_navigation_by_location.html", "newTop");
  152. </script>
  153. </head>
  154. <body>
  155. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=785310">Mozilla Bug 785310</a>
  156. <p id="display"></p>
  157. <div id="content">
  158. Tests for Bug 785310
  159. </div>
  160. </body>
  161. </html>