Safely sandbox and execute user submitted JavaScript?

后端 未结 5 1609
别跟我提以往
别跟我提以往 2020-11-30 00:52

I would like to have the ability to let users submit arbitrary JavaScript code, which is then sent to a Node.JS server and safely executed before the output is sent back to

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 01:10

    One alternative would be to use http://github.com/patriksimek/vm2:

    $ npm install vm2
    

    then:

    const {VM} = require('vm2');
    const vm = new VM();
    
    vm.run(`1 + 1`);  // => 2
    

    as mentioned in comments of other answers.

    I don't know how secure it is, but it at least claims that it runs untrusted code securely (in its README). And I couldn't find any obvious security issues so far as solutions suggested in other answers here.

提交回复
热议问题