Skip to content

Commit 0deecf8

Browse files
rvaggsaper
authored andcommitted
module: apply null bytes check to module read path
PR-URL: nodejs/node#8277
1 parent a9d8ec7 commit 0deecf8

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/fs.js

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ function makeStatsCallback(cb) {
154154
};
155155
}
156156

157+
// This is duplicated in module.js and needs to be moved to internal/fs.js
158+
// once it is OK again to include internal/ resources in fs.js.
159+
// See: https://github.com/nodejs/node/pull/6413
157160
function nullCheck(path, callback) {
158161
if (('' + path).indexOf('\u0000') !== -1) {
159162
var er = new Error('Path must be a string without null bytes');

lib/module.js

+18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ function stat(filename) {
4747
stat.cache = null;
4848

4949

50+
// This is duplicated from fs.js and needs to be moved to internal/fs.js
51+
// once it is OK again to include internal/ resources in fs.js.
52+
// See: https://github.com/nodejs/node/pull/6413
53+
function nullCheck(path, callback) {
54+
if (('' + path).indexOf('\u0000') !== -1) {
55+
var er = new Error('Path must be a string without null bytes');
56+
er.code = 'ENOENT';
57+
if (typeof callback !== 'function')
58+
throw er;
59+
process.nextTick(callback, er);
60+
return false;
61+
}
62+
return true;
63+
}
64+
65+
5066
function Module(id, parent) {
5167
this.id = id;
5268
this.exports = {};
@@ -159,6 +175,8 @@ function tryExtensions(p, exts, isMain) {
159175

160176
var warned = false;
161177
Module._findPath = function(request, paths, isMain) {
178+
nullCheck(request);
179+
162180
if (path.isAbsolute(request)) {
163181
paths = [''];
164182
} else if (!paths || paths.length === 0) {

test/parallel/test-fs-null-bytes.js

+16
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ fs.exists('foo\u0000bar', common.mustCall((exists) => {
137137
assert(!exists);
138138
}));
139139
assert(!fs.existsSync('foo\u0000bar'));
140+
141+
function checkRequire(arg) {
142+
assert.throws(function() {
143+
console.error(`require(${JSON.stringify(arg)})`);
144+
require(arg);
145+
}, expectedError);
146+
}
147+
148+
checkRequire('\u0000');
149+
checkRequire('foo\u0000bar');
150+
checkRequire('foo\u0000');
151+
checkRequire('foo/\u0000');
152+
checkRequire('foo/\u0000.js');
153+
checkRequire('\u0000/foo');
154+
checkRequire('./foo/\u0000');
155+
checkRequire('./\u0000/foo');

0 commit comments

Comments
 (0)