Skip to content

Commit 58478a5

Browse files
authored
Merge pull request #448 from XmiliaH/fix-442
Allow to load modules in non strict mode
2 parents 6b0b71a + cb647c0 commit 58478a5

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ Unlike `VM`, `NodeVM` allows you to require modules in the same way that you wou
144144
* `require.context` - `host` (default) to require modules in the host and proxy them into the sandbox. `sandbox` to load, compile, and require modules in the sandbox. Except for `events`, built-in modules are always required in the host and proxied into the sandbox.
145145
* `require.import` - An array of modules to be loaded into NodeVM on start.
146146
* `require.resolve` - An additional lookup function in case a module wasn't found in one of the traditional node lookup paths.
147+
* `require.customRequire` - Use instead of the `require` function to load modules from the host.
148+
* `require.strict` - `false` to not force strict mode on modules loaded by require (default: `true`).
147149
* `nesting` - **WARNING**: Allowing this is a security risk as scripts can create a NodeVM which can require any host module. `true` to enable VMs nesting (default: `false`).
148150
* `wrapper` - `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script.
149151
* `argv` - Array to be passed to `process.argv`.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface VMRequire {
2424
resolve?: (moduleName: string, parentDirname: string) => string | undefined;
2525
/** Custom require to require host and built-in modules. */
2626
customRequire?: (id: string) => any;
27+
/** Load modules in strict mode. (default: true) */
28+
strict?: boolean;
2729
}
2830

2931
/**

lib/nodevm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class NodeVM extends VM {
184184
* @param {resolveCallback} [options.require.resolve] - An additional lookup function in case a module wasn't
185185
* found in one of the traditional node lookup paths.
186186
* @param {customRequire} [options.require.customRequire=require] - Custom require to require host and built-in modules.
187+
* @param {boolean} [option.require.strict=true] - Load required modules in strict mode.
187188
* @param {boolean} [options.nesting=false] -
188189
* <b>WARNING: Allowing this is a security risk as scripts can create a NodeVM which can require any host module.</b>
189190
* Allow nesting of VMs.

lib/resolver-compat.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function makeExternalMatcher(obj) {
4646

4747
class LegacyResolver extends DefaultResolver {
4848

49-
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, externals, allowTransitive) {
50-
super(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler);
49+
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict, externals, allowTransitive) {
50+
super(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict);
5151
this.externals = externals;
5252
this.currMod = undefined;
5353
this.trustedMods = new WeakMap();
@@ -282,7 +282,8 @@ function resolverFromOptions(vm, options, override, compiler) {
282282
root: rootPaths,
283283
resolve: customResolver,
284284
customRequire: hostRequire = defaultRequire,
285-
context = 'host'
285+
context = 'host',
286+
strict = true,
286287
} = options;
287288

288289
const builtins = genBuiltinsFromOptions(vm, builtinOpt, mockOpt, override);
@@ -325,7 +326,7 @@ function resolverFromOptions(vm, options, override, compiler) {
325326
}
326327

327328
if (typeof externalOpt !== 'object') {
328-
return new DefaultResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler);
329+
return new DefaultResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, strict);
329330
}
330331

331332
let transitive = false;
@@ -336,7 +337,7 @@ function resolverFromOptions(vm, options, override, compiler) {
336337
transitive = context === 'sandbox' && externalOpt.transitive;
337338
}
338339
externals = external.map(makeExternalMatcher);
339-
return new LegacyResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, externals, transitive);
340+
return new LegacyResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, strict, externals, transitive);
340341
}
341342

342343
exports.resolverFromOptions = resolverFromOptions;

lib/resolver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ class Resolver {
140140

141141
class DefaultResolver extends Resolver {
142142

143-
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler) {
143+
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict) {
144144
super(builtinModules, globalPaths, hostRequire);
145145
this.checkPath = checkPath;
146146
this.pathContext = pathContext;
147147
this.customResolver = customResolver;
148148
this.compiler = compiler;
149+
this.strict = strict;
149150
this.packageCache = {__proto__: null};
150151
this.scriptCache = {__proto__: null};
151152
}
@@ -200,7 +201,7 @@ class DefaultResolver extends Resolver {
200201
this.checkAccess(mod, filename);
201202
if (this.pathContext(filename, 'js') === 'sandbox') {
202203
const script = this.readScript(filename);
203-
vm.run(script, {filename, strict: true, module: mod, wrapper: 'none', dirname: mod.path});
204+
vm.run(script, {filename, strict: this.strict, module: mod, wrapper: 'none', dirname: mod.path});
204205
} else {
205206
const m = this.hostRequire(filename);
206207
mod.exports = vm.readonly(m);

0 commit comments

Comments
 (0)