Skip to content

Instantly share code, notes, and snippets.

@FND
Created February 3, 2011 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FND/809174 to your computer and use it in GitHub Desktop.
Save FND/809174 to your computer and use it in GitHub Desktop.
securely running untrusted code in Node.js
Usage:
$ node sandbox.js
based on LintServer by Jakob Kruse:
https://github.com/jkruse/LintServer
var fs = require("fs");
var vm;
try {
vm = require("vm");
} catch(exc) { // Node.js v0.2
vm = process.binding("evals").Script;
}
var filename = "untrusted.js";
var source = fs.readFileSync("./" + filename, "utf8");
var sandbox = {};
vm.runInNewContext(source, sandbox, filename);
var fs = require("fs");
var text = fs.readFileSync("./dispatch.js", "utf8");
console.log(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment