Fix for bug 604957 (Change sandbox constructor to take one optional argument instead of two). r=mrbkap, a=blocking.

This commit is contained in:
Peter Van der Beken 2010-10-15 11:02:42 -07:00
parent 5c799c5502
commit c66e34a29f
2 changed files with 95 additions and 33 deletions

View File

@ -3167,7 +3167,7 @@ NS_IMPL_THREADSAFE_RELEASE(nsXPCComponents_utils_Sandbox)
#ifndef XPCONNECT_STANDALONE
nsresult
xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSObject *proto,
bool bypassXray)
bool wantXrays)
{
// Create the sandbox global object
nsresult rv;
@ -3219,7 +3219,7 @@ xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSOb
JSObject *sandbox;
rv = xpc_CreateGlobalObject(cx, &SandboxClass, origin, principal,
!bypassXray, &sandbox, &compartment);
wantXrays, &sandbox, &compartment);
NS_ENSURE_SUCCESS(rv, rv);
js::AutoObjectRooter tvr(cx, sandbox);
@ -3234,7 +3234,7 @@ xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSOb
if (!ok)
return NS_ERROR_XPC_UNEXPECTED;
if (xpc::WrapperFactory::IsXrayWrapper(proto) && bypassXray) {
if (xpc::WrapperFactory::IsXrayWrapper(proto) && !wantXrays) {
jsval v;
if (!JS_GetProperty(cx, proto, "wrappedJSObject", &v))
return NS_ERROR_XPC_UNEXPECTED;
@ -3377,22 +3377,41 @@ nsXPCComponents_utils_Sandbox::CallOrConstruct(nsIXPConnectWrappedNative *wrappe
}
JSObject *proto = nsnull;
bool bypassXray = false;
bool wantXrays = true;
if (argc > 1) {
if (!JSVAL_IS_OBJECT(argv[1]))
return ThrowAndFail(NS_ERROR_INVALID_ARG, cx, _retval);
proto = JSVAL_TO_OBJECT(argv[1]);
JSObject *optionsObject = JSVAL_TO_OBJECT(argv[1]);
jsval option;
if (argc > 2) {
if (!JSVAL_IS_BOOLEAN(argv[2]))
JSBool found;
if (!JS_HasProperty(cx, optionsObject, "sandboxPrototype", &found))
return NS_ERROR_INVALID_ARG;
if (found) {
if (!JS_GetProperty(cx, optionsObject, "sandboxPrototype", &option) ||
!JSVAL_IS_OBJECT(option)) {
return ThrowAndFail(NS_ERROR_INVALID_ARG, cx, _retval);
}
bypassXray = JSVAL_TO_BOOLEAN(argv[2]);
proto = JSVAL_TO_OBJECT(option);
}
if (!JS_HasProperty(cx, optionsObject, "wantXrays", &found))
return NS_ERROR_INVALID_ARG;
if (found) {
if (!JS_GetProperty(cx, optionsObject, "wantXrays", &option) ||
!JSVAL_IS_BOOLEAN(option)) {
return ThrowAndFail(NS_ERROR_INVALID_ARG, cx, _retval);
}
wantXrays = JSVAL_TO_BOOLEAN(option);
}
}
rv = xpc_CreateSandboxObject(cx, vp, prinOrSop, proto, bypassXray);
rv = xpc_CreateSandboxObject(cx, vp, prinOrSop, proto, wantXrays);
if (NS_FAILED(rv)) {
return ThrowAndFail(rv, cx, _retval);

View File

@ -17,11 +17,11 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=533596
<iframe type="content"
src="http://example.org/tests/js/src/xpconnect/tests/mochitest/file_evalInSandbox.html"
onload="go(this, true)">
onload="checkCrossOrigin(this)">
</iframe>
<iframe type="content"
src="data:text/html,&lt;html&gt;&lt;body&gt;&lt;script&gt;document.foo %3D 'bar'%3B&lt;%2Fscript&gt;&lt;%2Fbody&gt;&lt;%2Fhtml&gt;"
onload="go(this, false)">
onload="checkSameOrigin(this)">
</iframe>
</body>
@ -32,7 +32,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=533596
const utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
var testsRun = 0;
function checkCrossOriginSandbox(sandbox)
{
is(utils.getClassName(sandbox),
@ -43,41 +42,85 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=533596
"Proxy",
"return value was rewrapped correctly");
}
function go(ifr, crossOrigin) {
function checkCrossOrigin(ifr) {
var win = ifr.contentWindow;
if (crossOrigin) {
var sandbox = new Cu.Sandbox(win, win, false);
var sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
checkCrossOriginSandbox(sandbox);
checkCrossOriginSandbox(sandbox);
ok(Cu.evalInSandbox("('wrappedJSObject' in this.document);", sandbox),
"wrappers inside eIS are XPCNativeWrappers");
ok(Cu.evalInSandbox("!('foo' in this.document);", sandbox),
"must not see expandos");
ok(Cu.evalInSandbox("('wrappedJSObject' in this.document);", sandbox),
"wrappers inside eIS are XPCNativeWrappers");
ok(Cu.evalInSandbox("!('foo' in this.document);", sandbox),
"must not see expandos");
sandbox = new Cu.Sandbox(win, win, true);
sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win } );
checkCrossOriginSandbox(sandbox);
checkCrossOriginSandbox(sandbox);
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"can see expandos");
}
else {
var sandbox = new Cu.Sandbox(win, win, false);
ok(Cu.evalInSandbox("('wrappedJSObject' in this.document);", sandbox),
"wrappers inside eIS are XPCNativeWrappers");
ok(Cu.evalInSandbox("!('foo' in this.document);", sandbox),
"must not see expandos");
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"must see expandos for a chrome sandbox");
sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
sandbox = new Cu.Sandbox(win, win, true);
checkCrossOriginSandbox(sandbox);
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"can see expandos");
}
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"can see expandos");
testDone();
}
function checkSameOrigin(ifr) {
var win = ifr.contentWindow;
var sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"must see expandos for a chrome sandbox");
sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win } );
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"must see expandos for a chrome sandbox");
sandbox =
new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
"can see expandos for a chrome sandbox");
testDone();
}
var testsRun = 0;
function testDone() {
if (++testsRun == 2)
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
try {
var sandbox = new Cu.Sandbox(win, { sandboxPrototype: undefined } );
ok(false, "undefined is not a valid prototype");
}
catch (e) {
ok(true, "undefined is not a valid prototype");
}
try {
var sandbox = new Cu.Sandbox(win, { wantXrays: undefined } );
ok(false, "undefined is not a valid value for wantXrays");
}
catch (e) {
ok(true, "undefined is not a valid value for wantXrays");
}
]]></script>
</window>