Spaces:
Running
Running
| const vm = require('vm'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const codePath = process.argv[2]; | |
| if (!codePath) { | |
| console.error('Usage: node js_sandbox.js <code_path>'); | |
| process.exit(1); | |
| } | |
| const safeConsole = { | |
| log: console.log, | |
| warn: console.warn, | |
| error: console.error, | |
| info: console.info, | |
| }; | |
| const sandbox = { | |
| console: safeConsole, | |
| Math, Date, JSON, | |
| parseInt, parseFloat, isNaN, isFinite, | |
| NaN, Infinity, undefined: undefined, null: null, | |
| Number, String, Boolean, Array, Object, RegExp, | |
| Map, Set, WeakMap, WeakSet, Promise, Symbol, | |
| Error, TypeError, RangeError, SyntaxError, ReferenceError, EvalError, URIError, | |
| ArrayBuffer, DataView, | |
| Int8Array, Int16Array, Int32Array, | |
| Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, | |
| Float32Array, Float64Array, | |
| BigInt, BigInt64Array, BigUint64Array, | |
| decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, | |
| atob, btoa, | |
| performance: { now: () => Date.now() }, | |
| setTimeout: setTimeout, | |
| clearTimeout: clearTimeout, | |
| setInterval: setInterval, | |
| clearInterval: clearInterval, | |
| }; | |
| const context = vm.createContext(sandbox); | |
| const code = fs.readFileSync(codePath, 'utf8'); | |
| try { | |
| vm.runInContext(code, context, { | |
| filename: 'code.js', | |
| timeout: 10000, | |
| displayErrors: true, | |
| }); | |
| } catch (err) { | |
| console.error(err.message); | |
| process.exit(1); | |
| } | |