index.d.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {EventEmitter} from 'events';
  2. /**
  3. * Require options for a VM
  4. */
  5. export interface VMRequire {
  6. /** Array of allowed built-in modules, accepts ["*"] for all. Using "*" increases the attack surface and potential
  7. * new modules allow to escape the sandbox. (default: none) */
  8. builtin?: string[];
  9. /*
  10. * `host` (default) to require modules in host and proxy them to sandbox. `sandbox` to load, compile and
  11. * require modules in sandbox. Built-in modules except `events` always required in host and proxied to sandbox
  12. */
  13. context?: "host" | "sandbox";
  14. /** `true`, an array of allowed external modules or an object with external options (default: `false`) */
  15. external?: boolean | string[] | { modules: string[], transitive: boolean };
  16. /** Array of modules to be loaded into NodeVM on start. */
  17. import?: string[];
  18. /** Restricted path(s) where local modules can be required (default: every path). */
  19. root?: string | string[];
  20. /** Collection of mock modules (both external or built-in). */
  21. mock?: any;
  22. /* An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. */
  23. resolve?: (moduleName: string, parentDirname: string) => string | undefined;
  24. /** Custom require to require host and built-in modules. */
  25. customRequire?: (id: string) => any;
  26. }
  27. /**
  28. * A custom compiler function for all of the JS that comes
  29. * into the VM
  30. */
  31. type CompilerFunction = (code: string, filename: string) => string;
  32. /**
  33. * Options for creating a VM
  34. */
  35. export interface VMOptions {
  36. /**
  37. * `javascript` (default) or `coffeescript` or custom compiler function (which receives the code, and it's file path).
  38. * The library expects you to have coffee-script pre-installed if the compiler is set to `coffeescript`.
  39. */
  40. compiler?: "javascript" | "coffeescript" | CompilerFunction;
  41. /** VM's global object. */
  42. sandbox?: any;
  43. /**
  44. * Script timeout in milliseconds. Timeout is only effective on code you run through `run`.
  45. * Timeout is NOT effective on any method returned by VM.
  46. */
  47. timeout?: number;
  48. /**
  49. * If set to `false` any calls to eval or function constructors (`Function`, `GeneratorFunction`, etc.) will throw an
  50. * `EvalError` (default: `true`).
  51. */
  52. eval?: boolean;
  53. /**
  54. * If set to `false` any attempt to compile a WebAssembly module will throw a `WebAssembly.CompileError` (default: `true`).
  55. */
  56. wasm?: boolean;
  57. /**
  58. * If set to `true` any attempt to run code using async will throw a `VMError` (default: `false`).
  59. * @deprecated Use `allowAsync` instead.
  60. */
  61. fixAsync?: boolean;
  62. /**
  63. * If set to `false` any attempt to run code using async will throw a `VMError` (default: `true`).
  64. */
  65. allowAsync?: boolean;
  66. }
  67. /**
  68. * Options for creating a NodeVM
  69. */
  70. export interface NodeVMOptions extends VMOptions {
  71. /** `inherit` to enable console, `redirect` to redirect to events, `off` to disable console (default: `inherit`). */
  72. console?: "inherit" | "redirect" | "off";
  73. /** `true` or an object to enable `require` options (default: `false`). */
  74. require?: true | VMRequire;
  75. /** **WARNING**: This should be disabled. It allows to create a NodeVM form within the sandbox which could return any host module.
  76. * `true` to enable VMs nesting (default: `false`). */
  77. nesting?: boolean;
  78. /** `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script. */
  79. wrapper?: "commonjs" | "none";
  80. /** File extensions that the internal module resolver should accept. */
  81. sourceExtensions?: string[];
  82. /**
  83. * Array of arguments passed to `process.argv`.
  84. * This object will not be copied and the script can change this object.
  85. */
  86. argv?: string[];
  87. /**
  88. * Environment map passed to `process.env`.
  89. * This object will not be copied and the script can change this object.
  90. */
  91. env?: any;
  92. /** Run modules in strict mode. Required modules are always strict. */
  93. strict?: boolean;
  94. }
  95. /**
  96. * VM is a simple sandbox, without `require` feature, to synchronously run an untrusted code.
  97. * Only JavaScript built-in objects + Buffer are available. Scheduling functions
  98. * (`setInterval`, `setTimeout` and `setImmediate`) are not available by default.
  99. */
  100. export class VM {
  101. constructor(options?: VMOptions);
  102. /** Direct access to the global sandbox object */
  103. readonly sandbox: any;
  104. /** Timeout to use for the run methods */
  105. timeout?: number;
  106. /** Runs the code */
  107. run(script: string|VMScript, options?: string|{filename?: string}): any;
  108. /** Runs the code in the specific file */
  109. runFile(filename: string): any;
  110. /** Loads all the values into the global object with the same names */
  111. setGlobals(values: any): this;
  112. /** Make a object visible as a global with a specific name */
  113. setGlobal(name: string, value: any): this;
  114. /** Get the global object with the specific name */
  115. getGlobal(name: string): any;
  116. /** Freezes the object inside VM making it read-only. Not available for primitive values. */
  117. freeze(object: any, name?: string): any;
  118. /** Freezes the object inside VM making it read-only. Not available for primitive values. */
  119. readonly(object: any): any;
  120. /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
  121. protect(object: any, name?: string): any;
  122. }
  123. /**
  124. * A VM with behavior more similar to running inside Node.
  125. */
  126. export class NodeVM extends EventEmitter implements VM {
  127. constructor(options?: NodeVMOptions);
  128. /** Require a module in VM and return it's exports. */
  129. require(module: string): any;
  130. /**
  131. * Create NodeVM and run code inside it.
  132. *
  133. * @param {string} script JavaScript code.
  134. * @param {string} [filename] File name (used in stack traces only).
  135. * @param {Object} [options] VM options.
  136. */
  137. static code(script: string, filename?: string, options?: NodeVMOptions): any;
  138. /**
  139. * Create NodeVM and run script from file inside it.
  140. *
  141. * @param {string} [filename] File name (used in stack traces only).
  142. * @param {Object} [options] VM options.
  143. */
  144. static file(filename: string, options?: NodeVMOptions): any;
  145. /** Direct access to the global sandbox object */
  146. readonly sandbox: any;
  147. /** Only here because of implements VM. Does nothing. */
  148. timeout?: number;
  149. /** Runs the code */
  150. run(js: string|VMScript, options?: string|{filename?: string, wrapper?: "commonjs" | "none", strict?: boolean}): any;
  151. /** Runs the code in the specific file */
  152. runFile(filename: string): any;
  153. /** Loads all the values into the global object with the same names */
  154. setGlobals(values: any): this;
  155. /** Make a object visible as a global with a specific name */
  156. setGlobal(name: string, value: any): this;
  157. /** Get the global object with the specific name */
  158. getGlobal(name: string): any;
  159. /** Freezes the object inside VM making it read-only. Not available for primitive values. */
  160. freeze(object: any, name?: string): any;
  161. /** Freezes the object inside VM making it read-only. Not available for primitive values. */
  162. readonly(object: any): any;
  163. /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
  164. protect(object: any, name?: string): any;
  165. }
  166. /**
  167. * You can increase performance by using pre-compiled scripts.
  168. * The pre-compiled VMScript can be run later multiple times. It is important to note that the code is not bound
  169. * to any VM (context); rather, it is bound before each run, just for that run.
  170. */
  171. export class VMScript {
  172. constructor(code: string, path: string, options?: {
  173. lineOffset?: number;
  174. columnOffset?: number;
  175. compiler?: "javascript" | "coffeescript" | CompilerFunction;
  176. });
  177. constructor(code: string, options?: {
  178. filename?: string,
  179. lineOffset?: number;
  180. columnOffset?: number;
  181. compiler?: "javascript" | "coffeescript" | CompilerFunction;
  182. });
  183. readonly code: string;
  184. readonly filename: string;
  185. readonly lineOffset: number;
  186. readonly columnOffset: number;
  187. readonly compiler: "javascript" | "coffeescript" | CompilerFunction;
  188. /**
  189. * Wraps the code
  190. * @deprecated
  191. */
  192. wrap(prefix: string, postfix: string): this;
  193. /** Compiles the code. If called multiple times, the code is only compiled once. */
  194. compile(): this;
  195. }
  196. /** Custom Error class */
  197. export class VMError extends Error {}