test_evalInSandbox.xul 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
  3. <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
  4. type="text/css"?>
  5. <!--
  6. https://bugzilla.mozilla.org/show_bug.cgi?id=533596
  7. -->
  8. <window title="Mozilla Bug 533596"
  9. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  10. <script type="application/javascript"
  11. src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  12. <!-- test results are displayed in the html:body -->
  13. <body xmlns="http://www.w3.org/1999/xhtml">
  14. <iframe src="http://example.org/tests/js/xpconnect/tests/mochitest/file_evalInSandbox.html"
  15. onload="checkCrossOrigin(this)">
  16. </iframe>
  17. <iframe src="chrome://mochitests/content/chrome/js/xpconnect/tests/chrome/file_evalInSandbox.html"
  18. onload="checkSameOrigin(this)">
  19. </iframe>
  20. </body>
  21. <!-- test code goes here -->
  22. <script type="application/javascript"><![CDATA[
  23. const Cu = Components.utils;
  24. const Ci = Components.interfaces;
  25. const utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
  26. .getInterface(Ci.nsIDOMWindowUtils);
  27. function checkCrossOriginSandbox(sandbox)
  28. {
  29. is(utils.getClassName(sandbox),
  30. "Proxy",
  31. "sandbox was wrapped correctly");
  32. is(utils.getClassName(Cu.evalInSandbox("this.document", sandbox)),
  33. "Proxy",
  34. "return value was rewrapped correctly");
  35. }
  36. function checkCrossOriginXrayedSandbox(sandbox)
  37. {
  38. ok(Cu.evalInSandbox("!('windowfoo' in window);", sandbox),
  39. "the window itself Xray is an XrayWrapper");
  40. ok(Cu.evalInSandbox("('wrappedJSObject' in this.document);", sandbox),
  41. "wrappers inside eIS are Xrays");
  42. ok(Cu.evalInSandbox("!('foo' in this.document);", sandbox),
  43. "must not see expandos");
  44. ok('wrappedJSObject' in Cu.evalInSandbox("this.document", sandbox),
  45. "wrappers returned from the sandbox are Xrays");
  46. ok(!("foo" in Cu.evalInSandbox("this.document", sandbox)),
  47. "must not see expandos in wrappers returned from the sandbox");
  48. ok('wrappedJSObject' in sandbox.document,
  49. "values obtained from the sandbox are Xrays");
  50. ok(!("foo" in sandbox.document),
  51. "must not see expandos in wrappers obtained from the sandbox");
  52. }
  53. function checkCrossOrigin(ifr) {
  54. var win = ifr.contentWindow;
  55. var sandbox =
  56. new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
  57. checkCrossOriginSandbox(sandbox);
  58. checkCrossOriginXrayedSandbox(sandbox);
  59. sandbox =
  60. new Cu.Sandbox(win, { sandboxPrototype: win } );
  61. checkCrossOriginSandbox(sandbox);
  62. checkCrossOriginXrayedSandbox(sandbox);
  63. sandbox =
  64. new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
  65. checkCrossOriginSandbox(sandbox);
  66. ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
  67. "can see expandos");
  68. ok(("foo" in Cu.evalInSandbox("this.document", sandbox)),
  69. "must see expandos in wrappers returned from the sandbox");
  70. ok(("foo" in sandbox.document),
  71. "must see expandos in wrappers obtained from the sandbox");
  72. testDone();
  73. }
  74. function checkSameOrigin(ifr) {
  75. var win = ifr.contentWindow;
  76. var sandbox =
  77. new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
  78. ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
  79. "must see expandos for a chrome sandbox");
  80. sandbox =
  81. new Cu.Sandbox(win, { sandboxPrototype: win } );
  82. ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
  83. "must see expandos for a chrome sandbox");
  84. sandbox =
  85. new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
  86. ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
  87. "can see expandos for a chrome sandbox");
  88. testDone();
  89. }
  90. var testsRun = 0;
  91. function testDone() {
  92. if (++testsRun == 2)
  93. SimpleTest.finish();
  94. }
  95. SimpleTest.waitForExplicitFinish();
  96. try {
  97. var sandbox = new Cu.Sandbox(this, { sandboxPrototype: undefined } );
  98. ok(false, "undefined is not a valid prototype");
  99. }
  100. catch (e) {
  101. ok(true, "undefined is not a valid prototype");
  102. }
  103. try {
  104. var sandbox = new Cu.Sandbox(this, { wantXrays: undefined } );
  105. ok(false, "undefined is not a valid value for wantXrays");
  106. }
  107. catch (e) {
  108. ok(true, "undefined is not a valid value for wantXrays");
  109. }
  110. // Crash test for bug 601829.
  111. try {
  112. Components.utils.evalInSandbox('', null);
  113. } catch (e) {
  114. ok(true, "didn't crash on a null sandbox object");
  115. }
  116. try {
  117. var sandbox = new Cu.Sandbox(this, { sameZoneAs: this } );
  118. ok(true, "sameZoneAs works");
  119. }
  120. catch (e) {
  121. ok(false, "sameZoneAs works");
  122. }
  123. // The 'let' keyword only appears with JS 1.7 and above. We use this fact
  124. // to make sure that sandboxes get explict JS versions and don't inherit
  125. // them from the most recent scripted frame.
  126. function checkExplicitVersions() {
  127. const Cu = Components.utils;
  128. var sb = new Cu.Sandbox(sop);
  129. Cu.evalInSandbox('let someVariable = 42', sb, '1.7');
  130. ok(true, "Didn't throw with let");
  131. try {
  132. Cu.evalInSandbox('let someVariable = 42', sb);
  133. ok(false, "Should have thrown with let");
  134. } catch (e) {
  135. ok(true, "Threw with let: " + e);
  136. }
  137. try {
  138. Cu.evalInSandbox('let someVariable = 42', sb, '1.5');
  139. ok(false, "Should have thrown with let");
  140. } catch (e) {
  141. ok(true, "Threw with let: " + e);
  142. }
  143. }
  144. var outerSB = new Cu.Sandbox(this);
  145. Cu.evalInSandbox(checkExplicitVersions.toSource(), outerSB, '1.7');
  146. outerSB.ok = ok;
  147. outerSB.sop = this;
  148. Cu.evalInSandbox('checkExplicitVersions();', outerSB);
  149. Cu.import("resource://gre/modules/jsdebugger.jsm");
  150. addDebuggerToGlobal(this);
  151. try {
  152. let dbg = new Debugger();
  153. let sandbox = new Cu.Sandbox(this, { invisibleToDebugger: false });
  154. dbg.addDebuggee(sandbox);
  155. ok(true, "debugger added visible value");
  156. } catch(e) {
  157. ok(false, "debugger could not add visible value");
  158. }
  159. try {
  160. let dbg = new Debugger();
  161. let sandbox = new Cu.Sandbox(this, { invisibleToDebugger: true });
  162. dbg.addDebuggee(sandbox);
  163. ok(false, "debugger added invisible value");
  164. } catch(e) {
  165. ok(true, "debugger did not add invisible value");
  166. }
  167. ]]></script>
  168. </window>