diff --git a/lib/module.js b/lib/module.js index ae6f4ff453ccc7..44a3d0ff31ab13 100644 --- a/lib/module.js +++ b/lib/module.js @@ -510,6 +510,10 @@ Module.prototype.load = function(filename) { Module.prototype.require = function(path) { assert(path, 'missing path'); assert(typeof path === 'string', 'path must be a string'); + // Ignore part of the path after null character if it exists + if (path.indexOf('\u0000') !== -1) { + path = path.split('\u0000')[0]; + } return Module._load(path, this, /* isMain */ false); }; diff --git a/test/parallel/test-require-exceptions.js b/test/parallel/test-require-exceptions.js index 57d0e9c218e206..70832d29589428 100644 --- a/test/parallel/test-require-exceptions.js +++ b/test/parallel/test-require-exceptions.js @@ -33,6 +33,12 @@ assert.throws(function() { require(`${common.fixturesDir}/throws_error`); }, /^Error: blah$/); +// Requiring the module with null character in +// path as first character of a component +assert.throws(function() { + require('../\u0000on'); +}, /^Error: Cannot find module '[.]{2}\/'$/); + // Requiring a module that does not exist should throw an // error with its `code` set to MODULE_NOT_FOUND assertModuleNotFound('/DOES_NOT_EXIST');