Skip to content

Commit

Permalink
Fix resolver issue
Browse files Browse the repository at this point in the history
  • Loading branch information
XmiliaH committed May 16, 2023
1 parent 2f446e5 commit cfa3fc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
17 changes: 3 additions & 14 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,8 @@ class DefaultResolver extends Resolver {
}

loadJS(vm, mod, filename) {
filename = this.pathResolve(filename);
this.checkAccess(mod, filename);
if (this.pathContext(filename, 'js') !== 'host') {
const script = this.readScript(filename);
vm.run(script, {filename, strict: this.isStrict(filename), module: mod, wrapper: 'none', dirname: mod.path});
} else {
const m = this.hostRequire(filename);
mod.exports = vm.readonly(m);
}
const script = this.readScript(filename);
vm.run(script, {filename, strict: this.isStrict(filename), module: mod, wrapper: 'none', dirname: mod.path});
}

loadJSON(vm, mod, filename) {
Expand All @@ -223,11 +216,7 @@ class DefaultResolver extends Resolver {
}

loadNode(vm, mod, filename) {
filename = this.pathResolve(filename);
this.checkAccess(mod, filename);
if (this.pathContext(filename, 'node') !== 'host') throw new VMError('Native modules can be required only with context set to \'host\'.');
const m = this.hostRequire(filename);
mod.exports = vm.readonly(m);
throw new VMError('Native modules can be required only with context set to \'host\'.');
}

customResolve(x, path, extList) {
Expand Down
35 changes: 25 additions & 10 deletions test/nodevm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const {NodeVM, VMScript, makeResolverFromLegacyOptions} = require('..');

global.isHost = true;

function isVMProxy(obj) {
const key = {};
const proto = Object.getPrototypeOf(obj);
if (!proto) return undefined;
proto.isVMProxy = key;
const proxy = obj.isVMProxy !== key;
delete proto.isVMProxy;
return proxy;
}

describe('NodeVM', () => {
let vm;

Expand Down Expand Up @@ -228,7 +238,7 @@ describe('modules', () => {
assert.ok(vm.run("require('module1')", __filename));
});

it('allows choosing a context by path', () => {
it('allows choosing a context by path legacy', () => {
const vm = new NodeVM({
require: {
external: {
Expand All @@ -241,15 +251,20 @@ describe('modules', () => {
}
}
});
function isVMProxy(obj) {
const key = {};
const proto = Object.getPrototypeOf(obj);
if (!proto) return undefined;
proto.isVMProxy = key;
const proxy = obj.isVMProxy !== key;
delete proto.isVMProxy;
return proxy;
}
assert.equal(isVMProxy(vm.run("module.exports = require('mocha')", __filename)), false, 'Mocha is a proxy');
assert.equal(isVMProxy(vm.run("module.exports = require('module1')", __filename)), true, 'Module1 is not a proxy');
});

it('allows choosing a context by path', () => {
const vm = new NodeVM({
require: {
external: true,
context(module) {
if (module.includes('mocha')) return 'host';
return 'sandbox';
}
}
});
assert.equal(isVMProxy(vm.run("module.exports = require('mocha')", __filename)), false, 'Mocha is a proxy');
assert.equal(isVMProxy(vm.run("module.exports = require('module1')", __filename)), true, 'Module1 is not a proxy');
});
Expand Down

0 comments on commit cfa3fc6

Please sign in to comment.