@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
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replace raw escape character strings with their escape characters.
|
|
3
|
+
* @param raw A string where escape characters are represented as raw string
|
|
4
|
+
* values like `\'` rather than `'`.
|
|
5
|
+
* @param allowOctals If `true`, will process the now-deprecated octal escape
|
|
6
|
+
* sequences (ie, `\111`).
|
|
7
|
+
* @returns The processed string, with escape characters replaced by their
|
|
8
|
+
* respective actual Unicode characters.
|
|
9
|
+
*/
|
|
10
|
+
export declare function unraw(raw: string): string;
|
|
11
|
+
export default unraw;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
export declare const AsyncFunction: Function;
|
|
2
|
+
export declare const GeneratorFunction: Function;
|
|
3
|
+
export declare const AsyncGeneratorFunction: Function;
|
|
4
|
+
import { IEvalContext } from './eval';
|
|
5
|
+
import { Change, Unknown } from './executor';
|
|
6
|
+
import { IConstants, IExecutionTree, Lisp, LispItem } from './parser';
|
|
7
|
+
import SandboxExec from './SandboxExec';
|
|
8
|
+
export type replacementCallback = (obj: any, isStaticAccess: boolean) => any;
|
|
9
|
+
export interface IOptionParams {
|
|
10
|
+
audit?: boolean;
|
|
11
|
+
forbidFunctionCalls?: boolean;
|
|
12
|
+
forbidFunctionCreation?: boolean;
|
|
13
|
+
prototypeReplacements?: Map<Function, replacementCallback>;
|
|
14
|
+
prototypeWhitelist?: Map<Function, Set<string>>;
|
|
15
|
+
globals?: IGlobals;
|
|
16
|
+
executionQuota?: bigint;
|
|
17
|
+
haltOnSandboxError?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface IOptions {
|
|
20
|
+
audit: boolean;
|
|
21
|
+
forbidFunctionCalls: boolean;
|
|
22
|
+
forbidFunctionCreation: boolean;
|
|
23
|
+
prototypeReplacements: Map<Function, replacementCallback>;
|
|
24
|
+
prototypeWhitelist: Map<Function, Set<string>>;
|
|
25
|
+
globals: IGlobals;
|
|
26
|
+
executionQuota?: bigint;
|
|
27
|
+
haltOnSandboxError?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface IContext {
|
|
30
|
+
sandbox: SandboxExec;
|
|
31
|
+
globalScope: Scope;
|
|
32
|
+
sandboxGlobal: ISandboxGlobal;
|
|
33
|
+
globalsWhitelist: Set<any>;
|
|
34
|
+
prototypeWhitelist: Map<any, Set<PropertyKey>>;
|
|
35
|
+
sandboxedFunctions: WeakSet<Function>;
|
|
36
|
+
options: IOptions;
|
|
37
|
+
auditReport?: IAuditReport;
|
|
38
|
+
ticks: Ticks;
|
|
39
|
+
}
|
|
40
|
+
export interface IAuditReport {
|
|
41
|
+
globalsAccess: Set<unknown>;
|
|
42
|
+
prototypeAccess: {
|
|
43
|
+
[name: string]: Set<PropertyKey>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export interface Ticks {
|
|
47
|
+
ticks: bigint;
|
|
48
|
+
tickLimit?: bigint;
|
|
49
|
+
}
|
|
50
|
+
export type SubscriptionSubject = object;
|
|
51
|
+
export interface IExecContext extends IExecutionTree {
|
|
52
|
+
ctx: IContext;
|
|
53
|
+
getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
|
|
54
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
55
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
56
|
+
setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
57
|
+
changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
58
|
+
registerSandboxFunction: (fn: (...args: any[]) => any) => void;
|
|
59
|
+
evals: Map<Function, Function>;
|
|
60
|
+
allowJit: boolean;
|
|
61
|
+
evalContext?: IEvalContext;
|
|
62
|
+
}
|
|
63
|
+
export interface ISandboxGlobal {
|
|
64
|
+
[key: string]: unknown;
|
|
65
|
+
}
|
|
66
|
+
interface SandboxGlobalConstructor {
|
|
67
|
+
new (globals: IGlobals): ISandboxGlobal;
|
|
68
|
+
}
|
|
69
|
+
export declare const SandboxGlobal: SandboxGlobalConstructor;
|
|
70
|
+
export type IGlobals = ISandboxGlobal;
|
|
71
|
+
export declare class ExecContext implements IExecContext {
|
|
72
|
+
ctx: IContext;
|
|
73
|
+
constants: IConstants;
|
|
74
|
+
tree: Lisp[];
|
|
75
|
+
getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
|
|
76
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
77
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
78
|
+
setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
79
|
+
changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
80
|
+
evals: Map<any, any>;
|
|
81
|
+
registerSandboxFunction: (fn: (...args: any[]) => any) => void;
|
|
82
|
+
allowJit: boolean;
|
|
83
|
+
evalContext?: IEvalContext | undefined;
|
|
84
|
+
constructor(ctx: IContext, constants: IConstants, tree: Lisp[], getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>, setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, evals: Map<any, any>, registerSandboxFunction: (fn: (...args: any[]) => any) => void, allowJit: boolean, evalContext?: IEvalContext | undefined);
|
|
85
|
+
}
|
|
86
|
+
export declare function createContext(sandbox: SandboxExec, options: IOptions): IContext;
|
|
87
|
+
export declare function createExecContext(sandbox: {
|
|
88
|
+
readonly setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
89
|
+
readonly changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
90
|
+
readonly sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
|
|
91
|
+
readonly context: IContext;
|
|
92
|
+
}, executionTree: IExecutionTree, evalContext?: IEvalContext): IExecContext;
|
|
93
|
+
export declare class CodeString {
|
|
94
|
+
start: number;
|
|
95
|
+
end: number;
|
|
96
|
+
ref: {
|
|
97
|
+
str: string;
|
|
98
|
+
};
|
|
99
|
+
constructor(str: string | CodeString);
|
|
100
|
+
substring(start: number, end?: number): CodeString;
|
|
101
|
+
get length(): number;
|
|
102
|
+
char(i: number): string | undefined;
|
|
103
|
+
toString(): string;
|
|
104
|
+
trimStart(): CodeString;
|
|
105
|
+
slice(start: number, end?: number): CodeString;
|
|
106
|
+
trim(): CodeString;
|
|
107
|
+
valueOf(): string;
|
|
108
|
+
}
|
|
109
|
+
export declare const reservedWords: Set<string>;
|
|
110
|
+
export declare const enum VarType {
|
|
111
|
+
let = "let",
|
|
112
|
+
const = "const",
|
|
113
|
+
var = "var"
|
|
114
|
+
}
|
|
115
|
+
export declare class Scope {
|
|
116
|
+
parent: Scope | null;
|
|
117
|
+
const: {
|
|
118
|
+
[key: string]: true;
|
|
119
|
+
};
|
|
120
|
+
let: {
|
|
121
|
+
[key: string]: true;
|
|
122
|
+
};
|
|
123
|
+
var: {
|
|
124
|
+
[key: string]: true;
|
|
125
|
+
};
|
|
126
|
+
globals: {
|
|
127
|
+
[key: string]: true;
|
|
128
|
+
};
|
|
129
|
+
allVars: {
|
|
130
|
+
[key: string]: unknown;
|
|
131
|
+
} & object;
|
|
132
|
+
functionThis?: Unknown;
|
|
133
|
+
constructor(parent: Scope | null, vars?: {}, functionThis?: Unknown);
|
|
134
|
+
get(key: string): Prop;
|
|
135
|
+
set(key: string, val: unknown): Prop<unknown>;
|
|
136
|
+
getWhereValScope(key: string, isThis: boolean): Scope | null;
|
|
137
|
+
getWhereVarScope(key: string, localScope?: boolean): Scope;
|
|
138
|
+
declare(key: string, type: VarType, value?: unknown, isGlobal?: boolean): Prop;
|
|
139
|
+
}
|
|
140
|
+
export interface IScope {
|
|
141
|
+
[key: string]: any;
|
|
142
|
+
}
|
|
143
|
+
export declare class FunctionScope implements IScope {
|
|
144
|
+
}
|
|
145
|
+
export declare class LocalScope implements IScope {
|
|
146
|
+
}
|
|
147
|
+
export declare class SandboxError extends Error {
|
|
148
|
+
}
|
|
149
|
+
export declare class SandboxExecutionQuotaExceededError extends SandboxError {
|
|
150
|
+
}
|
|
151
|
+
export declare class SandboxExecutionTreeError extends SandboxError {
|
|
152
|
+
}
|
|
153
|
+
export declare class SandboxCapabilityError extends SandboxError {
|
|
154
|
+
}
|
|
155
|
+
export declare class SandboxAccessError extends SandboxError {
|
|
156
|
+
}
|
|
157
|
+
export declare function isLisp<Type extends Lisp = Lisp>(item: LispItem | LispItem): item is Type;
|
|
158
|
+
export declare const enum LispType {
|
|
159
|
+
None = 0,
|
|
160
|
+
Prop = 1,
|
|
161
|
+
StringIndex = 2,
|
|
162
|
+
Let = 3,
|
|
163
|
+
Const = 4,
|
|
164
|
+
Call = 5,
|
|
165
|
+
KeyVal = 6,
|
|
166
|
+
Number = 7,
|
|
167
|
+
Return = 8,
|
|
168
|
+
Assign = 9,
|
|
169
|
+
InlineFunction = 10,
|
|
170
|
+
ArrowFunction = 11,
|
|
171
|
+
CreateArray = 12,
|
|
172
|
+
If = 13,
|
|
173
|
+
IfCase = 14,
|
|
174
|
+
InlineIf = 15,
|
|
175
|
+
InlineIfCase = 16,
|
|
176
|
+
SpreadObject = 17,
|
|
177
|
+
SpreadArray = 18,
|
|
178
|
+
ArrayProp = 19,
|
|
179
|
+
PropOptional = 20,
|
|
180
|
+
CallOptional = 21,
|
|
181
|
+
CreateObject = 22,
|
|
182
|
+
Group = 23,
|
|
183
|
+
Not = 24,
|
|
184
|
+
IncrementBefore = 25,
|
|
185
|
+
IncrementAfter = 26,
|
|
186
|
+
DecrementBefore = 27,
|
|
187
|
+
DecrementAfter = 28,
|
|
188
|
+
And = 29,
|
|
189
|
+
Or = 30,
|
|
190
|
+
StrictNotEqual = 31,
|
|
191
|
+
StrictEqual = 32,
|
|
192
|
+
Plus = 33,
|
|
193
|
+
Var = 34,
|
|
194
|
+
GlobalSymbol = 35,
|
|
195
|
+
Literal = 36,
|
|
196
|
+
Function = 37,
|
|
197
|
+
Loop = 38,
|
|
198
|
+
Try = 39,
|
|
199
|
+
Switch = 40,
|
|
200
|
+
SwitchCase = 41,
|
|
201
|
+
Block = 42,
|
|
202
|
+
Expression = 43,
|
|
203
|
+
Await = 44,
|
|
204
|
+
New = 45,
|
|
205
|
+
Throw = 46,
|
|
206
|
+
Minus = 47,
|
|
207
|
+
Divide = 48,
|
|
208
|
+
Power = 49,
|
|
209
|
+
Multiply = 50,
|
|
210
|
+
Modulus = 51,
|
|
211
|
+
Equal = 52,
|
|
212
|
+
NotEqual = 53,
|
|
213
|
+
SmallerEqualThan = 54,
|
|
214
|
+
LargerEqualThan = 55,
|
|
215
|
+
SmallerThan = 56,
|
|
216
|
+
LargerThan = 57,
|
|
217
|
+
Negative = 58,
|
|
218
|
+
Positive = 59,
|
|
219
|
+
Typeof = 60,
|
|
220
|
+
Delete = 61,
|
|
221
|
+
Instanceof = 62,
|
|
222
|
+
In = 63,
|
|
223
|
+
Inverse = 64,
|
|
224
|
+
SubractEquals = 65,
|
|
225
|
+
AddEquals = 66,
|
|
226
|
+
DivideEquals = 67,
|
|
227
|
+
PowerEquals = 68,
|
|
228
|
+
MultiplyEquals = 69,
|
|
229
|
+
ModulusEquals = 70,
|
|
230
|
+
BitNegateEquals = 71,
|
|
231
|
+
BitAndEquals = 72,
|
|
232
|
+
BitOrEquals = 73,
|
|
233
|
+
UnsignedShiftRightEquals = 74,
|
|
234
|
+
ShiftRightEquals = 75,
|
|
235
|
+
ShiftLeftEquals = 76,
|
|
236
|
+
BitAnd = 77,
|
|
237
|
+
BitOr = 78,
|
|
238
|
+
BitNegate = 79,
|
|
239
|
+
BitShiftLeft = 80,
|
|
240
|
+
BitShiftRight = 81,
|
|
241
|
+
BitUnsignedShiftRight = 82,
|
|
242
|
+
BigInt = 83,
|
|
243
|
+
LiteralIndex = 84,
|
|
244
|
+
RegexIndex = 85,
|
|
245
|
+
LoopAction = 86,
|
|
246
|
+
Void = 87,
|
|
247
|
+
True = 88,
|
|
248
|
+
NullishCoalescing = 89,
|
|
249
|
+
AndEquals = 90,
|
|
250
|
+
OrEquals = 91,
|
|
251
|
+
NullishCoalescingEquals = 92,
|
|
252
|
+
LispEnumSize = 93
|
|
253
|
+
}
|
|
254
|
+
export declare class Prop<T = unknown> {
|
|
255
|
+
context: T;
|
|
256
|
+
prop: PropertyKey;
|
|
257
|
+
isConst: boolean;
|
|
258
|
+
isGlobal: boolean;
|
|
259
|
+
isVariable: boolean;
|
|
260
|
+
constructor(context: T, prop: PropertyKey, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
|
|
261
|
+
get<T = unknown>(context: IExecContext): T;
|
|
262
|
+
}
|
|
263
|
+
export declare function hasOwnProperty(obj: unknown, prop: PropertyKey): boolean;
|
|
264
|
+
export {};
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Reusable AsyncFunction constructor reference
|
|
4
|
+
const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
|
|
5
|
+
const GeneratorFunction = Object.getPrototypeOf(function* () { }).constructor;
|
|
6
|
+
const AsyncGeneratorFunction = Object.getPrototypeOf(async function* () { }).constructor;
|
|
7
|
+
const SandboxGlobal = function SandboxGlobal(globals) {
|
|
8
|
+
for (const i in globals) {
|
|
9
|
+
this[i] = globals[i];
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
class ExecContext {
|
|
13
|
+
constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
|
|
14
|
+
this.ctx = ctx;
|
|
15
|
+
this.constants = constants;
|
|
16
|
+
this.tree = tree;
|
|
17
|
+
this.getSubscriptions = getSubscriptions;
|
|
18
|
+
this.setSubscriptions = setSubscriptions;
|
|
19
|
+
this.changeSubscriptions = changeSubscriptions;
|
|
20
|
+
this.setSubscriptionsGlobal = setSubscriptionsGlobal;
|
|
21
|
+
this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
|
|
22
|
+
this.evals = evals;
|
|
23
|
+
this.registerSandboxFunction = registerSandboxFunction;
|
|
24
|
+
this.allowJit = allowJit;
|
|
25
|
+
this.evalContext = evalContext;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function createContext(sandbox, options) {
|
|
29
|
+
const sandboxGlobal = new SandboxGlobal(options.globals);
|
|
30
|
+
const context = {
|
|
31
|
+
sandbox: sandbox,
|
|
32
|
+
globalsWhitelist: new Set(Object.values(options.globals)),
|
|
33
|
+
prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
|
|
34
|
+
options,
|
|
35
|
+
globalScope: new Scope(null, options.globals, sandboxGlobal),
|
|
36
|
+
sandboxGlobal,
|
|
37
|
+
ticks: { ticks: 0n, tickLimit: options.executionQuota },
|
|
38
|
+
sandboxedFunctions: new WeakSet(),
|
|
39
|
+
};
|
|
40
|
+
context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
|
|
41
|
+
// Fetch API 构造函数本身(静态方法如 Response.json)也加白名单
|
|
42
|
+
if (typeof Response !== 'undefined')
|
|
43
|
+
context.prototypeWhitelist.set(Response.prototype, new Set());
|
|
44
|
+
if (typeof Request !== 'undefined')
|
|
45
|
+
context.prototypeWhitelist.set(Request.prototype, new Set());
|
|
46
|
+
if (typeof Headers !== 'undefined')
|
|
47
|
+
context.prototypeWhitelist.set(Headers.prototype, new Set());
|
|
48
|
+
if (typeof FormData !== 'undefined')
|
|
49
|
+
context.prototypeWhitelist.set(FormData.prototype, new Set());
|
|
50
|
+
if (typeof Blob !== 'undefined')
|
|
51
|
+
context.prototypeWhitelist.set(Blob.prototype, new Set());
|
|
52
|
+
if (typeof URLSearchParams !== 'undefined')
|
|
53
|
+
context.prototypeWhitelist.set(URLSearchParams.prototype, new Set());
|
|
54
|
+
if (typeof AbortController !== 'undefined')
|
|
55
|
+
context.prototypeWhitelist.set(AbortController.prototype, new Set());
|
|
56
|
+
if (typeof ReadableStream !== 'undefined')
|
|
57
|
+
context.prototypeWhitelist.set(ReadableStream.prototype, new Set());
|
|
58
|
+
if (typeof TransformStream !== 'undefined')
|
|
59
|
+
context.prototypeWhitelist.set(TransformStream.prototype, new Set());
|
|
60
|
+
if (typeof WritableStream !== 'undefined')
|
|
61
|
+
context.prototypeWhitelist.set(WritableStream.prototype, new Set());
|
|
62
|
+
if (typeof TextEncoder !== 'undefined')
|
|
63
|
+
context.prototypeWhitelist.set(TextEncoder.prototype, new Set());
|
|
64
|
+
if (typeof TextDecoder !== 'undefined')
|
|
65
|
+
context.prototypeWhitelist.set(TextDecoder.prototype, new Set());
|
|
66
|
+
return context;
|
|
67
|
+
}
|
|
68
|
+
function createExecContext(sandbox, executionTree, evalContext) {
|
|
69
|
+
const evals = new Map();
|
|
70
|
+
const execContext = new ExecContext(sandbox.context, executionTree.constants, executionTree.tree, new Set(), new WeakMap(), new WeakMap(), sandbox.setSubscriptions, sandbox.changeSubscriptions, evals, (fn) => sandbox.sandboxFunctions.set(fn, execContext), !!evalContext, evalContext);
|
|
71
|
+
if (evalContext) {
|
|
72
|
+
const func = evalContext.sandboxFunction(execContext);
|
|
73
|
+
const asyncFunc = evalContext.sandboxAsyncFunction(execContext);
|
|
74
|
+
evals.set(Function, func);
|
|
75
|
+
evals.set(AsyncFunction, asyncFunc);
|
|
76
|
+
evals.set(GeneratorFunction, func);
|
|
77
|
+
evals.set(AsyncGeneratorFunction, asyncFunc);
|
|
78
|
+
evals.set(eval, evalContext.sandboxedEval(func, execContext));
|
|
79
|
+
evals.set(setTimeout, evalContext.sandboxedSetTimeout(func, execContext));
|
|
80
|
+
evals.set(setInterval, evalContext.sandboxedSetInterval(func, execContext));
|
|
81
|
+
evals.set(clearTimeout, evalContext.sandboxedClearTimeout(execContext));
|
|
82
|
+
evals.set(clearInterval, evalContext.sandboxedClearInterval(execContext));
|
|
83
|
+
for (const [key, value] of evals) {
|
|
84
|
+
sandbox.context.prototypeWhitelist.set(value.prototype, new Set());
|
|
85
|
+
sandbox.context.prototypeWhitelist.set(key.prototype, new Set());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return execContext;
|
|
89
|
+
}
|
|
90
|
+
class CodeString {
|
|
91
|
+
constructor(str) {
|
|
92
|
+
this.ref = { str: '' };
|
|
93
|
+
if (str instanceof CodeString) {
|
|
94
|
+
this.ref = str.ref;
|
|
95
|
+
this.start = str.start;
|
|
96
|
+
this.end = str.end;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.ref.str = str;
|
|
100
|
+
this.start = 0;
|
|
101
|
+
this.end = str.length;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
substring(start, end) {
|
|
105
|
+
if (!this.length)
|
|
106
|
+
return this;
|
|
107
|
+
start = this.start + start;
|
|
108
|
+
if (start < 0) {
|
|
109
|
+
start = 0;
|
|
110
|
+
}
|
|
111
|
+
if (start > this.end) {
|
|
112
|
+
start = this.end;
|
|
113
|
+
}
|
|
114
|
+
end = end === undefined ? this.end : this.start + end;
|
|
115
|
+
if (end < 0) {
|
|
116
|
+
end = 0;
|
|
117
|
+
}
|
|
118
|
+
if (end > this.end) {
|
|
119
|
+
end = this.end;
|
|
120
|
+
}
|
|
121
|
+
const code = new CodeString(this);
|
|
122
|
+
code.start = start;
|
|
123
|
+
code.end = end;
|
|
124
|
+
return code;
|
|
125
|
+
}
|
|
126
|
+
get length() {
|
|
127
|
+
const len = this.end - this.start;
|
|
128
|
+
return len < 0 ? 0 : len;
|
|
129
|
+
}
|
|
130
|
+
char(i) {
|
|
131
|
+
if (this.start === this.end)
|
|
132
|
+
return undefined;
|
|
133
|
+
return this.ref.str[this.start + i];
|
|
134
|
+
}
|
|
135
|
+
toString() {
|
|
136
|
+
return this.ref.str.substring(this.start, this.end);
|
|
137
|
+
}
|
|
138
|
+
trimStart() {
|
|
139
|
+
const found = /^\s+/.exec(this.toString());
|
|
140
|
+
const code = new CodeString(this);
|
|
141
|
+
if (found) {
|
|
142
|
+
code.start += found[0].length;
|
|
143
|
+
}
|
|
144
|
+
return code;
|
|
145
|
+
}
|
|
146
|
+
slice(start, end) {
|
|
147
|
+
if (start < 0) {
|
|
148
|
+
start = this.end - this.start + start;
|
|
149
|
+
}
|
|
150
|
+
if (start < 0) {
|
|
151
|
+
start = 0;
|
|
152
|
+
}
|
|
153
|
+
if (end === undefined) {
|
|
154
|
+
end = this.end - this.start;
|
|
155
|
+
}
|
|
156
|
+
if (end < 0) {
|
|
157
|
+
end = this.end - this.start + end;
|
|
158
|
+
}
|
|
159
|
+
if (end < 0) {
|
|
160
|
+
end = 0;
|
|
161
|
+
}
|
|
162
|
+
return this.substring(start, end);
|
|
163
|
+
}
|
|
164
|
+
trim() {
|
|
165
|
+
const code = this.trimStart();
|
|
166
|
+
const found = /\s+$/.exec(code.toString());
|
|
167
|
+
if (found) {
|
|
168
|
+
code.end -= found[0].length;
|
|
169
|
+
}
|
|
170
|
+
return code;
|
|
171
|
+
}
|
|
172
|
+
valueOf() {
|
|
173
|
+
return this.toString();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function keysOnly(obj) {
|
|
177
|
+
const ret = Object.assign({}, obj);
|
|
178
|
+
for (const key in ret) {
|
|
179
|
+
ret[key] = true;
|
|
180
|
+
}
|
|
181
|
+
return ret;
|
|
182
|
+
}
|
|
183
|
+
const reservedWords = new Set([
|
|
184
|
+
'await',
|
|
185
|
+
'break',
|
|
186
|
+
'case',
|
|
187
|
+
'catch',
|
|
188
|
+
'class',
|
|
189
|
+
'const',
|
|
190
|
+
'continue',
|
|
191
|
+
'debugger',
|
|
192
|
+
'default',
|
|
193
|
+
'delete',
|
|
194
|
+
'do',
|
|
195
|
+
'else',
|
|
196
|
+
'enum',
|
|
197
|
+
'export',
|
|
198
|
+
'extends',
|
|
199
|
+
'false',
|
|
200
|
+
'finally',
|
|
201
|
+
'for',
|
|
202
|
+
'function',
|
|
203
|
+
'if',
|
|
204
|
+
'implements',
|
|
205
|
+
'import',
|
|
206
|
+
'in',
|
|
207
|
+
'instanceof',
|
|
208
|
+
'let',
|
|
209
|
+
'new',
|
|
210
|
+
'null',
|
|
211
|
+
'return',
|
|
212
|
+
'super',
|
|
213
|
+
'switch',
|
|
214
|
+
'this',
|
|
215
|
+
'throw',
|
|
216
|
+
'true',
|
|
217
|
+
'try',
|
|
218
|
+
'typeof',
|
|
219
|
+
'var',
|
|
220
|
+
'void',
|
|
221
|
+
'while',
|
|
222
|
+
'with',
|
|
223
|
+
]);
|
|
224
|
+
class Scope {
|
|
225
|
+
constructor(parent, vars = {}, functionThis) {
|
|
226
|
+
this.const = {};
|
|
227
|
+
this.let = {};
|
|
228
|
+
this.var = {};
|
|
229
|
+
const isFuncScope = functionThis !== undefined || parent === null;
|
|
230
|
+
this.parent = parent;
|
|
231
|
+
this.allVars = vars;
|
|
232
|
+
this.let = isFuncScope ? this.let : keysOnly(vars);
|
|
233
|
+
this.var = isFuncScope ? keysOnly(vars) : this.var;
|
|
234
|
+
this.globals = parent === null ? keysOnly(vars) : {};
|
|
235
|
+
this.functionThis = functionThis;
|
|
236
|
+
}
|
|
237
|
+
get(key) {
|
|
238
|
+
const isThis = key === 'this';
|
|
239
|
+
const scope = this.getWhereValScope(key, isThis);
|
|
240
|
+
if (scope && isThis) {
|
|
241
|
+
return new Prop({ this: scope.functionThis }, key, false, false, true);
|
|
242
|
+
}
|
|
243
|
+
if (!scope) {
|
|
244
|
+
return new Prop(undefined, key);
|
|
245
|
+
}
|
|
246
|
+
return new Prop(scope.allVars, key, key in scope.const, key in scope.globals, true);
|
|
247
|
+
}
|
|
248
|
+
set(key, val) {
|
|
249
|
+
if (key === 'this')
|
|
250
|
+
throw new SyntaxError('"this" cannot be assigned');
|
|
251
|
+
if (reservedWords.has(key))
|
|
252
|
+
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
253
|
+
const prop = this.get(key);
|
|
254
|
+
if (prop.context === undefined) {
|
|
255
|
+
throw new ReferenceError(`Variable '${key}' was not declared.`);
|
|
256
|
+
}
|
|
257
|
+
if (prop.context === null) {
|
|
258
|
+
throw new TypeError(`Cannot set properties of null, (setting '${key}')`);
|
|
259
|
+
}
|
|
260
|
+
if (prop.isConst) {
|
|
261
|
+
throw new TypeError(`Assignment to constant variable`);
|
|
262
|
+
}
|
|
263
|
+
if (prop.isGlobal) {
|
|
264
|
+
throw new SandboxError(`Cannot override global variable '${key}'`);
|
|
265
|
+
}
|
|
266
|
+
prop.context[prop.prop] = val;
|
|
267
|
+
return prop;
|
|
268
|
+
}
|
|
269
|
+
getWhereValScope(key, isThis) {
|
|
270
|
+
if (isThis) {
|
|
271
|
+
if (this.functionThis !== undefined) {
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
return this.parent?.getWhereValScope(key, isThis) || null;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
return this.parent?.getWhereValScope(key, isThis) || null;
|
|
282
|
+
}
|
|
283
|
+
getWhereVarScope(key, localScope = false) {
|
|
284
|
+
if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
if (this.parent === null || localScope || this.functionThis !== undefined) {
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
return this.parent.getWhereVarScope(key, localScope);
|
|
291
|
+
}
|
|
292
|
+
declare(key, type, value = undefined, isGlobal = false) {
|
|
293
|
+
if (key === 'this')
|
|
294
|
+
throw new SyntaxError('"this" cannot be declared');
|
|
295
|
+
if (reservedWords.has(key))
|
|
296
|
+
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
297
|
+
const existingScope = this.getWhereVarScope(key, type !== "var" /* VarType.var */);
|
|
298
|
+
if (type === "var" /* VarType.var */) {
|
|
299
|
+
if (existingScope.var[key]) {
|
|
300
|
+
existingScope.allVars[key] = value;
|
|
301
|
+
if (!isGlobal) {
|
|
302
|
+
delete existingScope.globals[key];
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
existingScope.globals[key] = true;
|
|
306
|
+
}
|
|
307
|
+
return new Prop(existingScope.allVars, key, false, existingScope.globals[key], true);
|
|
308
|
+
}
|
|
309
|
+
else if (key in existingScope.allVars) {
|
|
310
|
+
throw new SyntaxError(`Identifier '${key}' has already been declared`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (key in existingScope.allVars) {
|
|
314
|
+
throw new SyntaxError(`Identifier '${key}' has already been declared`);
|
|
315
|
+
}
|
|
316
|
+
if (isGlobal) {
|
|
317
|
+
existingScope.globals[key] = true;
|
|
318
|
+
}
|
|
319
|
+
existingScope[type][key] = true;
|
|
320
|
+
existingScope.allVars[key] = value;
|
|
321
|
+
return new Prop(this.allVars, key, type === "const" /* VarType.const */, isGlobal, true);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
class FunctionScope {
|
|
325
|
+
}
|
|
326
|
+
class LocalScope {
|
|
327
|
+
}
|
|
328
|
+
class SandboxError extends Error {
|
|
329
|
+
}
|
|
330
|
+
class SandboxExecutionQuotaExceededError extends SandboxError {
|
|
331
|
+
}
|
|
332
|
+
class SandboxExecutionTreeError extends SandboxError {
|
|
333
|
+
}
|
|
334
|
+
class SandboxCapabilityError extends SandboxError {
|
|
335
|
+
}
|
|
336
|
+
class SandboxAccessError extends SandboxError {
|
|
337
|
+
}
|
|
338
|
+
function isLisp(item) {
|
|
339
|
+
return (Array.isArray(item) &&
|
|
340
|
+
typeof item[0] === 'number' &&
|
|
341
|
+
item[0] !== 0 /* LispType.None */ &&
|
|
342
|
+
item[0] !== 88 /* LispType.True */);
|
|
343
|
+
}
|
|
344
|
+
class Prop {
|
|
345
|
+
constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
|
|
346
|
+
this.context = context;
|
|
347
|
+
this.prop = prop;
|
|
348
|
+
this.isConst = isConst;
|
|
349
|
+
this.isGlobal = isGlobal;
|
|
350
|
+
this.isVariable = isVariable;
|
|
351
|
+
}
|
|
352
|
+
get(context) {
|
|
353
|
+
const ctx = this.context;
|
|
354
|
+
if (ctx === undefined)
|
|
355
|
+
throw new ReferenceError(`${this.prop.toString()} is not defined`);
|
|
356
|
+
if (ctx === null)
|
|
357
|
+
throw new TypeError(`Cannot read properties of null, (reading '${this.prop.toString()}')`);
|
|
358
|
+
context.getSubscriptions.forEach((cb) => cb(ctx, this.prop.toString()));
|
|
359
|
+
return ctx[this.prop];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function hasOwnProperty(obj, prop) {
|
|
363
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
exports.AsyncFunction = AsyncFunction;
|
|
367
|
+
exports.AsyncGeneratorFunction = AsyncGeneratorFunction;
|
|
368
|
+
exports.CodeString = CodeString;
|
|
369
|
+
exports.ExecContext = ExecContext;
|
|
370
|
+
exports.FunctionScope = FunctionScope;
|
|
371
|
+
exports.GeneratorFunction = GeneratorFunction;
|
|
372
|
+
exports.LocalScope = LocalScope;
|
|
373
|
+
exports.Prop = Prop;
|
|
374
|
+
exports.SandboxAccessError = SandboxAccessError;
|
|
375
|
+
exports.SandboxCapabilityError = SandboxCapabilityError;
|
|
376
|
+
exports.SandboxError = SandboxError;
|
|
377
|
+
exports.SandboxExecutionQuotaExceededError = SandboxExecutionQuotaExceededError;
|
|
378
|
+
exports.SandboxExecutionTreeError = SandboxExecutionTreeError;
|
|
379
|
+
exports.SandboxGlobal = SandboxGlobal;
|
|
380
|
+
exports.Scope = Scope;
|
|
381
|
+
exports.createContext = createContext;
|
|
382
|
+
exports.createExecContext = createExecContext;
|
|
383
|
+
exports.hasOwnProperty = hasOwnProperty;
|
|
384
|
+
exports.isLisp = isLisp;
|
|
385
|
+
exports.reservedWords = reservedWords;
|