scriptable 0.2.0 → 0.2.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.
@@ -1,790 +0,0 @@
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
- // 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
-
684
- // ==========================================================================
685
- // 9. Sandbox escape via builtin return values
686
- // ==========================================================================
687
-
688
- describe("sandbox escape: via builtin return values", () => {
689
- it("blocks constructor on array returned by map", () => {
690
- const code = `
691
- result = map([1,2], (x) => x);
692
- result.constructor;
693
- `;
694
- expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
695
- });
696
-
697
- it("blocks constructor on object returned by assign", () => {
698
- const code = `
699
- obj = assign({ a: 1 });
700
- obj.constructor;
701
- `;
702
- expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
703
- });
704
-
705
- it("blocks constructor on string returned by join", () => {
706
- const code = `
707
- s = join(["a", "b"], ",");
708
- s.constructor;
709
- `;
710
- expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
711
- });
712
- });
713
-
714
- // ==========================================================================
715
- // 10. Sandbox escape — original break-out.js tests (preserved)
716
- // ==========================================================================
717
-
718
- describe("original break-out tests (preserved)", () => {
719
- it("this.constructor.constructor", () => {
720
- const code = `bar = this.constructor.constructor`;
721
- expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
722
- });
723
-
724
- it("object.constructor", () => {
725
- const code = `
726
- bar = { any: "thing" };
727
- func = bar.constructor;
728
- func2 = func("return process");
729
- process = func2();
730
- `;
731
- expect(() => run(code)).toThrow(
732
- "Access to property 'constructor' is not allowed"
733
- );
734
- });
735
-
736
- it("arguments.callee.caller", () => {
737
- const code = `
738
- function getFunc () {
739
- return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
740
- }
741
- func = getFunc();
742
- func.call(func, "var fs = process.mainModule.require('fs');");
743
- `;
744
- expect(() => run(code)).toThrow(
745
- new ReferenceError(`"arguments" is not defined (3:15)`)
746
- );
747
- });
748
- });
749
-
750
- // ==========================================================================
751
- // 11. Safe property access still works
752
- // ==========================================================================
753
-
754
- describe("safe property access still works", () => {
755
- it("array .length", () => {
756
- const code = `[1, 2, 3].length`;
757
- const { result } = run(code);
758
- expect(result).toEqual(3);
759
- });
760
-
761
- it("string .length", () => {
762
- const code = `a = "hello"; a.length`;
763
- const { result } = run(code);
764
- expect(result).toEqual(5);
765
- });
766
-
767
- it("object property access", () => {
768
- const code = `a = { foo: "bar" }; a.foo`;
769
- const { result } = run(code);
770
- expect(result).toEqual("bar");
771
- });
772
-
773
- it("nested object property access", () => {
774
- const code = `a.b.c`;
775
- const { result } = run(code, { a: { b: { c: 42 } } });
776
- expect(result).toEqual(42);
777
- });
778
-
779
- it("computed property access with string", () => {
780
- const code = `a = { foo: "bar" }; a["foo"]`;
781
- const { result } = run(code);
782
- expect(result).toEqual("bar");
783
- });
784
-
785
- it("array index access", () => {
786
- const code = `a = [10, 20, 30]; a[1]`;
787
- const { result } = run(code);
788
- expect(result).toEqual(20);
789
- });
790
- });