Skip to content

Commit

Permalink
Merge pull request #275 from XmiliaH/fix-260
Browse files Browse the repository at this point in the history
Require helpers statically in main.
  • Loading branch information
patriksimek committed Mar 29, 2020
2 parents d267a32 + 4cd78be commit 2049e4d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
12 changes: 12 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function match(wildcard, s) {
const regexString = escapeRegExp(wildcard).replace(/\\\*/g, '\\S*').replace(/\\\?/g, '.');
const regex = new RegExp(regexString);
return regex.test(s);
}

module.exports = {match};
4 changes: 3 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const vm = require('vm');
const pa = require('path');
const {EventEmitter} = require('events');
const {INSPECT_MAX_BYTES} = require('buffer');
const helpers = require('./helpers.js');

/**
* Load a script from a file and compile it.
Expand Down Expand Up @@ -1181,7 +1182,8 @@ const HOST = {
Symbol,
INSPECT_MAX_BYTES,
VM,
NodeVM
NodeVM,
helpers
};

exports.VMError = VMError;
Expand Down
3 changes: 1 addition & 2 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
const {Script} = host.require('vm');
const fs = host.require('fs');
const pa = host.require('path');
const {match} = host.require('../lib/wildcard');

const BUILTIN_MODULES = host.process.binding('natives');
const parseJSON = JSON.parse;
Expand Down Expand Up @@ -265,7 +264,7 @@ return ((vm, host) => {
if (typeof vm.options.require.external === 'object') {
const { external, transitive } = _parseExternalOptions(vm.options.require.external);

const isWhitelisted = external.some(ext => match(ext, moduleName)) || (transitive && parentAllowsTransitive);
const isWhitelisted = external.some(ext => host.helpers.match(ext, moduleName)) || (transitive && parentAllowsTransitive);
if (!isWhitelisted) {
throw new VMError(`The module '${moduleName}' is not whitelisted in VM.`, 'EDENIED');
}
Expand Down
7 changes: 0 additions & 7 deletions lib/wildcard.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/wildcard.js → test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */

const {match} = require('../lib/wildcard');
const {match} = require('../lib/helpers');
const assert = require('assert');

describe('wildcard matching', () => {
Expand Down

0 comments on commit 2049e4d

Please sign in to comment.