Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants