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.
@@ -1,5 +1,30 @@
1
1
  import { parse as external_acorn_parse } from "acorn";
2
2
  import globals from "globals";
3
+ function _array_like_to_array(arr, len) {
4
+ if (null == len || len > arr.length) len = arr.length;
5
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
6
+ return arr2;
7
+ }
8
+ function _array_without_holes(arr) {
9
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
10
+ }
11
+ function _iterable_to_array(iter) {
12
+ if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
13
+ }
14
+ function _non_iterable_spread() {
15
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
16
+ }
17
+ function _to_consumable_array(arr) {
18
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
19
+ }
20
+ function _unsupported_iterable_to_array(o, minLen) {
21
+ if (!o) return;
22
+ if ("string" == typeof o) return _array_like_to_array(o, minLen);
23
+ var n = Object.prototype.toString.call(o).slice(8, -1);
24
+ if ("Object" === n && o.constructor) n = o.constructor.name;
25
+ if ("Map" === n || "Set" === n) return Array.from(n);
26
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
27
+ }
3
28
  var mutableMethods = [
4
29
  "Array.prototype.push",
5
30
  "Array.prototype.pop",
@@ -17,6 +42,8 @@ var mutableMethods = [
17
42
  "Object.freeze",
18
43
  "Object.setPrototypeOf",
19
44
  "Object.assign",
45
+ "Object.prototype.__defineGetter__",
46
+ "Object.prototype.__defineSetter__",
20
47
  "Reflect.set",
21
48
  "Reflect.defineProperty",
22
49
  "Reflect.deleteProperty",
@@ -138,9 +165,73 @@ var mutableMethods = [
138
165
  "FormData.prototype.set",
139
166
  "Headers.prototype.append",
140
167
  "Headers.prototype.delete",
141
- "Headers.prototype.set"
168
+ "Headers.prototype.set",
169
+ "Function.prototype.call",
170
+ "Function.prototype.apply",
171
+ "Function.prototype.bind",
172
+ "Function.prototype.constructor",
173
+ "Object.prototype.__lookupGetter__",
174
+ "Object.prototype.__lookupSetter__",
175
+ "Object.prototype.constructor"
142
176
  ];
143
- function _array_like_to_array(arr, len) {
177
+ var dangerousMethods = [
178
+ "Object.getPrototypeOf",
179
+ "Object.getOwnPropertyDescriptor",
180
+ "Object.getOwnPropertyDescriptors",
181
+ "Object.getOwnPropertyNames",
182
+ "Object.getOwnPropertySymbols",
183
+ "Object.getOwnPropertyDescriptors"
184
+ ];
185
+ mutableMethods.push("Object.prototype.__proto__");
186
+ var blockedMethods = _to_consumable_array(mutableMethods).concat(_to_consumable_array(dangerousMethods));
187
+ var blockedGlobalBuiltIns = [
188
+ "Function",
189
+ "GeneratorFunction",
190
+ "AsyncFunction",
191
+ "AsyncGeneratorFunction",
192
+ "eval",
193
+ "setTimeout",
194
+ "setInterval",
195
+ "clearTimeout",
196
+ "clearInterval",
197
+ "setImmediate",
198
+ "XMLHttpRequest",
199
+ "fetch",
200
+ "WebSocket",
201
+ "globalThis",
202
+ "process",
203
+ "require",
204
+ "module",
205
+ "exports",
206
+ "global",
207
+ "Buffer",
208
+ "setImmediate",
209
+ "clearImmediate",
210
+ "importScripts",
211
+ "Worker",
212
+ "SharedWorker",
213
+ "ServiceWorker",
214
+ "BroadcastChannel",
215
+ "MessageChannel",
216
+ "MessagePort",
217
+ "postMessage",
218
+ "window",
219
+ "document",
220
+ "navigator",
221
+ "location",
222
+ "localStorage",
223
+ "sessionStorage",
224
+ "indexedDB",
225
+ "performance",
226
+ "Proxy",
227
+ "Reflect",
228
+ "Atomics",
229
+ "WebAssembly",
230
+ "console",
231
+ "Intl",
232
+ "Deno"
233
+ ];
234
+ function Evaluator_array_like_to_array(arr, len) {
144
235
  if (null == len || len > arr.length) len = arr.length;
145
236
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
146
237
  return arr2;
@@ -148,8 +239,8 @@ function _array_like_to_array(arr, len) {
148
239
  function _array_with_holes(arr) {
149
240
  if (Array.isArray(arr)) return arr;
150
241
  }
151
- function _array_without_holes(arr) {
152
- if (Array.isArray(arr)) return _array_like_to_array(arr);
242
+ function Evaluator_array_without_holes(arr) {
243
+ if (Array.isArray(arr)) return Evaluator_array_like_to_array(arr);
153
244
  }
154
245
  function _class_call_check(instance, Constructor) {
155
246
  if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
@@ -185,13 +276,13 @@ function _instanceof(left, right) {
185
276
  if (null != right && "undefined" != typeof Symbol && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);
186
277
  return left instanceof right;
187
278
  }
188
- function _iterable_to_array(iter) {
279
+ function Evaluator_iterable_to_array(iter) {
189
280
  if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
190
281
  }
191
282
  function _non_iterable_rest() {
192
283
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
193
284
  }
194
- function _non_iterable_spread() {
285
+ function Evaluator_non_iterable_spread() {
195
286
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
196
287
  }
197
288
  function _set_prototype_of(o, p) {
@@ -202,21 +293,21 @@ function _set_prototype_of(o, p) {
202
293
  return _set_prototype_of(o, p);
203
294
  }
204
295
  function _to_array(arr) {
205
- return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
296
+ return _array_with_holes(arr) || Evaluator_iterable_to_array(arr) || Evaluator_unsupported_iterable_to_array(arr) || _non_iterable_rest();
206
297
  }
207
- function _to_consumable_array(arr) {
208
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
298
+ function Evaluator_to_consumable_array(arr) {
299
+ return Evaluator_array_without_holes(arr) || Evaluator_iterable_to_array(arr) || Evaluator_unsupported_iterable_to_array(arr) || Evaluator_non_iterable_spread();
209
300
  }
210
301
  function _type_of(obj) {
211
302
  return obj && "undefined" != typeof Symbol && obj.constructor === Symbol ? "symbol" : typeof obj;
212
303
  }
213
- function _unsupported_iterable_to_array(o, minLen) {
304
+ function Evaluator_unsupported_iterable_to_array(o, minLen) {
214
305
  if (!o) return;
215
- if ("string" == typeof o) return _array_like_to_array(o, minLen);
306
+ if ("string" == typeof o) return Evaluator_array_like_to_array(o, minLen);
216
307
  var n = Object.prototype.toString.call(o).slice(8, -1);
217
308
  if ("Object" === n && o.constructor) n = o.constructor.name;
218
309
  if ("Map" === n || "Set" === n) return Array.from(n);
219
- if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
310
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return Evaluator_array_like_to_array(o, minLen);
220
311
  }
221
312
  function _is_native_reflect_construct() {
222
313
  try {
@@ -227,15 +318,17 @@ function _is_native_reflect_construct() {
227
318
  })();
228
319
  }
229
320
  var ERROR_MESSAGES = {
230
- DELETE_NOT_SUPPORTED: "Delete operator is not allow",
231
- MUTABLE_METHOD: "Mutable method is not allowed",
232
- NEW_FUNCTION_NOT_ALLOWED: "Cannot use new with Function constructor",
233
- NOT_A_FUNCTION: "is not a function",
234
- PROPERTY_READ_ERROR: "Cannot read property",
235
- VARIABLE_NOT_DEFINED: "is not defined",
236
- FUNCTION_CONSTRUCTOR_NOT_ALLOWED: "Function constructor is not allowed",
237
- THIS_NOT_ALLOWED: "'this' keyword is not allowed",
238
- NOT_A_VALID_SYNTAX: "is not a valid syntax"
321
+ CAN_NOT_READ_PROPERTY: "Cannot read property of {0} (reading '{1}')",
322
+ IS_NOT_FUNCTION: "{0} is not a function",
323
+ IS_NOT_DEFINED: "{0} is not defined",
324
+ IS_NOT_VALID_SYNTAX: "{0} is not a valid syntax",
325
+ IS_NOT_ALLOWED: "{0} is not allowed"
326
+ };
327
+ var renderErrorMessage = function(template) {
328
+ var context = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
329
+ return template.replace(/{(\w+)}/g, function(_, key) {
330
+ return String(context[key]);
331
+ });
239
332
  };
240
333
  var BINARY_OPERATION_MAP = {
241
334
  "+": function(a, b) {
@@ -308,17 +401,15 @@ var BINARY_OPERATION_MAP = {
308
401
  function createGlobalScope() {
309
402
  var scope = Object.create(null);
310
403
  var builtin = globals.builtin;
311
- Object.keys(builtin).forEach(function(key) {
312
- if (key in globalThis && "eval" !== key && "globalThis" !== key) {
313
- var isWritable = builtin[key];
314
- Object.defineProperty(scope, key, {
315
- value: globalThis[key],
316
- writable: isWritable,
317
- enumerable: false,
318
- configurable: false
319
- });
320
- }
321
- });
404
+ for(var key in builtin)if (!blockedGlobalBuiltIns.includes(key)) {
405
+ var isWritable = builtin[key];
406
+ Object.defineProperty(scope, key, {
407
+ value: globalThis[key],
408
+ writable: isWritable,
409
+ enumerable: false,
410
+ configurable: false
411
+ });
412
+ }
322
413
  Object.defineProperty(scope, "globalThis", {
323
414
  value: scope,
324
415
  writable: false,
@@ -327,14 +418,14 @@ function createGlobalScope() {
327
418
  });
328
419
  return scope;
329
420
  }
330
- var getMutableMethods = function() {
331
- var MUTABLE_METHODS = null;
421
+ var getBlockedMethods = function() {
422
+ var BLOCKED_METHODS = null;
332
423
  return function() {
333
- if (MUTABLE_METHODS) return MUTABLE_METHODS;
334
- var set = new Set();
424
+ if (BLOCKED_METHODS) return BLOCKED_METHODS;
425
+ var map = new Map();
335
426
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
336
427
  try {
337
- for(var _iterator = mutableMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
428
+ for(var _iterator = blockedMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
338
429
  var path = _step.value;
339
430
  var _path_split = _to_array(path.split(".")), object = _path_split[0], properties = _path_split.slice(1);
340
431
  var current = globalThis[object];
@@ -358,7 +449,7 @@ var getMutableMethods = function() {
358
449
  if (_didIteratorError1) throw _iteratorError1;
359
450
  }
360
451
  }
361
- if ("function" == typeof current) set.add(current);
452
+ if ("function" == typeof current) map.set(current, path);
362
453
  }
363
454
  } catch (err) {
364
455
  _didIteratorError = true;
@@ -370,8 +461,8 @@ var getMutableMethods = function() {
370
461
  if (_didIteratorError) throw _iteratorError;
371
462
  }
372
463
  }
373
- MUTABLE_METHODS = set;
374
- return MUTABLE_METHODS;
464
+ BLOCKED_METHODS = map;
465
+ return BLOCKED_METHODS;
375
466
  };
376
467
  }();
377
468
  var Evaluator_Evaluator = /*#__PURE__*/ function() {
@@ -448,6 +539,10 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
448
539
  return this.handleSpreadElement(node);
449
540
  case "ObjectExpression":
450
541
  return this.handleObjectExpression(node);
542
+ case "FunctionExpression":
543
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
544
+ "Function expression"
545
+ ]));
451
546
  case "ArrowFunctionExpression":
452
547
  return this.handleArrowFunctionExpression(node);
453
548
  case "CallExpression":
@@ -456,22 +551,32 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
456
551
  return this.visit(node.test) ? this.visit(node.consequent) : this.visit(node.alternate);
457
552
  case "NewExpression":
458
553
  if ("Identifier" !== node.callee.type) throw new Error("Unsupported callee type '".concat(node.callee.type, "' in new expression"));
459
- if ("Function" === node.callee.name) throw new Error(ERROR_MESSAGES.NEW_FUNCTION_NOT_ALLOWED);
554
+ if ("Function" === node.callee.name) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
555
+ "new Function() constructor"
556
+ ]));
460
557
  var Constructor = this.visit(node.callee);
461
558
  var args = node.arguments.length ? node.arguments.map(function(arg) {
462
559
  return _this.visit(arg);
463
560
  }) : [];
464
- return _construct(Constructor, _to_consumable_array(args));
561
+ return _construct(Constructor, Evaluator_to_consumable_array(args));
465
562
  case "ChainExpression":
466
563
  return this.visit(node.expression);
467
564
  case "TemplateLiteral":
468
565
  return this.handleTemplateLiteral(node);
469
566
  case "ThisExpression":
470
- throw new Error(ERROR_MESSAGES.THIS_NOT_ALLOWED);
567
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
568
+ "'this' expression"
569
+ ]));
570
+ case "WithStatement":
571
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
572
+ "'with' statement"
573
+ ]));
471
574
  default:
472
575
  var content = this.source.slice(node.start, node.end);
473
576
  if (content.length > 20) content = content.slice(0, 17) + "...";
474
- throw new Error("'".concat(content, "'") + " " + ERROR_MESSAGES.NOT_A_VALID_SYNTAX);
577
+ throw new Error("'".concat(content, "'") + " " + renderErrorMessage(ERROR_MESSAGES.IS_NOT_VALID_SYNTAX, [
578
+ content
579
+ ]));
475
580
  }
476
581
  }
477
582
  },
@@ -520,7 +625,9 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
520
625
  case "void":
521
626
  return void this.visit(node.argument);
522
627
  case "delete":
523
- throw new Error(ERROR_MESSAGES.DELETE_NOT_SUPPORTED);
628
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
629
+ "Delete operator"
630
+ ]));
524
631
  default:
525
632
  throw new Error("Unsupported unary operator: ".concat(node.operator));
526
633
  }
@@ -546,7 +653,9 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
546
653
  if (_didIteratorError) throw _iteratorError;
547
654
  }
548
655
  }
549
- throw new ReferenceError("".concat(name, " ").concat(ERROR_MESSAGES.VARIABLE_NOT_DEFINED));
656
+ throw new ReferenceError(renderErrorMessage(ERROR_MESSAGES.IS_NOT_DEFINED, [
657
+ name
658
+ ]));
550
659
  }
551
660
  },
552
661
  {
@@ -555,9 +664,15 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
555
664
  var object = this.visit(node.object);
556
665
  var isStaticProperty = "Identifier" === node.property.type && !node.computed;
557
666
  var property = isStaticProperty ? node.property.name : this.visit(node.property);
667
+ if (null != object && object[property] === (null == object ? void 0 : object.__proto__)) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
668
+ "Accessing prototype properties"
669
+ ]));
558
670
  if (null == object) {
559
671
  if (node.optional) return;
560
- throw new TypeError("".concat(ERROR_MESSAGES.PROPERTY_READ_ERROR, " '").concat(property, "' of ").concat(object));
672
+ throw new TypeError(renderErrorMessage(ERROR_MESSAGES.CAN_NOT_READ_PROPERTY, [
673
+ object,
674
+ property
675
+ ]));
561
676
  }
562
677
  return object[property];
563
678
  }
@@ -571,7 +686,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
571
686
  var value = this.visit(element);
572
687
  if ("SpreadElement" === element.type) {
573
688
  var _result;
574
- (_result = result).push.apply(_result, _to_consumable_array(value));
689
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
575
690
  } else result.push(value);
576
691
  }
577
692
  return result;
@@ -632,18 +747,18 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
632
747
  key: "handleCallExpression",
633
748
  value: function(node) {
634
749
  var _this = this;
635
- if ("MemberExpression" === node.callee.type) {
636
- var object = this.visit(node.callee.object);
637
- if (getMutableMethods().has(object)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
638
- }
639
- var calledString = getNodeString(node.callee);
640
750
  var func = this.visit(node.callee);
641
- if ("function" != typeof func) {
642
- var isOptional = node.optional || node.callee.optional;
643
- if (null == func && isOptional) return;
644
- throw new TypeError("".concat(calledString, " ").concat(ERROR_MESSAGES.NOT_A_FUNCTION));
751
+ var isOptional = node.optional || node.callee.optional;
752
+ if (null == func && isOptional) return;
753
+ if (func === Function) throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
754
+ "Function constructor"
755
+ ]));
756
+ if (getBlockedMethods().has(func)) {
757
+ var path = getBlockedMethods().get(func);
758
+ throw new Error(renderErrorMessage(ERROR_MESSAGES.IS_NOT_ALLOWED, [
759
+ path
760
+ ]));
645
761
  }
646
- if (func === Function) throw new Error(ERROR_MESSAGES.FUNCTION_CONSTRUCTOR_NOT_ALLOWED);
647
762
  var args = function() {
648
763
  if (0 === node.arguments.length) return [];
649
764
  var result = [];
@@ -652,13 +767,18 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
652
767
  var value = _this.visit(element);
653
768
  if ("SpreadElement" === element.type) {
654
769
  var _result;
655
- (_result = result).push.apply(_result, _to_consumable_array(value));
770
+ (_result = result).push.apply(_result, Evaluator_to_consumable_array(value));
656
771
  } else result.push(value);
657
772
  }
658
773
  return result;
659
774
  }();
660
- if (getMutableMethods().has(func)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
661
775
  var target = "MemberExpression" === node.callee.type ? this.visit(node.callee.object) : null;
776
+ if ("function" != typeof func) {
777
+ var calledString = getNodeString(node.callee);
778
+ throw new TypeError(renderErrorMessage(ERROR_MESSAGES.IS_NOT_FUNCTION, [
779
+ calledString
780
+ ]));
781
+ }
662
782
  return func.apply(target, args);
663
783
  }
664
784
  },