Skip to content

Commit

Permalink
Merge pull request #278 from mbargiel/feature/fix-268-revert-sanitize…
Browse files Browse the repository at this point in the history
…-name

fix #268: Revert "fix #246: remove any double quotes or single quotes…
  • Loading branch information
silkentrance committed Aug 26, 2022
2 parents 7e96f4d + a831713 commit 4c2d3f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
25 changes: 5 additions & 20 deletions lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions test/name-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const
assert = require('assert'),
os = require('os'),
path = require('path'),
inbandStandardTests = require('./name-inband-standard'),
tmp = require('../lib/tmp');

Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions test/name-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const
assert = require('assert'),
os = require('os'),
path = require('path'),
inbandStandardTests = require('./name-inband-standard'),
tmp = require('../lib/tmp');

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 4c2d3f2

Please sign in to comment.