fabricemarcelin fabricemarcelin - 1 year ago 219
Javascript Question

Nodejs in sandbox

I'm using nodejs as a middle-man between a client browser and the server to handle all the requests. I'm trying to use nodejs as a filter tool and highlight (if not) all malicious scripts. But I realize that nodejs let the script to run with the current environment privilege. So, I decide to run it in a new context by installing sandbox (

npm install sandbox
git clone git://github.com/gf3/sandbox.git
However when I run node I have this following error:

TypeError: Cannot call method
\'runInNewContext\' of undefined'

Any ideas anyone?

Answer Source

Have you looked at the inbuild sandbox abilities of node 0.4.6.

var localVar = 123,
    usingscript, evaled,
    vm = require('vm');

usingscript = vm.runInThisContext('localVar = 1;',
console.log('localVar: ' + localVar + ', usingscript: ' +
evaled = eval('localVar = 1;');
console.log('localVar: ' + localVar + ', evaled: ' +

// localVar: 123, usingscript: 1
// localVar: 1, evaled: 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download