Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Created August 6, 2020 04:24
Show Gist options
  • Save VitorLuizC/0596fce9762460927cbe08cbbec5b3c7 to your computer and use it in GitHub Desktop.
Save VitorLuizC/0596fce9762460927cbe08cbbec5b3c7 to your computer and use it in GitHub Desktop.
A way to execute unsafe code on Node.js.
'use strict';
const vm = require('vm');
const createContext = () => {
const context = Object.create(null);
return vm.createContext(context);
};
const execute = (code) => {
const script = vm.Script(code);
script.runInContext(createContext(), {
timeout: 2000, // 2s
breakOnSigint: false,
displayErrors: true,
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment