Bug 1042840 - Remove evalInWindow. r=gabor

This commit is contained in:
Bobby Holley 2014-07-28 11:19:25 -07:00
parent 2df92a8d49
commit 6aa5f09145
5 changed files with 4 additions and 174 deletions

View File

@ -122,7 +122,7 @@ interface ScheduledGCCallback : nsISupports
/**
* interface of Components.utils
*/
[scriptable, uuid(1a6c4db7-4b90-4919-9cd8-161ac14a7189)]
[scriptable, uuid(10bcc595-ccca-4ead-aae3-d12b47cd9fff)]
interface nsIXPCComponents_Utils : nsISupports
{
@ -381,16 +381,6 @@ interface nsIXPCComponents_Utils : nsISupports
[implicit_jscontext]
boolean isProxy(in jsval vobject);
/*
* Similar to evalInSandbox except this one is used to eval a script in the
* scope of a window. Also note, that the return value and the possible exceptions
* in the script are structured cloned, unless they are natives (then they are just
* wrapped).
* Principal of the caller must subsume the target's.
*/
[implicit_jscontext]
jsval evalInWindow(in AString source, in jsval window);
/*
* To be called from JS only.
*

View File

@ -373,116 +373,6 @@ ExportFunction(JSContext *cx, HandleValue vfunction, HandleValue vscope, HandleV
return true;
}
static bool
GetFilenameAndLineNumber(JSContext *cx, nsACString &filename, unsigned &lineno)
{
JS::AutoFilename scriptFilename;
if (JS::DescribeScriptedCaller(cx, &scriptFilename, &lineno)) {
if (const char *cfilename = scriptFilename.get()) {
filename.Assign(nsDependentCString(cfilename));
return true;
}
}
return false;
}
bool
EvalInWindow(JSContext *cx, const nsAString &source, HandleObject scope, MutableHandleValue rval)
{
// If we cannot unwrap we must not eval in it.
RootedObject targetScope(cx, CheckedUnwrap(scope));
if (!targetScope) {
JS_ReportError(cx, "Permission denied to eval in target scope");
return false;
}
// Make sure that we have a window object.
RootedObject inner(cx, CheckedUnwrap(targetScope, /* stopAtOuter = */ false));
nsCOMPtr<nsIGlobalObject> global;
nsCOMPtr<nsPIDOMWindow> window;
if (!JS_IsGlobalObject(inner) ||
!(global = GetNativeForGlobal(inner)) ||
!(window = do_QueryInterface(global)))
{
JS_ReportError(cx, "Second argument must be a window");
return false;
}
nsCOMPtr<nsIScriptContext> context =
(static_cast<nsGlobalWindow*>(window.get()))->GetScriptContext();
if (!context) {
JS_ReportError(cx, "Script context needed");
return false;
}
nsCString filename;
unsigned lineNo;
if (!GetFilenameAndLineNumber(cx, filename, lineNo)) {
// Default values for non-scripted callers.
filename.AssignLiteral("Unknown");
lineNo = 0;
}
RootedObject cxGlobal(cx, JS::CurrentGlobalOrNull(cx));
{
// CompileOptions must be created from the context
// we will execute this script in.
JSContext *wndCx = context->GetNativeContext();
AutoCxPusher pusher(wndCx);
JS::CompileOptions compileOptions(wndCx);
compileOptions.setFileAndLine(filename.get(), lineNo);
// We don't want the JS engine to automatically report
// uncaught exceptions.
nsJSUtils::EvaluateOptions evaluateOptions;
evaluateOptions.setReportUncaught(false);
nsresult rv = nsJSUtils::EvaluateString(wndCx,
source,
targetScope,
compileOptions,
evaluateOptions,
rval);
if (NS_FAILED(rv)) {
// If there was an exception we get it as a return value, if
// the evaluation failed for some other reason, then a default
// exception is raised.
MOZ_ASSERT(!JS_IsExceptionPending(wndCx),
"Exception should be delivered as return value.");
if (rval.isUndefined()) {
MOZ_ASSERT(rv == NS_ERROR_OUT_OF_MEMORY);
return false;
}
// If there was an exception thrown we should set it
// on the calling context.
RootedValue exn(wndCx, rval);
// First we should reset the return value.
rval.set(UndefinedValue());
// Then clone the exception.
JSAutoCompartment ac(wndCx, cxGlobal);
StackScopedCloneOptions cloneOptions;
cloneOptions.wrapReflectors = true;
if (StackScopedClone(wndCx, cloneOptions, &exn))
js::SetPendingExceptionCrossContext(cx, exn);
return false;
}
}
// Let's clone the return value back to the callers compartment.
StackScopedCloneOptions cloneOptions;
cloneOptions.wrapReflectors = true;
if (!StackScopedClone(cx, cloneOptions, rval)) {
rval.set(UndefinedValue());
return false;
}
return true;
}
bool
CreateObjectIn(JSContext *cx, HandleValue vobj, CreateObjectInOptions &options,
MutableHandleValue rval)

View File

@ -255,37 +255,6 @@ SandboxExportFunction(JSContext *cx, unsigned argc, jsval *vp)
return ExportFunction(cx, args[0], args[1], options, args.rval());
}
/*
* Expected type of the arguments:
* value evalInWindow(string script,
* object window)
*/
static bool
SandboxEvalInWindow(JSContext *cx, unsigned argc, jsval *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() < 2) {
JS_ReportError(cx, "Function requires two arguments");
return false;
}
if (!args[0].isString() || !args[1].isObject()) {
JS_ReportError(cx, "Invalid arguments");
return false;
}
RootedString srcString(cx, args[0].toString());
RootedObject targetScope(cx, &args[1].toObject());
nsAutoJSString srcAutoString;
if (!srcAutoString.init(cx, srcString)) {
JS_ReportError(cx, "Source string is invalid");
return false;
}
return EvalInWindow(cx, srcAutoString, targetScope, args.rval());
}
static bool
SandboxCreateObjectIn(JSContext *cx, unsigned argc, jsval *vp)
{
@ -949,7 +918,6 @@ xpc::CreateSandboxObject(JSContext *cx, MutableHandleValue vp, nsISupports *prin
if (options.wantExportHelpers &&
(!JS_DefineFunction(cx, sandbox, "exportFunction", SandboxExportFunction, 3, 0) ||
!JS_DefineFunction(cx, sandbox, "evalInWindow", SandboxEvalInWindow, 2, 0) ||
!JS_DefineFunction(cx, sandbox, "createObjectIn", SandboxCreateObjectIn, 2, 0) ||
!JS_DefineFunction(cx, sandbox, "cloneInto", SandboxCloneInto, 3, 0) ||
!JS_DefineFunction(cx, sandbox, "isProxy", SandboxIsProxy, 1, 0)))

View File

@ -3008,20 +3008,6 @@ nsXPCComponents_Utils::IsProxy(HandleValue vobj, JSContext *cx, bool *rval)
return NS_OK;
}
/* jsval evalInWindow(in string source, in jsval window); */
NS_IMETHODIMP
nsXPCComponents_Utils::EvalInWindow(const nsAString &source, HandleValue window,
JSContext *cx, MutableHandleValue rval)
{
if (!window.isObject())
return NS_ERROR_INVALID_ARG;
RootedObject rwindow(cx, &window.toObject());
if (!xpc::EvalInWindow(cx, source, rwindow, rval))
return NS_ERROR_FAILURE;
return NS_OK;
}
/* jsval exportFunction(in jsval vfunction, in jsval vscope, in jsval voptions); */
NS_IMETHODIMP
nsXPCComponents_Utils::ExportFunction(HandleValue vfunction, HandleValue vscope,

View File

@ -25,10 +25,10 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=877673
sb.frame = frame;
sb.exceptionCb = exceptionCb;
if (exceptionCb) {
return Cu.evalInSandbox("try {evalInWindow('" + script + "',frame); ok(false, 'Exception should have been thrown.')} catch(e) {exceptionCb(e)}", sb);
return Cu.evalInSandbox("try {frame.eval('" + script + "'); ok(false, 'Exception should have been thrown.')} catch(e) {exceptionCb(e)}", sb);
}
return Cu.evalInSandbox("evalInWindow('" + script + "',frame)", sb);
return Cu.evalInSandbox("frame.eval('" + script + "')", sb);
}
function testSameOrigin(frame) {
@ -49,10 +49,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=877673
executeIn(frame.contentWindow, "throw 42", function(e){is(e, 42,
"Exception was thrown from script.")});
executeIn(frame.contentDocument, "var a = 42;", function(e){ok(e.toString().indexOf("Second argument must be a window") > -1,
"Passing non-window to evalInWindow should throw.");});
// evalInWindow is also available from Cu:
is(Cu.evalInWindow("document.str", frame.contentWindow), "foobar");
testDone();
}
@ -74,4 +70,4 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=877673
<iframe src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_empty.html"
onload="testCrossOrigin(this)">
</iframe>
</window>
</window>