vm2 is a Node.js sandbox that can run untrusted code with whitelisted Node's built-in modules.
Questions tagged [node-vm2]
25 questions
10
votes
1 answer
NodeJS VM2 proper way to access console when set to 'redirect'
I'm using the VM2 package to run user code. I'm trying to intercept console output and have set the NodeVM object's console property to 'redirect':
// Create a new sandbox VM for this request
const vm = new NodeVM( {
console:…

Greg
- 819
- 1
- 8
- 21
6
votes
1 answer
NodeJS vm use cases
I am confused about the use cases of the VM module in NodeJS.
After reading a little bit in the documentations about this module is just looks like a fancy way to do eval.
Anyone used it and lived to tell the tail about it use cases?

Amit Wagner
- 3,134
- 3
- 19
- 35
5
votes
2 answers
How to get the result of the code which is run by Node vm2
Recently, i have been trying to implement sandbox execution using the package vm2 which has be published by @Patrik Šimek
I am trying to run some js code, which i am considering it to be a custom Logic, i am storing this logic in a string…

Nishi Bangar
- 720
- 9
- 20
4
votes
1 answer
How to link the imported dependencies of module created by vm.SourceTextModule to it?
Let's say we are creating a module called app by constructing a new vm.SourceTextModule object:
const context = {
exports: {},
console, // custom console object
};
const sandbox = vm.createContext(context);
const app = new…

pospolitaki
- 313
- 2
- 9
4
votes
0 answers
Supplement for Vm2 js which can securely run untrusted code in languages other than Javascript
I am trying to implement a Node js web app, a simpler version of which is that users submit code files in multiple programming languages like C++, Python, Java, Js etc and the output produced,is shown to them. However, I am unable to find a way in…

Taimoor Ali
- 158
- 1
- 7
4
votes
0 answers
nodeJS make code run in isolated vm context Or make a specific global variable
In a project that I'm working on now, I need to have different context for each client that is connected to TCP server...
Code that I will run on VM in the isolated context
var sbox = { Client : require("./Client"), server: {clients:[]},…

Janso123
- 192
- 1
- 12
3
votes
1 answer
When use vm2 in worker_threads, is it possible to share a NodeVM instance between workers?
I am using worker_threads and vm2 to implement a serverless-like thing, but I cannot get a NodeVM instance in the main thread and then pass through workData(because of worker_threads's limitation), so I can only new NodeVM in a worker thread per…

Yuefei Ma
- 161
- 1
- 5
2
votes
0 answers
VM2 optional objects freezzing
VM2 out of the box provides a very strict contexts isolation. Including wrapping all the objects with proxies and making them freezed
Is it possible optionally disable such high restricted isolation in case just a simple context. isolation is…

sky master
- 21
- 2
2
votes
0 answers
VM2 Usage with Webpack
I've been having trouble using webpack with a typescript project that uses vm2.webpack --config webpack.config.js gives the following error:
ERROR in index.js from Terser
Invalid function parameter…

WeffJen
- 70
- 4
2
votes
1 answer
How to understand the following code to escape the vm2 sandbox in node.js
I try to understand the following code as much as possible, because understanding it helps me a lot
"use strict";
const {VM} = require('vm2');
const untrusted = `var process;
try{
Object.defineProperty(Buffer.from(""), "", {get set(){
…

xinyong peng
- 23
- 1
- 5
2
votes
1 answer
VM : setTimeout is not working in browser vm.runInNewContext
I am executing a JS script using vm module in browser like this with details below.
vm.runInNewContext(codeToEval, sandboxObject);
setTimeout, setInterval and other interval built in methods do not work, even if I expose them in sandboxObject…

ahmadalibaloch
- 5,851
- 2
- 50
- 59
2
votes
1 answer
Combine NodeJS Fibers + VM Sandbox
I want to run some untrusted code in Node that might look like this:
for (var i = 0; i < 5; i++){
green_led(1);
sleep(500);
green_led(0);
sleep(500);
}
Using Fibers, I got the synchronous behaviour working as expected:
var Fiber =…

Jodes
- 14,118
- 26
- 97
- 156
1
vote
1 answer
Running dynamically user typed functions within Nodejs application without server restart?
I am trying to look at a possibility of having users add their own code within a Node app running an express server.
Scenario -
User clicks on save of a form and wants to do self business validations for example. This can be used as middleware or…

Deepankar
- 138
- 1
- 14
1
vote
0 answers
How to disable/block Node from requiring Addons?
I'm using vm2 sandbox to only allow requiring some internal modules but how do you block requiring the Node Addons? The Addons might as well be doing the same things like disallowed modules. Do I strip it in /node/src/ or is there some other way? I…

tuckuns
- 11
- 2
1
vote
2 answers
In Node.js, how can I return a Promise from vm2?
I have some asynchronous JavaScript code, which I'd like to run using vm2 / NodeVM.
The code tests two functions: a user-submitted function evenAndOdd and a pre-defined function solution using Node's built-in assert library.
My question is, how can…

Bret Cameron
- 451
- 1
- 4
- 18