Skip to content

Commit 0d7652e

Browse files
vitolobVictorino Machava
andauthored
Backport fix for GHSA-7h2j-956f-4vf2 to v1 (#101)
Co-authored-by: Victorino Machava <victorino.machava@vm.co.mz>
1 parent 6c353ca commit 0d7652e

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var escOpen = '\0OPEN'+Math.random()+'\0';
88
var escClose = '\0CLOSE'+Math.random()+'\0';
99
var escComma = '\0COMMA'+Math.random()+'\0';
1010
var escPeriod = '\0PERIOD'+Math.random()+'\0';
11+
var EXPANSION_MAX = 100000;
12+
13+
module.exports.EXPANSION_MAX = EXPANSION_MAX;
1114

1215
function numeric(str) {
1316
return parseInt(str, 10) == str
@@ -62,10 +65,13 @@ function parseCommaParts(str) {
6265
return parts;
6366
}
6467

65-
function expandTop(str) {
68+
function expandTop(str, options) {
6669
if (!str)
6770
return [];
6871

72+
options = options || {};
73+
var max = options.max == null ? EXPANSION_MAX : options.max;
74+
6975
// I don't know why Bash 4.3 does this, but it does.
7076
// Anything starting with {} will have the first two bytes preserved
7177
// but *only* at the top level, so {},a}b will not expand to anything,
@@ -76,7 +82,7 @@ function expandTop(str) {
7682
str = '\\{\\}' + str.substr(2);
7783
}
7884

79-
return expand(escapeBraces(str), true).map(unescapeBraces);
85+
return expand(escapeBraces(str), max, true).map(unescapeBraces);
8086
}
8187

8288
function identity(e) {
@@ -97,7 +103,7 @@ function gte(i, y) {
97103
return i >= y;
98104
}
99105

100-
function expand(str, isTop) {
106+
function expand(str, max, isTop) {
101107
var expansions = [];
102108

103109
var m = balanced('{', '}', str);
@@ -111,7 +117,7 @@ function expand(str, isTop) {
111117
// {a},b}
112118
if (m.post.match(/,(?!,).*\}/)) {
113119
str = m.pre + '{' + m.body + escClose + m.post;
114-
return expand(str);
120+
return expand(str, max, true);
115121
}
116122
return [str];
117123
}
@@ -123,10 +129,10 @@ function expand(str, isTop) {
123129
n = parseCommaParts(m.body);
124130
if (n.length === 1) {
125131
// x{{a,b}}y ==> x{a}y x{b}y
126-
n = expand(n[0], false).map(embrace);
132+
n = expand(n[0], max, false).map(embrace);
127133
if (n.length === 1) {
128134
var post = m.post.length
129-
? expand(m.post, false)
135+
? expand(m.post, max, false)
130136
: [''];
131137
return post.map(function(p) {
132138
return m.pre + n[0] + p;
@@ -141,7 +147,7 @@ function expand(str, isTop) {
141147
// no need to expand pre, since it is guaranteed to be free of brace-sets
142148
var pre = m.pre;
143149
var post = m.post.length
144-
? expand(m.post, false)
150+
? expand(m.post, max, false)
145151
: [''];
146152

147153
var N;
@@ -185,11 +191,11 @@ function expand(str, isTop) {
185191
N.push(c);
186192
}
187193
} else {
188-
N = concatMap(n, function(el) { return expand(el, false) });
194+
N = concatMap(n, function(el) { return expand(el, max, false) });
189195
}
190196

191197
for (var j = 0; j < N.length; j++) {
192-
for (var k = 0; k < post.length; k++) {
198+
for (var k = 0; k < post.length && expansions.length < max; k++) {
193199
var expansion = pre + N[j] + post[k];
194200
if (!isTop || isSequence || expansion)
195201
expansions.push(expansion);

test/sequence.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,25 @@ test('alphabetic sequences with step count', function(t) {
4848
t.end();
4949
});
5050

51+
test('sequence dos', function(t) {
52+
var str = '{1..10}'.repeat(10);
53+
var expanded = expand(str);
54+
var expanded10 = expand(str, { max: 10 });
55+
56+
t.equal(expanded.length, 100000, 'expansion is limited');
57+
t.deepEqual(expanded10, [
58+
'1111111111',
59+
'1111111112',
60+
'1111111113',
61+
'1111111114',
62+
'1111111115',
63+
'1111111116',
64+
'1111111117',
65+
'1111111118',
66+
'1111111119',
67+
'11111111110'
68+
], 'custom max truncates expansion');
69+
t.equal(expanded10.length, 10, 'custom max is respected');
70+
t.end();
71+
});
72+

0 commit comments

Comments
 (0)