-
Notifications
You must be signed in to change notification settings - Fork 68
fix: add ReDoS protection to regex pattern validation #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
fix: add ReDoS protection to regex pattern validation #522
Conversation
Implements comprehensive protection against Regular Expression Denial of Service (ReDoS) attacks in the username filtering system. ## Problem The _check_username_list_regex() function compiled user-provided regex patterns without validation, allowing malicious patterns like (a+)+ to cause exponential-time execution and CPU exhaustion. This could be exploited via Config.ignored_users, Config.enabled_users, or Config.vip_users fields to create a Denial of Service condition. ## Solution Added multi-layer validation in new _is_safe_regex() function: 1. Length validation: Reject patterns > 100 characters 2. Complexity check: Reject patterns with > 50 special regex chars 3. Catastrophic backtracking detection: Identify nested quantifiers - Patterns like (a+)+, (a*)*, (a|ab)* are detected and rejected 4. Syntax validation: Ensure valid regex syntax before compilation Updated _check_username_list_regex() to validate all patterns before compilation. Unsafe patterns are logged and skipped gracefully. ## Testing - Added 15 comprehensive tests in TestReDoSProtection class - All 35 tests pass (15 new + 20 existing) - Tests cover: safe patterns, dangerous patterns, edge cases, performance, and full integration scenarios ## Security Impact - Prevents ReDoS attacks that could hang the application - Maintains backward compatibility with safe regex patterns - Minimal performance overhead (< 1ms per pattern validation) - Comprehensive logging for security monitoring ## Files Changed - buffalogs/impossible_travel/modules/alert_filter.py: Added validation - buffalogs/impossible_travel/tests/detection/test_alert_filter.py: Added tests - REDOS_FIX_DOCUMENTATION.md: Complete technical documentation
|
|
Fix CI linting failures by applying Black formatting to: - buffalogs/impossible_travel/modules/alert_filter.py - buffalogs/impossible_travel/tests/detection/test_alert_filter.py Changes are purely cosmetic (line breaks, import formatting) to meet Black 25.1.0 code style requirements used in CI.
ce3f01b to
3f72991
Compare
Fix CI linting failures by sorting imports according to project isort configuration. Changes apply isort 6.0.1 formatting to: - buffalogs/impossible_travel/modules/alert_filter.py - buffalogs/impossible_travel/tests/detection/test_alert_filter.py Imports are now correctly ordered and formatted to fit within the 160-character line length limit.
|
Ok, great, let's discuss a few doubts:
|
|
@Lorygold Hello let me know if it sounds reasonable to you so i will implement it |
|
1- Great! To achieve this, I was thinking we could update migration What do you think? Do you see any better alternatives? |
|
@Lorygold Hello work is under progress but halted due to my ongoing exams i will try to push fix over the weekend. May I ask why this Pr was marked as draft. |
|
No problem! It’s marked as a draft simply because it’s not finished yet, so it serves as a reminder that it shouldn’t be merged for now |
Implements comprehensive protection against Regular Expression Denial of Service (ReDoS) attacks in the username filtering system.
Problem
The _check_username_list_regex() function compiled user-provided regex patterns without validation, allowing malicious patterns like (a+)+ to cause exponential-time execution and CPU exhaustion. This could be exploited via Config.ignored_users, Config.enabled_users, or Config.vip_users fields to create a Denial of Service condition.
Solution
Added multi-layer validation in new _is_safe_regex() function:
Updated _check_username_list_regex() to validate all patterns before compilation. Unsafe patterns are logged and skipped gracefully.
Testing
Security Impact
Files Changed