patriksimek/vm2

[QUESTION] context: "sandbox" executing express, gives "Cannot GET resource"

PaTiToMaSteR opened this issue · 0 comments

Thanks for vm2, it's really incredible tool.

Expectations: sanbox a nodejs app allowing the app to only access its folder modules.
What's wrong: while trying to use the context "sandbox" the app seems to not work properly

const { NodeVM, VMScript } = require('vm2');
const vm = new NodeVM({
	console: 'inherit',
	nesting: false,
	argv: [],
	require: {
		context: "sandbox",
		builtin: ["*"],
		external: true,
		root: `${__dirname}/apps/example_full_nodejs`,
		// FileSystem to load files from 
		//fs: VMFileSystemInterface;
	},
	sandbox: {
		log: console.log,
		properties: {
			version: "0.1.0",
			PORT: "8080" 
		}
	}
});

const script = new VMScript(scriptToExec, sandboxPath);
console.log(vm.run(script));

example_full_nodejs has the typical node_modules, seems to load, however doesn't serve the requests. if I change it to "host" it works. I'm not sure if it's a bug.

Of course the alternative is wrap a express object (which works) however is a pity that this doesn't work. I hope it's just my config.

example_full_nodejs/index.js:

const express = require("express");
const app = express();
app.get("/", (req, res) =>
{
	res.send("Hello World");
});

app.get("/hello", (req, res) =>
{
	res.send("World");
});

app.listen(properties.PORT, () =>
{
	console.log(`Listening for requests on http://localhost:${properties.PORT}`);
});

Thanks a lot for your answers!