test_sameOriginPolicy.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=801576
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 801576</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=801576">Mozilla Bug 801576</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. </div>
  17. <pre id="test">
  18. <script type="application/javascript">
  19. /** Test for the same-origin policy. **/
  20. SimpleTest.waitForExplicitFinish();
  21. function check(obj, prop, allowed, write) {
  22. var accessed = false;
  23. try {
  24. if (write) {
  25. try {
  26. obj[prop] = 2;
  27. accessed = true;
  28. } catch (e) {}
  29. Object.defineProperty(obj, 'prop', {getter: function() {}, setter: null});
  30. }
  31. else
  32. obj[prop];
  33. accessed = true;
  34. } catch (e) {}
  35. is(accessed, allowed, prop + " is correctly (in)accessible for " + (write ? 'write' : 'read'));
  36. }
  37. var crossOriginReadableWindowProps = ['blur', 'close', 'closed', 'focus',
  38. 'frames', 'location', 'length',
  39. 'opener', 'parent', 'postMessage',
  40. 'self', 'top', 'window',
  41. /* indexed and named accessors */
  42. '0', 'subframe'];
  43. function isCrossOriginReadable(obj, prop) {
  44. if (obj == "Window")
  45. return crossOriginReadableWindowProps.indexOf(prop) != -1;
  46. if (obj == "Location")
  47. return prop == 'replace';
  48. return false;
  49. }
  50. function isCrossOriginWritable(obj, prop) {
  51. if (obj == "Window")
  52. return prop == 'location';
  53. if (obj == "Location")
  54. return prop == 'href';
  55. }
  56. // NB: we don't want to succeed with writes, so we only check them when it should be denied.
  57. function testAll(sameOrigin) {
  58. var win = document.getElementById('ifr').contentWindow;
  59. // Build a list of properties to check from the properties available on our
  60. // window.
  61. var props = [];
  62. for (var prop in window) { props.push(prop); }
  63. // On android, this appears to be on the window but not on the iframe. It's
  64. // not really relevant to this test, so just skip it.
  65. if (props.indexOf('crypto') != -1)
  66. props.splice(props.indexOf('crypto'), 1);
  67. // Add the named grand-child, since that won't appear on our window.
  68. props.push('subframe');
  69. for (var prop of props) {
  70. check(win, prop, sameOrigin || isCrossOriginReadable('Window', prop), /* write = */ false);
  71. if (!sameOrigin && !isCrossOriginWritable('Window', prop))
  72. check(win, prop, false, /* write = */ true);
  73. }
  74. for (var prop in window.location) {
  75. check(win.location, prop, sameOrigin || isCrossOriginReadable('Location', prop));
  76. if (!sameOrigin && !isCrossOriginWritable('Location', prop))
  77. check(win.location, prop, false, /* write = */ true);
  78. }
  79. }
  80. var loadCount = 0;
  81. function go() {
  82. ++loadCount;
  83. if (loadCount == 1) {
  84. testAll(true);
  85. document.getElementById('ifr').contentWindow.location = 'http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html';
  86. }
  87. else {
  88. is(loadCount, 2);
  89. testAll(false);
  90. SimpleTest.finish();
  91. }
  92. }
  93. </script>
  94. </pre>
  95. <iframe id="ifr" onload="go();" src="file_empty.html"></iframe>
  96. </body>
  97. </html>