From 782d21d796c9cef5bb6589e9edae5e931d0aea00 Mon Sep 17 00:00:00 2001 From: Dang Dat Date: Mon, 5 Jan 2026 14:58:18 +0700 Subject: [PATCH] fixup! TF-4224 Reduce ReDoS vulnerability --- core/test/utils/redos_vulnerability_test.dart | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/core/test/utils/redos_vulnerability_test.dart b/core/test/utils/redos_vulnerability_test.dart index e8f4f14a0..75ab412df 100644 --- a/core/test/utils/redos_vulnerability_test.dart +++ b/core/test/utils/redos_vulnerability_test.dart @@ -65,7 +65,7 @@ void main() { }); }); - group('NormalizeLineHeightInStyleTransformer', () { + group('NormalizeLineHeightInStyleTransformer tests regex patterns in isolation', () { test('should handle very long style strings efficiently', () { final longStyle = 'color: red; ' * 10000 + 'line-height: 1px;'; @@ -447,16 +447,16 @@ void main() { }); group('AppUtils._emailLocalhostRegex', () { + final localhostRegex = RegExp( + r'^(?:"[^"\r\n]+"|[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*)@localhost$', + ); test('should handle very long email addresses efficiently', () { final longEmail = '${'a' * 10000}@localhost'; final stopwatch = Stopwatch()..start(); // Note: We can't directly test AppUtils.isEmailLocalhost in core tests // But we can test the regex pattern - final regex = RegExp( - r'^(?:"[^"\r\n]+"|[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*)@localhost$', - ); - final result = regex.hasMatch(longEmail); + final result = localhostRegex.hasMatch(longEmail); stopwatch.stop(); expect(stopwatch.elapsedMilliseconds, lessThan(100)); @@ -464,10 +464,6 @@ void main() { }); test('should correctly match valid localhost emails', () { - final regex = RegExp( - r'^(?:"[^"\r\n]+"|[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*)@localhost$', - ); - const validEmails = [ 'user@localhost', 'user.name@localhost', @@ -475,16 +471,12 @@ void main() { ]; for (final email in validEmails) { - expect(regex.hasMatch(email.trim()), isTrue, + expect(localhostRegex.hasMatch(email.trim()), isTrue, reason: 'Should match: $email'); } }); test('should reject invalid localhost emails', () { - final regex = RegExp( - r'^(?:"[^"\r\n]+"|[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*)@localhost$', - ); - const invalidEmails = [ 'user@localhost.com', '@localhost', @@ -493,7 +485,7 @@ void main() { ]; for (final email in invalidEmails) { - expect(regex.hasMatch(email.trim()), isFalse, + expect(localhostRegex.hasMatch(email.trim()), isFalse, reason: 'Should not match: $email'); } });