ecma-evaluator 2.0.2 → 2.0.5

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.
@@ -29,6 +29,9 @@ __webpack_require__.d(__webpack_exports__, {
29
29
  evalTemplate: function() {
30
30
  return evalTemplate;
31
31
  },
32
+ TemplateParser: function() {
33
+ return TemplateParser_TemplateParser;
34
+ },
32
35
  evalExpression: function() {
33
36
  return evalExpression;
34
37
  },
@@ -38,6 +41,31 @@ __webpack_require__.d(__webpack_exports__, {
38
41
  });
39
42
  var external_acorn_namespaceObject = require("acorn");
40
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
+ }
41
69
  var mutableMethods = [
42
70
  "Array.prototype.push",
43
71
  "Array.prototype.pop",
@@ -55,6 +83,8 @@ var mutableMethods = [
55
83
  "Object.freeze",
56
84
  "Object.setPrototypeOf",
57
85
  "Object.assign",
86
+ "Object.prototype.__defineGetter__",
87
+ "Object.prototype.__defineSetter__",
58
88
  "Reflect.set",
59
89
  "Reflect.defineProperty",
60
90
  "Reflect.deleteProperty",
@@ -176,9 +206,73 @@ var mutableMethods = [
176
206
  "FormData.prototype.set",
177
207
  "Headers.prototype.append",
178
208
  "Headers.prototype.delete",
179
- "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"
180
217
  ];
181
- 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) {
182
276
  if (null == len || len > arr.length) len = arr.length;
183
277
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
184
278
  return arr2;
@@ -186,8 +280,8 @@ function _array_like_to_array(arr, len) {
186
280
  function _array_with_holes(arr) {
187
281
  if (Array.isArray(arr)) return arr;
188
282
  }
189
- function _array_without_holes(arr) {
190
- 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);
191
285
  }
192
286
  function _class_call_check(instance, Constructor) {
193
287
  if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
@@ -223,13 +317,13 @@ function _instanceof(left, right) {
223
317
  if (null != right && "undefined" != typeof Symbol && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);
224
318
  return left instanceof right;
225
319
  }
226
- function _iterable_to_array(iter) {
320
+ function Evaluator_iterable_to_array(iter) {
227
321
  if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
228
322
  }
229
323
  function _non_iterable_rest() {
230
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.");
231
325
  }
232
- function _non_iterable_spread() {
326
+ function Evaluator_non_iterable_spread() {
233
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.");
234
328
  }
235
329
  function _set_prototype_of(o, p) {
@@ -240,21 +334,21 @@ function _set_prototype_of(o, p) {
240
334
  return _set_prototype_of(o, p);
241
335
  }
242
336
  function _to_array(arr) {
243
- 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();
244
338
  }
245
- function _to_consumable_array(arr) {
246
- 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();
247
341
  }
248
342
  function _type_of(obj) {
249
343
  return obj && "undefined" != typeof Symbol && obj.constructor === Symbol ? "symbol" : typeof obj;
250
344
  }
251
- function _unsupported_iterable_to_array(o, minLen) {
345
+ function Evaluator_unsupported_iterable_to_array(o, minLen) {
252
346
  if (!o) return;
253
- if ("string" == typeof o) return _array_like_to_array(o, minLen);
347
+ if ("string" == typeof o) return Evaluator_array_like_to_array(o, minLen);
254
348
  var n = Object.prototype.toString.call(o).slice(8, -1);
255
349
  if ("Object" === n && o.constructor) n = o.constructor.name;
256
350
  if ("Map" === n || "Set" === n) return Array.from(n);
257
- 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);
258
352
  }
259
353
  function _is_native_reflect_construct() {
260
354
  try {
@@ -266,14 +360,17 @@ function _is_native_reflect_construct() {
266
360
  }
267
361
  var ERROR_MESSAGES = {
268
362
  DELETE_NOT_SUPPORTED: "Delete operator is not allow",
269
- MUTABLE_METHOD: "Mutable method is not allowed",
270
363
  NEW_FUNCTION_NOT_ALLOWED: "Cannot use new with Function constructor",
271
364
  NOT_A_FUNCTION: "is not a function",
272
365
  PROPERTY_READ_ERROR: "Cannot read property",
273
366
  VARIABLE_NOT_DEFINED: "is not defined",
274
367
  FUNCTION_CONSTRUCTOR_NOT_ALLOWED: "Function constructor is not allowed",
275
368
  THIS_NOT_ALLOWED: "'this' keyword is not allowed",
276
- NOT_A_VALID_SYNTAX: "is not a valid syntax"
369
+ NOT_A_VALID_SYNTAX: "is not a valid syntax",
370
+ ACCESSING_PROTOTYPE_NOT_ALLOWED: "Accessing prototype properties is not allowed",
371
+ WITH_NOT_ALLOWED: "'with' statement is not allowed",
372
+ FUNCTION_EXPRESSION_NOT_ALLOWED: "Function expressions are not allowed",
373
+ METHOD_NOT_ALLOWED: "is not allowed"
277
374
  };
278
375
  var BINARY_OPERATION_MAP = {
279
376
  "+": function(a, b) {
@@ -346,17 +443,15 @@ var BINARY_OPERATION_MAP = {
346
443
  function createGlobalScope() {
347
444
  var scope = Object.create(null);
348
445
  var builtin = external_globals_namespaceObject.builtin;
349
- Object.keys(builtin).forEach(function(key) {
350
- if (key in globalThis && "eval" !== key && "globalThis" !== key) {
351
- var isWritable = builtin[key];
352
- Object.defineProperty(scope, key, {
353
- value: globalThis[key],
354
- writable: isWritable,
355
- enumerable: false,
356
- configurable: false
357
- });
358
- }
359
- });
446
+ for(var key in builtin)if (!blockedGlobalBuiltIns.includes(key)) {
447
+ var isWritable = builtin[key];
448
+ Object.defineProperty(scope, key, {
449
+ value: globalThis[key],
450
+ writable: isWritable,
451
+ enumerable: false,
452
+ configurable: false
453
+ });
454
+ }
360
455
  Object.defineProperty(scope, "globalThis", {
361
456
  value: scope,
362
457
  writable: false,
@@ -365,14 +460,14 @@ function createGlobalScope() {
365
460
  });
366
461
  return scope;
367
462
  }
368
- var getMutableMethods = function() {
369
- var MUTABLE_METHODS = null;
463
+ var getBlockedMethods = function() {
464
+ var BLOCKED_METHODS = null;
370
465
  return function() {
371
- if (MUTABLE_METHODS) return MUTABLE_METHODS;
372
- var set = new Set();
466
+ if (BLOCKED_METHODS) return BLOCKED_METHODS;
467
+ var map = new Map();
373
468
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
374
469
  try {
375
- for(var _iterator = mutableMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
470
+ for(var _iterator = blockedMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
376
471
  var path = _step.value;
377
472
  var _path_split = _to_array(path.split(".")), object = _path_split[0], properties = _path_split.slice(1);
378
473
  var current = globalThis[object];
@@ -396,7 +491,7 @@ var getMutableMethods = function() {
396
491
  if (_didIteratorError1) throw _iteratorError1;
397
492
  }
398
493
  }
399
- if ("function" == typeof current) set.add(current);
494
+ if ("function" == typeof current) map.set(current, path);
400
495
  }
401
496
  } catch (err) {
402
497
  _didIteratorError = true;
@@ -408,8 +503,8 @@ var getMutableMethods = function() {
408
503
  if (_didIteratorError) throw _iteratorError;
409
504
  }
410
505
  }
411
- MUTABLE_METHODS = set;
412
- return MUTABLE_METHODS;
506
+ BLOCKED_METHODS = map;
507
+ return BLOCKED_METHODS;
413
508
  };
414
509
  }();
415
510
  var Evaluator_Evaluator = /*#__PURE__*/ function() {
@@ -480,14 +575,14 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
480
575
  return node.value;
481
576
  case "MemberExpression":
482
577
  return this.handleMemberExpression(node);
483
- case "ObjectExpression":
484
- return this.handleObjectExpression(node);
485
578
  case "ArrayExpression":
486
579
  return this.handleArrayExpression(node);
487
580
  case "SpreadElement":
488
581
  return this.handleSpreadElement(node);
489
582
  case "ObjectExpression":
490
583
  return this.handleObjectExpression(node);
584
+ case "FunctionExpression":
585
+ throw new Error(ERROR_MESSAGES.FUNCTION_EXPRESSION_NOT_ALLOWED);
491
586
  case "ArrowFunctionExpression":
492
587
  return this.handleArrowFunctionExpression(node);
493
588
  case "CallExpression":
@@ -501,13 +596,15 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
501
596
  var args = node.arguments.length ? node.arguments.map(function(arg) {
502
597
  return _this.visit(arg);
503
598
  }) : [];
504
- return _construct(Constructor, _to_consumable_array(args));
599
+ return _construct(Constructor, Evaluator_to_consumable_array(args));
505
600
  case "ChainExpression":
506
601
  return this.visit(node.expression);
507
602
  case "TemplateLiteral":
508
603
  return this.handleTemplateLiteral(node);
509
604
  case "ThisExpression":
510
605
  throw new Error(ERROR_MESSAGES.THIS_NOT_ALLOWED);
606
+ case "WithStatement":
607
+ throw new Error(ERROR_MESSAGES.WITH_NOT_ALLOWED);
511
608
  default:
512
609
  var content = this.source.slice(node.start, node.end);
513
610
  if (content.length > 20) content = content.slice(0, 17) + "...";
@@ -595,6 +692,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
595
692
  var object = this.visit(node.object);
596
693
  var isStaticProperty = "Identifier" === node.property.type && !node.computed;
597
694
  var property = isStaticProperty ? node.property.name : this.visit(node.property);
695
+ if (null != object && object[property] === (null == object ? void 0 : object.__proto__)) throw new Error(ERROR_MESSAGES.ACCESSING_PROTOTYPE_NOT_ALLOWED);
598
696
  if (null == object) {
599
697
  if (node.optional) return;
600
698
  throw new TypeError("".concat(ERROR_MESSAGES.PROPERTY_READ_ERROR, " '").concat(property, "' of ").concat(object));
@@ -602,6 +700,21 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
602
700
  return object[property];
603
701
  }
604
702
  },
703
+ {
704
+ key: "handleArrayExpression",
705
+ value: function(node) {
706
+ var result = [];
707
+ for(var i = 0; i < node.elements.length; i++){
708
+ var element = node.elements.at(i);
709
+ var value = this.visit(element);
710
+ if ("SpreadElement" === element.type) {
711
+ var _result;
712
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
713
+ } else result.push(value);
714
+ }
715
+ return result;
716
+ }
717
+ },
605
718
  {
606
719
  key: "handleObjectExpression",
607
720
  value: function(node) {
@@ -631,21 +744,6 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
631
744
  return obj;
632
745
  }
633
746
  },
634
- {
635
- key: "handleArrayExpression",
636
- value: function(node) {
637
- var result = [];
638
- for(var i = 0; i < node.elements.length; i++){
639
- var element = node.elements.at(i);
640
- var value = this.visit(element);
641
- if ("SpreadElement" === element.type) {
642
- var _result;
643
- (_result = result).push.apply(_result, _to_consumable_array(value));
644
- } else result.push(value);
645
- }
646
- return result;
647
- }
648
- },
649
747
  {
650
748
  key: "handleSpreadElement",
651
749
  value: function(node) {
@@ -672,10 +770,6 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
672
770
  key: "handleCallExpression",
673
771
  value: function(node) {
674
772
  var _this = this;
675
- if ("MemberExpression" === node.callee.type) {
676
- var object = this.visit(node.callee.object);
677
- if (getMutableMethods().has(object)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
678
- }
679
773
  var calledString = getNodeString(node.callee);
680
774
  var func = this.visit(node.callee);
681
775
  if ("function" != typeof func) {
@@ -684,6 +778,10 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
684
778
  throw new TypeError("".concat(calledString, " ").concat(ERROR_MESSAGES.NOT_A_FUNCTION));
685
779
  }
686
780
  if (func === Function) throw new Error(ERROR_MESSAGES.FUNCTION_CONSTRUCTOR_NOT_ALLOWED);
781
+ if (getBlockedMethods().has(func)) {
782
+ var path = getBlockedMethods().get(func);
783
+ throw new Error("".concat(path, " ").concat(ERROR_MESSAGES.METHOD_NOT_ALLOWED));
784
+ }
687
785
  var args = function() {
688
786
  if (0 === node.arguments.length) return [];
689
787
  var result = [];
@@ -692,12 +790,11 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
692
790
  var value = _this.visit(element);
693
791
  if ("SpreadElement" === element.type) {
694
792
  var _result;
695
- (_result = result).push.apply(_result, _to_consumable_array(value));
793
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
696
794
  } else result.push(value);
697
795
  }
698
796
  return result;
699
797
  }();
700
- if (getMutableMethods().has(func)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
701
798
  var target = "MemberExpression" === node.callee.type ? this.visit(node.callee.object) : null;
702
799
  return func.apply(target, args);
703
800
  }
@@ -936,10 +1033,12 @@ function evalTemplate(template, context, templateParserOptions) {
936
1033
  return result;
937
1034
  }
938
1035
  exports.Evaluator = __webpack_exports__.Evaluator;
1036
+ exports.TemplateParser = __webpack_exports__.TemplateParser;
939
1037
  exports.evalExpression = __webpack_exports__.evalExpression;
940
1038
  exports.evalTemplate = __webpack_exports__.evalTemplate;
941
1039
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
942
1040
  "Evaluator",
1041
+ "TemplateParser",
943
1042
  "evalExpression",
944
1043
  "evalTemplate"
945
1044
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];