Skip to content

Commit 9ec700b

Browse files
joyeecheungapapirovski
authored andcommitted
fs: validate path in fs.readFile
PR-URL: #17852 Refs: #17667 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 03b8ac1 commit 9ec700b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/fs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ fs.readFile = function(path, options, callback) {
389389
req.oncomplete(null, path);
390390
});
391391
return;
392+
} else if (typeof path !== 'string' && !(path instanceof Buffer)) {
393+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
394+
['string', 'Buffer', 'URL']);
392395
}
393396

394397
binding.open(pathModule.toNamespacedPath(path),

test/parallel/test-fs-readfile-error.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
const fs = require('fs');
2425

2526
// Test that fs.readFile fails correctly on a non-existent file.
2627

@@ -54,3 +55,12 @@ test({ NODE_DEBUG: 'fs' }, common.mustCall((data) => {
5455
assert(/EISDIR/.test(data));
5556
assert(/test-fs-readfile-error/.test(data));
5657
}));
58+
59+
common.expectsError(
60+
() => { fs.readFile(() => {}); },
61+
{
62+
code: 'ERR_INVALID_ARG_TYPE',
63+
message: 'The "path" argument must be one of type string, Buffer, or URL',
64+
type: TypeError
65+
}
66+
);

0 commit comments

Comments
 (0)