pitboss-ng
Version:
Run untrusted code in a seperate process using VM2 module. With timeout and memory limit management
45 lines (40 loc) • 1.09 kB
JavaScript
var Pitboss = require('../').Pitboss;
var untrustedCode = "if (typeof returnMe === 'undefined') {\n\
var returnMe = { theArray: [] };\n\
}\n\
var sortAsNumbers = function (a, b) {\n\
var aInt = parseInt(a, 10), bInt = parseInt(b, 10);\n\
if (aInt === bInt) {\n\
return 0;\n\
} else if (aInt < bInt) {\n\
return -1;\n\
}\n\
return 1;\n\
};\n\
console.log('Message from ' + os.platform() + ' OS VM: returnMe =', returnMe);\n\
\n\
returnMe.theArray.sort(sortAsNumbers);\n\
returnMe;\n\
";
var sandbox = new Pitboss(untrustedCode, {
memoryLimit: 64*1024,
timeout: 5*1000,
heartBeatTick: 500
});
var returnMe = {
theArray: ['-1', '-5', '10', '15', '-3', '0', '20', '21', '-7', '4']
};
sandbox.run({
context: {
returnMe: returnMe
},
libraries: ['console', 'os']
}, function callback (err, result) {
sandbox.kill();
if (err) {
console.error('An Error in sandbox happened:', err);
return;
}
console.log('Local variable is not sorted (touched):', returnMe.theArray);
console.log('Result is an array of numbers (sorted):', result.theArray);
});