Skip to content

Commit

Permalink
module: apply null bytes check to module read path
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg authored and saper committed Aug 17, 2017
1 parent a9d8ec7 commit 0deecf8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ function makeStatsCallback(cb) {
};
}

// This is duplicated in module.js and needs to be moved to internal/fs.js
// once it is OK again to include internal/ resources in fs.js.
// See: https://github.com/nodejs/node/pull/6413
function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes');
Expand Down
18 changes: 18 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ function stat(filename) {
stat.cache = null;


// This is duplicated from fs.js and needs to be moved to internal/fs.js
// once it is OK again to include internal/ resources in fs.js.
// See: https://github.com/nodejs/node/pull/6413
function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes');
er.code = 'ENOENT';
if (typeof callback !== 'function')
throw er;
process.nextTick(callback, er);
return false;
}
return true;
}


function Module(id, parent) {
this.id = id;
this.exports = {};
Expand Down Expand Up @@ -159,6 +175,8 @@ function tryExtensions(p, exts, isMain) {

var warned = false;
Module._findPath = function(request, paths, isMain) {
nullCheck(request);

if (path.isAbsolute(request)) {
paths = [''];
} else if (!paths || paths.length === 0) {
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-fs-null-bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@ fs.exists('foo\u0000bar', common.mustCall((exists) => {
assert(!exists);
}));
assert(!fs.existsSync('foo\u0000bar'));

function checkRequire(arg) {
assert.throws(function() {
console.error(`require(${JSON.stringify(arg)})`);
require(arg);
}, expectedError);
}

checkRequire('\u0000');
checkRequire('foo\u0000bar');
checkRequire('foo\u0000');
checkRequire('foo/\u0000');
checkRequire('foo/\u0000.js');
checkRequire('\u0000/foo');
checkRequire('./foo/\u0000');
checkRequire('./\u0000/foo');

0 comments on commit 0deecf8

Please sign in to comment.