fs: validate path in fs.exists{Sync}

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:01:11 +08:00 committed by Anatoli Papirovski
parent 9ec700b073
commit 71396a200d
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 18 additions and 0 deletions

View File

@ -340,6 +340,10 @@ fs.accessSync = function(path, mode) {
fs.exists = function(path, callback) {
if (handleError((path = getPathFromURL(path)), cb))
return;
if (typeof path !== 'string' && !(path instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
['string', 'Buffer', 'URL']);
}
if (!nullCheck(path, cb)) return;
var req = new FSReqWrap();
req.oncomplete = cb;
@ -361,6 +365,9 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
fs.existsSync = function(path) {
try {
handleError((path = getPathFromURL(path)));
if (typeof path !== 'string' && !(path instanceof Buffer)) {
return false;
}
nullCheck(path);
binding.stat(pathModule.toNamespacedPath(path));
return true;

View File

@ -42,3 +42,14 @@ fs.exists(new URL('https://foo'), common.mustCall(function(y) {
assert(fs.existsSync(f));
assert(!fs.existsSync(`${f}-NO`));
common.expectsError(
() => { fs.exists(() => {}); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "path" argument must be one of type string, Buffer, or URL',
type: TypeError
}
);
assert(!fs.existsSync());