Bug 734891 - part 5: Adding optional XHR constructor to sandbox

This commit is contained in:
Gabor Krizsanits 2012-06-09 15:19:27 -07:00
parent 55c9f351c4
commit b0dae02358
4 changed files with 87 additions and 0 deletions

View File

@ -2945,6 +2945,25 @@ SandboxImport(JSContext *cx, unsigned argc, jsval *vp)
return JS_SetPropertyById(cx, thisobj, id, &argv[0]);
}
static JSBool
CreateXMLHttpRequest(JSContext *cx, unsigned argc, jsval *vp)
{
JSObject *global = JS_GetGlobalForScopeChain(cx);
MOZ_ASSERT(global);
nsCOMPtr<nsISupports> inst;
nsresult rv;
inst = do_CreateInstance("@mozilla.org/xmlextras/xmlhttprequest;1", &rv);
if (NS_FAILED(rv))
return false;
rv = nsContentUtils::WrapNative(cx, global, inst, vp);
if (NS_FAILED(rv))
return false;
return true;
}
static JSBool
sandbox_enumerate(JSContext *cx, JSHandleObject obj)
{
@ -3244,6 +3263,10 @@ xpc_CreateSandboxObject(JSContext *cx, jsval *vp, nsISupports *prinOrSop, Sandbo
if (!JS_DefineFunctions(cx, sandbox, SandboxFunctions))
return NS_ERROR_XPC_UNEXPECTED;
if (options.wantXHRConstructor &&
!JS_DefineFunction(cx, sandbox, "XMLHttpRequest", CreateXMLHttpRequest, 0, JSFUN_CONSTRUCTOR))
return NS_ERROR_XPC_UNEXPECTED;
}
if (vp) {
@ -3506,6 +3529,10 @@ ParseOptionsObject(JSContext *cx, jsval from, SandboxOptions &options)
rv = GetBoolPropFromOptions(cx, optionsObject,
"wantComponents", &options.wantComponents);
NS_ENSURE_SUCCESS(rv, rv);
rv = GetBoolPropFromOptions(cx, optionsObject,
"wantXHRConstructor", &options.wantXHRConstructor);
NS_ENSURE_SUCCESS(rv, rv);
rv = GetStringPropFromOptions(cx, optionsObject,
"sandboxName", options.sandboxName);

View File

@ -4330,11 +4330,13 @@ struct SandboxOptions {
SandboxOptions()
: wantXrays(true)
, wantComponents(true)
, wantXHRConstructor(false)
, proto(NULL)
{ }
bool wantXrays;
bool wantComponents;
bool wantXHRConstructor;
JSObject* proto;
nsCString sandboxName;
};

View File

@ -0,0 +1,57 @@
do_load_httpd_js();
var httpserver = new nsHttpServer();
var testpath = "/simple";
var httpbody = "<?xml version='1.0' ?><root>0123456789</root>";
var cu = Components.utils;
var sb = cu.Sandbox(["http://www.example.com",
"http://localhost:4444/simple"],
{wantXHRConstructor: true});
function createXHR(async)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:4444/simple", async);
return xhr;
}
function checkResults(xhr)
{
if (xhr.readyState != 4)
return false;
do_check_eq(xhr.status, 200);
do_check_eq(xhr.responseText, httpbody);
var root_node = xhr.responseXML.getElementsByTagName('root').item(0);
do_check_eq(root_node.firstChild.data, "0123456789");
return true;
}
function run_test()
{
httpserver.registerPathHandler(testpath, serverHandler);
httpserver.start(4444);
// Test sync XHR sending
cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb);
var res = cu.evalInSandbox('var sync = createXHR(); sync.send(null); sync', sb);
checkResults(res);
// Test async XHR sending
var async = cu.evalInSandbox('var async = createXHR(true); async', sb);
async.addEventListener("readystatechange", function(event) {
if (checkResults(async))
httpserver.stop(do_test_finished);
}, false);
async.send(null);
do_test_pending();
}
function serverHandler(metadata, response)
{
response.setHeader("Content-Type", "text/xml", false);
response.bodyOutputStream.write(httpbody, httpbody.length);
}

View File

@ -28,3 +28,4 @@ fail-if = os == "android"
[test_want_components.js]
[test_components.js]
[test_allowedDomains.js]
[test_allowedDomainsXHR.js]