Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/janl/sandbox.js
https://github.com/janl/sandbox.js
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/janl/sandbox.js
- Owner: janl
- Fork: true (KlausTrainer/sandbox.js)
- Created: 2013-09-21T12:50:26.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-09-21T13:12:49.000Z (over 11 years ago)
- Last Synced: 2024-04-07T01:27:50.740Z (9 months ago)
- Language: JavaScript
- Size: 87.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sandbox.js
This is a module that allows for executing functions within a sandbox in
Node.js. It provides a `runInSandbox` function that takes a function as
argument, as well as an optional context and an optional module whitelist. If
the context contains a `require` property and a whitelist is specified, the
`require` property will be replaced by a "secure" require function before the
specified function is executed in the sandbox. The "secure" require function
is a wrapper around the default require function provided by Node.js, and does
nothing more than either loading a module or throwing an error, based on the
whitelist. If no whitelist is specified, we default to an empty whitelist.## Usage
````javascript
var sandbox = require("./sandbox"),
whitelist = ["console"],
context = {require: require},
theAnswerFun = function() { return 42; },
consoleFun = function() { require("console").log("Hello World!"); },
httpFun = function() { return require("http").STATUS_CODES['200']; };sandbox.runInSandbox(theAnswerFun); // => 42
sandbox.runInSandbox(consoleFun); // => ReferenceError: require is not defined
sandbox.runInSandbox(consoleFun, context); // => Error: 'console' is not whitelisted
sandbox.runInSandbox(consoleFun, context, whitelist); // => Hello World!sandbox.runInSandbox(httpFun, context); // => 'OK'
sandbox.runInSandbox(httpFun, context, whitelist); // => Error: 'http' is not whitelisted
````## Running the Tests
````Shell
nodeunit test
````This requires nodeunit to be installed as a global package (`npm install
-g nodeunit`).