fs: validate path in fs.readFile

PR-URL: https://github.com/nodejs/node/pull/17852
Refs: https://github.com/nodejs/node/pull/17667
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Joyee Cheung 2017-12-25 05:00:24 +08:00 committed by Anatoli Papirovski
parent 03b8ac14e7
commit 9ec700b073
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 13 additions and 0 deletions

View File

@ -389,6 +389,9 @@ fs.readFile = function(path, options, callback) {
req.oncomplete(null, path);
});
return;
} else if (typeof path !== 'string' && !(path instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
['string', 'Buffer', 'URL']);
}
binding.open(pathModule.toNamespacedPath(path),

View File

@ -21,6 +21,7 @@
'use strict';
const common = require('../common');
const fs = require('fs');
// Test that fs.readFile fails correctly on a non-existent file.
@ -54,3 +55,12 @@ test({ NODE_DEBUG: 'fs' }, common.mustCall((data) => {
assert(/EISDIR/.test(data));
assert(/test-fs-readfile-error/.test(data));
}));
common.expectsError(
() => { fs.readFile(() => {}); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "path" argument must be one of type string, Buffer, or URL',
type: TypeError
}
);