ecma-evaluator 2.0.4 → 2.0.6

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.
@@ -41,6 +41,31 @@ __webpack_require__.d(__webpack_exports__, {
41
41
  });
42
42
  var external_acorn_namespaceObject = require("acorn");
43
43
  var external_globals_namespaceObject = require("globals");
44
+ function _array_like_to_array(arr, len) {
45
+ if (null == len || len > arr.length) len = arr.length;
46
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
47
+ return arr2;
48
+ }
49
+ function _array_without_holes(arr) {
50
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
51
+ }
52
+ function _iterable_to_array(iter) {
53
+ if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
54
+ }
55
+ function _non_iterable_spread() {
56
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
57
+ }
58
+ function _to_consumable_array(arr) {
59
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
60
+ }
61
+ function _unsupported_iterable_to_array(o, minLen) {
62
+ if (!o) return;
63
+ if ("string" == typeof o) return _array_like_to_array(o, minLen);
64
+ var n = Object.prototype.toString.call(o).slice(8, -1);
65
+ if ("Object" === n && o.constructor) n = o.constructor.name;
66
+ if ("Map" === n || "Set" === n) return Array.from(n);
67
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
68
+ }
44
69
  var mutableMethods = [
45
70
  "Array.prototype.push",
46
71
  "Array.prototype.pop",
@@ -58,6 +83,8 @@ var mutableMethods = [
58
83
  "Object.freeze",
59
84
  "Object.setPrototypeOf",
60
85
  "Object.assign",
86
+ "Object.prototype.__defineGetter__",
87
+ "Object.prototype.__defineSetter__",
61
88
  "Reflect.set",
62
89
  "Reflect.defineProperty",
63
90
  "Reflect.deleteProperty",
@@ -179,9 +206,73 @@ var mutableMethods = [
179
206
  "FormData.prototype.set",
180
207
  "Headers.prototype.append",
181
208
  "Headers.prototype.delete",
182
- "Headers.prototype.set"
209
+ "Headers.prototype.set",
210
+ "Function.prototype.call",
211
+ "Function.prototype.apply",
212
+ "Function.prototype.bind",
213
+ "Function.prototype.constructor",
214
+ "Object.prototype.__lookupGetter__",
215
+ "Object.prototype.__lookupSetter__",
216
+ "Object.prototype.constructor"
183
217
  ];
184
- function _array_like_to_array(arr, len) {
218
+ var dangerousMethods = [
219
+ "Object.getPrototypeOf",
220
+ "Object.getOwnPropertyDescriptor",
221
+ "Object.getOwnPropertyDescriptors",
222
+ "Object.getOwnPropertyNames",
223
+ "Object.getOwnPropertySymbols",
224
+ "Object.getOwnPropertyDescriptors"
225
+ ];
226
+ mutableMethods.push("Object.prototype.__proto__");
227
+ var blockedMethods = _to_consumable_array(mutableMethods).concat(_to_consumable_array(dangerousMethods));
228
+ var blockedGlobalBuiltIns = [
229
+ "Function",
230
+ "GeneratorFunction",
231
+ "AsyncFunction",
232
+ "AsyncGeneratorFunction",
233
+ "eval",
234
+ "setTimeout",
235
+ "setInterval",
236
+ "clearTimeout",
237
+ "clearInterval",
238
+ "setImmediate",
239
+ "XMLHttpRequest",
240
+ "fetch",
241
+ "WebSocket",
242
+ "globalThis",
243
+ "process",
244
+ "require",
245
+ "module",
246
+ "exports",
247
+ "global",
248
+ "Buffer",
249
+ "setImmediate",
250
+ "clearImmediate",
251
+ "importScripts",
252
+ "Worker",
253
+ "SharedWorker",
254
+ "ServiceWorker",
255
+ "BroadcastChannel",
256
+ "MessageChannel",
257
+ "MessagePort",
258
+ "postMessage",
259
+ "window",
260
+ "document",
261
+ "navigator",
262
+ "location",
263
+ "localStorage",
264
+ "sessionStorage",
265
+ "indexedDB",
266
+ "performance",
267
+ "Proxy",
268
+ "Reflect",
269
+ "Atomics",
270
+ "WebAssembly",
271
+ "console",
272
+ "Intl",
273
+ "Deno"
274
+ ];
275
+ function Evaluator_array_like_to_array(arr, len) {
185
276
  if (null == len || len > arr.length) len = arr.length;
186
277
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
187
278
  return arr2;
@@ -189,8 +280,8 @@ function _array_like_to_array(arr, len) {
189
280
  function _array_with_holes(arr) {
190
281
  if (Array.isArray(arr)) return arr;
191
282
  }
192
- function _array_without_holes(arr) {
193
- if (Array.isArray(arr)) return _array_like_to_array(arr);
283
+ function Evaluator_array_without_holes(arr) {
284
+ if (Array.isArray(arr)) return Evaluator_array_like_to_array(arr);
194
285
  }
195
286
  function _class_call_check(instance, Constructor) {
196
287
  if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
@@ -226,13 +317,13 @@ function _instanceof(left, right) {
226
317
  if (null != right && "undefined" != typeof Symbol && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);
227
318
  return left instanceof right;
228
319
  }
229
- function _iterable_to_array(iter) {
320
+ function Evaluator_iterable_to_array(iter) {
230
321
  if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
231
322
  }
232
323
  function _non_iterable_rest() {
233
324
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
234
325
  }
235
- function _non_iterable_spread() {
326
+ function Evaluator_non_iterable_spread() {
236
327
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
237
328
  }
238
329
  function _set_prototype_of(o, p) {
@@ -243,21 +334,21 @@ function _set_prototype_of(o, p) {
243
334
  return _set_prototype_of(o, p);
244
335
  }
245
336
  function _to_array(arr) {
246
- return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
337
+ return _array_with_holes(arr) || Evaluator_iterable_to_array(arr) || Evaluator_unsupported_iterable_to_array(arr) || _non_iterable_rest();
247
338
  }
248
- function _to_consumable_array(arr) {
249
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
339
+ function Evaluator_to_consumable_array(arr) {
340
+ return Evaluator_array_without_holes(arr) || Evaluator_iterable_to_array(arr) || Evaluator_unsupported_iterable_to_array(arr) || Evaluator_non_iterable_spread();
250
341
  }
251
342
  function _type_of(obj) {
252
343
  return obj && "undefined" != typeof Symbol && obj.constructor === Symbol ? "symbol" : typeof obj;
253
344
  }
254
- function _unsupported_iterable_to_array(o, minLen) {
345
+ function Evaluator_unsupported_iterable_to_array(o, minLen) {
255
346
  if (!o) return;
256
- if ("string" == typeof o) return _array_like_to_array(o, minLen);
347
+ if ("string" == typeof o) return Evaluator_array_like_to_array(o, minLen);
257
348
  var n = Object.prototype.toString.call(o).slice(8, -1);
258
349
  if ("Object" === n && o.constructor) n = o.constructor.name;
259
350
  if ("Map" === n || "Set" === n) return Array.from(n);
260
- if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
351
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return Evaluator_array_like_to_array(o, minLen);
261
352
  }
262
353
  function _is_native_reflect_construct() {
263
354
  try {
@@ -268,15 +359,17 @@ function _is_native_reflect_construct() {
268
359
  })();
269
360
  }
270
361
  var ERROR_MESSAGES = {
271
- DELETE_NOT_SUPPORTED: "Delete operator is not allow",
272
- MUTABLE_METHOD: "Mutable method is not allowed",
273
- NEW_FUNCTION_NOT_ALLOWED: "Cannot use new with Function constructor",
274
- NOT_A_FUNCTION: "is not a function",
275
- PROPERTY_READ_ERROR: "Cannot read property",
276
- VARIABLE_NOT_DEFINED: "is not defined",
277
- FUNCTION_CONSTRUCTOR_NOT_ALLOWED: "Function constructor is not allowed",
278
- THIS_NOT_ALLOWED: "'this' keyword is not allowed",
279
- NOT_A_VALID_SYNTAX: "is not a valid syntax"
362
+ CAN_NOT_READ_PROPERTY: "Cannot read property of {0} (reading '{1}')",
363
+ IS_NOT_FUNCTION: "{0} is not a function",
364
+ IS_NOT_DEFINED: "{0} is not defined",
365
+ IS_NOT_VALID_SYNTAX: "{0} is not a valid syntax",
366
+ IS_NOT_ALLOWED: "{0} is not allowed"
367
+ };
368
+ var renderErrorMessage = function(template) {
369
+ var context = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
370
+ return template.replace(/{(\w+)}/g, function(_, key) {
371
+ return String(context[key]);
372
+ });
280
373
  };
281
374
  var BINARY_OPERATION_MAP = {
282
375
  "+": function(a, b) {
@@ -349,17 +442,15 @@ var BINARY_OPERATION_MAP = {
349
442
  function createGlobalScope() {
350
443
  var scope = Object.create(null);
351
444
  var builtin = external_globals_namespaceObject.builtin;
352
- Object.keys(builtin).forEach(function(key) {
353
- if (key in globalThis && "eval" !== key && "globalThis" !== key) {
354
- var isWritable = builtin[key];
355
- Object.defineProperty(scope, key, {
356
- value: globalThis[key],
357
- writable: isWritable,
358
- enumerable: false,
359
- configurable: false
360
- });
361
- }
362
- });
445
+ for(var key in builtin)if (!blockedGlobalBuiltIns.includes(key)) {
446
+ var isWritable = builtin[key];
447
+ Object.defineProperty(scope, key, {
448
+ value: globalThis[key],
449
+ writable: isWritable,
450
+ enumerable: false,
451
+ configurable: false
452
+ });
453
+ }
363
454
  Object.defineProperty(scope, "globalThis", {
364
455
  value: scope,
365
456
  writable: false,
@@ -368,14 +459,14 @@ function createGlobalScope() {
368
459
  });
369
460
  return scope;
370
461
  }
371
- var getMutableMethods = function() {
372
- var MUTABLE_METHODS = null;
462
+ var getBlockedMethods = function() {
463
+ var BLOCKED_METHODS = null;
373
464
  return function() {
374
- if (MUTABLE_METHODS) return MUTABLE_METHODS;
375
- var set = new Set();
465
+ if (BLOCKED_METHODS) return BLOCKED_METHODS;
466
+ var map = new Map();
376
467
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
377
468
  try {
378
- for(var _iterator = mutableMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
469
+ for(var _iterator = blockedMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
379
470
  var path = _step.value;
380
471
  var _path_split = _to_array(path.split(".")), object = _path_split[0], properties = _path_split.slice(1);
381
472
  var current = globalThis[object];
@@ -399,7 +490,7 @@ var getMutableMethods = function() {
399
490
  if (_didIteratorError1) throw _iteratorError1;
400
491
  }
401
492
  }
402
- if ("function" == typeof current) set.add(current);
493
+ if ("function" == typeof current) map.set(current, path);
403
494
  }
404
495
  } catch (err) {
405
496
  _didIteratorError = true;
@@ -411,8 +502,8 @@ var getMutableMethods = function() {
411
502
  if (_didIteratorError) throw _iteratorError;
412
503
  }
413
504
  }
414
- MUTABLE_METHODS = set;
415
- return MUTABLE_METHODS;
505
+ BLOCKED_METHODS = map;
506
+ return BLOCKED_METHODS;
416
507
  };
417
508
  }();
418
509
  var Evaluator_Evaluator = /*#__PURE__*/ function() {
@@ -489,6 +580,10 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
489
580
  return this.handleSpreadElement(node);
490
581
  case "ObjectExpression":
491
582
  return this.handleObjectExpression(node);
583
+ case "FunctionExpression":
584
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
585
+ "Function expression"
586
+ ]));
492
587
  case "ArrowFunctionExpression":
493
588
  return this.handleArrowFunctionExpression(node);
494
589
  case "CallExpression":
@@ -497,22 +592,32 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
497
592
  return this.visit(node.test) ? this.visit(node.consequent) : this.visit(node.alternate);
498
593
  case "NewExpression":
499
594
  if ("Identifier" !== node.callee.type) throw new Error("Unsupported callee type '".concat(node.callee.type, "' in new expression"));
500
- if ("Function" === node.callee.name) throw new Error(ERROR_MESSAGES.NEW_FUNCTION_NOT_ALLOWED);
595
+ if ("Function" === node.callee.name) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
596
+ "new Function() constructor"
597
+ ]));
501
598
  var Constructor = this.visit(node.callee);
502
599
  var args = node.arguments.length ? node.arguments.map(function(arg) {
503
600
  return _this.visit(arg);
504
601
  }) : [];
505
- return _construct(Constructor, _to_consumable_array(args));
602
+ return _construct(Constructor, Evaluator_to_consumable_array(args));
506
603
  case "ChainExpression":
507
604
  return this.visit(node.expression);
508
605
  case "TemplateLiteral":
509
606
  return this.handleTemplateLiteral(node);
510
607
  case "ThisExpression":
511
- throw new Error(ERROR_MESSAGES.THIS_NOT_ALLOWED);
608
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
609
+ "'this' expression"
610
+ ]));
611
+ case "WithStatement":
612
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
613
+ "'with' statement"
614
+ ]));
512
615
  default:
513
616
  var content = this.source.slice(node.start, node.end);
514
617
  if (content.length > 20) content = content.slice(0, 17) + "...";
515
- throw new Error("'".concat(content, "'") + " " + ERROR_MESSAGES.NOT_A_VALID_SYNTAX);
618
+ throw new Error("'".concat(content, "'") + " " + renderErrorMessage(ERROR_MESSAGES.IS_NOT_VALID_SYNTAX, [
619
+ content
620
+ ]));
516
621
  }
517
622
  }
518
623
  },
@@ -561,7 +666,9 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
561
666
  case "void":
562
667
  return void this.visit(node.argument);
563
668
  case "delete":
564
- throw new Error(ERROR_MESSAGES.DELETE_NOT_SUPPORTED);
669
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
670
+ "Delete operator"
671
+ ]));
565
672
  default:
566
673
  throw new Error("Unsupported unary operator: ".concat(node.operator));
567
674
  }
@@ -587,7 +694,9 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
587
694
  if (_didIteratorError) throw _iteratorError;
588
695
  }
589
696
  }
590
- throw new ReferenceError("".concat(name, " ").concat(ERROR_MESSAGES.VARIABLE_NOT_DEFINED));
697
+ throw new ReferenceError(renderErrorMessage(ERROR_MESSAGES.IS_NOT_DEFINED, [
698
+ name
699
+ ]));
591
700
  }
592
701
  },
593
702
  {
@@ -596,9 +705,15 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
596
705
  var object = this.visit(node.object);
597
706
  var isStaticProperty = "Identifier" === node.property.type && !node.computed;
598
707
  var property = isStaticProperty ? node.property.name : this.visit(node.property);
708
+ if (null != object && object[property] === (null == object ? void 0 : object.__proto__)) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
709
+ "Accessing prototype properties"
710
+ ]));
599
711
  if (null == object) {
600
712
  if (node.optional) return;
601
- throw new TypeError("".concat(ERROR_MESSAGES.PROPERTY_READ_ERROR, " '").concat(property, "' of ").concat(object));
713
+ throw new TypeError(renderErrorMessage(ERROR_MESSAGES.CAN_NOT_READ_PROPERTY, [
714
+ object,
715
+ property
716
+ ]));
602
717
  }
603
718
  return object[property];
604
719
  }
@@ -612,7 +727,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
612
727
  var value = this.visit(element);
613
728
  if ("SpreadElement" === element.type) {
614
729
  var _result;
615
- (_result = result).push.apply(_result, _to_consumable_array(value));
730
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
616
731
  } else result.push(value);
617
732
  }
618
733
  return result;
@@ -673,18 +788,18 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
673
788
  key: "handleCallExpression",
674
789
  value: function(node) {
675
790
  var _this = this;
676
- if ("MemberExpression" === node.callee.type) {
677
- var object = this.visit(node.callee.object);
678
- if (getMutableMethods().has(object)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
679
- }
680
- var calledString = getNodeString(node.callee);
681
791
  var func = this.visit(node.callee);
682
- if ("function" != typeof func) {
683
- var isOptional = node.optional || node.callee.optional;
684
- if (null == func && isOptional) return;
685
- throw new TypeError("".concat(calledString, " ").concat(ERROR_MESSAGES.NOT_A_FUNCTION));
792
+ var isOptional = node.optional || node.callee.optional;
793
+ if (null == func && isOptional) return;
794
+ if (func === Function) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
795
+ "Function constructor"
796
+ ]));
797
+ if (getBlockedMethods().has(func)) {
798
+ var path = getBlockedMethods().get(func);
799
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
800
+ path
801
+ ]));
686
802
  }
687
- if (func === Function) throw new Error(ERROR_MESSAGES.FUNCTION_CONSTRUCTOR_NOT_ALLOWED);
688
803
  var args = function() {
689
804
  if (0 === node.arguments.length) return [];
690
805
  var result = [];
@@ -693,13 +808,18 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
693
808
  var value = _this.visit(element);
694
809
  if ("SpreadElement" === element.type) {
695
810
  var _result;
696
- (_result = result).push.apply(_result, _to_consumable_array(value));
811
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
697
812
  } else result.push(value);
698
813
  }
699
814
  return result;
700
815
  }();
701
- if (getMutableMethods().has(func)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
702
816
  var target = "MemberExpression" === node.callee.type ? this.visit(node.callee.object) : null;
817
+ if ("function" != typeof func) {
818
+ var calledString = getNodeString(node.callee);
819
+ throw new TypeError(renderErrorMessage(ERROR_MESSAGES.IS_NOT_FUNCTION, [
820
+ calledString
821
+ ]));
822
+ }
703
823
  return func.apply(target, args);
704
824
  }
705
825
  },