-
Notifications
You must be signed in to change notification settings - Fork 1
π‘οΈ Sentinel: [MEDIUM] Fix sensitive data leak in debug logs #126
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import logging | ||
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing module docstring Warning test
Missing module docstring
Check warningCode scanning / Pylint (reported by Codacy) Missing module docstring Warning test
Missing module docstring
|
||
| import pytest | ||
Check warningCode scanning / Prospector (reported by Codacy) Unable to import 'pytest' (import-error) Warning test
Unable to import 'pytest' (import-error)
|
||
| from unittest.mock import MagicMock, patch | ||
Check warningCode scanning / Pylint (reported by Codacy) standard import "from unittest.mock import MagicMock, patch" should be placed before "import pytest" Warning test
standard import "from unittest.mock import MagicMock, patch" should be placed before "import pytest"
Check warningCode scanning / Pylintpython3 (reported by Codacy) standard import "from unittest.mock import MagicMock, patch" should be placed before "import pytest" Warning test
standard import "from unittest.mock import MagicMock, patch" should be placed before "import pytest"
|
||
| import httpx | ||
Check warningCode scanning / Prospector (reported by Codacy) Unable to import 'httpx' (import-error) Warning test
Unable to import 'httpx' (import-error)
|
||
| import main | ||
|
|
||
| # Mock httpx.HTTPError to include a response with sensitive data | ||
| def create_mock_error(status_code, text, request_url="https://example.com"): | ||
Check warningCode scanning / Pylint (reported by Codacy) Missing function docstring Warning test
Missing function docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing function or method docstring Warning test
Missing function or method docstring
|
||
| response = MagicMock(spec=httpx.Response) | ||
| response.status_code = status_code | ||
| response.text = text | ||
| response.request = MagicMock(spec=httpx.Request) | ||
| response.request.url = request_url | ||
|
|
||
| # Use HTTPStatusError which accepts request and response | ||
| error = httpx.HTTPStatusError(f"HTTP Error {status_code}", request=response.request, response=response) | ||
Check warningCode scanning / Pylint (reported by Codacy) Line too long (107/100) Warning test
Line too long (107/100)
Check warningCode scanning / Pylintpython3 (reported by Codacy) Line too long (107/100) Warning test
Line too long (107/100)
|
||
| return error | ||
|
|
||
| def test_retry_request_sanitizes_token_in_debug_logs(caplog): | ||
Check warningCode scanning / Pylint (reported by Codacy) Missing function docstring Warning test
Missing function docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing function or method docstring Warning test
Missing function or method docstring
|
||
| # Setup sensitive data | ||
| sensitive_token = "SECRET_TOKEN_123" | ||
Check noticeCode scanning / Bandit Possible hardcoded password: 'SECRET_TOKEN_123' Note test
Possible hardcoded password: 'SECRET_TOKEN_123'
|
||
| main.TOKEN = sensitive_token | ||
|
|
||
| # Configure logging to capture DEBUG | ||
| caplog.set_level(logging.DEBUG) | ||
|
|
||
| # Mock a request function that always raises an error with the token in response | ||
| mock_func = MagicMock() | ||
| error_text = f"Invalid token: {sensitive_token}" | ||
| mock_func.side_effect = create_mock_error(401, error_text) | ||
|
|
||
| # Call _retry_request (it re-raises the exception) | ||
| with pytest.raises(httpx.HTTPError): | ||
| # Set retries to 1 to fail fast | ||
| main._retry_request(mock_func, max_retries=1, delay=0) | ||
Check warningCode scanning / Prospector (reported by Codacy) Access to a protected member _retry_request of a client class (protected-access) Warning test
Access to a protected member _retry_request of a client class (protected-access)
Check noticeCode scanning / Pylint (reported by Codacy) Access to a protected member _retry_request of a client class Note test
Access to a protected member _retry_request of a client class
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Access to a protected member _retry_request of a client class Note test
Access to a protected member _retry_request of a client class
|
||
|
|
||
| # Check logs | ||
| assert "Response content:" in caplog.text | ||
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.
|
||
| assert sensitive_token not in caplog.text | ||
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.
|
||
| assert "[REDACTED]" in caplog.text | ||
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_push_rules_sanitizes_token_in_debug_logs(caplog): | ||
Check warningCode scanning / Pylint (reported by Codacy) Missing function docstring Warning test
Missing function docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing function or method docstring Warning test
Missing function or method docstring
|
||
| # Setup sensitive data | ||
| sensitive_token = "SECRET_TOKEN_456" | ||
Check noticeCode scanning / Bandit Possible hardcoded password: 'SECRET_TOKEN_456' Note test
Possible hardcoded password: 'SECRET_TOKEN_456'
|
||
| main.TOKEN = sensitive_token | ||
|
|
||
| # Configure logging to capture DEBUG | ||
| caplog.set_level(logging.DEBUG) | ||
|
|
||
| # Mock dependencies | ||
| mock_client = MagicMock(spec=httpx.Client) | ||
|
|
||
| # Let's mock client.post to raise error | ||
| error_text = f"Bad Rule with token {sensitive_token}" | ||
| mock_client.post.side_effect = create_mock_error(400, error_text) | ||
|
|
||
| # Patch time.sleep to avoid waiting | ||
| with patch("time.sleep"): | ||
| res = main.push_rules( | ||
| profile_id="p1", | ||
| folder_name="f1", | ||
| folder_id="fid1", | ||
| do=0, | ||
| status=1, | ||
| hostnames=["rule1"], | ||
| existing_rules=set(), | ||
| client=mock_client | ||
| ) | ||
|
|
||
| # push_rules catches the error and returns False (or continues if batch failed) | ||
| assert res 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.
|
||
|
|
||
| # Check logs | ||
| assert "Response content:" in caplog.text | ||
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.
|
||
| assert sensitive_token not in caplog.text | ||
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.
|
||
| assert "[REDACTED]" in caplog.text | ||
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_api_client_configuration(): | ||
Check warningCode scanning / Pylint (reported by Codacy) Missing function docstring Warning test
Missing function docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing function or method docstring Warning test
Missing function or method docstring
|
||
| # Setup token | ||
| main.TOKEN = "test_token" | ||
Check noticeCode scanning / Bandit Possible hardcoded password: 'test_token' Note test
Possible hardcoded password: 'test_token'
|
||
|
|
||
| with main._api_client() as client: | ||
Check warningCode scanning / Prospector (reported by Codacy) Access to a protected member _api_client of a client class (protected-access) Warning test
Access to a protected member _api_client of a client class (protected-access)
Check noticeCode scanning / Pylint (reported by Codacy) Access to a protected member _api_client of a client class Note test
Access to a protected member _api_client of a client class
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Access to a protected member _api_client of a client class Note test
Access to a protected member _api_client of a client class
|
||
| # Check User-Agent | ||
| assert client.headers["User-Agent"] == "Control-D-Sync/0.1.0" | ||
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.
|
||
| # Check Authorization | ||
| assert client.headers["Authorization"] == "Bearer test_token" | ||
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.
|
||
| # Check follow_redirects (in httpx < 0.20 it was allow_redirects, now follow_redirects) | ||
| assert client.follow_redirects 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_gh_client_configuration(): | ||
Check warningCode scanning / Pylint (reported by Codacy) Missing function docstring Warning test
Missing function docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing function or method docstring Warning test
Missing function or method docstring
|
||
| client = main._gh | ||
Check warningCode scanning / Prospector (reported by Codacy) Access to a protected member _gh of a client class (protected-access) Warning test
Access to a protected member _gh of a client class (protected-access)
Check noticeCode scanning / Pylint (reported by Codacy) Access to a protected member _gh of a client class Note test
Access to a protected member _gh of a client class
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Access to a protected member _gh of a client class Note test
Access to a protected member _gh of a client class
|
||
| # Check User-Agent | ||
| assert client.headers["User-Agent"] == "Control-D-Sync/0.1.0" | ||
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.
|
||
| # Check follow_redirects | ||
| assert client.follow_redirects 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.
|
||
Check notice
Code scanning / Remark-lint (reported by Codacy)
Warn when references to undefined definitions are found. Note