fixup! TF-4224 Reduce ReDoS vulnerability

This commit is contained in:
Dang Dat
2025-12-24 14:41:37 +07:00
committed by Dat H. Pham
parent 2b909aeb1c
commit 86dec27d67
6 changed files with 424 additions and 39 deletions
@@ -9,6 +9,8 @@ abstract class DomTransformer {
const DomTransformer();
static final _backgroundImageRegex = RegExp(r'''\bbackground-image\s*:\s*url\(\s*['"]?([^' ")]+)['"]?\s*\)''');
/// Uses the `DOM` [document] to transform the `document`.
///
/// All changes will be visible to subsequent transformers.
@@ -27,8 +29,7 @@ abstract class DomTransformer {
Tuple2<String, String>? findImageUrlFromStyleTag(String style) {
try {
final regExp = RegExp(r'''\bbackground-image\s*:\s*url\(\s*['"]?([^' ")]+)['"]?\s*\)''');
final match = regExp.firstMatch(style);
final match = _backgroundImageRegex.firstMatch(style);
if (match == null) {
return null;
}
@@ -7,6 +7,7 @@ class NormalizeLineHeightInStyleTransformer extends DomTransformer {
const NormalizeLineHeightInStyleTransformer();
static const _removableLineHeights = ['1px', '100%'];
static final _multipleSpacesRegex = RegExp(r' {2,}');
@override
Future<void> process({
@@ -30,7 +31,7 @@ class NormalizeLineHeightInStyleTransformer extends DomTransformer {
var updatedStyle = originalStyle.replaceAll(pattern, '').trim();
// Remove extra spaces (>=2 spaces → 1 space)
updatedStyle = updatedStyle.replaceAll(RegExp(r' {2,}'), ' ');
updatedStyle = updatedStyle.replaceAll(_multipleSpacesRegex, ' ');
if (updatedStyle != originalStyle) {
if (updatedStyle.isEmpty) {