Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

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

Open
PaTiToMaSteR opened this issue Dec 31, 2022 · 0 comments
Labels

Comments

@PaTiToMaSteR
Copy link

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants