-
Notifications
You must be signed in to change notification settings - Fork 295
/
main.js
651 lines (561 loc) · 15.2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* eslint-disable global-require, no-use-before-define */
'use strict';
const fs = require('fs');
const vm = require('vm');
const pa = require('path');
const {EventEmitter} = require('events');
const {INSPECT_MAX_BYTES} = require('buffer');
const COFFEE_SCRIPT_COMPILER = {compiler: null};
function getCoffeeScriptCompiler() {
if (!COFFEE_SCRIPT_COMPILER.compiler) {
try {
const coffeeScript = require('coffee-script');
COFFEE_SCRIPT_COMPILER.compiler = (code, filename) => {
return coffeeScript.compile(code, {header: false, bare: true});
};
} catch (e) {
throw new VMError('Coffee-Script compiler is not installed.');
}
}
return COFFEE_SCRIPT_COMPILER.compiler;
}
function jsCompiler(code, filename) {
return code;
}
function lookupCompiler(compiler) {
if ('function' === typeof compiler) return compiler;
switch (compiler) {
case 'coffeescript':
case 'coffee-script':
case 'cs':
case 'text/coffeescript':
return getCoffeeScriptCompiler();
case 'javascript':
case 'java-script':
case 'js':
case 'text/javascript':
return jsCompiler;
default:
throw new VMError(`Unsupported compiler '${compiler}'.`);
}
}
/**
* Class Script
*
* @class
*/
class VMScript {
/**
* Create VMScript instance.
*
* @param {String} code Code to run.
* @param {String} [filename] Filename that shows up in any stack traces produced from this script.
* @param {{ lineOffset: number, columnOffset: number }} [options] Options that define vm.Script options.
* @return {VMScript}
*/
constructor(code, filename, options) {
this._code = String(code);
this.options = options || {};
this.filename = filename || this.options.filename || 'vm.js';
this._prefix = '';
this._suffix = '';
this._compiledVM = null;
this._compiledNodeVM = null;
this._compiler = lookupCompiler(this.options.compiler || 'javascript');
this._unresolvedFilename = this.options.filename || this.filename;
}
/**
* Wraps the code.
* Will invalidate the code cache.
*
* @return {VMScript}
*/
wrap(prefix, suffix) {
const strPrefix = String(prefix);
const strSuffix = String(suffix);
if (this._prefix === strPrefix && this._suffix === strSuffix) return this;
this._prefix = strPrefix;
this._suffix = strSuffix;
this._compiledVM = null;
this._compiledNodeVM = null;
return this;
}
/**
* This code will be compiled to VM code.
*
* @return {VMScript}
*/
compile() {
return this._compileVM();
}
/**
* For backwards compatibility.
*
* @return {String} The wrapped code
*/
get code() {
return this._prefix + this._code + this._suffix;
}
/**
* For backwards compatibility.
* Will invalidate the code cache.
*
* @param {String} newCode The new code to run.
*/
set code(newCode) {
const strNewCode = String(newCode);
if (strNewCode === this._prefix + this._code + this._suffix) return;
this._code = strNewCode;
this._prefix = '';
this._suffix = '';
this._compiledVM = null;
this._compiledNodeVM = null;
}
/**
* Will compile the code for VM and cache it
*
* @return {VMScript}
*/
_compileVM() {
if (this._compiledVM) return this;
this._compiledVM = new vm.Script(this._compiler(this._prefix + this._code + this._suffix, this._unresolvedFilename), {
filename: this.filename,
displayErrors: false,
lineOffset: this.options.lineOffset || 0,
columnOffset: this.options.columnOffset || 0
});
return this;
}
/**
* Will compile the code for NodeVM and cache it
*
* @return {VMScript}
*/
_compileNodeVM() {
if (this._compiledNodeVM) return this;
this._compiledNodeVM = new vm.Script('(function (exports, require, module, __filename, __dirname) { ' +
this._compiler(this._prefix + this._code + this._suffix, this._unresolvedFilename) + '\n})', {
filename: this.filename,
displayErrors: false,
lineOffset: this.options.lineOffset || 0,
columnOffset: this.options.columnOffset || 0
});
return this;
}
_runInVM(context) {
return this._compiledVM.runInContext(context, {
filename: this.filename,
displayErrors: false
});
}
_runInNodeVM(context) {
return this._compiledNodeVM.runInContext(context, {
filename: this.filename,
displayErrors: false
});
}
}
function loadScript(filename) {
const data = fs.readFileSync(filename, 'utf8');
return new VMScript(data, filename);
}
const SCRIPT_CACHE = {
cf: loadScript(`${__dirname}/contextify.js`).wrap('(function(require, host) { ', '\n})')._compileVM(),
sb: loadScript(`${__dirname}/sandbox.js`).wrap('(function (vm, host, Contextify, Decontextify, Buffer) { ', '\n})')._compileVM(),
exp: new VMScript('({exports: {}})')._compileVM(),
runTimeout: new VMScript('fn()', 'timeout_bridge.js')._compileVM()
};
const TIMEOUT_CONTEXT = {context: null};
function doWithTimeout(fn, timeout) {
if (!TIMEOUT_CONTEXT.context) {
TIMEOUT_CONTEXT.context = vm.createContext();
}
TIMEOUT_CONTEXT.context.fn = fn;
try {
return SCRIPT_CACHE.runTimeout._compiledVM.runInContext(TIMEOUT_CONTEXT.context, {
filename: SCRIPT_CACHE.runTimeout.filename,
displayErrors: false,
timeout
});
} finally {
TIMEOUT_CONTEXT.context.fn = null;
}
}
/**
* Class VM.
*
* @property {Object} options VM options.
*/
class VM extends EventEmitter {
/**
* Create VM instance.
*
* @param {Object} [options] VM options.
* @return {VM}
*/
constructor(options = {}) {
super();
// defaults
this.options = {
timeout: options.timeout,
sandbox: options.sandbox,
compiler: lookupCompiler(options.compiler || 'javascript'),
eval: options.eval === false ? false : true,
wasm: options.wasm === false ? false : true
};
const host = {
version: parseInt(process.versions.node.split('.')[0]),
console,
String,
Number,
Buffer,
Boolean,
Array,
Date,
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError,
RegExp,
Function,
Object,
VMError,
Proxy,
Reflect,
Map,
WeakMap,
Set,
WeakSet,
Promise,
Symbol,
INSPECT_MAX_BYTES
};
this._context = vm.createContext(undefined, {
codeGeneration: {
strings: this.options.eval,
wasm: this.options.wasm
}
});
Reflect.defineProperty(this, '_internal', {
value: SCRIPT_CACHE.cf._runInVM(this._context).call(this._context, require, host)
});
// prepare global sandbox
if (this.options.sandbox) {
if ('object' !== typeof this.options.sandbox) {
throw new VMError('Sandbox must be object.');
}
for (const name in this.options.sandbox) {
if (Object.prototype.hasOwnProperty.call(this.options.sandbox, name)) {
this._internal.Contextify.globalValue(this.options.sandbox[name], name);
}
}
}
}
/**
* Freezes the object inside VM making it read-only. Not available for primitive values.
*
* @static
* @param {*} object Object to freeze.
* @param {String} [globalName] Whether to add the object to global.
* @return {*} Object to freeze.
*/
freeze(value, globalName) {
this._internal.Contextify.readonly(value);
if (globalName) this._internal.Contextify.globalValue(value, globalName);
return value;
}
/**
* Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values.
*
* @static
* @param {*} object Object to protect.
* @param {String} [globalName] Whether to add the object to global.
* @return {*} Object to protect.
*/
protect(value, globalName) {
this._internal.Contextify.protected(value);
if (globalName) this._internal.Contextify.globalValue(value, globalName);
return value;
}
/**
* Run the code in VM.
*
* @param {String} code Code to run.
* @param {String} [filename] Filename that shows up in any stack traces produced from this script.
* @return {*} Result of executed code.
*/
run(code, filename) {
const script = code instanceof VMScript ? code : new VMScript(code, filename, {compiler: this.options.compiler});
script._compileVM();
if (!this.options.timeout) {
try {
return this._internal.Decontextify.value(script._runInVM(this._context));
} catch (e) {
throw this._internal.Decontextify.value(e);
}
}
return doWithTimeout(()=>{
try {
return this._internal.Decontextify.value(script._runInVM(this._context));
} catch (e) {
throw this._internal.Decontextify.value(e);
}
}, this.options.timeout);
}
}
/**
* Class NodeVM.
*
* @class
* @extends {EventEmitter}
* @property {Object} module Pointer to main module.
*/
class NodeVM extends EventEmitter {
/**
* Create NodeVM instance.
*
* Unlike VM, NodeVM lets you use require same way like in regular node.
*
* @param {Object} [options] VM options.
* @return {NodeVM}
*/
constructor(options = {}) {
super();
// defaults
this.options = {
sandbox: options.sandbox,
console: options.console || 'inherit',
require: options.require || false,
compiler: lookupCompiler(options.compiler || 'javascript'),
eval: options.eval === false ? false : true,
wasm: options.wasm === false ? false : true,
nesting: options.nesting || false,
wrapper: options.wrapper || 'commonjs',
sourceExtensions: options.sourceExtensions || ['js']
};
const host = {
version: parseInt(process.versions.node.split('.')[0]),
require,
process,
console,
setTimeout,
setInterval,
setImmediate,
clearTimeout,
clearInterval,
clearImmediate,
String,
Number,
Buffer,
Boolean,
Array,
Date,
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError,
RegExp,
Function,
Object,
VMError,
Proxy,
Reflect,
Map,
WeakMap,
Set,
WeakSet,
Promise,
Symbol,
INSPECT_MAX_BYTES
};
if (this.options.nesting) {
host.VM = VM;
host.NodeVM = NodeVM;
}
this._context = vm.createContext(undefined, {
codeGeneration: {
strings: this.options.eval,
wasm: this.options.wasm
}
});
Object.defineProperty(this, '_internal', {
value: SCRIPT_CACHE.cf._runInVM(this._context).call(this._context, require, host)
});
const closure = SCRIPT_CACHE.sb._runInVM(this._context);
Object.defineProperty(this, '_prepareRequire', {
value: closure.call(this._context, this, host, this._internal.Contextify, this._internal.Decontextify, this._internal.Buffer)
});
// prepare global sandbox
if (this.options.sandbox) {
if ('object' !== typeof this.options.sandbox) {
throw new VMError('Sandbox must be object.');
}
for (const name in this.options.sandbox) {
if (Object.prototype.hasOwnProperty.call(this.options.sandbox, name)) {
this._internal.Contextify.globalValue(this.options.sandbox[name], name);
}
}
}
if (this.options.require && this.options.require.import) {
if (!Array.isArray(this.options.require.import)) {
this.options.require.import = [this.options.require.import];
}
for (let i = 0, l = this.options.require.import.length; i < l; i++) {
this.require(this.options.require.import[i]);
}
}
}
/**
* @deprecated
*/
call(method, ...args) {
if ('function' === typeof method) {
return method.apply(args);
} else {
throw new VMError('Unrecognized method type.');
}
}
/**
* Freezes the object inside VM making it read-only. Not available for primitive values.
*
* @static
* @param {*} object Object to freeze.
* @param {String} [globalName] Whether to add the object to global.
* @return {*} Object to freeze.
*/
freeze(value, globalName) {
this._internal.Contextify.readonly(value);
if (global) this._internal.Contextify.globalValue(value, globalName);
return value;
}
/**
* Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values.
*
* @static
* @param {*} object Object to protect.
* @param {String} [globalName] Whether to add the object to global.
* @return {*} Object to protect.
*/
protect(value, globalName) {
this._internal.Contextify.protected(value);
if (global) this._internal.Contextify.globalValue(value, globalName);
return value;
}
/**
* Require a module in VM and return it's exports.
*
* @param {String} module Module name.
* @return {*} Exported module.
*/
require(module) {
return this.run(`module.exports = require('${module}');`, 'vm.js');
}
/**
* Run the code in NodeVM.
*
* First time you run this method, code is executed same way like in node's regular `require` - it's executed with
* `module`, `require`, `exports`, `__dirname`, `__filename` variables and expect result in `module.exports'.
*
* @param {String} code Code to run.
* @param {String} [filename] Filename that shows up in any stack traces produced from this script.
* @return {*} Result of executed code.
*/
run(code, filename) {
let dirname;
let returned;
let resolvedFilename;
if (filename) {
resolvedFilename = pa.resolve(filename);
dirname = pa.dirname(filename);
} else {
resolvedFilename = null;
dirname = null;
}
const module = SCRIPT_CACHE.exp._runInVM(this._context);
const script = code instanceof VMScript ? code : new VMScript(code, resolvedFilename, {compiler: this.options.compiler, filename});
script._compileNodeVM();
try {
const closure = script._runInNodeVM(this._context);
returned = closure.call(this._context, module.exports, this._prepareRequire(dirname), module, filename, dirname);
} catch (e) {
throw this._internal.Decontextify.value(e);
}
if (this.options.wrapper === 'commonjs') {
return this._internal.Decontextify.value(module.exports);
} else {
return this._internal.Decontextify.value(returned);
}
}
/**
* Create NodeVM and run code inside it.
*
* @param {String} script Javascript code.
* @param {String} [filename] File name (used in stack traces only).
* @param {Object} [options] VM options.
* @return {NodeVM} VM.
*/
static code(script, filename, options) {
if (filename != null) {
if ('object' === typeof filename) {
options = filename;
filename = null;
} else if ('string' === typeof filename) {
filename = pa.resolve(filename);
} else {
throw new VMError('Invalid arguments.');
}
}
if (arguments.length > 3) {
throw new VMError('Invalid number of arguments.');
}
return new NodeVM(options).run(script, filename);
}
/**
* Create NodeVM and run script from file inside it.
*
* @param {String} [filename] File name (used in stack traces only).
* @param {Object} [options] VM options.
* @return {NodeVM} VM.
*/
static file(filename, options) {
filename = pa.resolve(filename);
if (!fs.existsSync(filename)) {
throw new VMError(`Script '${filename}' not found.`);
}
if (fs.statSync(filename).isDirectory()) {
throw new VMError('Script must be file, got directory.');
}
return new NodeVM(options).run(fs.readFileSync(filename, 'utf8'), filename);
}
}
/**
* VMError.
*
* @class
* @extends {Error}
* @property {String} stack Call stack.
* @property {String} message Error message.
*/
class VMError extends Error {
/**
* Create VMError instance.
*
* @param {String} message Error message.
* @return {VMError}
*/
constructor(message) {
super(message);
this.name = 'VMError';
Error.captureStackTrace(this, this.constructor);
}
}
exports.VMError = VMError;
exports.NodeVM = NodeVM;
exports.VM = VM;
exports.VMScript = VMScript;