scriptable 0.0.6 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/workflows/release.yml +62 -0
- package/CHANGELOG.md +14 -0
- package/README.md +185 -0
- package/dist/binary-ops.d.ts +2 -0
- package/dist/binary-ops.js +16 -0
- package/dist/builtin-functions.d.ts +3 -0
- package/dist/builtin-functions.js +151 -0
- package/dist/environment.d.ts +16 -0
- package/dist/environment.js +111 -0
- package/dist/evaluate.d.ts +5 -0
- package/dist/evaluate.js +232 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +26 -0
- package/dist/logic-ops.d.ts +2 -0
- package/dist/logic-ops.js +5 -0
- package/package.json +22 -52
- package/scripts/release.js +127 -0
- package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
- package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
- package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
- package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
- package/src/__tests__/{functions.js → functions.test.ts} +7 -8
- package/src/__tests__/{logic.js → logic.test.ts} +2 -3
- package/src/__tests__/new-features.test.ts +158 -0
- package/src/__tests__/security.test.ts +399 -0
- package/src/__tests__/{variables.js → variables.test.ts} +3 -6
- package/src/binary-ops.ts +18 -0
- package/src/builtin-functions.ts +177 -0
- package/src/environment.ts +132 -0
- package/src/evaluate.ts +275 -0
- package/src/index.ts +42 -0
- package/src/logic-ops.ts +6 -0
- package/tsconfig.json +16 -0
- package/.npmignore +0 -1
- package/src/__tests__/index.js +0 -85
- package/src/binary-ops.js +0 -13
- package/src/builtin-functions.js +0 -58
- package/src/environment.js +0 -73
- package/src/evaluate.js +0 -174
- package/src/index.js +0 -26
- package/src/logic-ops.js +0 -6
- package/yarn.lock +0 -2632
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const DEBUG = true;
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
4
3
|
|
|
5
4
|
const run = runner();
|
|
6
5
|
|
|
@@ -29,11 +28,11 @@ describe("function", () => {
|
|
|
29
28
|
|
|
30
29
|
it("has access to env", () => {
|
|
31
30
|
const code = `
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
greet = (name) => "hello " + name;
|
|
32
|
+
greet(customer.name);
|
|
34
33
|
`;
|
|
35
|
-
const { result } = run(code, { customer: {
|
|
36
|
-
expect(result).toEqual("
|
|
34
|
+
const { result } = run(code, { customer: { name: "Jason" } });
|
|
35
|
+
expect(result).toEqual("hello Jason");
|
|
37
36
|
});
|
|
38
37
|
|
|
39
38
|
it("can call a builtin with a function as arg", () => {
|
|
@@ -44,7 +43,7 @@ describe("function", () => {
|
|
|
44
43
|
const { env } = run(code, vars);
|
|
45
44
|
expect(env).toEqual({
|
|
46
45
|
prices: [11, 22, 33],
|
|
47
|
-
nprices: [11, 22, 33]
|
|
46
|
+
nprices: [11, 22, 33],
|
|
48
47
|
});
|
|
49
48
|
});
|
|
50
49
|
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
const run = runner();
|
|
5
|
+
|
|
6
|
+
describe("template literals", () => {
|
|
7
|
+
it("evaluates a simple template literal", () => {
|
|
8
|
+
const code = "result = `hello world`";
|
|
9
|
+
const { env } = run(code, {});
|
|
10
|
+
expect(env.result).toEqual("hello world");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("evaluates template literal with expression", () => {
|
|
14
|
+
const code = "name = \"Jason\"; result = `hello ${name}`";
|
|
15
|
+
const { env } = run(code, {});
|
|
16
|
+
expect(env.result).toEqual("hello Jason");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("evaluates template literal with multiple expressions", () => {
|
|
20
|
+
const code = "a = 1; b = 2; result = `${a} + ${b} = ${a + b}`";
|
|
21
|
+
const { env } = run(code, {});
|
|
22
|
+
expect(env.result).toEqual("1 + 2 = 3");
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("conditional (ternary) expression", () => {
|
|
27
|
+
it("evaluates truthy ternary", () => {
|
|
28
|
+
const code = `result = true ? "yes" : "no"`;
|
|
29
|
+
const { env } = run(code, {});
|
|
30
|
+
expect(env.result).toEqual("yes");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("evaluates falsy ternary", () => {
|
|
34
|
+
const code = `result = false ? "yes" : "no"`;
|
|
35
|
+
const { env } = run(code, {});
|
|
36
|
+
expect(env.result).toEqual("no");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("evaluates ternary with complex test", () => {
|
|
40
|
+
const code = `x = 5; result = x > 3 ? "big" : "small"`;
|
|
41
|
+
const { env } = run(code, {});
|
|
42
|
+
expect(env.result).toEqual("big");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("spread element", () => {
|
|
47
|
+
it("spreads in array literal", () => {
|
|
48
|
+
const code = `
|
|
49
|
+
a = [1, 2];
|
|
50
|
+
b = [...a, 3, 4];
|
|
51
|
+
`;
|
|
52
|
+
const { env } = run(code, {});
|
|
53
|
+
expect(env.b).toEqual([1, 2, 3, 4]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("spreads in function call", () => {
|
|
57
|
+
const code = `
|
|
58
|
+
args = [1, 2, 3];
|
|
59
|
+
result = concat(args, ...args);
|
|
60
|
+
`;
|
|
61
|
+
const { env } = run(code, {});
|
|
62
|
+
expect(env.result).toEqual([1, 2, 3, 1, 2, 3]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("spreads in object literal", () => {
|
|
66
|
+
const code = `
|
|
67
|
+
base = { a: 1, b: 2 };
|
|
68
|
+
result = { ...base, c: 3 };
|
|
69
|
+
`;
|
|
70
|
+
const { env } = run(code, {});
|
|
71
|
+
expect(env.result).toEqual({ a: 1, b: 2, c: 3 });
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("unary expressions", () => {
|
|
76
|
+
it("negation", () => {
|
|
77
|
+
const code = `result = !true`;
|
|
78
|
+
const { env } = run(code, {});
|
|
79
|
+
expect(env.result).toEqual(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("unary minus", () => {
|
|
83
|
+
const code = `result = -5`;
|
|
84
|
+
const { env } = run(code, {});
|
|
85
|
+
expect(env.result).toEqual(-5);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("typeof", () => {
|
|
89
|
+
const code = `result = typeof "hello"`;
|
|
90
|
+
const { env } = run(code, {});
|
|
91
|
+
expect(env.result).toEqual("string");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("const/let declarations with expressions", () => {
|
|
96
|
+
it("const with function call", () => {
|
|
97
|
+
const code = `
|
|
98
|
+
const items = concat([1, 2], 3);
|
|
99
|
+
`;
|
|
100
|
+
const { env } = run(code, {});
|
|
101
|
+
expect(env.items).toEqual([1, 2, 3]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("let with binary expression", () => {
|
|
105
|
+
const code = `
|
|
106
|
+
let x = 5 + 3;
|
|
107
|
+
`;
|
|
108
|
+
const { env } = run(code, {});
|
|
109
|
+
expect(env.x).toEqual(8);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("if/else", () => {
|
|
114
|
+
it("evaluates else branch", () => {
|
|
115
|
+
const code = `
|
|
116
|
+
if (false) {
|
|
117
|
+
result = "yes";
|
|
118
|
+
} else {
|
|
119
|
+
result = "no";
|
|
120
|
+
}
|
|
121
|
+
`;
|
|
122
|
+
const { env } = run(code, {});
|
|
123
|
+
expect(env.result).toEqual("no");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("evaluates else-if chain", () => {
|
|
127
|
+
const code = `
|
|
128
|
+
x = 2;
|
|
129
|
+
if (x == 1) {
|
|
130
|
+
result = "one";
|
|
131
|
+
} else if (x == 2) {
|
|
132
|
+
result = "two";
|
|
133
|
+
} else {
|
|
134
|
+
result = "other";
|
|
135
|
+
}
|
|
136
|
+
`;
|
|
137
|
+
const { env } = run(code, {});
|
|
138
|
+
expect(env.result).toEqual("two");
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("custom injected functions", () => {
|
|
143
|
+
it("uses injected functions", () => {
|
|
144
|
+
const run = runner({
|
|
145
|
+
functions: {
|
|
146
|
+
add: (a: number, b: number) => a + b,
|
|
147
|
+
greet: (name: string) => `hello ${name}`,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const code = `
|
|
151
|
+
const sum = add(1, 2);
|
|
152
|
+
const msg = greet("world");
|
|
153
|
+
`;
|
|
154
|
+
const { env } = run(code, {});
|
|
155
|
+
expect(env.sum).toEqual(3);
|
|
156
|
+
expect(env.msg).toEqual("hello world");
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
const run = runner();
|
|
5
|
+
|
|
6
|
+
// ==========================================================================
|
|
7
|
+
// 1. Sandbox escape — prototype chain to Function constructor
|
|
8
|
+
// ==========================================================================
|
|
9
|
+
|
|
10
|
+
describe("sandbox escape: prototype chain", () => {
|
|
11
|
+
it("blocks obj.constructor", () => {
|
|
12
|
+
const code = `a = {}; a.constructor`;
|
|
13
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("blocks obj.constructor.constructor (Function access)", () => {
|
|
17
|
+
const code = `a = {}; a.constructor.constructor`;
|
|
18
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("blocks array.constructor.constructor", () => {
|
|
22
|
+
const code = `a = [1,2]; a.constructor.constructor("return this")()`;
|
|
23
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("blocks string.constructor", () => {
|
|
27
|
+
const code = `a = "hello"; a.constructor`;
|
|
28
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("blocks number.constructor", () => {
|
|
32
|
+
// Wrap in parens to avoid parser ambiguity
|
|
33
|
+
const code = `a = 42; a.constructor`;
|
|
34
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("blocks function.constructor (reaches Function)", () => {
|
|
38
|
+
const code = `
|
|
39
|
+
f = (x) => x;
|
|
40
|
+
f.constructor("return this")();
|
|
41
|
+
`;
|
|
42
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("blocks constructor via computed property", () => {
|
|
46
|
+
const code = `a = {}; a["constructor"]`;
|
|
47
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("blocks constructor via computed string concatenation", () => {
|
|
51
|
+
const code = `a = {}; a["const" + "ructor"]`;
|
|
52
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("blocks constructor via variable indirection", () => {
|
|
56
|
+
const code = `
|
|
57
|
+
a = {};
|
|
58
|
+
key = "constructor";
|
|
59
|
+
a[key];
|
|
60
|
+
`;
|
|
61
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ==========================================================================
|
|
66
|
+
// 2. Sandbox escape — __proto__ access
|
|
67
|
+
// ==========================================================================
|
|
68
|
+
|
|
69
|
+
describe("sandbox escape: __proto__", () => {
|
|
70
|
+
it("blocks __proto__ read", () => {
|
|
71
|
+
const code = `a = {}; a.__proto__`;
|
|
72
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("blocks __proto__ computed read", () => {
|
|
76
|
+
const code = `a = {}; a["__proto__"]`;
|
|
77
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("blocks __proto__ assignment", () => {
|
|
81
|
+
const code = `a = {}; a.__proto__ = {}`;
|
|
82
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// 3. Sandbox escape — prototype access
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
|
|
90
|
+
describe("sandbox escape: prototype", () => {
|
|
91
|
+
it("blocks .prototype access", () => {
|
|
92
|
+
const code = `
|
|
93
|
+
a = {};
|
|
94
|
+
f = a.constructor;
|
|
95
|
+
`;
|
|
96
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("blocks .prototype via computed", () => {
|
|
100
|
+
const code = `a = {}; a["prototype"]`;
|
|
101
|
+
expect(() => run(code)).toThrow("Access to property 'prototype' is not allowed");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ==========================================================================
|
|
106
|
+
// 4. Prototype pollution via deep assignment
|
|
107
|
+
// ==========================================================================
|
|
108
|
+
|
|
109
|
+
describe("prototype pollution via deep path assignment", () => {
|
|
110
|
+
it("blocks constructor in deep assignment path", () => {
|
|
111
|
+
const code = `foo.constructor.prototype.polluted = "yes"`;
|
|
112
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
113
|
+
"Access to property 'constructor' is not allowed"
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("blocks __proto__ in deep assignment path", () => {
|
|
118
|
+
const code = `foo.__proto__.polluted = "yes"`;
|
|
119
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
120
|
+
"Access to property '__proto__' is not allowed"
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("does not pollute Object.prototype even if attempted", () => {
|
|
125
|
+
// Verify no pollution leaked from prior tests
|
|
126
|
+
expect((Object.prototype as any).polluted).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ==========================================================================
|
|
131
|
+
// 5. Sandbox escape — legacy property descriptors
|
|
132
|
+
// ==========================================================================
|
|
133
|
+
|
|
134
|
+
describe("sandbox escape: legacy descriptor methods", () => {
|
|
135
|
+
it("blocks __defineGetter__", () => {
|
|
136
|
+
const code = `a = {}; a.__defineGetter__`;
|
|
137
|
+
expect(() => run(code)).toThrow(
|
|
138
|
+
"Access to property '__defineGetter__' is not allowed"
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("blocks __defineSetter__", () => {
|
|
143
|
+
const code = `a = {}; a.__defineSetter__`;
|
|
144
|
+
expect(() => run(code)).toThrow(
|
|
145
|
+
"Access to property '__defineSetter__' is not allowed"
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("blocks __lookupGetter__", () => {
|
|
150
|
+
const code = `a = {}; a.__lookupGetter__`;
|
|
151
|
+
expect(() => run(code)).toThrow(
|
|
152
|
+
"Access to property '__lookupGetter__' is not allowed"
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("blocks __lookupSetter__", () => {
|
|
157
|
+
const code = `a = {}; a.__lookupSetter__`;
|
|
158
|
+
expect(() => run(code)).toThrow(
|
|
159
|
+
"Access to property '__lookupSetter__' is not allowed"
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// ==========================================================================
|
|
165
|
+
// 6. Sandbox escape — this / arguments / globals
|
|
166
|
+
// ==========================================================================
|
|
167
|
+
|
|
168
|
+
describe("sandbox escape: forbidden expressions", () => {
|
|
169
|
+
it("blocks this", () => {
|
|
170
|
+
const code = `this`;
|
|
171
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("blocks arguments keyword", () => {
|
|
175
|
+
const code = `
|
|
176
|
+
function f() { return arguments; }
|
|
177
|
+
f();
|
|
178
|
+
`;
|
|
179
|
+
expect(() => run(code)).toThrow('"arguments" is not defined');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("cannot access process", () => {
|
|
183
|
+
const code = `process`;
|
|
184
|
+
expect(() => run(code)).toThrow('"process" is not defined');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("cannot access require", () => {
|
|
188
|
+
const code = `require("fs")`;
|
|
189
|
+
expect(() => run(code)).toThrow("require is not a function");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("cannot access globalThis", () => {
|
|
193
|
+
const code = `globalThis`;
|
|
194
|
+
expect(() => run(code)).toThrow('"globalThis" is not defined');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("cannot access eval", () => {
|
|
198
|
+
const code = `eval("1+1")`;
|
|
199
|
+
expect(() => run(code)).toThrow("eval is not a function");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("cannot access Function", () => {
|
|
203
|
+
const code = `Function("return 1")`;
|
|
204
|
+
expect(() => run(code)).toThrow("Function is not a function");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("blocks import()", () => {
|
|
208
|
+
const code = `import("fs")`;
|
|
209
|
+
expect(() => run(code)).toThrow();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("blocks new expressions", () => {
|
|
213
|
+
const code = `new Array(10)`;
|
|
214
|
+
expect(() => run(code)).toThrow("Cannot evaluate NewExpression");
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ==========================================================================
|
|
219
|
+
// 7. DoS — infinite recursion
|
|
220
|
+
// ==========================================================================
|
|
221
|
+
|
|
222
|
+
describe("DoS: infinite recursion", () => {
|
|
223
|
+
// V8's call stack limit catches deep recursion before our step counter
|
|
224
|
+
// (each interpreter call creates many real stack frames). Either mechanism
|
|
225
|
+
// is an acceptable guard — the important thing is execution stops.
|
|
226
|
+
|
|
227
|
+
it("stops infinite recursive function", () => {
|
|
228
|
+
const code = `
|
|
229
|
+
function f() { return f(); }
|
|
230
|
+
f();
|
|
231
|
+
`;
|
|
232
|
+
expect(() => run(code)).toThrow();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("stops mutual recursion", () => {
|
|
236
|
+
const code = `
|
|
237
|
+
function a() { return b(); }
|
|
238
|
+
function b() { return a(); }
|
|
239
|
+
a();
|
|
240
|
+
`;
|
|
241
|
+
expect(() => run(code)).toThrow();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("stops deep self-calling arrow", () => {
|
|
245
|
+
const code = `
|
|
246
|
+
f = (n) => f(n);
|
|
247
|
+
f(1);
|
|
248
|
+
`;
|
|
249
|
+
expect(() => run(code)).toThrow();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("step counter catches recursion with low limit", () => {
|
|
253
|
+
const tightRun = runner({ maxSteps: 50 });
|
|
254
|
+
const code = `
|
|
255
|
+
function f(n) { if (n > 0) { return f(n - 1); } return 0; }
|
|
256
|
+
f(100);
|
|
257
|
+
`;
|
|
258
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// ==========================================================================
|
|
263
|
+
// 8. DoS — execution limit configurability
|
|
264
|
+
// ==========================================================================
|
|
265
|
+
|
|
266
|
+
describe("DoS: execution limits", () => {
|
|
267
|
+
it("respects custom maxSteps", () => {
|
|
268
|
+
const tightRun = runner({ maxSteps: 10 });
|
|
269
|
+
// Even simple code will exceed 10 steps
|
|
270
|
+
const code = `a = 1; b = 2; c = 3; d = 4; e = 5;`;
|
|
271
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("allows disabling step limit with 0", () => {
|
|
275
|
+
const noLimitRun = runner({ maxSteps: 0 });
|
|
276
|
+
const code = `a = 1; b = 2; c = a + b;`;
|
|
277
|
+
const { env } = noLimitRun(code);
|
|
278
|
+
expect(env.c).toEqual(3);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("default limit allows normal-sized scripts", () => {
|
|
282
|
+
// Build a moderately complex script
|
|
283
|
+
let code = "";
|
|
284
|
+
for (let i = 0; i < 100; i++) {
|
|
285
|
+
code += `x${i} = ${i};\n`;
|
|
286
|
+
}
|
|
287
|
+
code += `result = x0 + x99;`;
|
|
288
|
+
const { env } = run(code);
|
|
289
|
+
expect(env.result).toEqual(99);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// ==========================================================================
|
|
294
|
+
// 9. Sandbox escape via builtin return values
|
|
295
|
+
// ==========================================================================
|
|
296
|
+
|
|
297
|
+
describe("sandbox escape: via builtin return values", () => {
|
|
298
|
+
it("blocks constructor on array returned by map", () => {
|
|
299
|
+
const code = `
|
|
300
|
+
result = map([1,2], (x) => x);
|
|
301
|
+
result.constructor;
|
|
302
|
+
`;
|
|
303
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("blocks constructor on object returned by assign", () => {
|
|
307
|
+
const code = `
|
|
308
|
+
obj = assign({ a: 1 });
|
|
309
|
+
obj.constructor;
|
|
310
|
+
`;
|
|
311
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("blocks constructor on string returned by join", () => {
|
|
315
|
+
const code = `
|
|
316
|
+
s = join(["a", "b"], ",");
|
|
317
|
+
s.constructor;
|
|
318
|
+
`;
|
|
319
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// ==========================================================================
|
|
324
|
+
// 10. Sandbox escape — original break-out.js tests (preserved)
|
|
325
|
+
// ==========================================================================
|
|
326
|
+
|
|
327
|
+
describe("original break-out tests (preserved)", () => {
|
|
328
|
+
it("this.constructor.constructor", () => {
|
|
329
|
+
const code = `bar = this.constructor.constructor`;
|
|
330
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("object.constructor", () => {
|
|
334
|
+
const code = `
|
|
335
|
+
bar = { any: "thing" };
|
|
336
|
+
func = bar.constructor;
|
|
337
|
+
func2 = func("return process");
|
|
338
|
+
process = func2();
|
|
339
|
+
`;
|
|
340
|
+
expect(() => run(code)).toThrow(
|
|
341
|
+
"Access to property 'constructor' is not allowed"
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("arguments.callee.caller", () => {
|
|
346
|
+
const code = `
|
|
347
|
+
function getFunc () {
|
|
348
|
+
return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
|
|
349
|
+
}
|
|
350
|
+
func = getFunc();
|
|
351
|
+
func.call(func, "var fs = process.mainModule.require('fs');");
|
|
352
|
+
`;
|
|
353
|
+
expect(() => run(code)).toThrow(
|
|
354
|
+
new ReferenceError(`"arguments" is not defined (3:15)`)
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// ==========================================================================
|
|
360
|
+
// 11. Safe property access still works
|
|
361
|
+
// ==========================================================================
|
|
362
|
+
|
|
363
|
+
describe("safe property access still works", () => {
|
|
364
|
+
it("array .length", () => {
|
|
365
|
+
const code = `[1, 2, 3].length`;
|
|
366
|
+
const { result } = run(code);
|
|
367
|
+
expect(result).toEqual(3);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("string .length", () => {
|
|
371
|
+
const code = `a = "hello"; a.length`;
|
|
372
|
+
const { result } = run(code);
|
|
373
|
+
expect(result).toEqual(5);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("object property access", () => {
|
|
377
|
+
const code = `a = { foo: "bar" }; a.foo`;
|
|
378
|
+
const { result } = run(code);
|
|
379
|
+
expect(result).toEqual("bar");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("nested object property access", () => {
|
|
383
|
+
const code = `a.b.c`;
|
|
384
|
+
const { result } = run(code, { a: { b: { c: 42 } } });
|
|
385
|
+
expect(result).toEqual(42);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("computed property access with string", () => {
|
|
389
|
+
const code = `a = { foo: "bar" }; a["foo"]`;
|
|
390
|
+
const { result } = run(code);
|
|
391
|
+
expect(result).toEqual("bar");
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("array index access", () => {
|
|
395
|
+
const code = `a = [10, 20, 30]; a[1]`;
|
|
396
|
+
const { result } = run(code);
|
|
397
|
+
expect(result).toEqual(20);
|
|
398
|
+
});
|
|
399
|
+
});
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
2
3
|
|
|
3
4
|
const run = runner();
|
|
4
5
|
|
|
5
|
-
const DEBUG = true;
|
|
6
|
-
|
|
7
6
|
describe("define global vars", () => {
|
|
8
7
|
it(`foo`, () => {
|
|
9
8
|
const code = `foo`;
|
|
@@ -22,9 +21,7 @@ describe("define global vars", () => {
|
|
|
22
21
|
it("const foo", () => {
|
|
23
22
|
const code = `const foo;`;
|
|
24
23
|
const vars = {};
|
|
25
|
-
expect(() => run(code, vars)).toThrow(
|
|
26
|
-
new SyntaxError("Unexpected token (1:9)")
|
|
27
|
-
);
|
|
24
|
+
expect(() => run(code, vars)).toThrow(SyntaxError);
|
|
28
25
|
});
|
|
29
26
|
|
|
30
27
|
it(`foo = "bar"`, () => {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const binaryOps: Record<string, (l: unknown, r: unknown) => unknown> = {
|
|
2
|
+
"+": (l: any, r: any) => l + r,
|
|
3
|
+
"-": (l: any, r: any) => l - r,
|
|
4
|
+
"*": (l: any, r: any) => l * r,
|
|
5
|
+
"/": (l: any, r: any) => l / r,
|
|
6
|
+
"%": (l: any, r: any) => l % r,
|
|
7
|
+
|
|
8
|
+
">": (l: any, r: any) => l > r,
|
|
9
|
+
"<": (l: any, r: any) => l < r,
|
|
10
|
+
">=": (l: any, r: any) => l >= r,
|
|
11
|
+
"<=": (l: any, r: any) => l <= r,
|
|
12
|
+
"==": (l: any, r: any) => l === r,
|
|
13
|
+
"!=": (l: any, r: any) => l !== r,
|
|
14
|
+
"===": (l: any, r: any) => l === r,
|
|
15
|
+
"!==": (l: any, r: any) => l !== r,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default binaryOps;
|