diff --git a/lib/tmp.js b/lib/tmp.js index b41c29d..aeba023 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -535,7 +535,7 @@ function _assertAndSanitizeOptions(options) { options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template); // for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to - options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name); + options.name = _isUndefined(options.name) ? undefined : options.name; options.prefix = _isUndefined(options.prefix) ? '' : options.prefix; options.postfix = _isUndefined(options.postfix) ? '' : options.postfix; } @@ -552,28 +552,13 @@ function _assertAndSanitizeOptions(options) { * @private */ function _resolvePath(name, tmpDir) { - const sanitizedName = _sanitizeName(name); - if (sanitizedName.startsWith(tmpDir)) { - return path.resolve(sanitizedName); + if (name.startsWith(tmpDir)) { + return path.resolve(name); } else { - return path.resolve(path.join(tmpDir, sanitizedName)); + return path.resolve(path.join(tmpDir, name)); } } -/** - * Sanitize the specified path name by removing all quote characters. - * - * @param name - * @returns {string} - * @private - */ -function _sanitizeName(name) { - if (_isBlank(name)) { - return name; - } - return name.replace(/["']/g, ''); -} - /** * Asserts whether specified name is relative to the specified tmpDir. * @@ -663,7 +648,7 @@ function setGracefulCleanup() { * @returns {string} the currently configured tmp dir */ function _getTmpDir(options) { - return path.resolve(_sanitizeName(options && options.tmpdir || os.tmpdir())); + return path.resolve(options && options.tmpdir || os.tmpdir()); } // Install process exit listener diff --git a/test/name-sync-test.js b/test/name-sync-test.js index 4a686fd..aa08993 100644 --- a/test/name-sync-test.js +++ b/test/name-sync-test.js @@ -4,6 +4,7 @@ const assert = require('assert'), os = require('os'), + path = require('path'), inbandStandardTests = require('./name-inband-standard'), tmp = require('../lib/tmp'); @@ -53,30 +54,30 @@ describe('tmp', function () { } }); }); - describe('on issue #246', function () { + describe('on issue #268', function () { const origfn = os.tmpdir; - it('must produce correct name on os.tmpdir() returning path that includes double quotes', function () { + it(`should not alter ${isWindows ? 'invalid' : 'valid'} path on os.tmpdir() returning path that includes double quotes`, function () { const tmpdir = isWindows ? '"C:\\Temp With Spaces"' : '"/tmp with spaces"'; os.tmpdir = function () { return tmpdir; }; const name = tmp.tmpNameSync(); + const index = name.indexOf(path.sep + tmpdir + path.sep); try { - assert.ok(name.indexOf('"') === -1); - assert.ok(name.startsWith(tmpdir.replace(/["']/g, ''))); + assert.ok(index > 0, `${tmpdir} should have been a subdirectory name in ${name}`); } finally { os.tmpdir = origfn; } }); - it('must produce correct name on os.tmpdir() returning path that includes single quotes', function () { + it('should not alter valid path on os.tmpdir() returning path that includes single quotes', function () { const tmpdir = isWindows ? '\'C:\\Temp With Spaces\'' : '\'/tmp with spaces\''; os.tmpdir = function () { return tmpdir; }; const name = tmp.tmpNameSync(); + const index = name.indexOf(path.sep + tmpdir + path.sep); try { - assert.ok(name.indexOf('\'') === -1); - assert.ok(name.startsWith(tmpdir.replace(/["']/g, ''))); + assert.ok(index > 0, `${tmpdir} should have been a subdirectory name in ${name}`); } finally { os.tmpdir = origfn; } diff --git a/test/name-test.js b/test/name-test.js index 746dfa0..4aac4f8 100644 --- a/test/name-test.js +++ b/test/name-test.js @@ -4,6 +4,7 @@ const assert = require('assert'), os = require('os'), + path = require('path'), inbandStandardTests = require('./name-inband-standard'), tmp = require('../lib/tmp'); @@ -63,15 +64,15 @@ describe('tmp', function () { }); }); }); - describe('on issue #246', function () { + describe('on issue #268', function () { const origfn = os.tmpdir; - it('must produce correct name on os.tmpdir() returning path that includes double quotes', function (done) { + it(`should not alter ${isWindows ? 'invalid' : 'valid'} path on os.tmpdir() returning path that includes double quotes`, function (done) { const tmpdir = isWindows ? '"C:\\Temp With Spaces"' : '"/tmp with spaces"'; os.tmpdir = function () { return tmpdir; }; tmp.tmpName(function (err, name) { + const index = name.indexOf(path.sep + tmpdir + path.sep); try { - assert.ok(name.indexOf('"') === -1); - assert.ok(name.startsWith(tmpdir.replace(/["']/g, ''))); + assert.ok(index > 0, `${tmpdir} should have been a subdirectory name in ${name}`); } catch (err) { return done(err); } finally { @@ -80,13 +81,13 @@ describe('tmp', function () { done(); }); }); - it('must produce correct name on os.tmpdir() returning path that includes single quotes', function (done) { + it('should not alter valid path on os.tmpdir() returning path that includes single quotes', function (done) { const tmpdir = isWindows ? '\'C:\\Temp With Spaces\'' : '\'/tmp with spaces\''; os.tmpdir = function () { return tmpdir; }; tmp.tmpName(function (err, name) { + const index = name.indexOf(path.sep + tmpdir + path.sep); try { - assert.ok(name.indexOf('\'') === -1); - assert.ok(name.startsWith(tmpdir.replace(/["']/g, ''))); + assert.ok(index > 0, `${tmpdir} should have been a subdirectory name in ${name}`); } catch (err) { return done(err); } finally {