From 05453392a4490dbe96edff0b38969cac874f18da Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Sat, 19 Sep 2026 02:45:51 -0300 Subject: [PATCH] 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 --- lib/fs.js | 4 ++-- test/parallel/test-fs-openAsBlobSync.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index d0de4a9bdc9c..85fdd0890604 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 aa135eb1fd5b..d0cbf12766b4 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',