Skip to content

Commit 71396a2

Browse files
joyeecheungapapirovski
authored andcommitted
fs: validate path in fs.exists{Sync}
PR-URL: #17852 Refs: #17667 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 9ec700b commit 71396a2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/fs.js

+7
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ fs.accessSync = function(path, mode) {
340340
fs.exists = function(path, callback) {
341341
if (handleError((path = getPathFromURL(path)), cb))
342342
return;
343+
if (typeof path !== 'string' && !(path instanceof Buffer)) {
344+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
345+
['string', 'Buffer', 'URL']);
346+
}
343347
if (!nullCheck(path, cb)) return;
344348
var req = new FSReqWrap();
345349
req.oncomplete = cb;
@@ -361,6 +365,9 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
361365
fs.existsSync = function(path) {
362366
try {
363367
handleError((path = getPathFromURL(path)));
368+
if (typeof path !== 'string' && !(path instanceof Buffer)) {
369+
return false;
370+
}
364371
nullCheck(path);
365372
binding.stat(pathModule.toNamespacedPath(path));
366373
return true;

test/parallel/test-fs-exists.js

+11
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@ fs.exists(new URL('https://foo'), common.mustCall(function(y) {
4242

4343
assert(fs.existsSync(f));
4444
assert(!fs.existsSync(`${f}-NO`));
45+
46+
common.expectsError(
47+
() => { fs.exists(() => {}); },
48+
{
49+
code: 'ERR_INVALID_ARG_TYPE',
50+
message: 'The "path" argument must be one of type string, Buffer, or URL',
51+
type: TypeError
52+
}
53+
);
54+
55+
assert(!fs.existsSync());

0 commit comments

Comments
 (0)