Skip to content

Commit

Permalink
module: validate request in require.resolve
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18359
Fixes: nodejs#18352
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
joyeecheung authored and MayaLekova committed May 8, 2018
1 parent 2c65435 commit 89dd70a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/internal/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const errors = require('internal/errors');

// Invoke with makeRequireFunction(module) where |module| is the Module object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
Expand All @@ -15,6 +17,10 @@ function makeRequireFunction(mod) {
}

function resolve(request, options) {
if (typeof request !== 'string') {
throw new errors.Error('ERR_INVALID_ARG_TYPE',
'request', 'string', request);
}
return Module._resolveFilename(request, mod, false, options);
}

Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-require-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');

Expand All @@ -38,3 +38,13 @@ assert.strictEqual('path', require.resolve('path'));
// Test configurable resolve() paths.
require(fixtures.path('require-resolve.js'));
require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));

const re = /^The "request" argument must be of type string\. Received type \w+$/;
[1, false, null, undefined, {}].forEach((value) => {
common.expectsError(
() => { require.resolve(value); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: re
});
});

0 comments on commit 89dd70a

Please sign in to comment.