@nyariv/sandboxjs 0.8.22 → 0.8.24

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.
Files changed (50) hide show
  1. package/.eslintignore +6 -0
  2. package/.eslintrc.js +22 -0
  3. package/.prettierrc +4 -0
  4. package/.vscode/settings.json +4 -0
  5. package/build/Sandbox.d.ts +11 -78
  6. package/build/Sandbox.js +21 -216
  7. package/build/SandboxExec.d.ts +25 -0
  8. package/build/SandboxExec.js +169 -0
  9. package/build/eval.d.ts +18 -0
  10. package/build/eval.js +43 -0
  11. package/build/executor.d.ts +56 -95
  12. package/build/executor.js +739 -815
  13. package/build/parser.d.ts +112 -74
  14. package/build/parser.js +504 -542
  15. package/build/unraw.js +13 -16
  16. package/build/utils.d.ts +242 -0
  17. package/build/utils.js +276 -0
  18. package/dist/Sandbox.d.ts +11 -78
  19. package/dist/Sandbox.js +106 -1
  20. package/dist/Sandbox.js.map +1 -1
  21. package/dist/Sandbox.min.js +1 -1
  22. package/dist/Sandbox.min.js.map +1 -1
  23. package/dist/SandboxExec.d.ts +25 -0
  24. package/dist/SandboxExec.js +173 -0
  25. package/dist/SandboxExec.js.map +1 -0
  26. package/dist/SandboxExec.min.js +2 -0
  27. package/dist/SandboxExec.min.js.map +1 -0
  28. package/dist/eval.d.ts +18 -0
  29. package/dist/executor.d.ts +56 -95
  30. package/dist/executor.js +1270 -0
  31. package/dist/executor.js.map +1 -0
  32. package/dist/node/Sandbox.d.ts +11 -78
  33. package/dist/node/Sandbox.js +37 -3091
  34. package/dist/node/SandboxExec.d.ts +25 -0
  35. package/dist/node/SandboxExec.js +176 -0
  36. package/dist/node/eval.d.ts +18 -0
  37. package/dist/node/executor.d.ts +56 -95
  38. package/dist/node/executor.js +1289 -0
  39. package/dist/node/parser.d.ts +112 -74
  40. package/dist/node/parser.js +1528 -0
  41. package/dist/node/utils.d.ts +242 -0
  42. package/dist/node/utils.js +290 -0
  43. package/dist/parser.d.ts +112 -74
  44. package/dist/parser.js +1514 -0
  45. package/dist/parser.js.map +1 -0
  46. package/dist/utils.d.ts +242 -0
  47. package/dist/utils.js +279 -0
  48. package/dist/utils.js.map +1 -0
  49. package/package.json +22 -14
  50. package/.github/workflows/npm-publish.yml +0 -34
package/build/unraw.js CHANGED
@@ -23,8 +23,7 @@ function parseHexToInt(hex) {
23
23
  */
24
24
  function validateAndParseHex(hex, errorName, enforcedLength) {
25
25
  const parsedHex = parseHexToInt(hex);
26
- if (Number.isNaN(parsedHex) ||
27
- (enforcedLength !== undefined && enforcedLength !== hex.length)) {
26
+ if (Number.isNaN(parsedHex) || (enforcedLength !== undefined && enforcedLength !== hex.length)) {
28
27
  throw new SyntaxError(errorName + ': ' + hex);
29
28
  }
30
29
  return parsedHex;
@@ -65,7 +64,7 @@ function parseUnicodeCode(code, surrogateCode) {
65
64
  * @returns `true` if the text is in the form `{*}`.
66
65
  */
67
66
  function isCurlyBraced(text) {
68
- return text.charAt(0) === "{" && text.charAt(text.length - 1) === "}";
67
+ return text.charAt(0) === '{' && text.charAt(text.length - 1) === '}';
69
68
  }
70
69
  /**
71
70
  * Parse a Unicode code point character escape code.
@@ -85,9 +84,7 @@ function parseUnicodeCodePointCode(codePoint) {
85
84
  return String.fromCodePoint(parsedCode);
86
85
  }
87
86
  catch (err) {
88
- throw err instanceof RangeError
89
- ? new SyntaxError('Code Point Limit:' + parsedCode)
90
- : err;
87
+ throw err instanceof RangeError ? new SyntaxError('Code Point Limit:' + parsedCode) : err;
91
88
  }
92
89
  }
93
90
  /**
@@ -95,13 +92,13 @@ function parseUnicodeCodePointCode(codePoint) {
95
92
  * Intentionally does not include characters that map to themselves like "\'".
96
93
  */
97
94
  const singleCharacterEscapes = new Map([
98
- ["b", "\b"],
99
- ["f", "\f"],
100
- ["n", "\n"],
101
- ["r", "\r"],
102
- ["t", "\t"],
103
- ["v", "\v"],
104
- ["0", "\0"]
95
+ ['b', '\b'],
96
+ ['f', '\f'],
97
+ ['n', '\n'],
98
+ ['r', '\r'],
99
+ ['t', '\t'],
100
+ ['v', '\v'],
101
+ ['0', '\0'],
105
102
  ]);
106
103
  /**
107
104
  * Parse a single character escape sequence and return the matching character.
@@ -142,7 +139,7 @@ export function unraw(raw) {
142
139
  // Compare groups to undefined because empty strings mean different errors
143
140
  // Otherwise, `\u` would fail the same as `\` which is wrong.
144
141
  if (backslash !== undefined) {
145
- return "\\";
142
+ return '\\';
146
143
  }
147
144
  if (hex !== undefined) {
148
145
  return parseHexadecimalCode(hex);
@@ -156,8 +153,8 @@ export function unraw(raw) {
156
153
  if (unicode !== undefined) {
157
154
  return parseUnicodeCode(unicode);
158
155
  }
159
- if (octal === "0") {
160
- return "\0";
156
+ if (octal === '0') {
157
+ return '\0';
161
158
  }
162
159
  if (octal !== undefined) {
163
160
  throw new SyntaxError('Octal Deprecation: ' + octal);
@@ -0,0 +1,242 @@
1
+ import { IEvalContext } from './eval';
2
+ import { Change, Unknown } from './executor';
3
+ import { IConstants, IExecutionTree, Lisp, LispItem } from './parser';
4
+ import SandboxExec from './SandboxExec';
5
+ export type replacementCallback = (obj: any, isStaticAccess: boolean) => any;
6
+ export interface IOptionParams {
7
+ audit?: boolean;
8
+ forbidFunctionCalls?: boolean;
9
+ forbidFunctionCreation?: boolean;
10
+ prototypeReplacements?: Map<new () => any, replacementCallback>;
11
+ prototypeWhitelist?: Map<any, Set<string>>;
12
+ globals: IGlobals;
13
+ executionQuota?: bigint;
14
+ onExecutionQuotaReached?: (ticks: Ticks, scope: Scope, context: IExecutionTree, tree: LispItem) => boolean | void;
15
+ }
16
+ export interface IOptions {
17
+ audit: boolean;
18
+ forbidFunctionCalls: boolean;
19
+ forbidFunctionCreation: boolean;
20
+ prototypeReplacements: Map<new () => any, replacementCallback>;
21
+ prototypeWhitelist: Map<any, Set<string>>;
22
+ globals: IGlobals;
23
+ executionQuota?: bigint;
24
+ onExecutionQuotaReached?: (ticks: Ticks, scope: Scope, context: IExecutionTree, tree: LispItem) => boolean | void;
25
+ }
26
+ export interface IContext {
27
+ sandbox: SandboxExec;
28
+ globalScope: Scope;
29
+ sandboxGlobal: ISandboxGlobal;
30
+ globalsWhitelist: Set<any>;
31
+ prototypeWhitelist: Map<any, Set<string>>;
32
+ options: IOptions;
33
+ auditReport?: IAuditReport;
34
+ }
35
+ export interface IAuditReport {
36
+ globalsAccess: Set<unknown>;
37
+ prototypeAccess: {
38
+ [name: string]: Set<string>;
39
+ };
40
+ }
41
+ export interface Ticks {
42
+ ticks: bigint;
43
+ }
44
+ export type SubscriptionSubject = object;
45
+ export interface IExecContext extends IExecutionTree {
46
+ ctx: IContext;
47
+ getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
48
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
49
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
50
+ setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
51
+ changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
52
+ registerSandboxFunction: (fn: (...args: any[]) => any) => void;
53
+ evals: Map<any, any>;
54
+ allowJit: boolean;
55
+ evalContext?: IEvalContext;
56
+ }
57
+ export interface ISandboxGlobal {
58
+ [key: string]: unknown;
59
+ }
60
+ interface SandboxGlobalConstructor {
61
+ new (globals: IGlobals): ISandboxGlobal;
62
+ }
63
+ export declare const SandboxGlobal: SandboxGlobalConstructor;
64
+ export type IGlobals = ISandboxGlobal;
65
+ export declare class ExecContext implements IExecContext {
66
+ ctx: IContext;
67
+ constants: IConstants;
68
+ tree: Lisp[];
69
+ getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
70
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
71
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
72
+ setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
73
+ changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
74
+ evals: Map<any, any>;
75
+ registerSandboxFunction: (fn: (...args: any[]) => any) => void;
76
+ allowJit: boolean;
77
+ evalContext?: IEvalContext | undefined;
78
+ 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);
79
+ }
80
+ export declare function createContext(sandbox: SandboxExec, options: IOptions): IContext;
81
+ export declare function createExecContext(sandbox: {
82
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
83
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
84
+ sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
85
+ context: IContext;
86
+ }, executionTree: IExecutionTree, evalContext?: IEvalContext): IExecContext;
87
+ export declare class CodeString {
88
+ start: number;
89
+ end: number;
90
+ ref: {
91
+ str: string;
92
+ };
93
+ constructor(str: string | CodeString);
94
+ substring(start: number, end?: number): CodeString;
95
+ get length(): number;
96
+ char(i: number): string | undefined;
97
+ toString(): string;
98
+ trimStart(): CodeString;
99
+ slice(start: number, end?: number): CodeString;
100
+ trim(): CodeString;
101
+ valueOf(): string;
102
+ }
103
+ export declare const enum VarType {
104
+ let = "let",
105
+ const = "const",
106
+ var = "var"
107
+ }
108
+ export declare class Scope {
109
+ parent: Scope | null;
110
+ const: {
111
+ [key: string]: true;
112
+ };
113
+ let: {
114
+ [key: string]: true;
115
+ };
116
+ var: {
117
+ [key: string]: true;
118
+ };
119
+ globals: {
120
+ [key: string]: true;
121
+ };
122
+ allVars: {
123
+ [key: string]: unknown;
124
+ } & Object;
125
+ functionThis?: Unknown;
126
+ constructor(parent: Scope | null, vars?: {}, functionThis?: Unknown);
127
+ get(key: string, functionScope?: boolean): Prop;
128
+ set(key: string, val: unknown): Prop;
129
+ declare(key: string, type: VarType, value?: unknown, isGlobal?: boolean): Prop;
130
+ }
131
+ export interface IScope {
132
+ [key: string]: any;
133
+ }
134
+ export declare class FunctionScope implements IScope {
135
+ }
136
+ export declare class LocalScope implements IScope {
137
+ }
138
+ export declare class SandboxError extends Error {
139
+ }
140
+ export declare function isLisp<Type extends Lisp = Lisp>(item: LispItem | LispItem): item is Type;
141
+ export declare const enum LispType {
142
+ None = 0,
143
+ Prop = 1,
144
+ StringIndex = 2,
145
+ Let = 3,
146
+ Const = 4,
147
+ Call = 5,
148
+ KeyVal = 6,
149
+ Number = 7,
150
+ Return = 8,
151
+ Assign = 9,
152
+ InlineFunction = 10,
153
+ ArrowFunction = 11,
154
+ CreateArray = 12,
155
+ If = 13,
156
+ IfCase = 14,
157
+ InlineIf = 15,
158
+ InlineIfCase = 16,
159
+ SpreadObject = 17,
160
+ SpreadArray = 18,
161
+ ArrayProp = 19,
162
+ PropOptional = 20,
163
+ CallOptional = 21,
164
+ CreateObject = 22,
165
+ Group = 23,
166
+ Not = 24,
167
+ IncrementBefore = 25,
168
+ IncrementAfter = 26,
169
+ DecrementBefore = 27,
170
+ DecrementAfter = 28,
171
+ And = 29,
172
+ Or = 30,
173
+ StrictNotEqual = 31,
174
+ StrictEqual = 32,
175
+ Plus = 33,
176
+ Var = 34,
177
+ GlobalSymbol = 35,
178
+ Literal = 36,
179
+ Function = 37,
180
+ Loop = 38,
181
+ Try = 39,
182
+ Switch = 40,
183
+ SwitchCase = 41,
184
+ Block = 42,
185
+ Expression = 43,
186
+ Await = 44,
187
+ New = 45,
188
+ Throw = 46,
189
+ Minus = 47,
190
+ Divide = 48,
191
+ Power = 49,
192
+ Multiply = 50,
193
+ Modulus = 51,
194
+ Equal = 52,
195
+ NotEqual = 53,
196
+ SmallerEqualThan = 54,
197
+ LargerEqualThan = 55,
198
+ SmallerThan = 56,
199
+ LargerThan = 57,
200
+ Negative = 58,
201
+ Positive = 59,
202
+ Typeof = 60,
203
+ Delete = 61,
204
+ Instanceof = 62,
205
+ In = 63,
206
+ Inverse = 64,
207
+ SubractEquals = 65,
208
+ AddEquals = 66,
209
+ DivideEquals = 67,
210
+ PowerEquals = 68,
211
+ MultiplyEquals = 69,
212
+ ModulusEquals = 70,
213
+ BitNegateEquals = 71,
214
+ BitAndEquals = 72,
215
+ BitOrEquals = 73,
216
+ UnsignedShiftRightEquals = 74,
217
+ ShiftRightEquals = 75,
218
+ ShiftLeftEquals = 76,
219
+ BitAnd = 77,
220
+ BitOr = 78,
221
+ BitNegate = 79,
222
+ BitShiftLeft = 80,
223
+ BitShiftRight = 81,
224
+ BitUnsignedShiftRight = 82,
225
+ BigInt = 83,
226
+ LiteralIndex = 84,
227
+ RegexIndex = 85,
228
+ LoopAction = 86,
229
+ Void = 87,
230
+ True = 88,
231
+ LispEnumSize = 89
232
+ }
233
+ export declare class Prop {
234
+ context: Unknown;
235
+ prop: string;
236
+ isConst: boolean;
237
+ isGlobal: boolean;
238
+ isVariable: boolean;
239
+ constructor(context: Unknown, prop: string, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
240
+ get<T = unknown>(context: IExecContext): T;
241
+ }
242
+ export {};
package/build/utils.js ADDED
@@ -0,0 +1,276 @@
1
+ export const SandboxGlobal = function SandboxGlobal(globals) {
2
+ if (globals === globalThis)
3
+ return globalThis;
4
+ for (const i in globals) {
5
+ this[i] = globals[i];
6
+ }
7
+ };
8
+ export class ExecContext {
9
+ constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
10
+ this.ctx = ctx;
11
+ this.constants = constants;
12
+ this.tree = tree;
13
+ this.getSubscriptions = getSubscriptions;
14
+ this.setSubscriptions = setSubscriptions;
15
+ this.changeSubscriptions = changeSubscriptions;
16
+ this.setSubscriptionsGlobal = setSubscriptionsGlobal;
17
+ this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
18
+ this.evals = evals;
19
+ this.registerSandboxFunction = registerSandboxFunction;
20
+ this.allowJit = allowJit;
21
+ this.evalContext = evalContext;
22
+ }
23
+ }
24
+ export function createContext(sandbox, options) {
25
+ const sandboxGlobal = new SandboxGlobal(options.globals);
26
+ const context = {
27
+ sandbox: sandbox,
28
+ globalsWhitelist: new Set(Object.values(options.globals)),
29
+ prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
30
+ options,
31
+ globalScope: new Scope(null, options.globals, sandboxGlobal),
32
+ sandboxGlobal,
33
+ };
34
+ context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
35
+ return context;
36
+ }
37
+ export function createExecContext(sandbox, executionTree, evalContext) {
38
+ const evals = new Map();
39
+ 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);
40
+ if (evalContext) {
41
+ const func = evalContext.sandboxFunction(execContext);
42
+ evals.set(Function, func);
43
+ evals.set(eval, evalContext.sandboxedEval(func));
44
+ evals.set(setTimeout, evalContext.sandboxedSetTimeout(func));
45
+ evals.set(setInterval, evalContext.sandboxedSetInterval(func));
46
+ }
47
+ return execContext;
48
+ }
49
+ export class CodeString {
50
+ constructor(str) {
51
+ this.ref = { str: '' };
52
+ if (str instanceof CodeString) {
53
+ this.ref = str.ref;
54
+ this.start = str.start;
55
+ this.end = str.end;
56
+ }
57
+ else {
58
+ this.ref.str = str;
59
+ this.start = 0;
60
+ this.end = str.length;
61
+ }
62
+ }
63
+ substring(start, end) {
64
+ if (!this.length)
65
+ return this;
66
+ start = this.start + start;
67
+ if (start < 0) {
68
+ start = 0;
69
+ }
70
+ if (start > this.end) {
71
+ start = this.end;
72
+ }
73
+ end = end === undefined ? this.end : this.start + end;
74
+ if (end < 0) {
75
+ end = 0;
76
+ }
77
+ if (end > this.end) {
78
+ end = this.end;
79
+ }
80
+ const code = new CodeString(this);
81
+ code.start = start;
82
+ code.end = end;
83
+ return code;
84
+ }
85
+ get length() {
86
+ const len = this.end - this.start;
87
+ return len < 0 ? 0 : len;
88
+ }
89
+ char(i) {
90
+ if (this.start === this.end)
91
+ return undefined;
92
+ return this.ref.str[this.start + i];
93
+ }
94
+ toString() {
95
+ return this.ref.str.substring(this.start, this.end);
96
+ }
97
+ trimStart() {
98
+ const found = /^\s+/.exec(this.toString());
99
+ const code = new CodeString(this);
100
+ if (found) {
101
+ code.start += found[0].length;
102
+ }
103
+ return code;
104
+ }
105
+ slice(start, end) {
106
+ if (start < 0) {
107
+ start = this.end - this.start + start;
108
+ }
109
+ if (start < 0) {
110
+ start = 0;
111
+ }
112
+ if (end === undefined) {
113
+ end = this.end - this.start;
114
+ }
115
+ if (end < 0) {
116
+ end = this.end - this.start + end;
117
+ }
118
+ if (end < 0) {
119
+ end = 0;
120
+ }
121
+ return this.substring(start, end);
122
+ }
123
+ trim() {
124
+ const code = this.trimStart();
125
+ const found = /\s+$/.exec(code.toString());
126
+ if (found) {
127
+ code.end -= found[0].length;
128
+ }
129
+ return code;
130
+ }
131
+ valueOf() {
132
+ return this.toString();
133
+ }
134
+ }
135
+ function keysOnly(obj) {
136
+ const ret = Object.assign({}, obj);
137
+ for (const key in ret) {
138
+ ret[key] = true;
139
+ }
140
+ return ret;
141
+ }
142
+ const reservedWords = new Set([
143
+ 'instanceof',
144
+ 'typeof',
145
+ 'return',
146
+ 'throw',
147
+ 'try',
148
+ 'catch',
149
+ 'if',
150
+ 'finally',
151
+ 'else',
152
+ 'in',
153
+ 'of',
154
+ 'var',
155
+ 'let',
156
+ 'const',
157
+ 'for',
158
+ 'delete',
159
+ 'false',
160
+ 'true',
161
+ 'while',
162
+ 'do',
163
+ 'break',
164
+ 'continue',
165
+ 'new',
166
+ 'function',
167
+ 'async',
168
+ 'await',
169
+ 'switch',
170
+ 'case',
171
+ ]);
172
+ export class Scope {
173
+ constructor(parent, vars = {}, functionThis) {
174
+ this.const = {};
175
+ this.let = {};
176
+ this.var = {};
177
+ const isFuncScope = functionThis !== undefined || parent === null;
178
+ this.parent = parent;
179
+ this.allVars = vars;
180
+ this.let = isFuncScope ? this.let : keysOnly(vars);
181
+ this.var = isFuncScope ? keysOnly(vars) : this.var;
182
+ this.globals = parent === null ? keysOnly(vars) : {};
183
+ this.functionThis = functionThis;
184
+ }
185
+ get(key, functionScope = false) {
186
+ const functionThis = this.functionThis;
187
+ if (key === 'this' && functionThis !== undefined) {
188
+ return new Prop({ this: functionThis }, key, true, false, true);
189
+ }
190
+ if (reservedWords.has(key))
191
+ throw new SyntaxError("Unexepected token '" + key + "'");
192
+ if (this.parent === null || !functionScope || functionThis !== undefined) {
193
+ if (this.globals.hasOwnProperty(key)) {
194
+ return new Prop(functionThis, key, false, true, true);
195
+ }
196
+ if (key in this.allVars && (!(key in {}) || this.allVars.hasOwnProperty(key))) {
197
+ return new Prop(this.allVars, key, this.const.hasOwnProperty(key), this.globals.hasOwnProperty(key), true);
198
+ }
199
+ if (this.parent === null) {
200
+ return new Prop(undefined, key);
201
+ }
202
+ }
203
+ return this.parent.get(key, functionScope);
204
+ }
205
+ set(key, val) {
206
+ if (key === 'this')
207
+ throw new SyntaxError('"this" cannot be assigned');
208
+ if (reservedWords.has(key))
209
+ throw new SyntaxError("Unexepected token '" + key + "'");
210
+ const prop = this.get(key);
211
+ if (prop.context === undefined) {
212
+ throw new ReferenceError(`Variable '${key}' was not declared.`);
213
+ }
214
+ if (prop.isConst) {
215
+ throw new TypeError(`Cannot assign to const variable '${key}'`);
216
+ }
217
+ if (prop.isGlobal) {
218
+ throw new SandboxError(`Cannot override global variable '${key}'`);
219
+ }
220
+ if (!(prop.context instanceof Object))
221
+ throw new SandboxError('Scope is not an object');
222
+ prop.context[prop.prop] = val;
223
+ return prop;
224
+ }
225
+ declare(key, type, value = undefined, isGlobal = false) {
226
+ if (key === 'this')
227
+ throw new SyntaxError('"this" cannot be declared');
228
+ if (reservedWords.has(key))
229
+ throw new SyntaxError("Unexepected token '" + key + "'");
230
+ if (type === 'var' && this.functionThis === undefined && this.parent !== null) {
231
+ return this.parent.declare(key, type, value, isGlobal);
232
+ }
233
+ else if ((this[type].hasOwnProperty(key) && type !== 'const' && !this.globals.hasOwnProperty(key)) ||
234
+ !(key in this.allVars)) {
235
+ if (isGlobal) {
236
+ this.globals[key] = true;
237
+ }
238
+ this[type][key] = true;
239
+ this.allVars[key] = value;
240
+ }
241
+ else {
242
+ throw new SandboxError(`Identifier '${key}' has already been declared`);
243
+ }
244
+ return new Prop(this.allVars, key, this.const.hasOwnProperty(key), isGlobal);
245
+ }
246
+ }
247
+ export class FunctionScope {
248
+ }
249
+ export class LocalScope {
250
+ }
251
+ export class SandboxError extends Error {
252
+ }
253
+ export function isLisp(item) {
254
+ return (Array.isArray(item) &&
255
+ typeof item[0] === 'number' &&
256
+ item[0] !== 0 /* LispType.None */ &&
257
+ item[0] !== 88 /* LispType.True */);
258
+ }
259
+ export class Prop {
260
+ constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
261
+ this.context = context;
262
+ this.prop = prop;
263
+ this.isConst = isConst;
264
+ this.isGlobal = isGlobal;
265
+ this.isVariable = isVariable;
266
+ }
267
+ get(context) {
268
+ const ctx = this.context;
269
+ if (ctx === undefined)
270
+ throw new ReferenceError(`${this.prop} is not defined`);
271
+ if (ctx === null)
272
+ throw new TypeError(`Cannot read properties of null, (reading '${this.prop}')`);
273
+ context.getSubscriptions.forEach((cb) => cb(ctx, this.prop));
274
+ return ctx[this.prop];
275
+ }
276
+ }
package/dist/Sandbox.d.ts CHANGED
@@ -1,90 +1,23 @@
1
- import { IGlobals, replacementCallback, IAuditReport, Change, ExecReturn, executeTree, executeTreeAsync, ops, assignCheck, execMany, execAsync, execSync, asyncDone, Scope, IScope, FunctionScope, LocalScope, syncDone } from "./executor.js";
2
- import { IExecutionTree, expectTypes, setLispType, LispItem, LispArray, IConstants } from "./parser.js";
3
- export { expectTypes, setLispType, ops as executionOps, assignCheck, execMany, execAsync, execSync, asyncDone, syncDone, executeTree, executeTreeAsync, FunctionScope, LocalScope, };
4
- export interface IOptions {
5
- audit?: boolean;
6
- forbidFunctionCalls?: boolean;
7
- forbidFunctionCreation?: boolean;
8
- prototypeReplacements?: Map<new () => any, replacementCallback>;
9
- prototypeWhitelist?: Map<any, Set<string>>;
10
- globals: IGlobals;
11
- executionQuota?: bigint;
12
- onExecutionQuotaReached?: (ticks: Ticks, scope: Scope, context: IExecutionTree, tree: LispItem) => boolean | void;
13
- }
14
- export interface IContext {
15
- sandbox: Sandbox;
16
- globalScope: Scope;
17
- sandboxGlobal: SandboxGlobal;
18
- globalsWhitelist?: Set<any>;
19
- prototypeWhitelist?: Map<any, Set<string>>;
20
- options: IOptions;
21
- auditReport?: IAuditReport;
22
- }
23
- export interface Ticks {
24
- ticks: bigint;
25
- }
26
- export interface IExecContext extends IExecutionTree {
27
- ctx: IContext;
28
- getSubscriptions: Set<(obj: object, name: string) => void>;
29
- setSubscriptions: WeakMap<object, Map<string, Set<(modification: Change) => void>>>;
30
- changeSubscriptions: WeakMap<object, Set<(modification: Change) => void>>;
31
- setSubscriptionsGlobal: WeakMap<object, Map<string, Set<(modification: Change) => void>>>;
32
- changeSubscriptionsGlobal: WeakMap<object, Set<(modification: Change) => void>>;
33
- registerSandboxFunction: (fn: (...args: any[]) => any) => void;
34
- evals: Map<any, any>;
35
- }
36
- export declare class SandboxGlobal {
37
- constructor(globals: IGlobals);
38
- }
39
- export declare class ExecContext implements IExecContext {
40
- ctx: IContext;
41
- constants: IConstants;
42
- tree: LispArray;
43
- getSubscriptions: Set<(obj: object, name: string) => void>;
44
- setSubscriptions: WeakMap<object, Map<string, Set<(modification: Change) => void>>>;
45
- changeSubscriptions: WeakMap<object, Set<(modification: Change) => void>>;
46
- setSubscriptionsGlobal: WeakMap<object, Map<string, Set<(modification: Change) => void>>>;
47
- changeSubscriptionsGlobal: WeakMap<object, Set<(modification: Change) => void>>;
48
- evals: Map<any, any>;
49
- registerSandboxFunction: (fn: (...args: any[]) => any) => void;
50
- constructor(ctx: IContext, constants: IConstants, tree: LispArray, getSubscriptions: Set<(obj: object, name: string) => void>, setSubscriptions: WeakMap<object, Map<string, Set<(modification: Change) => void>>>, changeSubscriptions: WeakMap<object, Set<(modification: Change) => void>>, setSubscriptionsGlobal: WeakMap<object, Map<string, Set<(modification: Change) => void>>>, changeSubscriptionsGlobal: WeakMap<object, Set<(modification: Change) => void>>, evals: Map<any, any>, registerSandboxFunction: (fn: (...args: any[]) => any) => void);
51
- }
52
- export default class Sandbox {
53
- context: IContext;
54
- setSubscriptions: WeakMap<object, Map<string, Set<(modification: Change) => void>>>;
55
- changeSubscriptions: WeakMap<object, Set<(modification: Change) => void>>;
56
- sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
57
- constructor(options?: IOptions);
58
- static get SAFE_GLOBALS(): IGlobals;
59
- static get SAFE_PROTOTYPES(): Map<any, Set<string>>;
60
- subscribeGet(callback: (obj: object, name: string) => void, context: IExecContext): {
61
- unsubscribe: () => void;
62
- };
63
- subscribeSet(obj: object, name: string, callback: (modification: Change) => void, context: Sandbox | IExecContext): {
64
- unsubscribe: () => void;
65
- };
66
- subscribeSetGlobal(obj: object, name: string, callback: (modification: Change) => void): {
67
- unsubscribe: () => void;
68
- };
69
- static audit<T>(code: string, scopes?: (IScope)[]): ExecReturn<T>;
70
- static parse(code: string): IExecutionTree;
71
- createContext(context: IContext, executionTree: IExecutionTree): any;
72
- getContext(fn: (...args: any[]) => any): IExecContext;
73
- executeTree<T>(context: IExecContext, scopes?: (IScope)[]): ExecReturn<T>;
74
- executeTreeAsync<T>(context: IExecContext, scopes?: (IScope)[]): Promise<ExecReturn<T>>;
75
- compile<T>(code: string, optimize?: boolean): (...scopes: (IScope)[]) => {
1
+ import { IExecContext, IOptionParams, IScope } from './utils.js';
2
+ import { ExecReturn } from './executor.js';
3
+ import SandboxExec from './SandboxExec.js';
4
+ export default class Sandbox extends SandboxExec {
5
+ constructor(options?: IOptionParams);
6
+ static audit<T>(code: string, scopes?: IScope[]): ExecReturn<T>;
7
+ static parse(code: string): import("./parser.js").IExecutionTree;
8
+ compile<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
76
9
  context: IExecContext;
77
10
  run: () => T;
78
11
  };
79
- compileAsync<T>(code: string, optimize?: boolean): (...scopes: (IScope)[]) => {
12
+ compileAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
80
13
  context: IExecContext;
81
14
  run: () => Promise<T>;
82
15
  };
83
- compileExpression<T>(code: string, optimize?: boolean): (...scopes: (IScope)[]) => {
16
+ compileExpression<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
84
17
  context: IExecContext;
85
18
  run: () => T;
86
19
  };
87
- compileExpressionAsync<T>(code: string, optimize?: boolean): (...scopes: (IScope)[]) => {
20
+ compileExpressionAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
88
21
  context: IExecContext;
89
22
  run: () => Promise<T>;
90
23
  };