Skip to content

Commit

Permalink
Verify that the argument passed to vm.runInContext() is a context obj…
Browse files Browse the repository at this point in the history
…ect.

Fixes nodejs#558.
  • Loading branch information
bnoordhuis committed Jul 5, 2011
1 parent c0d3f1f commit 06c9a4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/node_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WrappedContext : ObjectWrap {

Persistent<Context> GetV8Context();
static Local<Object> NewInstance();
static bool InstanceOf(Handle<Value> value);

protected:

Expand Down Expand Up @@ -110,6 +111,11 @@ void WrappedContext::Initialize(Handle<Object> target) {
}


bool WrappedContext::InstanceOf(Handle<Value> value) {
return !value.IsEmpty() && constructor_template->HasInstance(value);
}


Handle<Value> WrappedContext::New(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -282,7 +288,9 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
}

const int sandbox_index = input_flag == compileCode ? 1 : 0;
if (context_flag == userContext && args.Length() < (sandbox_index + 1)) {
if (context_flag == userContext
&& !WrappedContext::InstanceOf(args[sandbox_index]))
{
return ThrowException(Exception::TypeError(
String::New("needs a 'context' argument.")));
}
Expand Down
13 changes: 12 additions & 1 deletion test/simple/test-script-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
var common = require('../common');
var assert = require('assert');

var Script = require('vm').Script;
var vm = require('vm');
var Script = vm.Script;
var script = new Script('"passed";');

common.debug('run in a new empty context');
Expand All @@ -44,3 +45,13 @@ assert.equal('lala', context.thing);

// Issue GH-227:
Script.runInNewContext('', null, 'some.js');

// GH-558, non-context argument segfaults / raises assertion
function isTypeError(o) {
return o instanceof TypeError;
}

[undefined, null, 0, 0.0, '', {}, []].forEach(function(e) {
assert.throws(function() { script.runInContext(e); }, isTypeError);
assert.throws(function() { vm.runInContext('', e); }, isTypeError);
});

0 comments on commit 06c9a4e

Please sign in to comment.