I am trying to build an online compiler for various languages. Here is a snippet of what I am trying to do.
const util = require('util');
const exec = util.promisify(require('child_process').exec);
exec('node node_temp.js').then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
});
So in case of JavaScript(Node).The user will type in some code and send it via a POST request, then the code will be copied into node_temp.js
and then will be executed.
Now there is a chance that the user might use fs
or any other means to delete all the files in system. How do I prevent my exec()
function to modify any system files.
Some answers suggest using vm
or vm2
. I have used both, although it creates a new sandbox, the exec
command still gives access to the manipulate local files.