Skip to content

Commit ecef871

Browse files
committed
fs: ensure nullCheck() callback is a function
Currently, nullCheck() will attempt to invoke any truthy value as a function if the path argument contains a null character. This commit validates that the callback is actually a function before trying to invoke it. fs.access() was vulnerable to this bug, as nullCheck() was called prior to type checking its callback. PR-URL: #887 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent ed240f4 commit ecef871

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

lib/fs.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function nullCheck(path, callback) {
8484
if (('' + path).indexOf('\u0000') !== -1) {
8585
var er = new Error('Path must be a string without null bytes.');
8686
er.code = 'ENOENT';
87-
if (!callback)
87+
if (typeof callback !== 'function')
8888
throw er;
8989
process.nextTick(function() {
9090
callback(er);
@@ -169,16 +169,16 @@ fs.Stats.prototype.isSocket = function() {
169169
});
170170

171171
fs.access = function(path, mode, callback) {
172-
if (!nullCheck(path, callback))
173-
return;
174-
175172
if (typeof mode === 'function') {
176173
callback = mode;
177174
mode = fs.F_OK;
178175
} else if (typeof callback !== 'function') {
179176
throw new TypeError('callback must be a function');
180177
}
181178

179+
if (!nullCheck(path, callback))
180+
return;
181+
182182
mode = mode | 0;
183183
var req = new FSReqWrap();
184184
req.oncomplete = makeCallback(callback);

test/parallel/test-fs-access.js

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ assert.throws(function() {
5757
fs.access(__filename, fs.F_OK);
5858
}, /callback must be a function/);
5959

60+
assert.throws(function() {
61+
fs.access(__filename, fs.F_OK, {});
62+
}, /callback must be a function/);
63+
6064
assert.doesNotThrow(function() {
6165
fs.accessSync(__filename);
6266
});

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ function check(async, sync) {
2020
async.apply(null, argsAsync);
2121
}
2222

23+
check(fs.access, fs.accessSync, 'foo\u0000bar');
24+
check(fs.access, fs.accessSync, 'foo\u0000bar', fs.F_OK);
2325
check(fs.appendFile, fs.appendFileSync, 'foo\u0000bar');
2426
check(fs.chmod, fs.chmodSync, 'foo\u0000bar', '0644');
2527
check(fs.chown, fs.chownSync, 'foo\u0000bar', 12, 34);
@@ -52,4 +54,3 @@ fs.exists('foo\u0000bar', function(exists) {
5254
assert(!exists);
5355
});
5456
assert(!fs.existsSync('foo\u0000bar'));
55-

0 commit comments

Comments
 (0)