sandbox 0.8.5 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/sandbox.mjs +3 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +27 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +46 -0
- package/package.json +32 -27
- package/.npmignore +0 -2
- package/README.md +0 -85
- package/UNLICENSE +0 -25
- package/example/example.js +0 -53
- package/lib/sandbox.js +0 -82
- package/lib/shovel.js +0 -88
- package/test/sandbox.js +0 -68
- package/tmp.js +0 -8
package/bin/sandbox.mjs
ADDED
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module is re-exported from `@vercel/sandbox-cli`.
|
|
3
|
+
*
|
|
4
|
+
* It provides a programmatic interface to embed the Vercel Sandbox CLI
|
|
5
|
+
* in a different CLI or application.
|
|
6
|
+
*
|
|
7
|
+
* @module sandbox/cli
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
11
|
+
export * from "@vercel/sandbox-cli";
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This module is re-exported from `@vercel/sandbox-cli`.
|
|
4
|
+
*
|
|
5
|
+
* It provides a programmatic interface to embed the Vercel Sandbox CLI
|
|
6
|
+
* in a different CLI or application.
|
|
7
|
+
*
|
|
8
|
+
* @module sandbox/cli
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
15
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
16
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
17
|
+
}
|
|
18
|
+
Object.defineProperty(o, k2, desc);
|
|
19
|
+
}) : (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
o[k2] = m[k];
|
|
22
|
+
}));
|
|
23
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
24
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
__exportStar(require("@vercel/sandbox-cli"), exports);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A programmatic interface to interact with Vercel Sandboxes.
|
|
3
|
+
*
|
|
4
|
+
* Using this package you can create, destroy, and run commands on Vercel Sandboxes.
|
|
5
|
+
*
|
|
6
|
+
* @see https://vercel.com/docs/vercel-sandbox
|
|
7
|
+
*
|
|
8
|
+
* @example Create a sandbox and run a command on the remote machine
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { Sandbox } from 'sandbox'
|
|
11
|
+
*
|
|
12
|
+
* const sandbox = await Sandbox.create();
|
|
13
|
+
* await sandbox.runCommand({
|
|
14
|
+
* cmd: 'echo',
|
|
15
|
+
* args: ['Hello, World!'],
|
|
16
|
+
* stdout: process.stdout,
|
|
17
|
+
* stderr: process.stderr,
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example Grab an existing sandbox by its ID:
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { Sandbox } from 'sandbox'
|
|
24
|
+
*
|
|
25
|
+
* const sandbox = await Sandbox.get({ sandboxId: "sbx_..." });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @packageDocumentation
|
|
29
|
+
*/
|
|
30
|
+
export * from "@vercel/sandbox";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* A programmatic interface to interact with Vercel Sandboxes.
|
|
4
|
+
*
|
|
5
|
+
* Using this package you can create, destroy, and run commands on Vercel Sandboxes.
|
|
6
|
+
*
|
|
7
|
+
* @see https://vercel.com/docs/vercel-sandbox
|
|
8
|
+
*
|
|
9
|
+
* @example Create a sandbox and run a command on the remote machine
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Sandbox } from 'sandbox'
|
|
12
|
+
*
|
|
13
|
+
* const sandbox = await Sandbox.create();
|
|
14
|
+
* await sandbox.runCommand({
|
|
15
|
+
* cmd: 'echo',
|
|
16
|
+
* args: ['Hello, World!'],
|
|
17
|
+
* stdout: process.stdout,
|
|
18
|
+
* stderr: process.stderr,
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Grab an existing sandbox by its ID:
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { Sandbox } from 'sandbox'
|
|
25
|
+
*
|
|
26
|
+
* const sandbox = await Sandbox.get({ sandboxId: "sbx_..." });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @packageDocumentation
|
|
30
|
+
*/
|
|
31
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
32
|
+
if (k2 === undefined) k2 = k;
|
|
33
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
34
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
35
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
36
|
+
}
|
|
37
|
+
Object.defineProperty(o, k2, desc);
|
|
38
|
+
}) : (function(o, m, k, k2) {
|
|
39
|
+
if (k2 === undefined) k2 = k;
|
|
40
|
+
o[k2] = m[k];
|
|
41
|
+
}));
|
|
42
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
43
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
__exportStar(require("@vercel/sandbox"), exports);
|
package/package.json
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sandbox",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"Dominic Tarr (http://cyber-hobo.blogspot.com)"
|
|
10
|
-
],
|
|
11
|
-
"version": "0.8.5",
|
|
12
|
-
"main": "./lib/sandbox",
|
|
13
|
-
"directories": {
|
|
14
|
-
"lib": "./lib"
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"sandbox": "./bin/sandbox.mjs"
|
|
15
9
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"default": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./cli": {
|
|
16
|
+
"default": "./dist/cli.js",
|
|
17
|
+
"types": "./dist/cli.d.ts"
|
|
18
|
+
}
|
|
22
19
|
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
20
|
+
"files": [
|
|
21
|
+
"README.md",
|
|
22
|
+
"dist",
|
|
23
|
+
"bin"
|
|
24
|
+
],
|
|
25
|
+
"private": false,
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@vercel/sandbox": "1.0.0-beta.0",
|
|
31
|
+
"@vercel/sandbox-cli": "1.0.0-beta.0"
|
|
26
32
|
},
|
|
27
33
|
"scripts": {
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"url": "http://github.com/gf3/sandbox/raw/master/UNLICENSE"
|
|
34
|
+
"clean": "rm -rf node_modules dist",
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"typedoc": "turbo run build && typedoc",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
33
38
|
}
|
|
34
|
-
}
|
|
39
|
+
}
|
package/.npmignore
DELETED
package/README.md
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# Node Sandbox
|
|
2
|
-
|
|
3
|
-
A nifty javascript sandbox for node.js.
|
|
4
|
-
|
|
5
|
-
## Some features
|
|
6
|
-
|
|
7
|
-
- Can be used to execute untrusted code.
|
|
8
|
-
- Support for timeouts (e.g. prevent infinite loops)
|
|
9
|
-
- Handles errors gracefully
|
|
10
|
-
- Restricted code (cannot access node.js methods)
|
|
11
|
-
- Supports `console.log` and `print` utility methods
|
|
12
|
-
|
|
13
|
-
## Example
|
|
14
|
-
|
|
15
|
-
Be sure to check out [example/example.js](https://github.com/gf3/sandbox/blob/master/example/example.js)
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
var s = new Sandbox()
|
|
19
|
-
s.run( '1 + 1 + " apples"', function( output ) {
|
|
20
|
-
// output.result == "2 apples"
|
|
21
|
-
})
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Documentation
|
|
25
|
-
|
|
26
|
-
Basic syntax: `sandbox_instance.run( code, hollaback )`
|
|
27
|
-
|
|
28
|
-
`code` is the string of Javascript to be executed.
|
|
29
|
-
|
|
30
|
-
`hollaback` is a function, and it's called with a single argument, `output`.
|
|
31
|
-
|
|
32
|
-
`output` is an object with two properties: `result` and `console`. The `result`
|
|
33
|
-
property is an inspected string of the return value of the code. The `console`
|
|
34
|
-
property is an array of all console output.
|
|
35
|
-
|
|
36
|
-
For example, given the following code:
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
function add( a, b ){
|
|
40
|
-
console.log( a )
|
|
41
|
-
console.log( b )
|
|
42
|
-
return a + b
|
|
43
|
-
}
|
|
44
|
-
add( 20, 22 )
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
The resulting output object is:
|
|
48
|
-
|
|
49
|
-
```javascript
|
|
50
|
-
{ result: "42"
|
|
51
|
-
, console: [ "20", "22" ]
|
|
52
|
-
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Installation & Running
|
|
56
|
-
|
|
57
|
-
Let's get it! The easiest way is through npm:
|
|
58
|
-
|
|
59
|
-
npm install sandbox
|
|
60
|
-
|
|
61
|
-
Or if you'd like to play with the code, see the examples, run the tests,
|
|
62
|
-
what-the-fuck-ever...
|
|
63
|
-
|
|
64
|
-
git clone git://github.com/gf3/sandbox.git
|
|
65
|
-
|
|
66
|
-
And run some examples:
|
|
67
|
-
|
|
68
|
-
node example/example.js
|
|
69
|
-
|
|
70
|
-
## Tests
|
|
71
|
-
|
|
72
|
-
To run the tests simply run the test file with node.
|
|
73
|
-
|
|
74
|
-
node test/sandbox_test.js
|
|
75
|
-
|
|
76
|
-
## License
|
|
77
|
-
|
|
78
|
-
Sandbox is [UNLICENSED](http://unlicense.org/).
|
|
79
|
-
|
|
80
|
-
## Author
|
|
81
|
-
|
|
82
|
-
- Written by [Gianni Chiappetta](http://github.com/gf3) – [gf3.ca](http://gf3.ca)
|
|
83
|
-
- Contributions by [Bradley Meck](https://github.com/bmeck)
|
|
84
|
-
- Contributions by [Dominic Tarr](http://github.com/dominictarr) – [cyber-hobo.blogspot.com](http://cyber-hobo.blogspot.com/)
|
|
85
|
-
|
package/UNLICENSE
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
This is free and unencumbered software released into the public domain.
|
|
2
|
-
|
|
3
|
-
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
-
distribute this software, either in source code form or as a compiled
|
|
5
|
-
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
-
means.
|
|
7
|
-
|
|
8
|
-
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
-
of this software dedicate any and all copyright interest in the
|
|
10
|
-
software to the public domain. We make this dedication for the benefit
|
|
11
|
-
of the public at large and to the detriment of our heirs and
|
|
12
|
-
successors. We intend this dedication to be an overt act of
|
|
13
|
-
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
-
software under copyright law.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
-
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
-
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
-
|
|
24
|
-
For more information, please refer to <http://unlicense.org/>
|
|
25
|
-
|
package/example/example.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
var Sandbox = require("../lib/sandbox")
|
|
2
|
-
, s = new Sandbox()
|
|
3
|
-
|
|
4
|
-
// Example 1 - Standard JS
|
|
5
|
-
s.run( "1 + 1", function( output ) {
|
|
6
|
-
console.log( "Example 1: " + output.result + "\n" )
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
// Example 2 - Something slightly more complex
|
|
10
|
-
s.run( "(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function( output ) {
|
|
11
|
-
console.log( "Example 2: " + output.result + "\n" )
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
// Example 3 - Syntax error
|
|
15
|
-
s.run( "lol)hai", function( output ) {
|
|
16
|
-
console.log( "Example 3: " + output.result + "\n" )
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Example 4 - Restricted code
|
|
20
|
-
s.run( "process.platform", function( output ) {
|
|
21
|
-
console.log( "Example 4: " + output.result + "\n" )
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
// Example 5 - Infinite loop
|
|
25
|
-
s.run( "while (true) {}", function( output ) {
|
|
26
|
-
console.log( "Example 5: " + output.result + "\n" )
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
// Example 6 - Caller Attack Failure
|
|
30
|
-
s.run( "(function foo() {return foo.caller.caller;})()", function( output ) {
|
|
31
|
-
console.log( "Example 6: " + output.result + "\n" )
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
// Example 7 - Argument Attack Failure
|
|
35
|
-
s.run( "(function foo() {return [].slice.call(foo.caller.arguments);})()", function( output ) {
|
|
36
|
-
console.log( "Example 7: " + output.result + "\n" )
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
// Example 8 - Type Coersion Attack Failure
|
|
40
|
-
s.run( "(function foo() {return {toJSON:function x(){return x.caller.caller.name}}})()", function( output ) {
|
|
41
|
-
console.log( "Example 8: " + output.result + "\n" )
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
// Example 9 - Global Attack Failure
|
|
45
|
-
s.run( "x=1;(function() {return this})().console.log.constructor('return this')()", function( output ) {
|
|
46
|
-
console.log( "Example 9: " + output.result + "\n" )
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
// Example 10 - Console Log
|
|
50
|
-
s.run( "var x = 5; console.log(x * x); x", function( output ) {
|
|
51
|
-
console.log( "Example 10: " + output.console + "\n" )
|
|
52
|
-
})
|
|
53
|
-
|
package/lib/sandbox.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
//-----------------------------------------------------------------------------
|
|
2
|
-
// Init
|
|
3
|
-
//-----------------------------------------------------------------------------
|
|
4
|
-
|
|
5
|
-
var fs = require('fs');
|
|
6
|
-
var path = require('path');
|
|
7
|
-
var spawn = require('child_process').spawn;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
//-----------------------------------------------------------------------------
|
|
11
|
-
// Constructor
|
|
12
|
-
//-----------------------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
function Sandbox(options) {
|
|
15
|
-
(this.options = options || {}).__proto__ = Sandbox.options;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//-----------------------------------------------------------------------------
|
|
20
|
-
// Instance Methods
|
|
21
|
-
//-----------------------------------------------------------------------------
|
|
22
|
-
|
|
23
|
-
Sandbox.prototype.run = function(code, hollaback) {
|
|
24
|
-
var timer;
|
|
25
|
-
var stdout = '';
|
|
26
|
-
var child = spawn(this.options.node, [this.options.shovel]);
|
|
27
|
-
var output = function(data) {
|
|
28
|
-
if (!!data)
|
|
29
|
-
stdout += data;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
if (typeof hollaback == 'undefined')
|
|
33
|
-
hollaback = console.log;
|
|
34
|
-
else
|
|
35
|
-
hollaback = hollaback.bind(this);
|
|
36
|
-
|
|
37
|
-
// Listen
|
|
38
|
-
child.stdout.on('data', output);
|
|
39
|
-
child.on('exit', function(code) {
|
|
40
|
-
clearTimeout(timer);
|
|
41
|
-
setImmediate(function(){
|
|
42
|
-
if (!code && !stdout)
|
|
43
|
-
hollaback({ result: 'Error', console: [] });
|
|
44
|
-
else
|
|
45
|
-
hollaback(JSON.parse(stdout));
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Go
|
|
50
|
-
child.stdin.write(code);
|
|
51
|
-
child.stdin.end();
|
|
52
|
-
timer = setTimeout(function() {
|
|
53
|
-
child.stdout.removeListener('output', output);
|
|
54
|
-
stdout = JSON.stringify({ result: 'TimeoutError', console: [] });
|
|
55
|
-
child.kill('SIGKILL');
|
|
56
|
-
}, this.options.timeout);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
//-----------------------------------------------------------------------------
|
|
61
|
-
// Class Properties
|
|
62
|
-
//-----------------------------------------------------------------------------
|
|
63
|
-
|
|
64
|
-
Sandbox.options = {
|
|
65
|
-
timeout: 500,
|
|
66
|
-
node: 'node',
|
|
67
|
-
shovel: path.join(__dirname, 'shovel.js')
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
fs.readFile(path.join(__dirname, '..', 'package.json'), function(err, data) {
|
|
71
|
-
if (err)
|
|
72
|
-
throw err;
|
|
73
|
-
else
|
|
74
|
-
Sandbox.info = JSON.parse(data);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
//-----------------------------------------------------------------------------
|
|
79
|
-
// Export
|
|
80
|
-
//-----------------------------------------------------------------------------
|
|
81
|
-
|
|
82
|
-
module.exports = Sandbox;
|
package/lib/shovel.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
//-----------------------------------------------------------------------------
|
|
2
|
-
// Init
|
|
3
|
-
//-----------------------------------------------------------------------------
|
|
4
|
-
|
|
5
|
-
var util = require('util')
|
|
6
|
-
var code;
|
|
7
|
-
var result;
|
|
8
|
-
var console;
|
|
9
|
-
var sandbox;
|
|
10
|
-
var Script;
|
|
11
|
-
var stdin;
|
|
12
|
-
|
|
13
|
-
if (!(Script = process.binding( 'evals').NodeScript))
|
|
14
|
-
if (!(Script = process.binding('evals').Script))
|
|
15
|
-
Script = require('vm');
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//-----------------------------------------------------------------------------
|
|
19
|
-
// Sandbox
|
|
20
|
-
//-----------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
// Get code
|
|
23
|
-
console = [];
|
|
24
|
-
code = '';
|
|
25
|
-
stdin = process.openStdin();
|
|
26
|
-
stdin.on('data', function(data) {
|
|
27
|
-
code += data;
|
|
28
|
-
});
|
|
29
|
-
stdin.on('end', run);
|
|
30
|
-
|
|
31
|
-
function getSafeRunner() {
|
|
32
|
-
var global = this;
|
|
33
|
-
// Keep it outside of strict mode
|
|
34
|
-
function UserScript(str) {
|
|
35
|
-
// We want a global scoped function that has implicit returns.
|
|
36
|
-
return Function('return eval('+JSON.stringify(str+'')+')');
|
|
37
|
-
}
|
|
38
|
-
// place with a closure that is not exposed thanks to strict mode
|
|
39
|
-
return function run(comm, src) {
|
|
40
|
-
// stop argument / caller attacks
|
|
41
|
-
"use strict";
|
|
42
|
-
var send = function send(event) {
|
|
43
|
-
"use strict";
|
|
44
|
-
//
|
|
45
|
-
// All comm must be serialized properly to avoid attacks, JSON or XJSON
|
|
46
|
-
//
|
|
47
|
-
comm.send(event, JSON.stringify([].slice.call(arguments,1)));
|
|
48
|
-
}
|
|
49
|
-
global.print = send.bind(global, 'stdout');
|
|
50
|
-
global.console = {};
|
|
51
|
-
global.process = { stdout: { write: send.bind(global, 'stdout') } };
|
|
52
|
-
var result = UserScript(src)();
|
|
53
|
-
send('end', result);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Run code
|
|
58
|
-
function run() {
|
|
59
|
-
var context = Script.createContext();
|
|
60
|
-
var safeRunner = Script.runInContext('('+getSafeRunner.toString()+')()', context);
|
|
61
|
-
var result;
|
|
62
|
-
try {
|
|
63
|
-
safeRunner({
|
|
64
|
-
send: function (event, value) {
|
|
65
|
-
"use strict";
|
|
66
|
-
|
|
67
|
-
switch (event) {
|
|
68
|
-
case 'stdout':
|
|
69
|
-
console.push(JSON.parse(value)[0]);
|
|
70
|
-
break;
|
|
71
|
-
case 'end':
|
|
72
|
-
result = JSON.parse(value)[0];
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}, code);
|
|
77
|
-
}
|
|
78
|
-
catch (e) {
|
|
79
|
-
result = e.name + ': ' + e.message;
|
|
80
|
-
// throw e;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
process.stdout.on('drain', function() {
|
|
84
|
-
process.exit(0);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
process.stdout.write(JSON.stringify({ result: util.inspect(result), console: console }));
|
|
88
|
-
}
|
package/test/sandbox.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
//-----------------------------------------------------------------------------
|
|
2
|
-
// Init
|
|
3
|
-
//-----------------------------------------------------------------------------
|
|
4
|
-
|
|
5
|
-
var should = require('should');
|
|
6
|
-
var Sandbox = require('../lib/sandbox');
|
|
7
|
-
var sb = new Sandbox();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
//-----------------------------------------------------------------------------
|
|
11
|
-
// Tests
|
|
12
|
-
//-----------------------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
describe('Sandbox', function() {
|
|
15
|
-
|
|
16
|
-
it('should execute basic javascript', function() {
|
|
17
|
-
sb.run('1 + 1', function(output) {
|
|
18
|
-
output.result.should.eql('2');
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should gracefully handle syntax errors', function() {
|
|
23
|
-
sb.run('hi )there', function(output) {
|
|
24
|
-
output.result.should.eql("'SyntaxError: Unexpected token )'");
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should effectively prevent code from accessing node', function() {
|
|
29
|
-
sb.run('process.platform', function(output) {
|
|
30
|
-
output.result.should.eql("'ReferenceError: process is not defined'");
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should effectively prevent code from circumventing the sandbox', function() {
|
|
35
|
-
sb.run("var sys=require('sys'); sys.puts('Up in your fridge')", function(output) {
|
|
36
|
-
output.result.should.eql("'ReferenceError: require is not defined'");
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should timeout on infinite loops', function() {
|
|
41
|
-
sb.run('while ( true ) {}', function(output) {
|
|
42
|
-
output.result.should.eql('TimeoutError');
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should allow console output via `console.log`', function() {
|
|
47
|
-
sb.run('console.log(7); 42', function(output) {
|
|
48
|
-
output.result.should.eql('42');
|
|
49
|
-
output.console[0].should.eql(7);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should allow console output via `print`', function() {
|
|
54
|
-
sb.run('print(7); 42', function(output) {
|
|
55
|
-
output.result.should.eql('42');
|
|
56
|
-
output.console[0].should.eql(7);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should maintain the order of sync. console output', function() {
|
|
61
|
-
sb.run('console.log("first"); console.log("second"); 42', function(output) {
|
|
62
|
-
output.result.should.eql('42');
|
|
63
|
-
output.console[0].should.eql('first');
|
|
64
|
-
output.console[1].should.eql('second');
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
});
|
package/tmp.js
DELETED