scriptable 0.1.1 → 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/CHANGELOG.md +28 -0
- package/README.md +13 -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 +2 -2
- 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/dist/logic-ops.d.ts +0 -2
- package/dist/logic-ops.js +0 -5
- package/src/logic-ops.ts +0 -6
|
@@ -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
|
// ==========================================================================
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
const run = runner();
|
|
5
|
+
|
|
6
|
+
// Regressions for three semantic/security bugs discovered in the v0.1.1 audit.
|
|
7
|
+
|
|
8
|
+
describe("functions lookup uses hasOwn (no Object.prototype walk)", () => {
|
|
9
|
+
it("`constructor()` does not resolve to Object via prototype chain", () => {
|
|
10
|
+
// Pre-fix: `functions['constructor']` inherited `Object` from Object.prototype,
|
|
11
|
+
// so this call succeeded and returned `{}`. Post-fix the builtin map is
|
|
12
|
+
// checked with hasOwn, so the call falls through to env lookup and throws.
|
|
13
|
+
expect(() => run(`constructor()`)).toThrow(
|
|
14
|
+
"Access to property 'constructor' is not allowed"
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("`toString()` does not resolve to Object.prototype.toString", () => {
|
|
19
|
+
expect(() => run(`toString()`)).toThrow(
|
|
20
|
+
/is not a function|is not defined/
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("`valueOf()` does not resolve to Object.prototype.valueOf", () => {
|
|
25
|
+
expect(() => run(`valueOf()`)).toThrow(/is not a function|is not defined/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("`hasOwnProperty()` does not resolve to Object.prototype method", () => {
|
|
29
|
+
expect(() => run(`hasOwnProperty()`)).toThrow(
|
|
30
|
+
/is not a function|is not defined/
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("real builtins still resolve", () => {
|
|
35
|
+
const { result } = run(`map([1, 2, 3], (x) => x * 2)`);
|
|
36
|
+
expect(result).toEqual([2, 4, 6]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("user-supplied functions map with Object.prototype inheritance is still safe", () => {
|
|
40
|
+
// A caller may pass a plain object literal. hasOwn defends against that
|
|
41
|
+
// too, not just the default builtins.
|
|
42
|
+
const run2 = runner({ functions: { add: (a: number, b: number) => a + b } });
|
|
43
|
+
expect(() => run2(`constructor()`)).toThrow(
|
|
44
|
+
/Access to property|is not a function|is not defined/
|
|
45
|
+
);
|
|
46
|
+
const { result } = run2(`add(2, 3)`);
|
|
47
|
+
expect(result).toEqual(5);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("LogicalExpression short-circuits", () => {
|
|
52
|
+
it("`false && side()` does not evaluate side effect", () => {
|
|
53
|
+
const code = `
|
|
54
|
+
count = 0;
|
|
55
|
+
side = () => { count = count + 1; return 1; };
|
|
56
|
+
result = false && side();
|
|
57
|
+
count;
|
|
58
|
+
`;
|
|
59
|
+
const { result } = run(code);
|
|
60
|
+
expect(result).toEqual(0);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("`true || side()` does not evaluate side effect", () => {
|
|
64
|
+
const code = `
|
|
65
|
+
count = 0;
|
|
66
|
+
side = () => { count = count + 1; return 1; };
|
|
67
|
+
result = true || side();
|
|
68
|
+
count;
|
|
69
|
+
`;
|
|
70
|
+
const { result } = run(code);
|
|
71
|
+
expect(result).toEqual(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("`true && side()` does evaluate the right side", () => {
|
|
75
|
+
const code = `
|
|
76
|
+
count = 0;
|
|
77
|
+
side = () => { count = count + 1; return 42; };
|
|
78
|
+
result = true && side();
|
|
79
|
+
[result, count];
|
|
80
|
+
`;
|
|
81
|
+
const { result } = run(code);
|
|
82
|
+
expect(result).toEqual([42, 1]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("`null ?? value` returns value (and `0 ?? x` returns 0)", () => {
|
|
86
|
+
expect(run(`null ?? "fallback"`).result).toEqual("fallback");
|
|
87
|
+
expect(run(`0 ?? "fallback"`).result).toEqual(0);
|
|
88
|
+
expect(run(`"" ?? "fallback"`).result).toEqual("");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("`x ?? side()` does not evaluate side effect when x is defined", () => {
|
|
92
|
+
const code = `
|
|
93
|
+
count = 0;
|
|
94
|
+
side = () => { count = count + 1; return "fallback"; };
|
|
95
|
+
result = "value" ?? side();
|
|
96
|
+
count;
|
|
97
|
+
`;
|
|
98
|
+
const { result } = run(code);
|
|
99
|
+
expect(result).toEqual(0);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("AssignmentExpression compound operators", () => {
|
|
104
|
+
it("`+=` adds", () => {
|
|
105
|
+
const { env } = run(`a = 10; a += 5;`);
|
|
106
|
+
expect(env.a).toEqual(15);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("`-=` subtracts", () => {
|
|
110
|
+
const { env } = run(`a = 10; a -= 3;`);
|
|
111
|
+
expect(env.a).toEqual(7);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("`*=` multiplies", () => {
|
|
115
|
+
const { env } = run(`a = 4; a *= 3;`);
|
|
116
|
+
expect(env.a).toEqual(12);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("`/=` divides", () => {
|
|
120
|
+
const { env } = run(`a = 12; a /= 4;`);
|
|
121
|
+
expect(env.a).toEqual(3);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("`%=` modulo", () => {
|
|
125
|
+
const { env } = run(`a = 10; a %= 3;`);
|
|
126
|
+
expect(env.a).toEqual(1);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("`+=` on strings concatenates", () => {
|
|
130
|
+
const { env } = run(`s = "hello"; s += " world";`);
|
|
131
|
+
expect(env.s).toEqual("hello world");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("compound assignment works on nested paths", () => {
|
|
135
|
+
const { env } = run(`o = { n: 1 }; o.n += 10;`);
|
|
136
|
+
expect(env.o.n).toEqual(11);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("`||=` writes only when lhs is falsy", () => {
|
|
140
|
+
expect(run(`a = 0; a ||= 5; a;`).result).toEqual(5);
|
|
141
|
+
expect(run(`a = 7; a ||= 5; a;`).result).toEqual(7);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("`&&=` writes only when lhs is truthy", () => {
|
|
145
|
+
expect(run(`a = 0; a &&= 5; a;`).result).toEqual(0);
|
|
146
|
+
expect(run(`a = 7; a &&= 5; a;`).result).toEqual(5);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("`??=` writes only when lhs is nullish", () => {
|
|
150
|
+
expect(run(`a = null; a ??= 5; a;`).result).toEqual(5);
|
|
151
|
+
expect(run(`a = 0; a ??= 5; a;`).result).toEqual(0);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("`||=` does not evaluate rhs when lhs is truthy", () => {
|
|
155
|
+
const code = `
|
|
156
|
+
count = 0;
|
|
157
|
+
side = () => { count = count + 1; return 42; };
|
|
158
|
+
a = 7;
|
|
159
|
+
a ||= side();
|
|
160
|
+
[a, count];
|
|
161
|
+
`;
|
|
162
|
+
const { result } = run(code);
|
|
163
|
+
expect(result).toEqual([7, 0]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("compound assignment still blocks dangerous paths on the LHS", () => {
|
|
167
|
+
expect(() => run(`o = {}; o.__proto__ += 1`)).toThrow(
|
|
168
|
+
"Access to property '__proto__' is not allowed"
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
});
|