2 <?xml-stylesheet href=
"chrome://global/skin" type=
"text/css"?>
3 <?xml-stylesheet href=
"chrome://mochikit/content/tests/SimpleTest/test.css"
6 https://bugzilla.mozilla.org/show_bug.cgi?id=533596
8 <window title=
"Mozilla Bug 533596"
9 xmlns=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
10 <script 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">
15 <iframe src=
"http://example.org/tests/js/xpconnect/tests/mochitest/file_evalInSandbox.html"
16 onload=
"checkCrossOrigin(this)">
18 <iframe src=
"chrome://mochitests/content/chrome/js/xpconnect/tests/chrome/file_evalInSandbox.html"
19 onload=
"checkSameOrigin(this)">
23 <!-- test code goes here -->
24 <script type=
"application/javascript"><![CDATA[
25 const utils = window.windowUtils;
27 function checkCrossOriginSandbox(sandbox)
29 is(utils.getClassName(sandbox),
31 "sandbox was wrapped correctly");
33 is(utils.getClassName(Cu.evalInSandbox(
"this.document", sandbox)),
35 "return value was rewrapped correctly");
38 function checkCrossOriginXrayedSandbox(sandbox)
40 ok(Cu.evalInSandbox(
"!('windowfoo' in window);", sandbox),
41 "the window itself Xray is an XrayWrapper");
42 ok(Cu.evalInSandbox(
"('wrappedJSObject' in this.document);", sandbox),
43 "wrappers inside eIS are Xrays");
44 ok(Cu.evalInSandbox(
"!('foo' in this.document);", sandbox),
45 "must not see expandos");
46 ok('wrappedJSObject' in Cu.evalInSandbox(
"this.document", sandbox),
47 "wrappers returned from the sandbox are Xrays");
48 ok(!(
"foo" in Cu.evalInSandbox(
"this.document", sandbox)),
49 "must not see expandos in wrappers returned from the sandbox");
51 ok('wrappedJSObject' in sandbox.document,
52 "values obtained from the sandbox are Xrays");
53 ok(!(
"foo" in sandbox.document),
54 "must not see expandos in wrappers obtained from the sandbox");
58 function checkCrossOrigin(ifr) {
59 var win = ifr.contentWindow;
61 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
63 checkCrossOriginSandbox(sandbox);
64 checkCrossOriginXrayedSandbox(sandbox);
67 new Cu.Sandbox(win, { sandboxPrototype: win } );
69 checkCrossOriginSandbox(sandbox);
70 checkCrossOriginXrayedSandbox(sandbox);
73 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
75 checkCrossOriginSandbox(sandbox);
77 ok(Cu.evalInSandbox(
"('foo' in this.document);", sandbox),
79 ok(!(
"foo" in Cu.evalInSandbox(
"this.document", sandbox)),
80 "must not see expandos in wrappers returned from the sandbox");
81 ok((
"foo" in Cu.waiveXrays(Cu.evalInSandbox(
"this.document", sandbox))),
82 "must see expandos in waived wrappers returned from the sandbox");
84 ok(!(
"foo" in sandbox.document),
85 "must not see expandos in wrappers obtained from the sandbox");
86 ok(
"foo" in Cu.waiveXrays(sandbox.document),
87 "must see expandos in wrappers obtained from the sandbox");
92 function checkSameOrigin(ifr) {
93 var win = ifr.contentWindow;
95 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
97 ok(Cu.evalInSandbox(
"('foo' in this.document);", sandbox),
98 "must see expandos for a chrome sandbox");
101 new Cu.Sandbox(win, { sandboxPrototype: win } );
103 ok(Cu.evalInSandbox(
"('foo' in this.document);", sandbox),
104 "must see expandos for a chrome sandbox");
107 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
109 ok(Cu.evalInSandbox(
"('foo' in this.document);", sandbox),
110 "can see expandos for a chrome sandbox");
116 function testDone() {
121 SimpleTest.waitForExplicitFinish();
124 var sandbox1 = new Cu.Sandbox(this, { sandboxPrototype: undefined } );
125 ok(false,
"undefined is not a valid prototype");
128 ok(true,
"undefined is not a valid prototype");
132 var sandbox2 = new Cu.Sandbox(this, { wantXrays: undefined } );
133 ok(false,
"undefined is not a valid value for wantXrays");
136 ok(true,
"undefined is not a valid value for wantXrays");
139 // Crash test for bug
601829.
141 Cu.evalInSandbox('', null);
143 ok(true,
"didn't crash on a null sandbox object");
147 var sandbox3 = new Cu.Sandbox(this, { sameZoneAs: this } );
148 ok(true,
"sameZoneAs works");
151 ok(false,
"sameZoneAs works");
154 // The 'let' keyword only appears with JS
1.7 and above. We use this fact
155 // to make sure that sandboxes get explict JS versions and don't inherit
156 // them from the most recent scripted frame.
157 function checkExplicitVersions() {
158 // eslint-disable-next-line no-undef
159 var sb = new Cu.Sandbox(sop);
160 Cu.evalInSandbox('let someVariable =
42', sb, '
1.7');
161 ok(true,
"Didn't throw with let");
163 Cu.evalInSandbox('let someVariable =
42', sb);
164 ok(false,
"Should have thrown with let");
166 ok(true,
"Threw with let: " + e);
169 Cu.evalInSandbox('let someVariable =
42', sb, '
1.5');
170 ok(false,
"Should have thrown with let");
172 ok(true,
"Threw with let: " + e);
175 var outerSB = new Cu.Sandbox(this);
176 Cu.evalInSandbox(checkExplicitVersions.toSource(), outerSB, '
1.7');
179 Cu.evalInSandbox('checkExplicitVersions();', outerSB);
181 const {addDebuggerToGlobal} = ChromeUtils.importESModule(
"resource://gre/modules/jsdebugger.sys.mjs");
182 addDebuggerToGlobal(globalThis);
185 let dbg = new Debugger();
186 let sandbox = new Cu.Sandbox(this, {
187 invisibleToDebugger: false,
188 freshCompartment: true,
190 dbg.addDebuggee(sandbox);
191 ok(true,
"debugger added visible value");
193 ok(false,
"debugger could not add visible value");
197 let dbg = new Debugger();
198 let sandbox = new Cu.Sandbox(this, { invisibleToDebugger: true });
199 dbg.addDebuggee(sandbox);
200 ok(false,
"debugger added invisible value");
202 ok(true,
"debugger did not add invisible value");