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:
parent
9ec700b073
commit
71396a200d
@ -340,6 +340,10 @@ fs.accessSync = function(path, mode) {
|
|||||||
fs.exists = function(path, callback) {
|
fs.exists = function(path, callback) {
|
||||||
if (handleError((path = getPathFromURL(path)), cb))
|
if (handleError((path = getPathFromURL(path)), cb))
|
||||||
return;
|
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;
|
if (!nullCheck(path, cb)) return;
|
||||||
var req = new FSReqWrap();
|
var req = new FSReqWrap();
|
||||||
req.oncomplete = cb;
|
req.oncomplete = cb;
|
||||||
@ -361,6 +365,9 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
|
|||||||
fs.existsSync = function(path) {
|
fs.existsSync = function(path) {
|
||||||
try {
|
try {
|
||||||
handleError((path = getPathFromURL(path)));
|
handleError((path = getPathFromURL(path)));
|
||||||
|
if (typeof path !== 'string' && !(path instanceof Buffer)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
nullCheck(path);
|
nullCheck(path);
|
||||||
binding.stat(pathModule.toNamespacedPath(path));
|
binding.stat(pathModule.toNamespacedPath(path));
|
||||||
return true;
|
return true;
|
||||||
|
@ -42,3 +42,14 @@ fs.exists(new URL('https://foo'), common.mustCall(function(y) {
|
|||||||
|
|
||||||
assert(fs.existsSync(f));
|
assert(fs.existsSync(f));
|
||||||
assert(!fs.existsSync(`${f}-NO`));
|
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());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user