-
Notifications
You must be signed in to change notification settings - Fork 73
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
|
|
ce3f01b to
3f72991
Compare
|
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 |
Per maintainer feedback on PR certego#522: - Add validate_regex_patterns() validator to check for unsafe patterns - Apply validator to ignored_users, enabled_users, vip_users fields - Add check_regex_patterns() to migration 0022 to audit existing data - Migration logs warnings for existing unsafe patterns (non-blocking) This ensures: 1. New configs are validated when saved (fail-fast at admin panel) 2. Existing configs are audited during migration (logged warnings) 3. No repeated validation during alert filtering (performance improvement) 4. Follows established pattern from populate_device_fingerprint Addresses: certego#522 (comment)
Added validate_regex_patterns() validator to validators.py that uses _is_safe_regex() to check patterns. This validator is now applied to the ignored_users, enabled_users, and vip_users fields in the Config model. Result: When admins try to save configs with unsafe patterns via the Django admin panel, they'll get a clear ValidationError explaining why the pattern was rejected. No repeated validation during alert processing.
Added check_regex_patterns() function to migration 0022 following the same pattern as populate_device_fingerprint(). The function: Iterates through all existing Config entries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file should be moved in the docs/guides/development folder
| if word == item: | ||
| return True | ||
|
|
||
| # ✅ NEW: Validate regex pattern before compilation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove all the AI emoticons everywhere, please
| if not isinstance(patterns_list, list): | ||
| raise ValidationError("Must be a list of patterns") | ||
|
|
||
| from impossible_travel.modules.alert_filter import _is_safe_regex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the imports should be at the beginning of the file, it's more readable
|
|
||
| from impossible_travel.modules.alert_filter import _is_safe_regex | ||
|
|
||
| unsafe = [p for p in patterns_list if not _is_safe_regex(p)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using set() could avoid duplicates:
unsafe = set()
for p in patterns_list:
if not _is_safe_regex(p):
unsafe.add(p)
| if not patterns_list: | ||
| return | ||
|
|
||
| if not isinstance(patterns_list, list): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this check a duplication?
|
@Lorygold Hello I removed REDOS_FIX_DOCUMENTATION.md from commit these are my personnel notes and accidentally went into commit |
Per maintainer feedback on PR certego#522: - Add validate_regex_patterns() validator to check for unsafe patterns - Apply validator to ignored_users, enabled_users, vip_users fields - Add check_regex_patterns() to migration 0022 to audit existing data - Migration logs warnings for existing unsafe patterns (non-blocking) This ensures: 1. New configs are validated when saved (fail-fast at admin panel) 2. Existing configs are audited during migration (logged warnings) 3. No repeated validation during alert filtering (performance improvement) 4. Follows established pattern from populate_device_fingerprint Addresses: certego#522 (comment)
6b0579d to
bd5040e
Compare
|
why all those files have been changed? some problems with the linters version locally and in the CI? |
|
@Lorygold TBH I could not really figure it out I did apply pre commit hook and it said the linting tests passed locally, but failed on github actions. |
.github/.pre-commit-config.yaml
Outdated
|
|
||
| - repo: https://github.com/psf/black | ||
| rev: 24.8.0 | ||
| rev: 25.1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe because you change this version?
|
you should apply changes only to the files needed for your features, not all those |
f45fc1b to
840f2a1
Compare
Implement multi-layer validation to prevent Regular Expression Denial of Service (ReDoS) attacks in username filtering patterns. Changes: - Add _is_safe_regex() and validate_regex_patterns() to validators.py - Apply validators to Config.ignored_users, enabled_users, vip_users - Add check_regex_patterns() to migration 0022 for legacy data audit - Add comprehensive tests (11 test methods covering all aspects) - Move documentation to docs/guides/development/redos_protection.md Security: CWE-1333 (ReDoS) Validation includes: - Length limit (max 100 chars) - Complexity check (max 50 special chars) - Nested quantifier detection - Syntax validation Closes certego#521
840f2a1 to
4384785
Compare
|
@Lorygold Hello I have made a clean commit and hope it works Current VersionsPre-commit config (
CI workflow (
I think it should be consistent also I would recommend shifting to ruff I am currently working on this issue in intel owl. however it can be a time consuming issue requiring discipline and also would require changing in all files. |
|
If you aligned your branch with the |
|
@Lorygold Hello ok thats great to know let me know whats left for pr to be closed and merged |
| review and update these patterns in the Django admin panel. | ||
| """ | ||
| Config = apps.get_model("impossible_travel", "Config") | ||
| from impossible_travel.validators import _is_safe_regex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please define all the imports at the beginning of the file
|
also @Noble-47 will give a check soon |
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