Skip to content

Commit

Permalink
fix raszi#268: Revert "fix raszi#246: remove any double quotes or sin…
Browse files Browse the repository at this point in the history
…gle quotes from os.tmpdir also sanitize dir option, the template option and the name option"

This reverts commit c8823e5.

Single quotes must not be removed from paths because they are valid (even if hard to use)
on all OSes. Double quotes are only disallowed on Windows, but tmp should not change any
arg it gets; instead, it should rely on the underlying fs API to fail with an error
that the user needs to fix.
  • Loading branch information
mbargiel committed Aug 23, 2022
1 parent 7e96f4d commit fbc65be
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 87 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
34 changes: 1 addition & 33 deletions test/name-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const
inbandStandardTests = require('./name-inband-standard'),
tmp = require('../lib/tmp');

const isWindows = os.platform() === 'win32';

describe('tmp', function () {
describe('#tmpNameSync()', function () {
Expand Down Expand Up @@ -40,9 +39,7 @@ describe('tmp', function () {
describe('on issue #176', function () {
const origfn = os.tmpdir;
it('must fail on invalid os.tmpdir()', function () {
os.tmpdir = function () {
return undefined;
};
os.tmpdir = function () { return undefined; };
try {
tmp.tmpNameSync();
assert.fail('should have failed');
Expand All @@ -53,35 +50,6 @@ describe('tmp', function () {
}
});
});
describe('on issue #246', function () {
const origfn = os.tmpdir;
it('must produce correct name 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();
try {
assert.ok(name.indexOf('"') === -1);
assert.ok(name.startsWith(tmpdir.replace(/["']/g, '')));
} finally {
os.tmpdir = origfn;
}
});
it('must produce correct name 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();
try {
assert.ok(name.indexOf('\'') === -1);
assert.ok(name.startsWith(tmpdir.replace(/["']/g, '')));
} finally {
os.tmpdir = origfn;
}
});
});
});

describe('when running standard outband tests', function () {
Expand Down
35 changes: 1 addition & 34 deletions test/name-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const
inbandStandardTests = require('./name-inband-standard'),
tmp = require('../lib/tmp');

const isWindows = os.platform() === 'win32';

describe('tmp', function () {
describe('#tmpName()', function () {
Expand Down Expand Up @@ -63,39 +62,6 @@ describe('tmp', function () {
});
});
});
describe('on issue #246', function () {
const origfn = os.tmpdir;
it('must produce correct name 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) {
try {
assert.ok(name.indexOf('"') === -1);
assert.ok(name.startsWith(tmpdir.replace(/["']/g, '')));
} catch (err) {
return done(err);
} finally {
os.tmpdir = origfn;
}
done();
});
});
it('must produce correct name 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) {
try {
assert.ok(name.indexOf('\'') === -1);
assert.ok(name.startsWith(tmpdir.replace(/["']/g, '')));
} catch (err) {
return done(err);
} finally {
os.tmpdir = origfn;
}
done();
});
});
});
});

describe('when running standard outband tests', function () {
Expand All @@ -105,3 +71,4 @@ describe('tmp', function () {
});
});
});

0 comments on commit fbc65be

Please sign in to comment.