Skip to content

Commit 3e9f04a

Browse files
authored
fix: Resolve ReDoS vulnerability from CVE-2021-35065 (#49)
1 parent 3ad9597 commit 3e9f04a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var isWin32 = require('os').platform() === 'win32';
66

77
var slash = '/';
88
var backslash = /\\/g;
9-
var enclosure = /[{[].*\/.*[}\]]$/;
109
var globby = /(^|[^\\])([{[]|\([^)]+$)/;
1110
var escaped = /\\([!*?|[\](){}])/g;
1211

@@ -24,7 +23,7 @@ module.exports = function globParent(str, opts) {
2423
}
2524

2625
// special case for strings ending in enclosure containing path separator
27-
if (enclosure.test(str)) {
26+
if (isEnclosure(str)) {
2827
str += slash;
2928
}
3029

@@ -39,3 +38,27 @@ module.exports = function globParent(str, opts) {
3938
// remove escape chars and return result
4039
return str.replace(escaped, '$1');
4140
};
41+
42+
43+
function isEnclosure(str) {
44+
var lastChar = str.slice(-1)
45+
46+
var enclosureStart;
47+
switch (lastChar) {
48+
case '}':
49+
enclosureStart = '{';
50+
break;
51+
case ']':
52+
enclosureStart = '[';
53+
break;
54+
default:
55+
return false;
56+
}
57+
58+
var foundIndex = str.indexOf(enclosureStart);
59+
if (foundIndex < 0) {
60+
return false;
61+
}
62+
63+
return str.slice(foundIndex + 1, -1).includes(slash);
64+
}

test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ describe('glob2base test patterns', function () {
224224

225225
done();
226226
});
227+
228+
it('should finish in reasonable time for \'{\' + \'/\'.repeat(n) [CVE-2021-35065]', function(done) {
229+
this.timeout(1000);
230+
gp('{' + '/'.repeat(500000));
231+
done();
232+
});
233+
234+
it('should finish in reasonable time for \'{\'.repeat(n)', function(done) {
235+
this.timeout(1000);
236+
gp('{'.repeat(500000));
237+
done();
238+
});
239+
240+
it('should finish in reasonable time for \'(\'.repeat(n)', function(done) {
241+
this.timeout(1000);
242+
gp('('.repeat(500000));
243+
done();
244+
});
227245
});
228246

229247
if (isWin32) {

0 commit comments

Comments
 (0)