Commit cc610f1cfbf for nodejs
commit cc610f1cfbf44f86be0eada4a225b7e64a7f8b65
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date: Fri Sep 25 16:44:48 2026 -0300
fs: validate falsy openAsBlob type option
openAsBlob() and openAsBlobSync() read the MIME type as
options.type || '' before validating it, causing falsy non-string
values to be replaced with the default before reaching validateString().
As a result, { type: 0 }, { type: false }, { type: null }, and
{ type: NaN } are accepted as an empty type, while { type: 1 }
throws ERR_INVALID_ARG_TYPE.
Default the option only when it is undefined so all other values are
validated against the documented string type.
Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66125
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
diff --git a/lib/fs.js b/lib/fs.js
index d0de4a9bdc9..85fdd089060 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -790,7 +790,7 @@ function openSync(path, flags, mode) {
*/
function openAsBlob(path, options = kEmptyObject) {
validateObject(options, 'options');
- const type = options.type || '';
+ const { type = '' } = options;
validateString(type, 'options.type');
const h = vfsState.handlers;
@@ -816,7 +816,7 @@ function openAsBlob(path, options = kEmptyObject) {
*/
function openAsBlobSync(path, options = kEmptyObject) {
validateObject(options, 'options');
- const type = options.type || '';
+ const { type = '' } = options;
validateString(type, 'options.type');
path = getValidatedPath(path);
diff --git a/test/parallel/test-fs-openAsBlobSync.js b/test/parallel/test-fs-openAsBlobSync.js
index aa135eb1fd5..d0cbf12766b 100644
--- a/test/parallel/test-fs-openAsBlobSync.js
+++ b/test/parallel/test-fs-openAsBlobSync.js
@@ -23,9 +23,14 @@ assert.throws(() => fs.openAsBlobSync(1), {
assert.throws(() => fs.openAsBlobSync(testfile, null), {
code: 'ERR_INVALID_ARG_TYPE',
});
-assert.throws(() => fs.openAsBlobSync(testfile, { type: 1 }), {
- code: 'ERR_INVALID_ARG_TYPE',
-});
+for (const type of [1, 0, false, null, NaN, {}]) {
+ assert.throws(() => fs.openAsBlobSync(testfile, { type }), {
+ code: 'ERR_INVALID_ARG_TYPE',
+ });
+ assert.throws(() => fs.openAsBlob(testfile, { type }), {
+ code: 'ERR_INVALID_ARG_TYPE',
+ });
+}
assert.throws(() => fs.openAsBlobSync(missing), {
code: 'ENOENT',
syscall: 'stat',