telebot
Version:
The easy way to write Telegram bots.
27 lines (25 loc) • 770 B
JavaScript
var vm = require('vm')
module.exports = function safeEval (code, context, opts) {
var sandbox = {}
var resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000)
sandbox[resultKey] = {}
var clearContext = `
(function(){
Function = undefined;
const keys = Object.getOwnPropertyNames(this).concat(['constructor']);
keys.forEach((key) => {
const item = this[key];
if(!item || typeof item.constructor !== 'function') return;
this[key].constructor = undefined;
});
})();
`
code = clearContext + resultKey + '=' + code
if (context) {
Object.keys(context).forEach(function (key) {
sandbox[key] = context[key]
})
}
vm.runInNewContext(code, sandbox, opts)
return sandbox[resultKey]
}