Skip to content

Commit

Permalink
fs: validate path in fs.exists{Sync}
Browse files Browse the repository at this point in the history
PR-URL: #17852
Refs: #17667
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
joyeecheung authored and apapirovski committed Dec 27, 2017
1 parent 9ec700b commit 71396a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-fs-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit 71396a2

Please sign in to comment.