Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You Can Now Pass an API into the Sandbox #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +40,7 @@ function Sandbox( options ) {

// Options
Sandbox.options =
{ timeout: 500
{ timeout: 1500
, node: 'node'
, shovel: path.join( __dirname, 'shovel.js' )
}
Expand Down
17 changes: 11 additions & 6 deletions lib/shovel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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 {
Expand All @@ -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 } ) );
}

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]/gf3/sandbox.git"
Expand Down