fixup! TF-4224 Reduce ReDoS vulnerability
This commit is contained in:
@@ -4,6 +4,7 @@ import 'dart:typed_data';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/domain/exceptions/string_exception.dart';
|
||||
import 'package:core/utils/mail/named_address.dart';
|
||||
import 'package:flutter/widgets.dart' show visibleForTesting;
|
||||
import 'package:html/dom.dart';
|
||||
import 'package:html/parser.dart';
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
@@ -21,6 +22,9 @@ class StringConvert {
|
||||
static final _asciiArtRegex = RegExp(r'[+\-|/\\=]');
|
||||
static final _namedAddressRegex = RegExp(r'''(?:(?:"([^"]+)"|'([^']+)')\s*)?<([^>]+)>''');
|
||||
|
||||
@visibleForTesting
|
||||
static RegExp get base64ValidationRegex => _base64ValidationRegex;
|
||||
|
||||
static String? writeEmptyToNull(String text) {
|
||||
if (text.isEmpty) return null;
|
||||
return text;
|
||||
|
||||
@@ -361,23 +361,9 @@ void main() {
|
||||
group('Regex initialization optimization', () {
|
||||
test('regex patterns should be reused, not recreated', () {
|
||||
// This test verifies that regex patterns are static and reused
|
||||
// We can't directly test if they're the same object, but we can
|
||||
// verify performance doesn't degrade with multiple calls
|
||||
|
||||
const iterations = 1000;
|
||||
final stopwatch = Stopwatch()..start();
|
||||
|
||||
for (var i = 0; i < iterations; i++) {
|
||||
StringConvert.isTextTable('| a | b |\n|---|---|\n| c | d |');
|
||||
HtmlUtils.wrapPlainTextLinks('<p>https://example.com</p>');
|
||||
StringConvert.extractEmailAddress('test@example.com, test2@example.com');
|
||||
}
|
||||
|
||||
stopwatch.stop();
|
||||
|
||||
// If regex patterns were being recreated each time, this would be much slower
|
||||
expect(stopwatch.elapsedMilliseconds, lessThan(500),
|
||||
reason: 'Multiple calls should be fast due to regex reuse');
|
||||
final regex1 = StringConvert.base64ValidationRegex;
|
||||
final regex2 = StringConvert.base64ValidationRegex;
|
||||
expect(identical(regex1, regex2), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user