Skip to content

Commit 77b7b0c

Browse files
authored
Fix ReDoS in currency filter. Resolves CVE-2022-25844. (#1)
1 parent 47bf11e commit 77b7b0c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/ng/filter/filters.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,35 @@ function currencyFilter($locale) {
6868
fractionSize = formats.PATTERNS[1].maxFrac;
6969
}
7070

71-
// If the currency symbol is empty, trim whitespace around the symbol
72-
var currencySymbolRe = !currencySymbol ? /\s*\u00A4\s*/g : /\u00A4/g;
73-
7471
// if null or undefined pass it through
7572
return (amount == null)
7673
? amount
77-
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
78-
replace(currencySymbolRe, currencySymbol);
74+
: currencySymbolReReplace(
75+
formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize),
76+
currencySymbol
77+
);
7978
};
79+
80+
function currencySymbolReReplace(input, currencySymbol) {
81+
if (!input.length) {
82+
return input;
83+
}
84+
// avoid the REDoS by doing this manually, because JS doesn't have positive quantifiers
85+
// If the currency symbol is empty, trim whitespace around the symbol
86+
var sp = input.split('\u00A4');
87+
if (!currencySymbol) {
88+
var i;
89+
for (i=0; i < sp.length; i++) {
90+
if (i > 0) {
91+
sp[i] = sp[i].replace(/^\s+/, '');
92+
}
93+
if (i < sp.length - 1) {
94+
sp[i] = sp[i].replace(/\s+$/, '');
95+
}
96+
}
97+
}
98+
return sp.join(currencySymbol);
99+
}
80100
}
81101

82102
/**

0 commit comments

Comments
 (0)