|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-implied-eval,no-shadow-restricted-names,@typescript-eslint/no-unsafe-call */ |
| 2 | +import type { variables } from './variables'; |
| 3 | + |
| 4 | +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; |
| 5 | + |
| 6 | +export function processScript( |
| 7 | + script: string, |
| 8 | + _variables: typeof variables, |
| 9 | + inline: boolean, |
| 10 | + async: true, |
| 11 | + ctx?: any |
| 12 | +): Promise<any>; |
| 13 | +export function processScript( |
| 14 | + script: string, |
| 15 | + _variables: typeof variables, |
| 16 | + inline: boolean, |
| 17 | + async: false, |
| 18 | + ctx?: any |
| 19 | +): any; |
| 20 | +export function processScript( |
| 21 | + script: string, |
| 22 | + _variables: typeof variables, |
| 23 | + inline: boolean, |
| 24 | + async: boolean, |
| 25 | + ctx?: any |
| 26 | +): any { |
| 27 | + const randomEvalName = 'eval_' + Math.random().toString().substring(2); |
| 28 | + const functionSet = { |
| 29 | + get: function (variable: string): any { |
| 30 | + return _variables.get(variable); |
| 31 | + }, |
| 32 | + set: function (variable: string, value: any): any { |
| 33 | + if (!_variables.has(variable)) |
| 34 | + Object.defineProperty(scope, variable, { |
| 35 | + get(): any { |
| 36 | + return _variables.get(variable); |
| 37 | + }, |
| 38 | + set(value): any { |
| 39 | + return _variables.set(variable, value); |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + _variables.set(variable, value); |
| 44 | + |
| 45 | + return ''; |
| 46 | + }, |
| 47 | + json: function (object: any): string { |
| 48 | + return JSON.stringify(object, null, 2); |
| 49 | + }, |
| 50 | + parse: function (str: string): any { |
| 51 | + return JSON.parse(str); |
| 52 | + }, |
| 53 | + require: function () { |
| 54 | + throw new Error('require is not allowed'); |
| 55 | + }, |
| 56 | + Function: function () { |
| 57 | + throw new Error('Function is not allowed'); |
| 58 | + }, |
| 59 | + __import_not_allowed: function () { |
| 60 | + throw new Error('import is not allowed'); |
| 61 | + }, |
| 62 | + globalThis: undefined, |
| 63 | + global: undefined, |
| 64 | + }; |
| 65 | + const scope = Object.assign( |
| 66 | + Object.keys(globalThis).reduce( |
| 67 | + (acc, k) => { |
| 68 | + acc[k] = undefined; |
| 69 | + return acc; |
| 70 | + }, |
| 71 | + {} as Record<string, any> |
| 72 | + ), |
| 73 | + functionSet, |
| 74 | + ctx |
| 75 | + ); |
| 76 | + |
| 77 | + for (const storeKey in _variables.getStore()) { |
| 78 | + Object.defineProperty(scope, randomEvalName, { |
| 79 | + value: eval, |
| 80 | + }); |
| 81 | + Object.defineProperty(scope, storeKey, { |
| 82 | + get(): any { |
| 83 | + return _variables.get(storeKey); |
| 84 | + }, |
| 85 | + set(value): any { |
| 86 | + return _variables.set(storeKey, value); |
| 87 | + }, |
| 88 | + }); |
| 89 | + } |
| 90 | + script = script.replace(/\bimport\(/g, '__import_not_allowed('); |
| 91 | + script = 'var eval=undefined;' + script; |
| 92 | + |
| 93 | + if (async) { |
| 94 | + if (inline) { |
| 95 | + return new AsyncFunction( |
| 96 | + 'script', |
| 97 | + 'with(this) { return eval(script) }' |
| 98 | + ).call(scope, script); |
| 99 | + } else { |
| 100 | + return new AsyncFunction('with(this) { ' + script + ' }').call(scope); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (inline) { |
| 105 | + return new Function('script', 'with(this) { return eval(script) }').call( |
| 106 | + scope, |
| 107 | + script |
| 108 | + ); |
| 109 | + } else { |
| 110 | + return new Function('with(this) { ' + script + ' }').call(scope); |
| 111 | + } |
| 112 | +} |
0 commit comments