Skip to content

Commit

Permalink
fix: Ensure glob-like characters are escaped in cwd & root options
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 17, 2022
1 parent c170ed3 commit 1289c1b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
30 changes: 28 additions & 2 deletions index.js
Expand Up @@ -9,16 +9,20 @@ module.exports = function(glob, options) {
var opts = options || {};

// ensure cwd is absolute
var cwd = path.resolve(opts.cwd ? opts.cwd : process.cwd());
var cwd = unescape(opts.cwd ? opts.cwd : process.cwd())
cwd = path.resolve(cwd);
cwd = unixify(cwd);
cwd = escape(cwd);

var rootDir = opts.root;
// if `options.root` is defined, ensure it's absolute
if (rootDir) {
rootDir = unescape(rootDir);
rootDir = unixify(rootDir);
if (process.platform === 'win32' || !isAbsolute(rootDir)) {
rootDir = unixify(path.resolve(rootDir));
}
rootDir = escape(rootDir);
}

// trim starting ./ from glob patterns
Expand Down Expand Up @@ -54,8 +58,30 @@ module.exports = function(glob, options) {
return ing.negated ? '!' + glob : glob;
};

function escape(path) {
return path
.replace(/\[(.*?)\]/g, "\\[$1\\]")
.replace(/\((.*?)\)/g, "\\($1\\)")
.replace(/\{(.*?)\}/g, "\\{$1\\}")
.replace(/\*/g, "\\*")
.replace(/\!/g, "\\!")
.replace(/\?/g, "\\?");
}

function unescape(path) {
return path
.replace(/\\\[(.*?)\\\]/g, "[$1]")
.replace(/\\\((.*?)\\\)/g, "($1)")
.replace(/\\\{(.*?)\\\}/g, "{$1}")
.replace(/\\\*/g, "*")
.replace(/\\\!/g, "!")
.replace(/\\\?/g, "?");
}

// Before calling unixify, we remove the escapes and then
// we add them back afterwards to avoid double-escaping
function unixify(filepath) {
return filepath.replace(/\\/g, '/');
return filepath.replace(/\\/g, "/");
}

function join(dir, glob) {
Expand Down
42 changes: 42 additions & 0 deletions test.js
Expand Up @@ -61,6 +61,27 @@ describe('resolve', function () {
assert.equal(actual, unixify(path.resolve('foo/a/*.js')));
});

it('should escape glob patterns in an absolute cwd', function () {
actual = resolve('a/*.js', {cwd: '/(foo)/[bar]/{baz}/*/**/?/!'});
var expected = unixify(path.resolve('/(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('/(foo)/[bar]/{baz}/*/**/?/!', '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('should escape glob patterns in a relative cwd', function () {
actual = resolve('a/*.js', {cwd: '(foo)/[bar]/{baz}/*/**/?/!'});
var expected = unixify(path.resolve('(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('(foo)/[bar]/{baz}/*/**/?/!', '\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('avoids double escaping in cwd', function () {
actual = resolve('a/*.js', {cwd: '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!'});
var expected = unixify(path.resolve('/(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('/(foo)/[bar]/{baz}/*/**/?/!', '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('should make a negative glob absolute from a cwd', function () {
actual = resolve('!a/*.js', {cwd: 'foo'});
assert.equal(actual, '!' + unixify(path.resolve('foo/a/*.js')));
Expand All @@ -76,6 +97,27 @@ describe('resolve', function () {
assert.equal(actual, unixify(path.resolve('/a/*.js')));
});

it('should escape glob patterns in an absolute root', function () {
actual = resolve('/a/*.js', {root: '/(foo)/[bar]/{baz}/*/**/?/!'});
var expected = unixify(path.resolve('/(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('/(foo)/[bar]/{baz}/*/**/?/!', '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('should escape glob patterns in a relative root', function () {
actual = resolve('/a/*.js', {root: '(foo)/[bar]/{baz}/*/**/?/!'});
var expected = unixify(path.resolve('(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('(foo)/[bar]/{baz}/*/**/?/!', '\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('avoids double escaping in root', function () {
actual = resolve('/a/*.js', {root: '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!'});
var expected = unixify(path.resolve('/(foo)/[bar]/{baz}/*/**/?/!/a/*.js'))
.replace('/(foo)/[bar]/{baz}/*/**/?/!', '/\\(foo\\)/\\[bar\\]/\\{baz\\}/\\*/\\*\\*/\\?/\\!');
assert.equal(actual, expected);
});

it('should make a glob absolute from a negative root path', function () {
actual = resolve('!/a/*.js', {root: 'foo'});
assert.equal(actual, '!' + unixify(path.resolve('foo/a/*.js')));
Expand Down

0 comments on commit 1289c1b

Please sign in to comment.