Commit 31fff58ab20 for nodejs
commit 31fff58ab208805f6948d7d271774a1706cef64c
Author: Yohannes Desta <108905193+yohannesjoya@users.noreply.github.com>
Date: Thu Nov 26 19:42:11 2026 +0300
fs: reject a mode above INT32_MAX instead of aborting
parseFileMode() accepted any 32-bit unsigned integer, but the native
bindings read the mode as a signed 32-bit integer and CHECK that it is
one. A mode between 2**31 and 2**32 - 1 therefore passed validation and
then aborted the process from fs.open(), fs.chmod(), fs.mkdir() and
their sync and promise variants. Validate the mode as a non-negative
Int32 instead so that the caller gets a RangeError.
ReadStream and WriteStream now validate options.mode in the constructor
too, so an invalid mode throws where the stream is created instead of
failing inside the open callback.
Fixes: https://github.com/nodejs/node/issues/62516
Refs: https://github.com/nodejs/node/pull/62538
Co-authored-by: wei_ds <wei_ds@126.com>
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67498
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js
index e66a256f811..a2f91bdfc55 100644
--- a/lib/internal/fs/streams.js
+++ b/lib/internal/fs/streams.js
@@ -27,6 +27,7 @@ const {
kEmptyObject,
} = require('internal/util');
const {
+ parseFileMode,
validateBoolean,
validateFunction,
validateInteger,
@@ -212,7 +213,7 @@ function ReadStream(path, options) {
// Path will be ignored when fd is specified, so it can be falsy
this.path = toPathIfFileURL(path);
this.flags = options.flags === undefined ? 'r' : options.flags;
- this.mode = options.mode === undefined ? 0o666 : options.mode;
+ this.mode = parseFileMode(options.mode, 'options.mode', 0o666);
validatePath(this.path);
} else {
@@ -371,7 +372,7 @@ function WriteStream(path, options) {
// Path will be ignored when fd is specified, so it can be falsy
this.path = toPathIfFileURL(path);
this.flags = options.flags === undefined ? 'w' : options.flags;
- this.mode = options.mode === undefined ? 0o666 : options.mode;
+ this.mode = parseFileMode(options.mode, 'options.mode', 0o666);
validatePath(this.path);
} else {
diff --git a/lib/internal/validators.js b/lib/internal/validators.js
index 1c4acd51613..977ee1db1ec 100644
--- a/lib/internal/validators.js
+++ b/lib/internal/validators.js
@@ -80,7 +80,8 @@ function parseFileMode(value, name, def) {
value = NumberParseInt(value, 8);
}
- validateUint32(value, name);
+ // The C++ bindings read the mode as a signed 32-bit integer.
+ validateInt32(value, name, 0);
// Coerce -0 to +0.
return value + 0;
}
diff --git a/test/parallel/test-fs-fchmod.js b/test/parallel/test-fs-fchmod.js
index 16425e7d1fe..9ada3897b81 100644
--- a/test/parallel/test-fs-fchmod.js
+++ b/test/parallel/test-fs-fchmod.js
@@ -42,12 +42,12 @@ assert.throws(() => fs.fchmod(1, '123x'), {
assert.throws(() => fs.fchmodSync(input, 0o666), errObj);
});
-[-1, 2 ** 32].forEach((input) => {
+[-1, 2 ** 31].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
- `4294967295. Received ${input}`
+ `2147483647. Received ${input}`
};
assert.throws(() => fs.fchmod(1, input, () => {}), errObj);
diff --git a/test/parallel/test-fs-lchmod.js b/test/parallel/test-fs-lchmod.js
index d4397102914..0223353fd08 100644
--- a/test/parallel/test-fs-lchmod.js
+++ b/test/parallel/test-fs-lchmod.js
@@ -53,12 +53,12 @@ assert.throws(() => fs.lchmodSync(f, '123x'), {
code: 'ERR_INVALID_ARG_VALUE'
});
-[-1, 2 ** 32].forEach((input) => {
+[-1, 2 ** 31].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
- `4294967295. Received ${input}`
+ `2147483647. Received ${input}`
};
assert.rejects(promises.lchmod(f, input, () => {}), errObj).then(common.mustCall());
diff --git a/test/parallel/test-fs-mode-out-of-int32-range.js b/test/parallel/test-fs-mode-out-of-int32-range.js
new file mode 100644
index 00000000000..27599683b03
--- /dev/null
+++ b/test/parallel/test-fs-mode-out-of-int32-range.js
@@ -0,0 +1,45 @@
+'use strict';
+
+// This tests that a mode above INT32_MAX is reported to the caller as a
+// RangeError instead of failing a CHECK in the native binding and aborting
+// the process.
+
+const common = require('../common');
+const assert = require('assert');
+const fs = require('fs');
+
+const tmpdir = require('../common/tmpdir');
+tmpdir.refresh();
+
+const file = tmpdir.resolve('file.txt');
+fs.writeFileSync(file, 'x');
+
+for (const mode of [2 ** 31, 2176057344, 2 ** 32 - 1]) {
+ const expected = (name) => ({
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: `The value of "${name}" is out of range. It must be >= 0 && <= ` +
+ `2147483647. Received ${mode}`,
+ });
+ assert.throws(() => fs.openSync(file, 'w', mode), expected('mode'));
+ assert.throws(() => fs.open(file, 'w', mode, common.mustNotCall()),
+ expected('mode'));
+ assert.rejects(fs.promises.open(file, 'w', mode), expected('mode'))
+ .then(common.mustCall());
+ assert.throws(() => fs.chmodSync(file, mode), expected('mode'));
+ assert.throws(() => fs.mkdirSync(tmpdir.resolve('dir'), { mode }),
+ expected('options.mode'));
+ assert.throws(
+ () => fs.mkdirSync(tmpdir.resolve('a/b'), { recursive: true, mode }),
+ expected('options.mode'),
+ );
+ assert.throws(() => fs.createReadStream(file, { mode }),
+ expected('options.mode'));
+ assert.throws(() => fs.createWriteStream(file, { mode }),
+ expected('options.mode'));
+}
+
+// The largest signed 32-bit value is still accepted.
+fs.closeSync(fs.openSync(file, 'w', 2 ** 31 - 1));
+fs.createWriteStream(file, { mode: 0o644 })
+ .on('open', common.mustCall(function() { this.close(); }));