@cuxt/sandboxjs 0.1.0
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/LICENSE +21 -0
- package/README.md +186 -0
- package/build/Sandbox.d.ts +25 -0
- package/build/Sandbox.js +62 -0
- package/build/SandboxExec.d.ts +66 -0
- package/build/SandboxExec.js +214 -0
- package/build/eval.d.ts +27 -0
- package/build/eval.js +205 -0
- package/build/executor.d.ts +124 -0
- package/build/executor.js +1546 -0
- package/build/parser.d.ts +154 -0
- package/build/parser.js +1527 -0
- package/build/unraw.d.ts +11 -0
- package/build/unraw.js +168 -0
- package/build/utils.d.ts +264 -0
- package/build/utils.js +362 -0
- package/dist/Sandbox.d.ts +25 -0
- package/dist/Sandbox.js +270 -0
- package/dist/Sandbox.js.map +1 -0
- package/dist/Sandbox.min.js +2 -0
- package/dist/Sandbox.min.js.map +1 -0
- package/dist/SandboxExec.d.ts +66 -0
- package/dist/SandboxExec.js +218 -0
- package/dist/SandboxExec.js.map +1 -0
- package/dist/SandboxExec.min.js +2 -0
- package/dist/SandboxExec.min.js.map +1 -0
- package/dist/eval.d.ts +27 -0
- package/dist/executor.d.ts +124 -0
- package/dist/executor.js +1550 -0
- package/dist/executor.js.map +1 -0
- package/dist/node/Sandbox.d.ts +25 -0
- package/dist/node/Sandbox.js +277 -0
- package/dist/node/SandboxExec.d.ts +66 -0
- package/dist/node/SandboxExec.js +225 -0
- package/dist/node/eval.d.ts +27 -0
- package/dist/node/executor.d.ts +124 -0
- package/dist/node/executor.js +1567 -0
- package/dist/node/parser.d.ts +154 -0
- package/dist/node/parser.js +1704 -0
- package/dist/node/unraw.d.ts +11 -0
- package/dist/node/utils.d.ts +264 -0
- package/dist/node/utils.js +385 -0
- package/dist/parser.d.ts +154 -0
- package/dist/parser.js +1690 -0
- package/dist/parser.js.map +1 -0
- package/dist/unraw.d.ts +11 -0
- package/dist/utils.d.ts +264 -0
- package/dist/utils.js +365 -0
- package/dist/utils.js.map +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 nyariv
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
[](https://github.com/nyariv/SandboxJS/blob/main/LICENSE)   [](https://github.com/nyariv/SandboxJS/issues) [](https://codecov.io/gh/nyariv/SandboxJS)
|
|
2
|
+
|
|
3
|
+
# SandboxJS - Safe eval runtime
|
|
4
|
+
|
|
5
|
+
This is a javascript sandboxing library. When embedding any kind of js code inside your app (either web or nodejs based) you are essentially giving access to the entire kingdom, hoping there is no malicious code in a dependency such as with supply chain attacks. For securing code, sandboxing is needed.
|
|
6
|
+
|
|
7
|
+
> a "sandbox" is a security mechanism for separating running programs, usually in an effort to mitigate system failures or software vulnerabilities from spreading. It is often used to execute untested or untrusted programs or code, possibly from unverified or untrusted third parties, suppliers, users or websites, without risking harm to the host machine or operating system. - Wikipedia
|
|
8
|
+
|
|
9
|
+
There are many vulnerable modules available on the global scope of a js environment, and unfortunately it is way to easy to get access to that if 3rd party code is allowed to be included. The main way is through the `eval` or `Function` globals because they execute code in the global context. Trying to block access to some global components through proxies or limiting scope variables is fruitless because of one main issue: _every function inherits the `Function` prototype, and can invoke `eval` by calling its constructor_, essentially making `eval` at most two properties away from anything in js.
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
```javascript
|
|
13
|
+
[].filter.constructor("alert('jailbreak')")()
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
To make matters worse, it is extremely difficult to blacklist functions because code is easily obfuscated. For example, it is possible to execute anything using only `(`, `)`, `[`, `]`, `!`, and `+`. ([source](http://www.jsfuck.com/))
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
[+!+[]]+[] // This evaluates to the number one, go a head type that in console
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**SandboxJS** solves this problem by parsing js code and executing it though its own js runtime, while in the process checking every single prototype function that is being called. This allows whitelisting anything and everything, regardless of obfuscation.
|
|
23
|
+
|
|
24
|
+
This means that you can potentially give different libraries different permissions, such as allowing `fetch()` for one library, or allowing access to the `Node` prototype for another, depending what the library requires and nothing more, and any objects that are gotten from the sandbox will remain sandboxed when used outside of it.
|
|
25
|
+
|
|
26
|
+
Additionaly, `eval` and `Function` are sandboxed as well, and can be used recursively safely, which is why they are considered safe globals in SandboxJS.
|
|
27
|
+
|
|
28
|
+
There is an `audit` method that will return all the accessed functions and prototypes during runtime if you need to know what permissions to give a certain library.
|
|
29
|
+
|
|
30
|
+
Since parsing and executing are separated, execution with SandboxJS can be sometimes even faster than `eval`, allowing to prepare the execution code ahead of time.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
npm install @nyariv/sandboxjs
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
The following is the bare minimum of code for using SandboxJS. This assumes safe whilelisted defaults.
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
const code = `return myTest;`;
|
|
44
|
+
const scope = { myTest: "hello world" };
|
|
45
|
+
const sandbox = new Sandbox();
|
|
46
|
+
const exec = sandbox.compile(code);
|
|
47
|
+
const result = exec(scope).run(); // result: "hello world"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
It is possible to defined multiple scopes in case you are reusing scopes with multiple layers.
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
const sandbox = new Sandbox();
|
|
54
|
+
|
|
55
|
+
const scopeA = {a: 1};
|
|
56
|
+
const scopeB = {b: 2};
|
|
57
|
+
const scopeC = {c: 3};
|
|
58
|
+
|
|
59
|
+
const code = `a = 4; let d = 5; let b = 6`;
|
|
60
|
+
const exec = sandbox.compile(code);
|
|
61
|
+
exec(scopeA, scopeB, scopeC).run();
|
|
62
|
+
|
|
63
|
+
console.log(scopeA); // {a: 4}
|
|
64
|
+
console.log(scopeB); // {b: 2}
|
|
65
|
+
console.log(scopeC); // {c: 3, d: 5, b: 6}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You can set your own whilelisted prototypes and global properties like so (`alert` and `Node` are added to whitelist in the following code):
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const prototypeWhitelist = Sandbox.SAFE_PROTOTYPES;
|
|
72
|
+
prototypeWhitelist.set(Node, new Set());
|
|
73
|
+
|
|
74
|
+
const globals = {...Sandbox.SAFE_GLOBALS, alert};
|
|
75
|
+
|
|
76
|
+
const sandbox = new Sandbox({globals, prototypeWhitelist});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
You can audit a piece of code, which will permit all globals and prototypes but will return a json with accessed globals and prototypes over time.
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
const code = `console.log("test")`;
|
|
83
|
+
console.log(Sandbox.audit(code));
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Safe Globals
|
|
87
|
+
|
|
88
|
+
- `globalThis`
|
|
89
|
+
- `Function`
|
|
90
|
+
- `eval`
|
|
91
|
+
- `setTimeout` - excluded by default
|
|
92
|
+
- `setInterval` - excluded by default
|
|
93
|
+
- `clearTimeout` - excluded by default
|
|
94
|
+
- `clearInterval` - excluded by default
|
|
95
|
+
- `console`
|
|
96
|
+
- `isFinite`
|
|
97
|
+
- `isNaN`
|
|
98
|
+
- `parseFloat`
|
|
99
|
+
- `parseInt`
|
|
100
|
+
- `decodeURI`
|
|
101
|
+
- `decodeURIComponent`
|
|
102
|
+
- `encodeURI`
|
|
103
|
+
- `encodeURIComponent`
|
|
104
|
+
- `escape`
|
|
105
|
+
- `unescape`
|
|
106
|
+
- `Boolean`
|
|
107
|
+
- `Number`
|
|
108
|
+
- `BigInt`
|
|
109
|
+
- `String`
|
|
110
|
+
- `Object`
|
|
111
|
+
- `Array`
|
|
112
|
+
- `Symbol`
|
|
113
|
+
- `Error`
|
|
114
|
+
- `EvalError`
|
|
115
|
+
- `RangeError`
|
|
116
|
+
- `ReferenceError`
|
|
117
|
+
- `SyntaxError`
|
|
118
|
+
- `TypeError`
|
|
119
|
+
- `URIError`
|
|
120
|
+
- `Int8Array`
|
|
121
|
+
- `Uint8Array`
|
|
122
|
+
- `Uint8ClampedArray`
|
|
123
|
+
- `Int16Array`
|
|
124
|
+
- `Uint16Array`
|
|
125
|
+
- `Int32Array`
|
|
126
|
+
- `Uint32Array`
|
|
127
|
+
- `Float32Array`
|
|
128
|
+
- `Float64Array`
|
|
129
|
+
- `Map`
|
|
130
|
+
- `Set`
|
|
131
|
+
- `WeakMap`
|
|
132
|
+
- `WeakSet`
|
|
133
|
+
- `Promise`
|
|
134
|
+
- `Intl`
|
|
135
|
+
- `JSON`
|
|
136
|
+
- `Math`
|
|
137
|
+
|
|
138
|
+
# Safe Prototypes
|
|
139
|
+
|
|
140
|
+
- `SandboxGlobal`
|
|
141
|
+
- `Function`
|
|
142
|
+
- `Boolean`
|
|
143
|
+
- `Object`
|
|
144
|
+
- `Number`
|
|
145
|
+
- `BigInt`
|
|
146
|
+
- `String`
|
|
147
|
+
- `Date`
|
|
148
|
+
- `RegExp`
|
|
149
|
+
- `Error`
|
|
150
|
+
- `Array`
|
|
151
|
+
- `Int8Array`
|
|
152
|
+
- `Uint8Array`
|
|
153
|
+
- `Uint8ClampedArray`
|
|
154
|
+
- `Int16Array`
|
|
155
|
+
- `Uint16Array`
|
|
156
|
+
- `Int32Array`
|
|
157
|
+
- `Uint32Array`
|
|
158
|
+
- `Float32Array`
|
|
159
|
+
- `Float64Array`
|
|
160
|
+
- `Map`
|
|
161
|
+
- `Set`
|
|
162
|
+
- `WeakMap`
|
|
163
|
+
- `WeakSet`
|
|
164
|
+
- `Promise`
|
|
165
|
+
|
|
166
|
+
## Goals
|
|
167
|
+
|
|
168
|
+
|Feature|Status|
|
|
169
|
+
|---|---|
|
|
170
|
+
|Prototype access protection|done|
|
|
171
|
+
|Globals access protection|done|
|
|
172
|
+
|Prototype proxying|done|
|
|
173
|
+
|Single line sandboxing|done|
|
|
174
|
+
|Multi line sandboxing|done|
|
|
175
|
+
|Functions support|done|
|
|
176
|
+
|Audit prototype and globals access|done|
|
|
177
|
+
|Code blocks (try/catch, ifs, and loops)|done|
|
|
178
|
+
|Async/await|done|
|
|
179
|
+
|Execution time protection|done|
|
|
180
|
+
|Extensibility|done|
|
|
181
|
+
|Full ECMAScript support|90%|
|
|
182
|
+
|Script source and import sandboxing|Won't fix - handled by 3rd party|
|
|
183
|
+
|DOM ownership and inherited permissions|See [scope-js](https://github.com/nyariv/scope-js)|
|
|
184
|
+
|Tests|done|
|
|
185
|
+
|
|
186
|
+
📋 **[ECMAScript Feature Implementation Status](TODO.md)** - See which JavaScript features are supported and tested (~90% of core ES5-ES2018 features)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { IExecContext, IOptionParams, IScope } from './utils.js';
|
|
2
|
+
import { ExecReturn } from './executor.js';
|
|
3
|
+
import SandboxExec from './SandboxExec.js';
|
|
4
|
+
export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
|
|
5
|
+
export default class Sandbox extends SandboxExec {
|
|
6
|
+
constructor(options?: IOptionParams);
|
|
7
|
+
static audit<T>(code: string, scopes?: IScope[]): ExecReturn<T>;
|
|
8
|
+
static parse(code: string): import("./parser.js").IExecutionTree;
|
|
9
|
+
compile<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
|
|
10
|
+
context: IExecContext;
|
|
11
|
+
run: () => T;
|
|
12
|
+
};
|
|
13
|
+
compileAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
|
|
14
|
+
context: IExecContext;
|
|
15
|
+
run: () => Promise<T>;
|
|
16
|
+
};
|
|
17
|
+
compileExpression<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
|
|
18
|
+
context: IExecContext;
|
|
19
|
+
run: () => T;
|
|
20
|
+
};
|
|
21
|
+
compileExpressionAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
|
|
22
|
+
context: IExecContext;
|
|
23
|
+
run: () => Promise<T>;
|
|
24
|
+
};
|
|
25
|
+
}
|
package/build/Sandbox.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createExecContext } from './utils.js';
|
|
2
|
+
import { createEvalContext } from './eval.js';
|
|
3
|
+
import parse from './parser.js';
|
|
4
|
+
import SandboxExec from './SandboxExec.js';
|
|
5
|
+
export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
|
|
6
|
+
export default class Sandbox extends SandboxExec {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super(options, createEvalContext());
|
|
9
|
+
}
|
|
10
|
+
static audit(code, scopes = []) {
|
|
11
|
+
const globals = {};
|
|
12
|
+
for (const i of Object.getOwnPropertyNames(globalThis)) {
|
|
13
|
+
globals[i] = globalThis[i];
|
|
14
|
+
}
|
|
15
|
+
const sandbox = new SandboxExec({
|
|
16
|
+
globals,
|
|
17
|
+
audit: true,
|
|
18
|
+
});
|
|
19
|
+
return sandbox.executeTree(createExecContext(sandbox, parse(code, true), createEvalContext()), scopes);
|
|
20
|
+
}
|
|
21
|
+
static parse(code) {
|
|
22
|
+
return parse(code);
|
|
23
|
+
}
|
|
24
|
+
compile(code, optimize = false) {
|
|
25
|
+
const parsed = parse(code, optimize);
|
|
26
|
+
const exec = (...scopes) => {
|
|
27
|
+
const context = createExecContext(this, parsed, this.evalContext);
|
|
28
|
+
return { context, run: () => this.executeTree(context, [...scopes]).result };
|
|
29
|
+
};
|
|
30
|
+
return exec;
|
|
31
|
+
}
|
|
32
|
+
compileAsync(code, optimize = false) {
|
|
33
|
+
const parsed = parse(code, optimize);
|
|
34
|
+
const exec = (...scopes) => {
|
|
35
|
+
const context = createExecContext(this, parsed, this.evalContext);
|
|
36
|
+
return {
|
|
37
|
+
context,
|
|
38
|
+
run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
return exec;
|
|
42
|
+
}
|
|
43
|
+
compileExpression(code, optimize = false) {
|
|
44
|
+
const parsed = parse(code, optimize, true);
|
|
45
|
+
const exec = (...scopes) => {
|
|
46
|
+
const context = createExecContext(this, parsed, this.evalContext);
|
|
47
|
+
return { context, run: () => this.executeTree(context, [...scopes]).result };
|
|
48
|
+
};
|
|
49
|
+
return exec;
|
|
50
|
+
}
|
|
51
|
+
compileExpressionAsync(code, optimize = false) {
|
|
52
|
+
const parsed = parse(code, optimize, true);
|
|
53
|
+
const exec = (...scopes) => {
|
|
54
|
+
const context = createExecContext(this, parsed, this.evalContext);
|
|
55
|
+
return {
|
|
56
|
+
context,
|
|
57
|
+
run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
return exec;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { IEvalContext } from './eval.js';
|
|
2
|
+
import { Change, ExecReturn } from './executor.js';
|
|
3
|
+
import { IContext, IExecContext, IGlobals, IOptionParams, IScope, Scope, SubscriptionSubject, Ticks } from './utils.js';
|
|
4
|
+
export { IOptions, IContext, IExecContext, LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
|
|
5
|
+
export default class SandboxExec {
|
|
6
|
+
evalContext?: IEvalContext | undefined;
|
|
7
|
+
readonly context: IContext;
|
|
8
|
+
readonly setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
9
|
+
readonly changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
10
|
+
readonly sandboxFunctions: WeakMap<Function, IExecContext>;
|
|
11
|
+
private haltSubscriptions;
|
|
12
|
+
private resumeSubscriptions;
|
|
13
|
+
halted: boolean;
|
|
14
|
+
timeoutHandleCounter: number;
|
|
15
|
+
readonly setTimeoutHandles: Map<number, {
|
|
16
|
+
handle: number;
|
|
17
|
+
haltsub: {
|
|
18
|
+
unsubscribe: () => void;
|
|
19
|
+
};
|
|
20
|
+
contsub: {
|
|
21
|
+
unsubscribe: () => void;
|
|
22
|
+
};
|
|
23
|
+
}>;
|
|
24
|
+
readonly setIntervalHandles: Map<number, {
|
|
25
|
+
handle: number;
|
|
26
|
+
haltsub: {
|
|
27
|
+
unsubscribe: () => void;
|
|
28
|
+
};
|
|
29
|
+
contsub: {
|
|
30
|
+
unsubscribe: () => void;
|
|
31
|
+
};
|
|
32
|
+
}>;
|
|
33
|
+
constructor(options?: IOptionParams, evalContext?: IEvalContext | undefined);
|
|
34
|
+
static get SAFE_GLOBALS(): IGlobals;
|
|
35
|
+
static get SAFE_PROTOTYPES(): Map<any, Set<string>>;
|
|
36
|
+
subscribeGet(callback: (obj: SubscriptionSubject, name: string) => void, context: IExecContext): {
|
|
37
|
+
unsubscribe: () => void;
|
|
38
|
+
};
|
|
39
|
+
subscribeSet(obj: object, name: string, callback: (modification: Change) => void, context: SandboxExec | IExecContext): {
|
|
40
|
+
unsubscribe: () => void;
|
|
41
|
+
};
|
|
42
|
+
subscribeSetGlobal(obj: SubscriptionSubject, name: string, callback: (modification: Change) => void): {
|
|
43
|
+
unsubscribe: () => void;
|
|
44
|
+
};
|
|
45
|
+
subscribeHalt(cb: (args?: {
|
|
46
|
+
error: Error;
|
|
47
|
+
ticks: Ticks;
|
|
48
|
+
scope: Scope;
|
|
49
|
+
context: IExecContext;
|
|
50
|
+
}) => void): {
|
|
51
|
+
unsubscribe: () => void;
|
|
52
|
+
};
|
|
53
|
+
subscribeResume(cb: () => void): {
|
|
54
|
+
unsubscribe: () => void;
|
|
55
|
+
};
|
|
56
|
+
haltExecution(haltContext?: {
|
|
57
|
+
error: Error;
|
|
58
|
+
ticks: Ticks;
|
|
59
|
+
scope: Scope;
|
|
60
|
+
context: IExecContext;
|
|
61
|
+
}): void;
|
|
62
|
+
resumeExecution(): void;
|
|
63
|
+
getContext(fn: (...args: any[]) => any): IExecContext | undefined;
|
|
64
|
+
executeTree<T>(context: IExecContext, scopes?: IScope[]): ExecReturn<T>;
|
|
65
|
+
executeTreeAsync<T>(context: IExecContext, scopes?: IScope[]): Promise<ExecReturn<T>>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { executeTree, executeTreeAsync } from './executor.js';
|
|
2
|
+
import { createContext, SandboxExecutionQuotaExceededError, SandboxGlobal, } from './utils.js';
|
|
3
|
+
export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
|
|
4
|
+
function subscribeSet(obj, name, callback, context) {
|
|
5
|
+
const names = context.setSubscriptions.get(obj) || new Map();
|
|
6
|
+
context.setSubscriptions.set(obj, names);
|
|
7
|
+
const callbacks = names.get(name) || new Set();
|
|
8
|
+
names.set(name, callbacks);
|
|
9
|
+
callbacks.add(callback);
|
|
10
|
+
let changeCbs;
|
|
11
|
+
const val = obj[name];
|
|
12
|
+
if (val instanceof Object) {
|
|
13
|
+
changeCbs = context.changeSubscriptions.get(val) || new Set();
|
|
14
|
+
changeCbs.add(callback);
|
|
15
|
+
context.changeSubscriptions.set(val, changeCbs);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
unsubscribe: () => {
|
|
19
|
+
callbacks.delete(callback);
|
|
20
|
+
changeCbs?.delete(callback);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export default class SandboxExec {
|
|
25
|
+
constructor(options, evalContext) {
|
|
26
|
+
this.evalContext = evalContext;
|
|
27
|
+
this.setSubscriptions = new WeakMap();
|
|
28
|
+
this.changeSubscriptions = new WeakMap();
|
|
29
|
+
this.sandboxFunctions = new WeakMap();
|
|
30
|
+
this.haltSubscriptions = new Set();
|
|
31
|
+
this.resumeSubscriptions = new Set();
|
|
32
|
+
this.halted = false;
|
|
33
|
+
this.timeoutHandleCounter = 0;
|
|
34
|
+
this.setTimeoutHandles = new Map();
|
|
35
|
+
this.setIntervalHandles = new Map();
|
|
36
|
+
const opt = Object.assign({
|
|
37
|
+
audit: false,
|
|
38
|
+
forbidFunctionCalls: false,
|
|
39
|
+
forbidFunctionCreation: false,
|
|
40
|
+
globals: SandboxExec.SAFE_GLOBALS,
|
|
41
|
+
prototypeWhitelist: SandboxExec.SAFE_PROTOTYPES,
|
|
42
|
+
prototypeReplacements: new Map(),
|
|
43
|
+
}, options || {});
|
|
44
|
+
this.context = createContext(this, opt);
|
|
45
|
+
}
|
|
46
|
+
static get SAFE_GLOBALS() {
|
|
47
|
+
return {
|
|
48
|
+
globalThis,
|
|
49
|
+
Function,
|
|
50
|
+
eval,
|
|
51
|
+
console: {
|
|
52
|
+
debug: console.debug,
|
|
53
|
+
error: console.error,
|
|
54
|
+
info: console.info,
|
|
55
|
+
log: console.log,
|
|
56
|
+
table: console.table,
|
|
57
|
+
warn: console.warn,
|
|
58
|
+
},
|
|
59
|
+
isFinite,
|
|
60
|
+
isNaN,
|
|
61
|
+
parseFloat,
|
|
62
|
+
parseInt,
|
|
63
|
+
decodeURI,
|
|
64
|
+
decodeURIComponent,
|
|
65
|
+
encodeURI,
|
|
66
|
+
encodeURIComponent,
|
|
67
|
+
escape,
|
|
68
|
+
unescape,
|
|
69
|
+
Boolean,
|
|
70
|
+
Number,
|
|
71
|
+
BigInt,
|
|
72
|
+
String,
|
|
73
|
+
Object,
|
|
74
|
+
Array,
|
|
75
|
+
Symbol,
|
|
76
|
+
Error,
|
|
77
|
+
EvalError,
|
|
78
|
+
RangeError,
|
|
79
|
+
ReferenceError,
|
|
80
|
+
SyntaxError,
|
|
81
|
+
TypeError,
|
|
82
|
+
URIError,
|
|
83
|
+
Int8Array,
|
|
84
|
+
Uint8Array,
|
|
85
|
+
Uint8ClampedArray,
|
|
86
|
+
Int16Array,
|
|
87
|
+
Uint16Array,
|
|
88
|
+
Int32Array,
|
|
89
|
+
Uint32Array,
|
|
90
|
+
Float32Array,
|
|
91
|
+
Float64Array,
|
|
92
|
+
Map,
|
|
93
|
+
Set,
|
|
94
|
+
WeakMap,
|
|
95
|
+
WeakSet,
|
|
96
|
+
Promise,
|
|
97
|
+
Intl,
|
|
98
|
+
JSON,
|
|
99
|
+
Math,
|
|
100
|
+
Date,
|
|
101
|
+
RegExp,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
static get SAFE_PROTOTYPES() {
|
|
105
|
+
const protos = [
|
|
106
|
+
SandboxGlobal,
|
|
107
|
+
Function,
|
|
108
|
+
Boolean,
|
|
109
|
+
Number,
|
|
110
|
+
BigInt,
|
|
111
|
+
String,
|
|
112
|
+
Date,
|
|
113
|
+
Error,
|
|
114
|
+
Array,
|
|
115
|
+
Int8Array,
|
|
116
|
+
Uint8Array,
|
|
117
|
+
Uint8ClampedArray,
|
|
118
|
+
Int16Array,
|
|
119
|
+
Uint16Array,
|
|
120
|
+
Int32Array,
|
|
121
|
+
Uint32Array,
|
|
122
|
+
Float32Array,
|
|
123
|
+
Float64Array,
|
|
124
|
+
Map,
|
|
125
|
+
Set,
|
|
126
|
+
WeakMap,
|
|
127
|
+
WeakSet,
|
|
128
|
+
Promise,
|
|
129
|
+
Symbol,
|
|
130
|
+
Date,
|
|
131
|
+
RegExp,
|
|
132
|
+
// Fetch API
|
|
133
|
+
Response,
|
|
134
|
+
Request,
|
|
135
|
+
Headers,
|
|
136
|
+
FormData,
|
|
137
|
+
];
|
|
138
|
+
const map = new Map();
|
|
139
|
+
protos.forEach((proto) => {
|
|
140
|
+
map.set(proto, new Set());
|
|
141
|
+
});
|
|
142
|
+
map.set(Object, new Set([
|
|
143
|
+
'constructor',
|
|
144
|
+
'name',
|
|
145
|
+
'entries',
|
|
146
|
+
'fromEntries',
|
|
147
|
+
'getOwnPropertyNames',
|
|
148
|
+
'is',
|
|
149
|
+
'keys',
|
|
150
|
+
'hasOwnProperty',
|
|
151
|
+
'isPrototypeOf',
|
|
152
|
+
'propertyIsEnumerable',
|
|
153
|
+
'toLocaleString',
|
|
154
|
+
'toString',
|
|
155
|
+
'valueOf',
|
|
156
|
+
'values',
|
|
157
|
+
]));
|
|
158
|
+
return map;
|
|
159
|
+
}
|
|
160
|
+
subscribeGet(callback, context) {
|
|
161
|
+
context.getSubscriptions.add(callback);
|
|
162
|
+
return { unsubscribe: () => context.getSubscriptions.delete(callback) };
|
|
163
|
+
}
|
|
164
|
+
subscribeSet(obj, name, callback, context) {
|
|
165
|
+
return subscribeSet(obj, name, callback, context);
|
|
166
|
+
}
|
|
167
|
+
subscribeSetGlobal(obj, name, callback) {
|
|
168
|
+
return subscribeSet(obj, name, callback, this);
|
|
169
|
+
}
|
|
170
|
+
subscribeHalt(cb) {
|
|
171
|
+
this.haltSubscriptions.add(cb);
|
|
172
|
+
return {
|
|
173
|
+
unsubscribe: () => {
|
|
174
|
+
this.haltSubscriptions.delete(cb);
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
subscribeResume(cb) {
|
|
179
|
+
this.resumeSubscriptions.add(cb);
|
|
180
|
+
return {
|
|
181
|
+
unsubscribe: () => {
|
|
182
|
+
this.resumeSubscriptions.delete(cb);
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
haltExecution(haltContext) {
|
|
187
|
+
if (this.halted)
|
|
188
|
+
return;
|
|
189
|
+
this.halted = true;
|
|
190
|
+
for (const cb of this.haltSubscriptions) {
|
|
191
|
+
cb(haltContext);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
resumeExecution() {
|
|
195
|
+
if (!this.halted)
|
|
196
|
+
return;
|
|
197
|
+
if (this.context.ticks.tickLimit && this.context.ticks.ticks >= this.context.ticks.tickLimit) {
|
|
198
|
+
throw new SandboxExecutionQuotaExceededError('Cannot resume execution: tick limit exceeded');
|
|
199
|
+
}
|
|
200
|
+
this.halted = false;
|
|
201
|
+
for (const cb of this.resumeSubscriptions) {
|
|
202
|
+
cb();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
getContext(fn) {
|
|
206
|
+
return this.sandboxFunctions.get(fn);
|
|
207
|
+
}
|
|
208
|
+
executeTree(context, scopes = []) {
|
|
209
|
+
return executeTree(context.ctx.ticks, context, context.tree, scopes);
|
|
210
|
+
}
|
|
211
|
+
executeTreeAsync(context, scopes = []) {
|
|
212
|
+
return executeTreeAsync(context.ctx.ticks, context, context.tree, scopes);
|
|
213
|
+
}
|
|
214
|
+
}
|
package/build/eval.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { lispifyFunction } from './parser.js';
|
|
2
|
+
import { IExecContext } from './utils.js';
|
|
3
|
+
export interface IEvalContext {
|
|
4
|
+
sandboxFunction: typeof sandboxFunction;
|
|
5
|
+
sandboxAsyncFunction: typeof sandboxAsyncFunction;
|
|
6
|
+
sandboxedEval: (func: SandboxFunction, context: IExecContext) => SandboxEval;
|
|
7
|
+
sandboxedSetTimeout: typeof sandboxedSetTimeout;
|
|
8
|
+
sandboxedSetInterval: typeof sandboxedSetInterval;
|
|
9
|
+
sandboxedClearTimeout: typeof sandboxedClearTimeout;
|
|
10
|
+
sandboxedClearInterval: typeof sandboxedClearInterval;
|
|
11
|
+
lispifyFunction: typeof lispifyFunction;
|
|
12
|
+
}
|
|
13
|
+
export type SandboxFunction = (code: string, ...args: string[]) => () => unknown;
|
|
14
|
+
export type SandboxEval = (code: string) => unknown;
|
|
15
|
+
export type SandboxSetTimeout = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
|
|
16
|
+
export type SandboxSetInterval = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
|
|
17
|
+
export type SandboxClearTimeout = (handle: number) => void;
|
|
18
|
+
export type SandboxClearInterval = (handle: number) => void;
|
|
19
|
+
export declare function createEvalContext(): IEvalContext;
|
|
20
|
+
export declare function sandboxFunction(context: IExecContext): SandboxFunction;
|
|
21
|
+
export type SandboxAsyncFunction = (code: string, ...args: string[]) => () => Promise<unknown>;
|
|
22
|
+
export declare function sandboxAsyncFunction(context: IExecContext): SandboxAsyncFunction;
|
|
23
|
+
export declare function sandboxedEval(func: SandboxFunction, context: IExecContext): SandboxEval;
|
|
24
|
+
export declare function sandboxedSetTimeout(func: SandboxFunction, context: IExecContext): SandboxSetTimeout;
|
|
25
|
+
export declare function sandboxedClearTimeout(context: IExecContext): SandboxClearTimeout;
|
|
26
|
+
export declare function sandboxedClearInterval(context: IExecContext): SandboxClearInterval;
|
|
27
|
+
export declare function sandboxedSetInterval(func: SandboxFunction, context: IExecContext): SandboxSetInterval;
|