test_child_navigation_by_location.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 child 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 testHtml = "<script>function onNav() { parent.parent.postMessage('childIframe', '*'); } window.onload = onNav; window.onhashchange = onNav;<\/script>";
  15. var testDataUri = "data:text/html," + testHtml;
  16. function runScriptNavigationTest(testCase) {
  17. window.onmessage = function(event) {
  18. if (event.data != 'childIframe') {
  19. ok(false, "event.data: got '" + event.data + "', expected 'childIframe'");
  20. }
  21. ok(!testCase.shouldBeBlocked, testCase.desc, "child navigation was NOT blocked");
  22. runNextTest();
  23. };
  24. try {
  25. window["parentIframe"].eval(testCase.script);
  26. } catch(e) {
  27. ok(testCase.shouldBeBlocked, testCase.desc, e.message);
  28. runNextTest();
  29. }
  30. }
  31. var testCaseIndex = -1;
  32. testCases = [
  33. {
  34. desc: "Test 1: cross origin child location.replace should NOT be blocked",
  35. script: "window['crossOriginChildIframe'].location.replace(\"" + testDataUri + "\")",
  36. shouldBeBlocked: false
  37. },
  38. {
  39. desc: "Test 2: cross origin child location.assign should be blocked",
  40. script: "window['crossOriginChildIframe'].location.assign(\"" + testDataUri + "\")",
  41. shouldBeBlocked: true
  42. },
  43. {
  44. desc: "Test 3: same origin child location.assign should NOT be blocked",
  45. script: "window['sameOriginChildIframe'].location.assign(\"" + testDataUri + "\")",
  46. shouldBeBlocked: false
  47. },
  48. {
  49. desc: "Test 4: cross origin child location.href should NOT be blocked",
  50. script: "window['crossOriginChildIframe'].location.href = \"" + testDataUri + "\"",
  51. shouldBeBlocked: false
  52. },
  53. {
  54. desc: "Test 5: cross origin child location.hash should be blocked",
  55. script: "window['crossOriginChildIframe'].location.hash = 'wibble'",
  56. shouldBeBlocked: true
  57. },
  58. {
  59. desc: "Test 6: same origin child location.hash should NOT be blocked",
  60. script: "window['sameOriginChildIframe'].location.hash = 'wibble'",
  61. shouldBeBlocked: false
  62. }
  63. ];
  64. function runNextTest() {
  65. ++testCaseIndex;
  66. if (testCaseIndex == testCases.length) {
  67. SimpleTest.finish();
  68. return;
  69. }
  70. runScriptNavigationTest(testCases[testCaseIndex]);
  71. }
  72. addLoadEvent(runNextTest);
  73. </script>
  74. </head>
  75. <body>
  76. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=785310">Mozilla Bug 785310</a>
  77. <p id="display"></p>
  78. <div id="content">
  79. Tests for Bug 785310
  80. </div>
  81. <iframe name="parentIframe" sandbox="allow-scripts allow-same-origin" src="data:text/html,<iframe name='sameOriginChildIframe'></iframe><iframe name='crossOriginChildIframe' sandbox='allow-scripts'></iframe>"</iframe>
  82. </body>
  83. </html>