Skip to content

Commit

Permalink
fix: escape some special characters before matching by (#35)
Browse files Browse the repository at this point in the history
regex. These characters are all actually accepted
by OpenSSH in host names and parsed correctly from
the config.
Fix #34
  • Loading branch information
roblourens authored Feb 9, 2020
1 parent 1414996 commit 4dfe434
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/glob.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict'

function escapeChars(str, chars) {
for (let char of chars) {
str = str.replace(new RegExp('\\' + char, 'g'), '\\' + char)
}

return str
}

function match(pattern, str) {
pattern = escapeChars(pattern, '\\()[]{}.+^$|');
pattern = pattern
.replace(/\./g, '\\.')
.replace(/\+/g, '\\+')
.replace(/\*/g, '.*')
.replace(/\?/g, '.?')

Expand Down
8 changes: 8 additions & 0 deletions test/test.glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ describe('glob', function() {
assert(glob('*/*', 'host1/host2'))
assert(glob('*+*', 'host1+host2'))
})

it('glob special chars', function() {
assert(glob('(foo', '(foo'))
assert(!glob('(foo)', 'foo'))
assert(glob('[foo]', '[foo]'))
assert(glob('{foo', '{foo'))
assert(glob('^foo|ba\\r$', '^foo|ba\\r$'))
})
})

0 comments on commit 4dfe434

Please sign in to comment.