Commit 1fb0cd69784 for nodejs
commit 1fb0cd69784931c7433c2428ef33810e98155572
Author: Yohannes Desta <108905193+yohannesjoya@users.noreply.github.com>
Date: Wed Nov 18 06:59:25 2026 +0300
fs: validate opendir() bufferSize before opening the directory
fs.opendir() only checked options.bufferSize in the Dir constructor,
which runs from the completion callback of the native opendir call.
An invalid value was thrown from there, so it could not be caught and
the process exited. fs.promises.opendir() is built on fs.opendir() and
had the same problem.
Check bufferSize before starting the native call, so fs.opendir()
throws to the caller and fs.promises.opendir() rejects.
Fixes: https://github.com/nodejs/node/issues/67182
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67183
Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
diff --git a/lib/internal/fs/dir.js b/lib/internal/fs/dir.js
index 32050f31ae6..4ecdfd9c07e 100644
--- a/lib/internal/fs/dir.js
+++ b/lib/internal/fs/dir.js
@@ -350,6 +350,13 @@ function opendir(path, options, callback) {
encoding: 'utf8',
});
+ // Validate bufferSize before opening the directory. The Dir constructor
+ // validates it too, but an error thrown from opendirCallback can't be caught.
+ // Copy the options and validate the copy so that the values checked here are
+ // the ones Dir receives.
+ options = getOptions({ bufferSize: 32, ...options });
+ validateUint32(options.bufferSize, 'options.bufferSize', true);
+
function opendirCallback(error, handle) {
if (error) {
callback(error);
diff --git a/test/parallel/test-fs-opendir-invalid-buffersize.js b/test/parallel/test-fs-opendir-invalid-buffersize.js
new file mode 100644
index 00000000000..c48bfa25a29
--- /dev/null
+++ b/test/parallel/test-fs-opendir-invalid-buffersize.js
@@ -0,0 +1,80 @@
+'use strict';
+
+// This tests that fs.opendir() and fs.promises.opendir() report an invalid
+// bufferSize to the caller instead of throwing from an internal callback.
+
+const common = require('../common');
+const assert = require('assert');
+const fs = require('fs');
+
+const tmpdir = require('../common/tmpdir');
+tmpdir.refresh();
+
+for (const bufferSize of [-1, 0, 0.5, 1.5, Infinity, NaN]) {
+ const expected = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError' };
+ assert.throws(
+ () => fs.opendir(tmpdir.path, { bufferSize }, common.mustNotCall()),
+ expected,
+ );
+ assert.rejects(fs.promises.opendir(tmpdir.path, { bufferSize }), expected)
+ .then(common.mustCall());
+}
+
+for (const bufferSize of ['', '1', null, undefined]) {
+ const expected = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' };
+ assert.throws(
+ () => fs.opendir(tmpdir.path, { bufferSize }, common.mustNotCall()),
+ expected,
+ );
+ assert.rejects(fs.promises.opendir(tmpdir.path, { bufferSize }), expected)
+ .then(common.mustCall());
+}
+
+// A valid bufferSize still opens the directory.
+fs.opendir(tmpdir.path, { bufferSize: 1 }, common.mustSucceed((dir) => {
+ dir.closeSync();
+}));
+
+// bufferSize is read only once, so a getter can't pass the validation and then
+// return an invalid value when the directory has been opened.
+{
+ let reads = 0;
+ const options = {
+ get bufferSize() {
+ return reads++ === 0 ? 1 : 0;
+ },
+ };
+ fs.opendir(tmpdir.path, options, common.mustSucceed((dir) => {
+ assert.strictEqual(reads, 1);
+ dir.closeSync();
+ }));
+}
+
+// The other options are copied and validated before the directory is opened,
+// so a getter can't pass the checks and then hand an invalid value to the Dir
+// constructor. The call either fails or succeeds, never from the internal
+// callback.
+for (let validReads = 0; validReads < 5; validReads++) {
+ let reads = 0;
+ const options = {
+ get encoding() {
+ return reads++ < validReads ? 'utf8' : 'bogus';
+ },
+ };
+ fs.promises.opendir(tmpdir.path, options).then(
+ (dir) => dir.closeSync(),
+ (err) => {
+ if (err.code !== 'ERR_INVALID_ARG_VALUE') throw err;
+ },
+ ).then(common.mustCall());
+}
+
+// A non-enumerable bufferSize is ignored, the same as in fs.opendirSync().
+{
+ const options = {};
+ Object.defineProperty(options, 'bufferSize', { value: 0 });
+ fs.opendirSync(tmpdir.path, options).closeSync();
+ fs.opendir(tmpdir.path, options, common.mustSucceed((dir) => {
+ dir.closeSync();
+ }));
+}