-
Notifications
You must be signed in to change notification settings - Fork 1
π‘οΈ Sentinel: [MEDIUM] Add input length limits to prevent DoS #412
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,6 +208,11 @@ | |
| # Path separators (prevent confusion and directory traversal attempts) | ||
| _DANGEROUS_FOLDER_CHARS.update(["/", "\\"]) | ||
|
|
||
| # Security: Input length limits | ||
| MAX_FOLDER_NAME_LENGTH = 64 | ||
| MAX_RULE_LENGTH = 255 | ||
| MAX_PROFILE_ID_LENGTH = 64 | ||
|
Comment on lines
+211
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # Security: Unicode Bidi control characters (prevent RTLO/homograph attacks) | ||
| # These characters can be used to mislead users about file extensions or content | ||
| # See: https://en.wikipedia.org/wiki/Right-to-left_override | ||
|
|
@@ -993,7 +998,7 @@ | |
| """ | ||
| if not PROFILE_ID_PATTERN.match(profile_id): | ||
| return False | ||
| if len(profile_id) > 64: | ||
| if len(profile_id) > MAX_PROFILE_ID_LENGTH: | ||
| return False | ||
| return True | ||
|
|
||
|
|
@@ -1009,8 +1014,8 @@ | |
| if log_errors: | ||
| if not PROFILE_ID_PATTERN.match(profile_id): | ||
| log.error("Invalid profile ID format (contains unsafe characters)") | ||
| elif len(profile_id) > 64: | ||
| log.error("Invalid profile ID length (max 64 chars)") | ||
| elif len(profile_id) > MAX_PROFILE_ID_LENGTH: | ||
| log.error(f"Invalid profile ID length (max {MAX_PROFILE_ID_LENGTH} chars)") | ||
Check warningCode scanning / Prospector (reported by Codacy) Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning
Use lazy % formatting in logging functions (logging-fstring-interpolation)
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Use lazy % formatting in logging functions Note
Use lazy % formatting in logging functions
|
||
| return False | ||
| return True | ||
|
|
||
|
|
@@ -1035,6 +1040,9 @@ | |
| if not rule: | ||
| return False | ||
|
|
||
| if len(rule) > MAX_RULE_LENGTH: | ||
| return False | ||
|
Comment on lines
+1043
to
+1044
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| # Strict whitelist to prevent injection | ||
| if not RULE_PATTERN.match(rule): | ||
| return False | ||
|
|
@@ -1056,6 +1064,9 @@ | |
| if not name or not name.strip() or not name.isprintable(): | ||
| return False | ||
|
|
||
| if len(name) > MAX_FOLDER_NAME_LENGTH: | ||
| return False | ||
|
|
||
| # Check for dangerous characters (pre-compiled at module level for performance) | ||
| if any(c in _DANGEROUS_FOLDER_CHARS or c in _BIDI_CONTROL_CHARS for c in name): | ||
| return False | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import pytest | ||
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing module docstring Warning test
Missing module docstring
Check warningCode scanning / Prospector (reported by Codacy) Unable to import 'pytest' (import-error) Warning test
Unable to import 'pytest' (import-error)
Check warningCode scanning / Prospector (reported by Codacy) Unused import pytest (unused-import) Warning test
Unused import pytest (unused-import)
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Unused import pytest Note test
Unused import pytest
Check warningCode scanning / Pylint (reported by Codacy) Missing module docstring Warning test
Missing module docstring
Check noticeCode scanning / Pylint (reported by Codacy) Unused import pytest Note test
Unused import pytest
|
||
| import main | ||
|
|
||
| def test_is_valid_folder_name_length_limit(): | ||
| """ | ||
| Test that folder names exceeding the maximum length are rejected. | ||
| Current behavior: Accepts any length. | ||
| Expected behavior: Should reject length > 64. | ||
| """ | ||
| # Create a name with 65 characters | ||
| long_name = "a" * 65 | ||
|
|
||
| # This should return False after the fix, but currently returns True | ||
| # We assert False to confirm the "failure" (vulnerability presence) or "success" (fix verification) | ||
Check warningCode scanning / Pylintpython3 (reported by Codacy) Line too long (103/100) Warning test
Line too long (103/100)
Check warningCode scanning / Pylint (reported by Codacy) Line too long (103/100) Warning test
Line too long (103/100)
|
||
| assert main.is_valid_folder_name(long_name) is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Check noticeCode scanning / Bandit (reported by Codacy) Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
Comment on lines
+5
to
+16
|
||
| def test_is_valid_folder_name_acceptable_length(): | ||
| """Test that folder names within limit are accepted.""" | ||
| name = "a" * 64 | ||
| assert main.is_valid_folder_name(name) is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Check noticeCode scanning / Bandit (reported by Codacy) Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| def test_is_valid_rule_length_limit(): | ||
| """ | ||
| Test that rules exceeding the maximum length are rejected. | ||
| Current behavior: Accepts any length (matching regex). | ||
| Expected behavior: Should reject length > 255. | ||
| """ | ||
| # Create a rule with 256 characters (valid chars) | ||
| long_rule = "a" * 256 + ".com" | ||
|
|
||
| # This should return False after the fix | ||
| assert main.is_valid_rule(long_rule) is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Check noticeCode scanning / Bandit (reported by Codacy) Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| def test_is_valid_rule_acceptable_length(): | ||
| """Test that rules within limit are accepted.""" | ||
| rule = "a" * 250 + ".com" | ||
| assert main.is_valid_rule(rule) is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment on lines
+28
to
+37
|
||
|
|
||
| def test_is_valid_profile_id_length_limit_constant(): | ||
| """ | ||
| Test that profile ID validation respects the length limit. | ||
| Note: This function already had a length check, we are just formalizing it with a constant. | ||
| """ | ||
| # 65 chars | ||
| long_id = "a" * 65 | ||
| assert main.validate_profile_id(long_id, log_errors=False) is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Check noticeCode scanning / Bandit (reported by Codacy) Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # 64 chars | ||
| valid_id = "a" * 64 | ||
| assert main.validate_profile_id(valid_id, log_errors=False) is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Check noticeCode scanning / Bandit (reported by Codacy) Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment on lines
+1
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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.
The addition of this section to the
sentinel.mdfile is a good practice for documenting security vulnerabilities and their fixes. It clearly outlines the vulnerability, the learning, and the prevention steps, which is valuable for future reference and knowledge sharing.