fix: add ReDoS protection to regex pattern validation#522
fix: add ReDoS protection to regex pattern validation#522kami922 wants to merge 1 commit intocertego:developfrom
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.
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.
remove all the AI emoticons everywhere, please
There was a problem hiding this comment.
all the imports should be at the beginning of the file, it's more readable
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
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.
please define all the imports at the beginning of the file
|
also @Noble-47 will give a check soon |
|
@Lorygold Hello any update on this one? |
|
Hi @kami922 I'll give you an update 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