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:
parent
03b8ac14e7
commit
9ec700b073
@ -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),
|
||||
|
@ -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
|
||||
}
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user