@cuxt/sandboxjs 0.1.1 → 0.1.4
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 -21
- package/README.md +198 -185
- package/{build → dist/cjs}/Sandbox.d.ts +15 -6
- package/dist/cjs/Sandbox.js +126 -0
- package/dist/{SandboxExec.d.ts → cjs/SandboxExec.d.ts} +8 -17
- package/dist/cjs/SandboxExec.js +227 -0
- package/{build/eval.d.ts → dist/cjs/eval/index.d.ts} +10 -2
- package/dist/cjs/eval/index.js +233 -0
- package/dist/cjs/executor/executorUtils.d.ts +161 -0
- package/dist/cjs/executor/executorUtils.js +930 -0
- package/dist/cjs/executor/index.d.ts +1 -0
- package/dist/cjs/executor/index.js +2 -0
- package/dist/cjs/executor/ops/assignment.d.ts +1 -0
- package/dist/cjs/executor/ops/assignment.js +88 -0
- package/dist/cjs/executor/ops/call.d.ts +1 -0
- package/dist/cjs/executor/ops/call.js +160 -0
- package/dist/cjs/executor/ops/comparison.d.ts +1 -0
- package/dist/cjs/executor/ops/comparison.js +36 -0
- package/dist/cjs/executor/ops/control.d.ts +1 -0
- package/dist/cjs/executor/ops/control.js +203 -0
- package/dist/cjs/executor/ops/functions.d.ts +1 -0
- package/dist/cjs/executor/ops/functions.js +55 -0
- package/dist/cjs/executor/ops/index.d.ts +0 -0
- package/dist/cjs/executor/ops/index.js +11 -0
- package/dist/cjs/executor/ops/literals.d.ts +1 -0
- package/dist/cjs/executor/ops/literals.js +45 -0
- package/dist/cjs/executor/ops/misc.d.ts +1 -0
- package/dist/cjs/executor/ops/misc.js +25 -0
- package/dist/cjs/executor/ops/object.d.ts +1 -0
- package/dist/cjs/executor/ops/object.js +51 -0
- package/dist/cjs/executor/ops/prop.d.ts +1 -0
- package/dist/cjs/executor/ops/prop.js +70 -0
- package/dist/cjs/executor/ops/unary.d.ts +1 -0
- package/dist/cjs/executor/ops/unary.js +48 -0
- package/dist/cjs/executor/ops/variables.d.ts +1 -0
- package/dist/cjs/executor/ops/variables.js +16 -0
- package/dist/cjs/executor/opsRegistry.d.ts +33 -0
- package/dist/cjs/executor/opsRegistry.js +8 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parser/index.d.ts +3 -0
- package/dist/cjs/parser/index.js +1 -0
- package/dist/{parser.d.ts → cjs/parser/lisp.d.ts} +29 -42
- package/dist/cjs/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/conditionals.js +135 -0
- package/dist/cjs/parser/lispTypes/control.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/control.js +204 -0
- package/dist/cjs/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/declarations.js +99 -0
- package/dist/cjs/parser/lispTypes/index.d.ts +3 -0
- package/dist/cjs/parser/lispTypes/index.js +17 -0
- package/dist/cjs/parser/lispTypes/operators.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/operators.js +252 -0
- package/dist/cjs/parser/lispTypes/shared.d.ts +38 -0
- package/dist/cjs/parser/lispTypes/structures.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/structures.js +188 -0
- package/dist/cjs/parser/lispTypes/values.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/values.js +89 -0
- package/dist/cjs/parser/parserUtils.d.ts +34 -0
- package/dist/cjs/parser/parserUtils.js +968 -0
- package/dist/cjs/utils/CodeString.d.ts +16 -0
- package/dist/cjs/utils/CodeString.js +64 -0
- package/dist/cjs/utils/ExecContext.d.ts +34 -0
- package/dist/cjs/utils/ExecContext.js +171 -0
- package/dist/cjs/utils/Prop.d.ts +16 -0
- package/dist/cjs/utils/Prop.js +81 -0
- package/dist/cjs/utils/Scope.d.ts +47 -0
- package/dist/cjs/utils/Scope.js +127 -0
- package/dist/cjs/utils/errors.d.ts +10 -0
- package/dist/cjs/utils/errors.js +12 -0
- package/dist/cjs/utils/functionReplacements.d.ts +11 -0
- package/dist/cjs/utils/functionReplacements.js +362 -0
- package/dist/cjs/utils/index.d.ts +7 -0
- package/dist/cjs/utils/index.js +7 -0
- package/dist/cjs/utils/types.d.ts +221 -0
- package/dist/cjs/utils/types.js +164 -0
- package/dist/cjs/utils/unraw.js +145 -0
- package/dist/{node → esm}/Sandbox.d.ts +15 -6
- package/dist/esm/Sandbox.js +115 -0
- package/dist/esm/Sandbox.js.map +1 -0
- package/{build → dist/esm}/SandboxExec.d.ts +8 -17
- package/dist/esm/SandboxExec.js +224 -0
- package/dist/esm/SandboxExec.js.map +1 -0
- package/dist/{eval.d.ts → esm/eval/index.d.ts} +10 -2
- package/dist/esm/eval/index.js +235 -0
- package/dist/esm/eval/index.js.map +1 -0
- package/dist/esm/executor/executorUtils.d.ts +161 -0
- package/dist/esm/executor/executorUtils.js +898 -0
- package/dist/esm/executor/executorUtils.js.map +1 -0
- package/dist/esm/executor/index.d.ts +1 -0
- package/dist/esm/executor/index.js +2 -0
- package/dist/esm/executor/ops/assignment.d.ts +1 -0
- package/dist/esm/executor/ops/assignment.js +90 -0
- package/dist/esm/executor/ops/assignment.js.map +1 -0
- package/dist/esm/executor/ops/call.d.ts +1 -0
- package/dist/esm/executor/ops/call.js +162 -0
- package/dist/esm/executor/ops/call.js.map +1 -0
- package/dist/esm/executor/ops/comparison.d.ts +1 -0
- package/dist/esm/executor/ops/comparison.js +38 -0
- package/dist/esm/executor/ops/comparison.js.map +1 -0
- package/dist/esm/executor/ops/control.d.ts +1 -0
- package/dist/esm/executor/ops/control.js +205 -0
- package/dist/esm/executor/ops/control.js.map +1 -0
- package/dist/esm/executor/ops/functions.d.ts +1 -0
- package/dist/esm/executor/ops/functions.js +57 -0
- package/dist/esm/executor/ops/functions.js.map +1 -0
- package/dist/esm/executor/ops/index.d.ts +0 -0
- package/dist/esm/executor/ops/index.js +11 -0
- package/dist/esm/executor/ops/literals.d.ts +1 -0
- package/dist/esm/executor/ops/literals.js +47 -0
- package/dist/esm/executor/ops/literals.js.map +1 -0
- package/dist/esm/executor/ops/misc.d.ts +1 -0
- package/dist/esm/executor/ops/misc.js +27 -0
- package/dist/esm/executor/ops/misc.js.map +1 -0
- package/dist/esm/executor/ops/object.d.ts +1 -0
- package/dist/esm/executor/ops/object.js +53 -0
- package/dist/esm/executor/ops/object.js.map +1 -0
- package/dist/esm/executor/ops/prop.d.ts +1 -0
- package/dist/esm/executor/ops/prop.js +72 -0
- package/dist/esm/executor/ops/prop.js.map +1 -0
- package/dist/esm/executor/ops/unary.d.ts +1 -0
- package/dist/esm/executor/ops/unary.js +50 -0
- package/dist/esm/executor/ops/unary.js.map +1 -0
- package/dist/esm/executor/ops/variables.d.ts +1 -0
- package/dist/esm/executor/ops/variables.js +18 -0
- package/dist/esm/executor/ops/variables.js.map +1 -0
- package/dist/esm/executor/opsRegistry.d.ts +33 -0
- package/dist/esm/executor/opsRegistry.js +9 -0
- package/dist/esm/executor/opsRegistry.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parser/index.d.ts +3 -0
- package/dist/esm/parser/index.js +1 -0
- package/{build/parser.d.ts → dist/esm/parser/lisp.d.ts} +29 -42
- package/dist/esm/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/esm/parser/lispTypes/conditionals.js +137 -0
- package/dist/esm/parser/lispTypes/conditionals.js.map +1 -0
- package/dist/esm/parser/lispTypes/control.d.ts +2 -0
- package/dist/esm/parser/lispTypes/control.js +206 -0
- package/dist/esm/parser/lispTypes/control.js.map +1 -0
- package/dist/esm/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/esm/parser/lispTypes/declarations.js +101 -0
- package/dist/esm/parser/lispTypes/declarations.js.map +1 -0
- package/dist/esm/parser/lispTypes/index.d.ts +3 -0
- package/dist/esm/parser/lispTypes/index.js +19 -0
- package/dist/esm/parser/lispTypes/index.js.map +1 -0
- package/dist/esm/parser/lispTypes/operators.d.ts +2 -0
- package/dist/esm/parser/lispTypes/operators.js +254 -0
- package/dist/esm/parser/lispTypes/operators.js.map +1 -0
- package/dist/esm/parser/lispTypes/shared.d.ts +38 -0
- package/dist/esm/parser/lispTypes/structures.d.ts +2 -0
- package/dist/esm/parser/lispTypes/structures.js +190 -0
- package/dist/esm/parser/lispTypes/structures.js.map +1 -0
- package/dist/esm/parser/lispTypes/values.d.ts +2 -0
- package/dist/esm/parser/lispTypes/values.js +91 -0
- package/dist/esm/parser/lispTypes/values.js.map +1 -0
- package/dist/esm/parser/parserUtils.d.ts +34 -0
- package/dist/esm/parser/parserUtils.js +959 -0
- package/dist/esm/parser/parserUtils.js.map +1 -0
- package/dist/esm/utils/CodeString.d.ts +16 -0
- package/dist/esm/utils/CodeString.js +66 -0
- package/dist/esm/utils/CodeString.js.map +1 -0
- package/dist/esm/utils/ExecContext.d.ts +34 -0
- package/dist/esm/utils/ExecContext.js +168 -0
- package/dist/esm/utils/ExecContext.js.map +1 -0
- package/dist/esm/utils/Prop.d.ts +16 -0
- package/dist/esm/utils/Prop.js +80 -0
- package/dist/esm/utils/Prop.js.map +1 -0
- package/dist/esm/utils/Scope.d.ts +47 -0
- package/dist/esm/utils/Scope.js +122 -0
- package/dist/esm/utils/Scope.js.map +1 -0
- package/dist/esm/utils/errors.d.ts +10 -0
- package/dist/esm/utils/errors.js +10 -0
- package/dist/esm/utils/errors.js.map +1 -0
- package/dist/esm/utils/functionReplacements.d.ts +11 -0
- package/dist/esm/utils/functionReplacements.js +361 -0
- package/dist/esm/utils/functionReplacements.js.map +1 -0
- package/dist/esm/utils/index.d.ts +7 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/types.d.ts +221 -0
- package/dist/esm/utils/types.js +160 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/unraw.js +147 -0
- package/dist/esm/utils/unraw.js.map +1 -0
- package/dist/umd/Sandbox.min.js +2 -0
- package/dist/umd/Sandbox.min.js.map +1 -0
- package/dist/umd/SandboxExec.min.js +2 -0
- package/dist/umd/SandboxExec.min.js.map +1 -0
- package/package.json +17 -16
- package/build/Sandbox.js +0 -62
- package/build/SandboxExec.js +0 -214
- package/build/eval.js +0 -205
- package/build/executor.d.ts +0 -124
- package/build/executor.js +0 -1554
- package/build/parser.js +0 -1527
- package/build/unraw.js +0 -168
- package/build/utils.d.ts +0 -264
- package/build/utils.js +0 -362
- package/dist/Sandbox.d.ts +0 -25
- package/dist/Sandbox.js +0 -270
- package/dist/Sandbox.js.map +0 -1
- package/dist/Sandbox.min.js +0 -2
- package/dist/Sandbox.min.js.map +0 -1
- package/dist/SandboxExec.js +0 -218
- package/dist/SandboxExec.js.map +0 -1
- package/dist/SandboxExec.min.js +0 -2
- package/dist/SandboxExec.min.js.map +0 -1
- package/dist/executor.d.ts +0 -124
- package/dist/executor.js +0 -1558
- package/dist/executor.js.map +0 -1
- package/dist/node/Sandbox.js +0 -277
- package/dist/node/SandboxExec.d.ts +0 -66
- package/dist/node/SandboxExec.js +0 -225
- package/dist/node/eval.d.ts +0 -27
- package/dist/node/executor.d.ts +0 -124
- package/dist/node/executor.js +0 -1575
- package/dist/node/parser.d.ts +0 -154
- package/dist/node/parser.js +0 -1704
- package/dist/node/utils.d.ts +0 -264
- package/dist/node/utils.js +0 -385
- package/dist/parser.js +0 -1690
- package/dist/parser.js.map +0 -1
- package/dist/unraw.d.ts +0 -11
- package/dist/utils.d.ts +0 -264
- package/dist/utils.js +0 -365
- package/dist/utils.js.map +0 -1
- /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
- /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
const require_errors = require("./errors.js");
|
|
2
|
+
//#region src/utils/functionReplacements.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if adding `expectTicks` would exceed the tick limit, and throws SandboxExecutionQuotaExceededError
|
|
5
|
+
* (which bypasses user try/catch) if so. Otherwise increments the tick counter.
|
|
6
|
+
*/
|
|
7
|
+
function checkTicksAndThrow(ctx, expectTicks) {
|
|
8
|
+
const { ticks } = ctx.ctx;
|
|
9
|
+
if (ticks.tickLimit !== void 0 && ticks.tickLimit <= ticks.ticks + expectTicks) throw new require_errors.SandboxExecutionQuotaExceededError("Execution quota exceeded");
|
|
10
|
+
ticks.ticks += expectTicks;
|
|
11
|
+
}
|
|
12
|
+
var typedArrayProtos = new Set([
|
|
13
|
+
Int8Array,
|
|
14
|
+
Uint8Array,
|
|
15
|
+
Uint8ClampedArray,
|
|
16
|
+
Int16Array,
|
|
17
|
+
Uint16Array,
|
|
18
|
+
Int32Array,
|
|
19
|
+
Uint32Array,
|
|
20
|
+
Float32Array,
|
|
21
|
+
Float64Array
|
|
22
|
+
].map((T) => Object.getPrototypeOf(T.prototype)));
|
|
23
|
+
function isTypedArray(obj) {
|
|
24
|
+
return ArrayBuffer.isView(obj) && !(obj instanceof DataView) && typedArrayProtos.has(Object.getPrototypeOf(Object.getPrototypeOf(obj)));
|
|
25
|
+
}
|
|
26
|
+
function makeReplacement(original, getTicks) {
|
|
27
|
+
return (ctx) => function(...args) {
|
|
28
|
+
checkTicksAndThrow(ctx, getTicks(this, args));
|
|
29
|
+
return original.apply(this, args);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
var arr = [];
|
|
33
|
+
var arrProto = Array.prototype;
|
|
34
|
+
function arrayTicks(complexity, original) {
|
|
35
|
+
return (thisArg, args) => {
|
|
36
|
+
if (!Array.isArray(thisArg)) return 0n;
|
|
37
|
+
const n = BigInt(thisArg.length);
|
|
38
|
+
switch (complexity) {
|
|
39
|
+
case "one": return 1n;
|
|
40
|
+
case "n": return n;
|
|
41
|
+
case "nlogn": return thisArg.length <= 1 ? 1n : n * BigInt(Math.round(Math.log2(thisArg.length)));
|
|
42
|
+
case "arrs": {
|
|
43
|
+
let ticks = 0n;
|
|
44
|
+
const maxDepth = original === arr.flat ? typeof args[0] === "number" ? args[0] : 1 : 1;
|
|
45
|
+
const recurse = (a, depth = 0) => {
|
|
46
|
+
ticks += BigInt(a.length);
|
|
47
|
+
if (depth >= maxDepth) return;
|
|
48
|
+
for (const item of a) if (Array.isArray(item)) recurse(item, depth + 1);
|
|
49
|
+
};
|
|
50
|
+
recurse(thisArg);
|
|
51
|
+
return ticks;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
var arrayReplacementDefs = [
|
|
57
|
+
[arr.push, "one"],
|
|
58
|
+
[arr.pop, "one"],
|
|
59
|
+
[arrProto.at, "one"],
|
|
60
|
+
[arr.fill, "n"],
|
|
61
|
+
[arr.includes, "n"],
|
|
62
|
+
[arr.indexOf, "n"],
|
|
63
|
+
[arr.lastIndexOf, "n"],
|
|
64
|
+
[arr.find, "n"],
|
|
65
|
+
[arr.findIndex, "n"],
|
|
66
|
+
[arrProto.findLast, "n"],
|
|
67
|
+
[arrProto.findLastIndex, "n"],
|
|
68
|
+
[arr.forEach, "n"],
|
|
69
|
+
[arr.map, "n"],
|
|
70
|
+
[arr.filter, "n"],
|
|
71
|
+
[arr.reduce, "n"],
|
|
72
|
+
[arr.reduceRight, "n"],
|
|
73
|
+
[arr.every, "n"],
|
|
74
|
+
[arr.some, "n"],
|
|
75
|
+
[arr.join, "n"],
|
|
76
|
+
[arr.reverse, "n"],
|
|
77
|
+
[arr.shift, "n"],
|
|
78
|
+
[arr.unshift, "n"],
|
|
79
|
+
[arr.splice, "n"],
|
|
80
|
+
[arr.slice, "n"],
|
|
81
|
+
[arr.copyWithin, "n"],
|
|
82
|
+
[arr.entries, "n"],
|
|
83
|
+
[arr.keys, "n"],
|
|
84
|
+
[arr.values, "n"],
|
|
85
|
+
[arrProto.toReversed, "n"],
|
|
86
|
+
[arrProto.toSpliced, "n"],
|
|
87
|
+
[arrProto.with, "n"],
|
|
88
|
+
[arr.toString, "n"],
|
|
89
|
+
[arr.toLocaleString, "n"],
|
|
90
|
+
[arr.sort, "nlogn"],
|
|
91
|
+
[arrProto.toSorted, "nlogn"],
|
|
92
|
+
[arr.flat, "arrs"],
|
|
93
|
+
[arr.flatMap, "arrs"],
|
|
94
|
+
[arr.concat, "arrs"]
|
|
95
|
+
];
|
|
96
|
+
var str = "";
|
|
97
|
+
var strProto = String.prototype;
|
|
98
|
+
function stringTicks(complexity) {
|
|
99
|
+
return (thisArg) => {
|
|
100
|
+
if (typeof thisArg !== "string") return 0n;
|
|
101
|
+
return complexity === "one" ? 1n : BigInt(thisArg.length);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
var stringReplacementDefs = [
|
|
105
|
+
[str.charAt, "one"],
|
|
106
|
+
[str.charCodeAt, "one"],
|
|
107
|
+
[str.codePointAt, "one"],
|
|
108
|
+
[strProto.at, "one"],
|
|
109
|
+
[str.indexOf, "n"],
|
|
110
|
+
[str.lastIndexOf, "n"],
|
|
111
|
+
[str.includes, "n"],
|
|
112
|
+
[str.startsWith, "n"],
|
|
113
|
+
[str.endsWith, "n"],
|
|
114
|
+
[str.slice, "n"],
|
|
115
|
+
[str.substring, "n"],
|
|
116
|
+
[str.padStart, "n"],
|
|
117
|
+
[str.padEnd, "n"],
|
|
118
|
+
[str.repeat, "n"],
|
|
119
|
+
[str.split, "n"],
|
|
120
|
+
[str.replace, "n"],
|
|
121
|
+
[strProto.replaceAll, "n"],
|
|
122
|
+
[str.match, "n"],
|
|
123
|
+
[str.matchAll, "n"],
|
|
124
|
+
[str.search, "n"],
|
|
125
|
+
[str.trim, "n"],
|
|
126
|
+
[str.trimStart, "n"],
|
|
127
|
+
[str.trimEnd, "n"],
|
|
128
|
+
[str.toLowerCase, "n"],
|
|
129
|
+
[str.toUpperCase, "n"],
|
|
130
|
+
[str.toLocaleLowerCase, "n"],
|
|
131
|
+
[str.toLocaleUpperCase, "n"],
|
|
132
|
+
[str.normalize, "n"],
|
|
133
|
+
[str.concat, "n"],
|
|
134
|
+
[str.toString, "n"],
|
|
135
|
+
[str.valueOf, "n"]
|
|
136
|
+
];
|
|
137
|
+
var _map = /* @__PURE__ */ new Map();
|
|
138
|
+
var mapReplacementDefs = [
|
|
139
|
+
[_map.get, "one"],
|
|
140
|
+
[_map.set, "one"],
|
|
141
|
+
[_map.has, "one"],
|
|
142
|
+
[_map.delete, "one"],
|
|
143
|
+
[_map.keys, "n"],
|
|
144
|
+
[_map.values, "n"],
|
|
145
|
+
[_map.entries, "n"],
|
|
146
|
+
[_map.forEach, "n"],
|
|
147
|
+
[_map.clear, "n"]
|
|
148
|
+
];
|
|
149
|
+
function mapTicks(complexity) {
|
|
150
|
+
return (thisArg) => {
|
|
151
|
+
if (!(thisArg instanceof Map)) return 0n;
|
|
152
|
+
return complexity === "one" ? 1n : BigInt(thisArg.size);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
var _set = /* @__PURE__ */ new Set();
|
|
156
|
+
var setReplacementDefs = [
|
|
157
|
+
[_set.add, "one"],
|
|
158
|
+
[_set.has, "one"],
|
|
159
|
+
[_set.delete, "one"],
|
|
160
|
+
[_set.values, "n"],
|
|
161
|
+
[_set.keys, "n"],
|
|
162
|
+
[_set.entries, "n"],
|
|
163
|
+
[_set.forEach, "n"],
|
|
164
|
+
[_set.clear, "n"]
|
|
165
|
+
];
|
|
166
|
+
function setTicks(complexity) {
|
|
167
|
+
return (thisArg) => {
|
|
168
|
+
if (!(thisArg instanceof Set)) return 0n;
|
|
169
|
+
return complexity === "one" ? 1n : BigInt(thisArg.size);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
var typedArrayReplacementDefs = [];
|
|
173
|
+
for (const proto of typedArrayProtos) {
|
|
174
|
+
if (proto.at) typedArrayReplacementDefs.push([proto.at, "one"]);
|
|
175
|
+
if (proto.set) typedArrayReplacementDefs.push([proto.set, "one"]);
|
|
176
|
+
for (const m of [
|
|
177
|
+
"fill",
|
|
178
|
+
"find",
|
|
179
|
+
"findIndex",
|
|
180
|
+
"findLast",
|
|
181
|
+
"findLastIndex",
|
|
182
|
+
"includes",
|
|
183
|
+
"indexOf",
|
|
184
|
+
"lastIndexOf",
|
|
185
|
+
"forEach",
|
|
186
|
+
"map",
|
|
187
|
+
"filter",
|
|
188
|
+
"reduce",
|
|
189
|
+
"reduceRight",
|
|
190
|
+
"every",
|
|
191
|
+
"some",
|
|
192
|
+
"join",
|
|
193
|
+
"reverse",
|
|
194
|
+
"slice",
|
|
195
|
+
"subarray",
|
|
196
|
+
"copyWithin",
|
|
197
|
+
"entries",
|
|
198
|
+
"keys",
|
|
199
|
+
"values",
|
|
200
|
+
"toReversed",
|
|
201
|
+
"with",
|
|
202
|
+
"toString",
|
|
203
|
+
"toLocaleString"
|
|
204
|
+
]) if (proto[m]) typedArrayReplacementDefs.push([proto[m], "n"]);
|
|
205
|
+
if (proto.sort) typedArrayReplacementDefs.push([proto.sort, "nlogn"]);
|
|
206
|
+
if (proto.toSorted) typedArrayReplacementDefs.push([proto.toSorted, "nlogn"]);
|
|
207
|
+
}
|
|
208
|
+
function typedArrayTicks(complexity) {
|
|
209
|
+
return (thisArg) => {
|
|
210
|
+
if (!isTypedArray(thisArg)) return 0n;
|
|
211
|
+
const n = BigInt(thisArg.length);
|
|
212
|
+
switch (complexity) {
|
|
213
|
+
case "one": return 1n;
|
|
214
|
+
case "n": return n;
|
|
215
|
+
case "nlogn": return thisArg.length <= 1 ? 1n : n * BigInt(Math.round(Math.log2(thisArg.length)));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
var mathReplacementDefs = [
|
|
220
|
+
[Math.max],
|
|
221
|
+
[Math.min],
|
|
222
|
+
[Math.hypot]
|
|
223
|
+
];
|
|
224
|
+
var _re = /x/;
|
|
225
|
+
var regexpReplacementDefs = [
|
|
226
|
+
_re.exec,
|
|
227
|
+
_re.test,
|
|
228
|
+
_re[Symbol.match],
|
|
229
|
+
_re[Symbol.matchAll],
|
|
230
|
+
_re[Symbol.replace],
|
|
231
|
+
_re[Symbol.search],
|
|
232
|
+
_re[Symbol.split]
|
|
233
|
+
];
|
|
234
|
+
var promiseReplacementDefs = [
|
|
235
|
+
Promise.all,
|
|
236
|
+
Promise.allSettled,
|
|
237
|
+
Promise.race,
|
|
238
|
+
...typeof Promise.any === "function" ? [Promise.any] : []
|
|
239
|
+
];
|
|
240
|
+
var objectReplacementDefs = [
|
|
241
|
+
[Object.prototype.hasOwnProperty, "one"],
|
|
242
|
+
[Object.prototype.propertyIsEnumerable, "one"],
|
|
243
|
+
[Object.prototype.isPrototypeOf, "one"],
|
|
244
|
+
[Object.create, "one"],
|
|
245
|
+
[Object.getPrototypeOf, "one"],
|
|
246
|
+
[Object.setPrototypeOf, "one"],
|
|
247
|
+
[Object.is, "one"],
|
|
248
|
+
[Object.defineProperty, "one"],
|
|
249
|
+
[Object.getOwnPropertyDescriptor, "one"],
|
|
250
|
+
[Object.isExtensible, "one"],
|
|
251
|
+
[Object.preventExtensions, "one"],
|
|
252
|
+
[Object.keys, "n"],
|
|
253
|
+
[Object.values, "n"],
|
|
254
|
+
[Object.entries, "n"],
|
|
255
|
+
[Object.assign, "n"],
|
|
256
|
+
[Object.fromEntries, "n"],
|
|
257
|
+
[Object.getOwnPropertyNames, "n"],
|
|
258
|
+
[Object.getOwnPropertySymbols, "n"],
|
|
259
|
+
[Object.getOwnPropertyDescriptors, "n"],
|
|
260
|
+
[Object.freeze, "n"],
|
|
261
|
+
[Object.seal, "n"],
|
|
262
|
+
[Object.isFrozen, "n"],
|
|
263
|
+
[Object.isSealed, "n"]
|
|
264
|
+
];
|
|
265
|
+
function objectTicks(complexity, isStatic) {
|
|
266
|
+
return (thisArg, args) => {
|
|
267
|
+
if (complexity === "one") return 1n;
|
|
268
|
+
const target = isStatic ? args[0] : thisArg;
|
|
269
|
+
if (target !== null && typeof target === "object") return BigInt(Object.keys(target).length);
|
|
270
|
+
return 1n;
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
var staticObjectMethods = new Set([
|
|
274
|
+
Object.keys,
|
|
275
|
+
Object.values,
|
|
276
|
+
Object.entries,
|
|
277
|
+
Object.assign,
|
|
278
|
+
Object.fromEntries,
|
|
279
|
+
Object.getOwnPropertyNames,
|
|
280
|
+
Object.getOwnPropertySymbols,
|
|
281
|
+
Object.getOwnPropertyDescriptors,
|
|
282
|
+
Object.freeze,
|
|
283
|
+
Object.seal,
|
|
284
|
+
Object.isFrozen,
|
|
285
|
+
Object.isSealed
|
|
286
|
+
]);
|
|
287
|
+
var DEFAULT_FUNCTION_REPLACEMENTS = /* @__PURE__ */ new Map();
|
|
288
|
+
var THIS_DEPENDENT_FUNCTION_REPLACEMENTS = /* @__PURE__ */ new Set();
|
|
289
|
+
for (const [original, complexity] of arrayReplacementDefs) {
|
|
290
|
+
if (!original) continue;
|
|
291
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, arrayTicks(complexity, original)));
|
|
292
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
293
|
+
}
|
|
294
|
+
for (const [original, complexity] of stringReplacementDefs) {
|
|
295
|
+
if (!original) continue;
|
|
296
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, stringTicks(complexity)));
|
|
297
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
298
|
+
}
|
|
299
|
+
for (const [original, complexity] of mapReplacementDefs) {
|
|
300
|
+
if (!original) continue;
|
|
301
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, mapTicks(complexity)));
|
|
302
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
303
|
+
}
|
|
304
|
+
for (const [original, complexity] of setReplacementDefs) {
|
|
305
|
+
if (!original) continue;
|
|
306
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, setTicks(complexity)));
|
|
307
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
308
|
+
}
|
|
309
|
+
for (const [original, complexity] of typedArrayReplacementDefs) {
|
|
310
|
+
if (!original) continue;
|
|
311
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, typedArrayTicks(complexity)));
|
|
312
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
313
|
+
}
|
|
314
|
+
for (const [original] of mathReplacementDefs) {
|
|
315
|
+
if (!original) continue;
|
|
316
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, (_thisArg, args) => BigInt(args.length)));
|
|
317
|
+
}
|
|
318
|
+
for (const original of [JSON.parse, JSON.stringify]) DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, (_thisArg, args) => {
|
|
319
|
+
const target = args[0];
|
|
320
|
+
if (typeof target === "string") return BigInt(target.length);
|
|
321
|
+
if (target !== null && typeof target === "object") return BigInt(Object.keys(target).length);
|
|
322
|
+
return 1n;
|
|
323
|
+
}));
|
|
324
|
+
for (const original of regexpReplacementDefs) {
|
|
325
|
+
if (!original) continue;
|
|
326
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, (_thisArg, args) => {
|
|
327
|
+
const input = args[0];
|
|
328
|
+
return typeof input === "string" ? BigInt(input.length) : 1n;
|
|
329
|
+
}));
|
|
330
|
+
THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
331
|
+
}
|
|
332
|
+
for (const original of promiseReplacementDefs) {
|
|
333
|
+
if (!original) continue;
|
|
334
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, (_thisArg, args) => {
|
|
335
|
+
const iterable = args[0];
|
|
336
|
+
return Array.isArray(iterable) ? BigInt(iterable.length) : 0n;
|
|
337
|
+
}));
|
|
338
|
+
}
|
|
339
|
+
for (const [original, complexity] of objectReplacementDefs) {
|
|
340
|
+
if (!original) continue;
|
|
341
|
+
const isStatic = staticObjectMethods.has(original);
|
|
342
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(original, makeReplacement(original, objectTicks(complexity, isStatic)));
|
|
343
|
+
if (!isStatic) THIS_DEPENDENT_FUNCTION_REPLACEMENTS.add(original);
|
|
344
|
+
}
|
|
345
|
+
if (Array.from) DEFAULT_FUNCTION_REPLACEMENTS.set(Array.from, makeReplacement(Array.from, (_thisArg, args) => {
|
|
346
|
+
const source = args[0];
|
|
347
|
+
if (source != null && typeof source.length === "number") return BigInt(source.length);
|
|
348
|
+
return 0n;
|
|
349
|
+
}));
|
|
350
|
+
if (typeof Array.fromAsync === "function") {
|
|
351
|
+
const fromAsync = Array.fromAsync;
|
|
352
|
+
DEFAULT_FUNCTION_REPLACEMENTS.set(fromAsync, makeReplacement(fromAsync, (_thisArg, args) => {
|
|
353
|
+
const source = args[0];
|
|
354
|
+
if (source != null && typeof source.length === "number") return BigInt(source.length);
|
|
355
|
+
return 0n;
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
exports.DEFAULT_FUNCTION_REPLACEMENTS = DEFAULT_FUNCTION_REPLACEMENTS;
|
|
360
|
+
exports.THIS_DEPENDENT_FUNCTION_REPLACEMENTS = THIS_DEPENDENT_FUNCTION_REPLACEMENTS;
|
|
361
|
+
exports.checkTicksAndThrow = checkTicksAndThrow;
|
|
362
|
+
exports.typedArrayProtos = typedArrayProtos;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { IEvalContext } from '../eval';
|
|
2
|
+
import { Change } from '../executor';
|
|
3
|
+
import { IExecutionTree } from '../parser';
|
|
4
|
+
import { default as SandboxExec } from '../SandboxExec';
|
|
5
|
+
import { Scope } from './Scope';
|
|
6
|
+
export declare const AsyncFunction: Function;
|
|
7
|
+
export declare const GeneratorFunction: Function;
|
|
8
|
+
export declare const AsyncGeneratorFunction: Function;
|
|
9
|
+
export interface IOptionParams {
|
|
10
|
+
audit?: boolean;
|
|
11
|
+
forbidFunctionCalls?: boolean;
|
|
12
|
+
forbidFunctionCreation?: boolean;
|
|
13
|
+
prototypeWhitelist?: Map<Function, Set<string>>;
|
|
14
|
+
globals?: IGlobals;
|
|
15
|
+
symbolWhitelist?: ISymbolWhitelist;
|
|
16
|
+
executionQuota?: bigint;
|
|
17
|
+
nonBlocking?: boolean;
|
|
18
|
+
haltOnSandboxError?: boolean;
|
|
19
|
+
maxParserRecursionDepth?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Additional function replacements to merge with the built-in tick-checking replacements.
|
|
22
|
+
* Maps a native function to a factory that receives the IContext and returns the replacement.
|
|
23
|
+
* When sandboxed code accesses a property that returns a mapped function, the factory is
|
|
24
|
+
* called once per context and the result is cached and returned instead.
|
|
25
|
+
*/
|
|
26
|
+
functionReplacements?: Map<Function, (ctx: IContext) => Function>;
|
|
27
|
+
}
|
|
28
|
+
export interface IOptions {
|
|
29
|
+
audit: boolean;
|
|
30
|
+
forbidFunctionCalls: boolean;
|
|
31
|
+
forbidFunctionCreation: boolean;
|
|
32
|
+
prototypeWhitelist: Map<Function, Set<PropertyKey>>;
|
|
33
|
+
globals: IGlobals;
|
|
34
|
+
symbolWhitelist: ISymbolWhitelist;
|
|
35
|
+
executionQuota?: bigint;
|
|
36
|
+
haltOnSandboxError?: boolean;
|
|
37
|
+
maxParserRecursionDepth: number;
|
|
38
|
+
nonBlocking: boolean;
|
|
39
|
+
functionReplacements: Map<Function, (ctx: IExecContext, builtInReplacement?: Function) => Function>;
|
|
40
|
+
}
|
|
41
|
+
export interface IContext {
|
|
42
|
+
sandbox: SandboxExec;
|
|
43
|
+
globalScope: Scope;
|
|
44
|
+
sandboxGlobal: ISandboxGlobal;
|
|
45
|
+
globalsWhitelist: Set<any>;
|
|
46
|
+
prototypeWhitelist: Map<any, Set<PropertyKey>>;
|
|
47
|
+
sandboxedFunctions: WeakSet<Function>;
|
|
48
|
+
sandboxSymbols: SandboxSymbolContext;
|
|
49
|
+
options: IOptions;
|
|
50
|
+
auditReport?: IAuditReport;
|
|
51
|
+
ticks: Ticks;
|
|
52
|
+
}
|
|
53
|
+
export interface IAuditReport {
|
|
54
|
+
globalsAccess: Set<unknown>;
|
|
55
|
+
prototypeAccess: {
|
|
56
|
+
[name: string]: Set<PropertyKey>;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export interface Ticks {
|
|
60
|
+
ticks: bigint;
|
|
61
|
+
tickLimit?: bigint;
|
|
62
|
+
nextYield?: bigint;
|
|
63
|
+
}
|
|
64
|
+
export type SubscriptionSubject = object;
|
|
65
|
+
export type HaltContext = {
|
|
66
|
+
type: 'error';
|
|
67
|
+
error: Error;
|
|
68
|
+
ticks: Ticks;
|
|
69
|
+
scope: Scope;
|
|
70
|
+
context: IExecContext;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'manual';
|
|
73
|
+
error?: never;
|
|
74
|
+
ticks?: never;
|
|
75
|
+
scope?: never;
|
|
76
|
+
context?: never;
|
|
77
|
+
} | {
|
|
78
|
+
type: 'yield';
|
|
79
|
+
error?: never;
|
|
80
|
+
ticks?: never;
|
|
81
|
+
scope?: never;
|
|
82
|
+
context?: never;
|
|
83
|
+
};
|
|
84
|
+
export interface IExecContext extends IExecutionTree {
|
|
85
|
+
ctx: IContext;
|
|
86
|
+
getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
|
|
87
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
88
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
89
|
+
setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
90
|
+
changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
91
|
+
registerSandboxFunction: (fn: (...args: any[]) => any) => void;
|
|
92
|
+
evals: Map<Function, Function>;
|
|
93
|
+
allowJit: boolean;
|
|
94
|
+
evalContext?: IEvalContext;
|
|
95
|
+
}
|
|
96
|
+
export interface ISandboxGlobal {
|
|
97
|
+
[key: string]: unknown;
|
|
98
|
+
}
|
|
99
|
+
export interface ISymbolWhitelist {
|
|
100
|
+
[key: string]: symbol;
|
|
101
|
+
}
|
|
102
|
+
export interface SandboxSymbolContext {
|
|
103
|
+
ctor?: Function;
|
|
104
|
+
registry: Map<string, symbol>;
|
|
105
|
+
reverseRegistry: Map<symbol, string>;
|
|
106
|
+
whitelist: ISymbolWhitelist;
|
|
107
|
+
}
|
|
108
|
+
export type IGlobals = ISandboxGlobal;
|
|
109
|
+
export interface IScope {
|
|
110
|
+
[key: string]: any;
|
|
111
|
+
}
|
|
112
|
+
export declare const NON_BLOCKING_THRESHOLD = 50000n;
|
|
113
|
+
export declare const reservedWords: Set<string>;
|
|
114
|
+
export declare const enum VarType {
|
|
115
|
+
let = "let",
|
|
116
|
+
const = "const",
|
|
117
|
+
var = "var",
|
|
118
|
+
internal = "internal"
|
|
119
|
+
}
|
|
120
|
+
export declare const enum LispType {
|
|
121
|
+
None = 0,
|
|
122
|
+
Prop = 1,
|
|
123
|
+
StringIndex = 2,
|
|
124
|
+
Let = 3,
|
|
125
|
+
Const = 4,
|
|
126
|
+
Call = 5,
|
|
127
|
+
KeyVal = 6,
|
|
128
|
+
Number = 7,
|
|
129
|
+
Return = 8,
|
|
130
|
+
Assign = 9,
|
|
131
|
+
InlineFunction = 10,
|
|
132
|
+
ArrowFunction = 11,
|
|
133
|
+
CreateArray = 12,
|
|
134
|
+
If = 13,
|
|
135
|
+
IfCase = 14,
|
|
136
|
+
InlineIf = 15,
|
|
137
|
+
InlineIfCase = 16,
|
|
138
|
+
SpreadObject = 17,
|
|
139
|
+
SpreadArray = 18,
|
|
140
|
+
ArrayProp = 19,
|
|
141
|
+
PropOptional = 20,
|
|
142
|
+
CallOptional = 21,
|
|
143
|
+
CreateObject = 22,
|
|
144
|
+
Group = 23,
|
|
145
|
+
Not = 24,
|
|
146
|
+
IncrementBefore = 25,
|
|
147
|
+
IncrementAfter = 26,
|
|
148
|
+
DecrementBefore = 27,
|
|
149
|
+
DecrementAfter = 28,
|
|
150
|
+
And = 29,
|
|
151
|
+
Or = 30,
|
|
152
|
+
StrictNotEqual = 31,
|
|
153
|
+
StrictEqual = 32,
|
|
154
|
+
Plus = 33,
|
|
155
|
+
Var = 34,
|
|
156
|
+
GlobalSymbol = 35,
|
|
157
|
+
Literal = 36,
|
|
158
|
+
Function = 37,
|
|
159
|
+
Loop = 38,
|
|
160
|
+
Try = 39,
|
|
161
|
+
Switch = 40,
|
|
162
|
+
SwitchCase = 41,
|
|
163
|
+
InternalBlock = 42,
|
|
164
|
+
Expression = 43,
|
|
165
|
+
Await = 44,
|
|
166
|
+
New = 45,
|
|
167
|
+
Throw = 46,
|
|
168
|
+
Minus = 47,
|
|
169
|
+
Divide = 48,
|
|
170
|
+
Power = 49,
|
|
171
|
+
Multiply = 50,
|
|
172
|
+
Modulus = 51,
|
|
173
|
+
Equal = 52,
|
|
174
|
+
NotEqual = 53,
|
|
175
|
+
SmallerEqualThan = 54,
|
|
176
|
+
LargerEqualThan = 55,
|
|
177
|
+
SmallerThan = 56,
|
|
178
|
+
LargerThan = 57,
|
|
179
|
+
Negative = 58,
|
|
180
|
+
Positive = 59,
|
|
181
|
+
Typeof = 60,
|
|
182
|
+
Delete = 61,
|
|
183
|
+
Instanceof = 62,
|
|
184
|
+
In = 63,
|
|
185
|
+
Inverse = 64,
|
|
186
|
+
SubractEquals = 65,
|
|
187
|
+
AddEquals = 66,
|
|
188
|
+
DivideEquals = 67,
|
|
189
|
+
PowerEquals = 68,
|
|
190
|
+
MultiplyEquals = 69,
|
|
191
|
+
ModulusEquals = 70,
|
|
192
|
+
BitNegateEquals = 71,
|
|
193
|
+
BitAndEquals = 72,
|
|
194
|
+
BitOrEquals = 73,
|
|
195
|
+
UnsignedShiftRightEquals = 74,
|
|
196
|
+
ShiftRightEquals = 75,
|
|
197
|
+
ShiftLeftEquals = 76,
|
|
198
|
+
BitAnd = 77,
|
|
199
|
+
BitOr = 78,
|
|
200
|
+
BitNegate = 79,
|
|
201
|
+
BitShiftLeft = 80,
|
|
202
|
+
BitShiftRight = 81,
|
|
203
|
+
BitUnsignedShiftRight = 82,
|
|
204
|
+
BigInt = 83,
|
|
205
|
+
LiteralIndex = 84,
|
|
206
|
+
RegexIndex = 85,
|
|
207
|
+
LoopAction = 86,
|
|
208
|
+
Void = 87,
|
|
209
|
+
True = 88,
|
|
210
|
+
NullishCoalescing = 89,
|
|
211
|
+
AndEquals = 90,
|
|
212
|
+
OrEquals = 91,
|
|
213
|
+
NullishCoalescingEquals = 92,
|
|
214
|
+
Block = 93,
|
|
215
|
+
Labeled = 94,
|
|
216
|
+
Internal = 95,
|
|
217
|
+
Yield = 96,
|
|
218
|
+
YieldDelegate = 97,
|
|
219
|
+
Hole = 98,
|
|
220
|
+
LispEnumSize = 99
|
|
221
|
+
}
|