From 989c204a58b686bc534d746b0341cbe657759374 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Mon, 14 Jun 2021 03:37:54 +0430 Subject: [PATCH] fs: allow empty string for temp directory prefix The `fs` lib module's `mkdtemp()` and `mkdtempSync()` methods were missing a validator, and weren't allowing the empty string as a valid prefix. PR-URL: https://github.com/nodejs/node/pull/39028 Reviewed-By: Darshan Sen Reviewed-By: Zijian Liu Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Khaidi Chu --- doc/api/fs.md | 11 +++++++++++ lib/fs.js | 13 ++++++------- lib/internal/fs/promises.js | 6 +++--- test/parallel/test-fs-mkdtemp-prefix-check.js | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 8c12ce0bdbd9ca..afb65b707244a5 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -795,6 +795,10 @@ rejection only when `recursive` is false. ### `fsPromises.mkdtemp(prefix[, options])` * `prefix` {string} @@ -2533,6 +2537,9 @@ See the POSIX mkdir(2) documentation for more details. * `prefix` {string} diff --git a/lib/fs.js b/lib/fs.js index 836550e7c283cb..2ffb5710c09060 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -119,7 +119,8 @@ const { parseFileMode, validateBuffer, validateInteger, - validateInt32 + validateInt32, + validateString, } = require('internal/validators'); let truncateWarn = true; @@ -1999,9 +2000,8 @@ realpath.native = (path, options, callback) => { function mkdtemp(prefix, options, callback) { callback = makeCallback(typeof options === 'function' ? options : callback); options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); const req = new FSReqCallback(); @@ -2012,9 +2012,8 @@ function mkdtemp(prefix, options, callback) { function mkdtempSync(prefix, options) { options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); const path = `${prefix}XXXXXX`; diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index b9b705caec2899..8405b5d0f5b3da 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -66,6 +66,7 @@ const { validateAbortSignal, validateBuffer, validateInteger, + validateString, } = require('internal/validators'); const pathModule = require('path'); const { promisify } = require('internal/util'); @@ -671,9 +672,8 @@ async function realpath(path, options) { async function mkdtemp(prefix, options) { options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix); warnOnNonPortableTemplate(prefix); return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises); diff --git a/test/parallel/test-fs-mkdtemp-prefix-check.js b/test/parallel/test-fs-mkdtemp-prefix-check.js index 1d9d88232a067e..33a06914a46e10 100644 --- a/test/parallel/test-fs-mkdtemp-prefix-check.js +++ b/test/parallel/test-fs-mkdtemp-prefix-check.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); -const prefixValues = [undefined, null, 0, true, false, 1, '']; +const prefixValues = [undefined, null, 0, true, false, 1]; function fail(value) { assert.throws(