diff --git a/lib/sandbox.js b/lib/sandbox.js index 62e7aba..b1a64d6 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -13,7 +13,8 @@ function Sandbox( options ) { // Any vars in da house? var timer , stdout = '' - , child = spawn( this.options.node, [this.options.shovel] ) + , args = this.options.api ? [this.options.shovel, this.options.api] : [this.options.shovel] + , child = spawn( this.options.node, args ) , output = function( data ) { if ( !!data ) stdout += data @@ -39,7 +40,7 @@ function Sandbox( options ) { // Options Sandbox.options = - { timeout: 500 + { timeout: 1500 , node: 'node' , shovel: path.join( __dirname, 'shovel.js' ) } diff --git a/lib/shovel.js b/lib/shovel.js index e5c5997..86ab8ad 100644 --- a/lib/shovel.js +++ b/lib/shovel.js @@ -7,7 +7,14 @@ var util = require( 'util' ) , console , sandbox , Script - , stdin; + , stdin + , apiContext = {}; + +// Optionally provide an API that the untrusted code +// will have access to. +if (process.argv[2]) { + apiContext = require(process.cwd() + '/' + process.argv[2].replace('.js', '')); +} if ( ! ( Script = process.binding( 'evals').NodeScript ) ) if ( ! ( Script = process.binding('evals').Script ) ) @@ -53,7 +60,7 @@ function getSafeRunner() { wat = 0 // Run code function run() { - var context = Script.createContext(); + var context = Script.createContext(apiContext); var safeRunner = Script.runInContext('('+getSafeRunner.toString()+')()', context); var result; try { @@ -76,10 +83,8 @@ function run() { result = e.name + ': ' + e.message; } - process.stdout.on( 'drain', function() { - process.exit(0) + process.on('exit', function() { + process.stdout.write( JSON.stringify( { result: util.inspect( result ), console: console } ) ); }); - - process.stdout.write( JSON.stringify( { result: util.inspect( result ), console: console } ) ); } diff --git a/package.json b/package.json index 6ce681f..356444f 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ , "directories" : { "lib" : "./lib" } , "engines" : [ "node >=0.5.0" ] , "devDependencies" : - { "should": ">=0.5.1" - } + { "should": ">=0.5.1"} , "repository" : { "type" : "git" , "url" : "https://gf3@github.com/gf3/sandbox.git"