scriptable 0.1.0 → 0.2.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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/workflows/release.yml +62 -0
- package/CHANGELOG.md +42 -0
- package/README.md +59 -0
- package/SECURITY_FIXES.md +81 -0
- package/dist/environment.d.ts +16 -5
- package/dist/environment.js +83 -35
- package/dist/evaluate.d.ts +0 -1
- package/dist/evaluate.js +194 -48
- package/dist/index.d.ts +28 -3
- package/dist/index.js +12 -7
- package/package.json +11 -8
- package/scripts/release.js +127 -0
- package/src/__tests__/audit-followups.test.ts +206 -0
- package/src/__tests__/security.test.ts +391 -0
- package/src/__tests__/semantic-fixes.test.ts +171 -0
- package/src/environment.ts +97 -37
- package/src/evaluate.ts +190 -51
- package/src/index.ts +48 -7
- package/.claude/settings.local.json +0 -10
- package/dist/logic-ops.d.ts +0 -2
- package/dist/logic-ops.js +0 -5
- package/src/logic-ops.ts +0 -6
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
// Regressions for four additional findings from the v0.1.1 audit that were
|
|
5
|
+
// deferred until after the initial hardening pass. See
|
|
6
|
+
// `.changeset/audit-followups.md` for narrative context.
|
|
7
|
+
|
|
8
|
+
describe("Finding 4 — literal-dot keys survive the path refactor", () => {
|
|
9
|
+
// Pre-fix, evalAsPath built a dotted string ("a.x.y"), so a key like
|
|
10
|
+
// `a["x.y"]` was split by deepSet into `a.x.y` on write but read back via
|
|
11
|
+
// a flat `obj["x.y"]` lookup — silent inconsistency. Post-fix, paths are
|
|
12
|
+
// string[] segments and dots inside a segment stay literal.
|
|
13
|
+
const run = runner();
|
|
14
|
+
|
|
15
|
+
it("a['x.y'] = 1 stores under the literal key 'x.y'", () => {
|
|
16
|
+
const { env } = run(`a = {}; a["x.y"] = 1;`);
|
|
17
|
+
expect(Object.prototype.hasOwnProperty.call(env.a, "x.y")).toBe(true);
|
|
18
|
+
expect(env.a["x.y"]).toBe(1);
|
|
19
|
+
expect(env.a.x).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("read-after-write through the same bracket expression round-trips", () => {
|
|
23
|
+
const { result } = run(`a = {}; a["x.y"] = 42; a["x.y"];`);
|
|
24
|
+
expect(result).toBe(42);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("compound assignment on a dotted literal key still works", () => {
|
|
28
|
+
const { env } = run(`a = { "x.y": 1 }; a["x.y"] += 10;`);
|
|
29
|
+
expect(env.a["x.y"]).toBe(11);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("blocked props remain blocked even when embedded in a dotted string", () => {
|
|
33
|
+
// 'a.__proto__' as a full literal key is NOT the blocked string "__proto__"
|
|
34
|
+
// (it's the 11-char string), so this is allowed as a plain property. The
|
|
35
|
+
// real prototype-pollution vector is the literal "__proto__" segment,
|
|
36
|
+
// still caught by assertSafeProperty.
|
|
37
|
+
expect(() => run(`a = {}; a["__proto__"] = 1;`)).toThrow(
|
|
38
|
+
"Access to property '__proto__' is not allowed"
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("Finding 5 — dotted paths resolve through parent scope", () => {
|
|
44
|
+
// Pre-fix, env.vars = Object.create(parent.vars) made parent variables
|
|
45
|
+
// visible via prototype chain, but deepHas used hasOwn on the first segment
|
|
46
|
+
// so `obj.method()` inside a nested function threw "obj.method is not a
|
|
47
|
+
// function". Post-fix, the first segment uses `in` (walks the chain);
|
|
48
|
+
// subsequent segments still use hasOwn so Object.prototype can't leak in.
|
|
49
|
+
const run = runner();
|
|
50
|
+
|
|
51
|
+
it("calls a method on a parent-scope object from inside a function", () => {
|
|
52
|
+
const { result } = run(`
|
|
53
|
+
obj = { double: (x) => x * 2 };
|
|
54
|
+
function f() { return obj.double(5); }
|
|
55
|
+
f();
|
|
56
|
+
`);
|
|
57
|
+
expect(result).toBe(10);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("reads a nested property on a parent-scope object", () => {
|
|
61
|
+
const { result } = run(`
|
|
62
|
+
config = { db: { host: "localhost", port: 5432 } };
|
|
63
|
+
function f() { return config.db.port; }
|
|
64
|
+
f();
|
|
65
|
+
`);
|
|
66
|
+
expect(result).toBe(5432);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("still blocks Object.prototype leak on subsequent segments", () => {
|
|
70
|
+
// `obj.toString` would resolve to Object.prototype.toString if hasOwn
|
|
71
|
+
// weren't applied after the first segment. Make sure it doesn't.
|
|
72
|
+
expect(() =>
|
|
73
|
+
run(`
|
|
74
|
+
obj = {};
|
|
75
|
+
function f() { return obj.toString(); }
|
|
76
|
+
f();
|
|
77
|
+
`)
|
|
78
|
+
).toThrow(/is not a function|is not defined/);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("still blocks constructor on any segment", () => {
|
|
82
|
+
expect(() =>
|
|
83
|
+
run(`
|
|
84
|
+
obj = {};
|
|
85
|
+
function f() { return obj.constructor; }
|
|
86
|
+
f();
|
|
87
|
+
`)
|
|
88
|
+
).toThrow("Access to property 'constructor' is not allowed");
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("Finding 7 — maxAllocSize bounds object key growth", () => {
|
|
93
|
+
it("spread accumulation under the cap still works", () => {
|
|
94
|
+
const run = runner({ maxSteps: 0, maxAllocSize: 50 });
|
|
95
|
+
const { env } = run(`
|
|
96
|
+
acc = {};
|
|
97
|
+
acc = {...acc, a: 1};
|
|
98
|
+
acc = {...acc, b: 2};
|
|
99
|
+
acc = {...acc, c: 3};
|
|
100
|
+
`);
|
|
101
|
+
expect(env.acc).toEqual({ a: 1, b: 2, c: 3 });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("recursive {...acc, [k]: k} growth trips the cap", () => {
|
|
105
|
+
// Use recursion instead of reduce(bigArr, ...) so the array literal check
|
|
106
|
+
// doesn't fire first — all that's in scope is a primitive counter. Each
|
|
107
|
+
// ObjectExpression spread projects ownCount + 1; once that exceeds
|
|
108
|
+
// maxAllocSize, checkLength throws.
|
|
109
|
+
const run = runner({ maxSteps: 0, maxAllocSize: 10 });
|
|
110
|
+
expect(() =>
|
|
111
|
+
run(`
|
|
112
|
+
grow = (acc, n) => n > 0 ? grow({...acc, ["k" + n]: n}, n - 1) : acc;
|
|
113
|
+
grow({}, 50);
|
|
114
|
+
`)
|
|
115
|
+
).toThrow(/Length.*exceeds maxAllocSize/);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("object literal with > maxAllocSize keys throws on construction", () => {
|
|
119
|
+
const run = runner({ maxAllocSize: 3 });
|
|
120
|
+
const keys = ["a: 1", "b: 2", "c: 3", "d: 4"].join(", ");
|
|
121
|
+
expect(() => run(`({ ${keys} })`)).toThrow(
|
|
122
|
+
/Length.*exceeds maxAllocSize/
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("repeated keys within one literal don't over-count", () => {
|
|
127
|
+
// Same key written twice is still one own key; cap shouldn't trip.
|
|
128
|
+
const run = runner({ maxAllocSize: 1 });
|
|
129
|
+
expect(() => run(`({ a: 1, a: 2 })`)).not.toThrow();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("objects under the cap still build", () => {
|
|
133
|
+
const run = runner({ maxAllocSize: 10 });
|
|
134
|
+
const { result } = run(`({ a: 1, b: 2, c: 3 })`);
|
|
135
|
+
expect(result).toEqual({ a: 1, b: 2, c: 3 });
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("Finding 6 — maxDurationMs bounds wall-clock runs", () => {
|
|
140
|
+
it("a long-running injected builtin trips the deadline on the next step", () => {
|
|
141
|
+
const run = runner({
|
|
142
|
+
maxSteps: 0,
|
|
143
|
+
maxDurationMs: 20,
|
|
144
|
+
functions: {
|
|
145
|
+
slow: (ms: number) => {
|
|
146
|
+
const end = Date.now() + ms;
|
|
147
|
+
while (Date.now() < end) {
|
|
148
|
+
/* burn */
|
|
149
|
+
}
|
|
150
|
+
return 1;
|
|
151
|
+
},
|
|
152
|
+
add: (a: number, b: number) => a + b,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
// slow(100) runs inside V8 (~100ms), overshoots the 20ms deadline. The
|
|
156
|
+
// next interpreter step — evaluating `add(1, 2)` — sees the deadline
|
|
157
|
+
// passed and throws.
|
|
158
|
+
expect(() => run(`slow(100); add(1, 2);`)).toThrow(
|
|
159
|
+
"Execution time exceeded"
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("scripts that finish inside the deadline succeed", () => {
|
|
164
|
+
const run = runner({ maxDurationMs: 5000 });
|
|
165
|
+
const { result } = run(`1 + 2 + 3`);
|
|
166
|
+
expect(result).toBe(6);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("maxDurationMs=0 disables the deadline", () => {
|
|
170
|
+
const run = runner({
|
|
171
|
+
maxDurationMs: 0,
|
|
172
|
+
maxSteps: 1000,
|
|
173
|
+
functions: {
|
|
174
|
+
slow: () => {
|
|
175
|
+
const end = Date.now() + 30;
|
|
176
|
+
while (Date.now() < end) {
|
|
177
|
+
/* burn */
|
|
178
|
+
}
|
|
179
|
+
return 1;
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
expect(() => run(`slow()`)).not.toThrow();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("deadline supersedes subsequent work even if maxSteps would still allow it", () => {
|
|
187
|
+
// Step counter never trips (maxSteps=0 disables), so wall-clock is the
|
|
188
|
+
// only gate. Confirm the deadline still fires.
|
|
189
|
+
const run = runner({
|
|
190
|
+
maxSteps: 0,
|
|
191
|
+
maxDurationMs: 10,
|
|
192
|
+
functions: {
|
|
193
|
+
slow: (ms: number) => {
|
|
194
|
+
const end = Date.now() + ms;
|
|
195
|
+
while (Date.now() < end) {
|
|
196
|
+
/* burn */
|
|
197
|
+
}
|
|
198
|
+
return 1;
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
expect(() => run(`slow(50); slow(50);`)).toThrow(
|
|
203
|
+
"Execution time exceeded"
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -290,6 +290,397 @@ describe("DoS: execution limits", () => {
|
|
|
290
290
|
});
|
|
291
291
|
});
|
|
292
292
|
|
|
293
|
+
// ==========================================================================
|
|
294
|
+
// 8b. DoS — step counter isolation across runs
|
|
295
|
+
// ==========================================================================
|
|
296
|
+
|
|
297
|
+
describe("DoS: step counter per-run isolation", () => {
|
|
298
|
+
it("re-entrance does not leak budget into the outer run", () => {
|
|
299
|
+
// Caller-injected function that synchronously runs another interpreter.
|
|
300
|
+
// Previously the module-global counter was reset by the inner run,
|
|
301
|
+
// letting the outer run silently bypass its maxSteps.
|
|
302
|
+
const inner = runner({ maxSteps: 100_000 });
|
|
303
|
+
const outer = runner({ maxSteps: 5 });
|
|
304
|
+
expect(() =>
|
|
305
|
+
outer(
|
|
306
|
+
`
|
|
307
|
+
runInner("x = 1;");
|
|
308
|
+
a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8;
|
|
309
|
+
`,
|
|
310
|
+
{ runInner: (code: string) => inner(code).env }
|
|
311
|
+
)
|
|
312
|
+
).toThrow("Execution limit exceeded");
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("two runners with different limits do not share state", () => {
|
|
316
|
+
const tight = runner({ maxSteps: 5 });
|
|
317
|
+
const loose = runner({ maxSteps: 100_000 });
|
|
318
|
+
// loose completes first; tight should still trip on its own budget.
|
|
319
|
+
loose(`a = 1; b = 2; c = 3; d = 4; e = 5;`);
|
|
320
|
+
expect(() => tight(`a = 1; b = 2; c = 3;`)).toThrow(
|
|
321
|
+
"Execution limit exceeded"
|
|
322
|
+
);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// ==========================================================================
|
|
327
|
+
// 8d. DoS — per-value allocation size
|
|
328
|
+
// ==========================================================================
|
|
329
|
+
|
|
330
|
+
describe("DoS: maxAllocSize", () => {
|
|
331
|
+
it("blocks exponential string doubling", () => {
|
|
332
|
+
// 26 doublings from 8 chars → ~512 MiB before this fix.
|
|
333
|
+
const code = `
|
|
334
|
+
s = "aaaaaaaa";
|
|
335
|
+
${"s = s + s;\n".repeat(30)}
|
|
336
|
+
s.length;
|
|
337
|
+
`;
|
|
338
|
+
expect(() => run(code)).toThrow(/maxAllocSize/);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("blocks exponential template literal growth", () => {
|
|
342
|
+
const code = `
|
|
343
|
+
s = "aaaaaaaa";
|
|
344
|
+
${"s = `${s}${s}`;\n".repeat(30)}
|
|
345
|
+
s.length;
|
|
346
|
+
`;
|
|
347
|
+
expect(() => run(code)).toThrow(/maxAllocSize/);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("blocks concat() builtin growth", () => {
|
|
351
|
+
const code = `
|
|
352
|
+
a = [1,2,3,4,5,6,7,8];
|
|
353
|
+
${"a = concat(a, a);\n".repeat(25)}
|
|
354
|
+
a.length;
|
|
355
|
+
`;
|
|
356
|
+
expect(() => run(code)).toThrow(/maxAllocSize/);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("blocks array spread growth", () => {
|
|
360
|
+
const code = `
|
|
361
|
+
a = [1,2,3,4,5,6,7,8];
|
|
362
|
+
b = [...a, ...a, ...a, ...a, ...a, ...a, ...a, ...a];
|
|
363
|
+
c = [...b, ...b, ...b, ...b, ...b, ...b, ...b, ...b];
|
|
364
|
+
d = [...c, ...c, ...c, ...c, ...c, ...c, ...c, ...c];
|
|
365
|
+
e = [...d, ...d, ...d, ...d, ...d, ...d, ...d, ...d];
|
|
366
|
+
f = [...e, ...e, ...e, ...e, ...e, ...e, ...e, ...e];
|
|
367
|
+
g = [...f, ...f, ...f, ...f, ...f, ...f, ...f, ...f];
|
|
368
|
+
g.length;
|
|
369
|
+
`;
|
|
370
|
+
expect(() => run(code)).toThrow(/maxAllocSize/);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("allows disabling with 0", () => {
|
|
374
|
+
const unbounded = runner({ maxAllocSize: 0, maxSteps: 1_000_000, maxCodeSize: 0 });
|
|
375
|
+
const code = `
|
|
376
|
+
s = "aaaaaaaa";
|
|
377
|
+
s = s + s; s = s + s; s = s + s; s = s + s;
|
|
378
|
+
s.length;
|
|
379
|
+
`;
|
|
380
|
+
const { result } = unbounded(code);
|
|
381
|
+
expect(result).toBe(128);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("respects a tight custom limit", () => {
|
|
385
|
+
const tight = runner({ maxAllocSize: 100 });
|
|
386
|
+
expect(() => tight(`a = "a"; a = a + a; a = a + a; a = a + a; a = a + a; a = a + a; a = a + a; a = a + a;`)).toThrow(
|
|
387
|
+
/maxAllocSize/
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("passes allocations within the default limit", () => {
|
|
392
|
+
const code = `
|
|
393
|
+
a = [1,2,3];
|
|
394
|
+
b = concat(a, a, a);
|
|
395
|
+
result = b.length;
|
|
396
|
+
`;
|
|
397
|
+
const { env } = run(code);
|
|
398
|
+
expect(env.result).toEqual(9);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
// ==========================================================================
|
|
403
|
+
// 8c. DoS — source size limit
|
|
404
|
+
// ==========================================================================
|
|
405
|
+
|
|
406
|
+
describe("DoS: maxCodeSize", () => {
|
|
407
|
+
it("rejects input over the default limit", () => {
|
|
408
|
+
const huge = "x = 1;\n".repeat(20_000); // ~140 KB
|
|
409
|
+
expect(() => run(huge)).toThrow(/maxCodeSize/);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("respects a custom limit", () => {
|
|
413
|
+
const tight = runner({ maxCodeSize: 10 });
|
|
414
|
+
expect(() => tight(`a = 1; b = 2; c = 3;`)).toThrow(/maxCodeSize/);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("allows disabling with 0", () => {
|
|
418
|
+
const unbounded = runner({ maxCodeSize: 0, maxSteps: 1_000_000 });
|
|
419
|
+
const code = "x = 1;\n".repeat(10_000);
|
|
420
|
+
expect(() => unbounded(code)).not.toThrow();
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("passes code within the default limit", () => {
|
|
424
|
+
const code = `a = 1; b = 2; result = a + b;`;
|
|
425
|
+
const { env } = run(code);
|
|
426
|
+
expect(env.result).toEqual(3);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
// ==========================================================================
|
|
431
|
+
// 8e. Object literal cannot introduce blocked keys
|
|
432
|
+
// ==========================================================================
|
|
433
|
+
|
|
434
|
+
describe("object literal: blocked keys", () => {
|
|
435
|
+
it("rejects __proto__ as a static key (would set prototype of new object)", () => {
|
|
436
|
+
const code = `x = { "__proto__": { injected: "yes" } };`;
|
|
437
|
+
expect(() => run(code)).toThrow(
|
|
438
|
+
"Access to property '__proto__' is not allowed"
|
|
439
|
+
);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("rejects __proto__ as an Identifier key", () => {
|
|
443
|
+
const code = `x = { __proto__: 1 };`;
|
|
444
|
+
expect(() => run(code)).toThrow(
|
|
445
|
+
"Access to property '__proto__' is not allowed"
|
|
446
|
+
);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("rejects constructor as a literal key", () => {
|
|
450
|
+
const code = `x = { "constructor": "pwn" };`;
|
|
451
|
+
expect(() => run(code)).toThrow(
|
|
452
|
+
"Access to property 'constructor' is not allowed"
|
|
453
|
+
);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it("rejects prototype as a key", () => {
|
|
457
|
+
const code = `x = { prototype: 1 };`;
|
|
458
|
+
expect(() => run(code)).toThrow(
|
|
459
|
+
"Access to property 'prototype' is not allowed"
|
|
460
|
+
);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("rejects computed key that evaluates to a blocked name", () => {
|
|
464
|
+
const code = `x = { ["con" + "structor"]: 1 };`;
|
|
465
|
+
expect(() => run(code)).toThrow(
|
|
466
|
+
"Access to property 'constructor' is not allowed"
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("evaluates computed Identifier keys as their value (bug fix)", () => {
|
|
471
|
+
const code = `k = "dynamic"; x = { [k]: 42 }; result = x.dynamic;`;
|
|
472
|
+
const { env } = run(code);
|
|
473
|
+
expect(env.result).toEqual(42);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it("spread source cannot introduce blocked keys into result", () => {
|
|
477
|
+
// Host passes an object with an own "constructor" key (e.g., by mistake).
|
|
478
|
+
// Spread should silently drop it rather than poison the user-created object.
|
|
479
|
+
const tainted = Object.create(null);
|
|
480
|
+
tainted.ok = "yes";
|
|
481
|
+
tainted.constructor = "evil";
|
|
482
|
+
const code = `x = { ...tainted }; ok = x.ok;`;
|
|
483
|
+
const { env } = run(code, { tainted });
|
|
484
|
+
expect(env.ok).toEqual("yes");
|
|
485
|
+
expect(Object.prototype.hasOwnProperty.call(env.x, "constructor")).toBe(
|
|
486
|
+
false
|
|
487
|
+
);
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// ==========================================================================
|
|
492
|
+
// 8f. Computed paths on LHS / callee evaluate the key's runtime value
|
|
493
|
+
// ==========================================================================
|
|
494
|
+
|
|
495
|
+
describe("computed paths: LHS and callee", () => {
|
|
496
|
+
it("computed assignment resolves key value, not identifier name", () => {
|
|
497
|
+
// Before the fix, `a[k] = v` wrote to `a.k` (using the identifier NAME).
|
|
498
|
+
// Post-fix, it writes to the property named by the *value* of k.
|
|
499
|
+
const code = `a = {}; k = "dynamic"; a[k] = 42; byValue = a.dynamic; byName = a.k;`;
|
|
500
|
+
const { env } = run(code);
|
|
501
|
+
expect(env.byValue).toEqual(42);
|
|
502
|
+
expect(env.byName).toBeUndefined();
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it("computed assignment blocks blocked keys resolved at runtime", () => {
|
|
506
|
+
const code = `a = {}; k = "constructor"; a[k] = "pwn";`;
|
|
507
|
+
expect(() => run(code)).toThrow(
|
|
508
|
+
"Access to property 'constructor' is not allowed"
|
|
509
|
+
);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("computed assignment blocks __proto__ resolved at runtime", () => {
|
|
513
|
+
const code = `a = {}; k = "__proto__"; a[k] = { bad: 1 };`;
|
|
514
|
+
expect(() => run(code)).toThrow(
|
|
515
|
+
"Access to property '__proto__' is not allowed"
|
|
516
|
+
);
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("nested computed assignment resolves keys at each level", () => {
|
|
520
|
+
const code = `a = { x: {} }; k = "inner"; a.x[k] = 7; result = a.x.inner;`;
|
|
521
|
+
const { env } = run(code);
|
|
522
|
+
expect(env.result).toEqual(7);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("calls a returned function value (CallExpression callee)", () => {
|
|
526
|
+
const code = `
|
|
527
|
+
make = () => (x) => x + 1;
|
|
528
|
+
result = make()(41);
|
|
529
|
+
`;
|
|
530
|
+
const { env } = run(code);
|
|
531
|
+
expect(env.result).toEqual(42);
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it("calls an IIFE arrow (ArrowFunctionExpression callee)", () => {
|
|
535
|
+
const code = `result = ((x) => x * 2)(21);`;
|
|
536
|
+
const { env } = run(code);
|
|
537
|
+
expect(env.result).toEqual(42);
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// ==========================================================================
|
|
542
|
+
// 8g. Recursive functions get fresh scope per invocation
|
|
543
|
+
// ==========================================================================
|
|
544
|
+
|
|
545
|
+
describe("function scope: per-invocation isolation", () => {
|
|
546
|
+
it("recursion preserves parent-frame params after child returns", () => {
|
|
547
|
+
// Before the fix, makeFunc hoisted `const scope = env.extend()` to
|
|
548
|
+
// closure-creation time, so every recursive call wrote into the same
|
|
549
|
+
// scope and clobbered the caller's `n`. The `[f(0), n][1]` shape forces a
|
|
550
|
+
// re-read of `n` AFTER the recursive call returns — with shared scope
|
|
551
|
+
// that read would see 0; with fresh per-invocation scope it sees 7.
|
|
552
|
+
const code = `
|
|
553
|
+
f = (n) => n == 0 ? 0 : [f(0), n][1];
|
|
554
|
+
result = f(7);
|
|
555
|
+
`;
|
|
556
|
+
const { env } = run(code);
|
|
557
|
+
expect(env.result).toEqual(7);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("nested self-recursion keeps each frame's locals isolated", () => {
|
|
561
|
+
// Builds an array with each frame's n. Without per-invocation scope, every
|
|
562
|
+
// element would be the innermost value (0) because the shared scope was
|
|
563
|
+
// overwritten on the way down.
|
|
564
|
+
const code = `
|
|
565
|
+
f = (n) => n == 0 ? [n] : [f(n - 1), n];
|
|
566
|
+
result = f(3);
|
|
567
|
+
flat = JSON.stringify(result);
|
|
568
|
+
`;
|
|
569
|
+
const { env } = run(code, { JSON });
|
|
570
|
+
expect(env.flat).toEqual("[[[[0],1],2],3]");
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("recursive factorial yields correct result", () => {
|
|
574
|
+
const code = `
|
|
575
|
+
fact = (n) => n <= 1 ? 1 : n * fact(n - 1);
|
|
576
|
+
result = fact(5);
|
|
577
|
+
`;
|
|
578
|
+
const { env } = run(code);
|
|
579
|
+
expect(env.result).toEqual(120);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it("arrow-function recursion sums 1..n correctly", () => {
|
|
583
|
+
const code = `
|
|
584
|
+
sum = (n) => n == 0 ? 0 : n + sum(n - 1);
|
|
585
|
+
result = sum(4);
|
|
586
|
+
`;
|
|
587
|
+
const { env } = run(code);
|
|
588
|
+
expect(env.result).toEqual(10);
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
// ==========================================================================
|
|
593
|
+
// 8h. Nested returns unwind to the enclosing function boundary
|
|
594
|
+
// ==========================================================================
|
|
595
|
+
|
|
596
|
+
describe("control flow: nested returns", () => {
|
|
597
|
+
it("return inside if short-circuits the rest of the function body", () => {
|
|
598
|
+
// Pre-fix, BlockStatement only broke on a top-level ReturnStatement, so
|
|
599
|
+
// the `return 100` inside the guard's consequent did not stop the loop;
|
|
600
|
+
// the remaining `f(0)` / `return n` kept executing and, for f(n>0), the
|
|
601
|
+
// recursive f(0) call with the fixed per-invocation scope looped forever.
|
|
602
|
+
const code = `
|
|
603
|
+
function f(n) {
|
|
604
|
+
if (n == 0) return 100;
|
|
605
|
+
f(0);
|
|
606
|
+
return n;
|
|
607
|
+
}
|
|
608
|
+
result = f(3);
|
|
609
|
+
`;
|
|
610
|
+
const { env } = run(code);
|
|
611
|
+
expect(env.result).toEqual(3);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("return inside nested if unwinds through both blocks", () => {
|
|
615
|
+
const code = `
|
|
616
|
+
function f(n) {
|
|
617
|
+
if (n > 0) {
|
|
618
|
+
if (n == 5) return "five";
|
|
619
|
+
return "other";
|
|
620
|
+
}
|
|
621
|
+
return "zero";
|
|
622
|
+
}
|
|
623
|
+
a = f(5);
|
|
624
|
+
b = f(3);
|
|
625
|
+
c = f(0);
|
|
626
|
+
`;
|
|
627
|
+
const { env } = run(code);
|
|
628
|
+
expect(env.a).toEqual("five");
|
|
629
|
+
expect(env.b).toEqual("other");
|
|
630
|
+
expect(env.c).toEqual("zero");
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it("early return skips side effects later in the body", () => {
|
|
634
|
+
const code = `
|
|
635
|
+
count = 0;
|
|
636
|
+
function f(n) {
|
|
637
|
+
if (n > 0) return "early";
|
|
638
|
+
count = count + 1;
|
|
639
|
+
return "late";
|
|
640
|
+
}
|
|
641
|
+
a = f(1);
|
|
642
|
+
b = f(0);
|
|
643
|
+
`;
|
|
644
|
+
const { env } = run(code);
|
|
645
|
+
expect(env.a).toEqual("early");
|
|
646
|
+
expect(env.b).toEqual("late");
|
|
647
|
+
expect(env.count).toEqual(1);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
it("return undefined (no argument) propagates undefined", () => {
|
|
651
|
+
const code = `
|
|
652
|
+
function f(n) {
|
|
653
|
+
if (n == 0) return;
|
|
654
|
+
return n * 2;
|
|
655
|
+
}
|
|
656
|
+
a = f(0);
|
|
657
|
+
b = f(3);
|
|
658
|
+
`;
|
|
659
|
+
const { env } = run(code);
|
|
660
|
+
expect(env.a).toBeUndefined();
|
|
661
|
+
expect(env.b).toEqual(6);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it("return from within nested function only unwinds the inner function", () => {
|
|
665
|
+
// Inner return must not escape past the inner function and stop the outer.
|
|
666
|
+
const code = `
|
|
667
|
+
function outer(n) {
|
|
668
|
+
inner = (x) => {
|
|
669
|
+
if (x > 0) return "inner";
|
|
670
|
+
return "zero";
|
|
671
|
+
};
|
|
672
|
+
tag = inner(n);
|
|
673
|
+
return tag + "-outer";
|
|
674
|
+
}
|
|
675
|
+
a = outer(1);
|
|
676
|
+
b = outer(0);
|
|
677
|
+
`;
|
|
678
|
+
const { env } = run(code);
|
|
679
|
+
expect(env.a).toEqual("inner-outer");
|
|
680
|
+
expect(env.b).toEqual("zero-outer");
|
|
681
|
+
});
|
|
682
|
+
});
|
|
683
|
+
|
|
293
684
|
// ==========================================================================
|
|
294
685
|
// 9. Sandbox escape via builtin return values
|
|
295
686
|
// ==========================================================================
|