According to the node.js vm module docs:
Note that running untrusted code is a tricky business requiring great care. To prevent accidental global variable leakage,
vm.runInNewContext
is quite useful, but safely running untrusted code requires a separate process.
If I want to build an untrusted code runner (for example, an online node.js console), this would be my natural approach:
- Each session of untrusted code would spawn a new process using the
child_process
module, which could be killed after a certain amount of time - The child process would use runInNewContext to run the code
Would this be a safe approach? What would I have to watch out for?