-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add Security Rules for Hardcoding Secrets in Python Applications #96
base: main
Are you sure you want to change the base?
Conversation
Sakshis seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
WalkthroughThis pull request introduces three new security rules for Python applications that address the hardcoding of secrets in various contexts: JWT encoding, Redis connections, and empty passwords in HTTP authentication. Each rule is defined in a YAML file and categorized with a severity level of "warning." The rules include utility functions for matching specific patterns in the code to identify potential security risks associated with hardcoded secrets. Additionally, new test configurations and snapshot files are created to validate the implementation of these rules. Changes
Possibly related PRs
Suggested reviewers
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 8
🧹 Outside diff range and nitpick comments (7)
tests/python/python-pyjwt-hardcoded-secret-python-test.yml (2)
3-4
: Consider adding more valid test cases.The valid section could be expanded to include more secure patterns:
- Environment variables:
os.getenv('JWT_SECRET')
- Secrets manager:
aws_secrets_manager.get_secret('jwt')
- Configuration files:
config.get('jwt_secret')
5-23
: Add test cases for additional hardcoding patterns.The invalid section could be enhanced with more patterns:
- F-strings:
jwt.encode(payload, f"secret{123}", algorithm="HS256")
- String concatenation:
"secret" + "123"
- Bytes literals:
b"secret"
- Multi-line strings:
"""secret"""
tests/python/python-redis-hardcoded-secret-python-test.yml (1)
10-34
: Add test cases for additional Redis connection patterns.Consider adding invalid test cases for:
- Redis connection strings:
redis://user:password@localhost:6379
- URL-encoded passwords:
redis://user:pass%40word@localhost:6379
- Cluster configurations
- Sentinel configurations
tests/python/python-requests-empty-password-python-test.yml (1)
1-54
: Consider adding security documentation.These test files implement important security checks. Consider:
- Adding comments explaining the security implications of each pattern
- Including references to relevant security standards (OWASP, CWE)
- Creating a README.md with:
- Security best practices
- Common vulnerabilities
- Remediation strategies
rules/python/security/python-requests-empty-password-python.yml (1)
78-78
: Remove trailing spaces.Several lines contain trailing spaces that should be removed for consistency.
Also applies to: 104-104, 111-111, 137-137, 329-329
🧰 Tools
🪛 yamllint (1.35.1)
[error] 78-78: trailing spaces
(trailing-spaces)
tests/__snapshots__/python-requests-empty-password-python-snapshot.yml (1)
4-6
: Consider using a mock server instead of httpbin.org.Using an external service in tests could lead to:
- Flaky tests if the service is down
- Potential security implications of sending test credentials
- Network-dependent test execution
Consider using a mock HTTP server for testing.
Also applies to: 49-51, 94-96
rules/python/security/python-pyjwt-hardcoded-secret-python.yml (1)
15-107
: Consider additional patterns for encoded secrets.The current patterns might miss base64-encoded secrets. Consider adding patterns to detect common encoding patterns:
- Base64-encoded strings
- Hex-encoded strings
- Environment variable string literals
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (9)
rules/python/security/python-pyjwt-hardcoded-secret-python.yml
(1 hunks)rules/python/security/python-redis-hardcoded-secret-python.yml
(1 hunks)rules/python/security/python-requests-empty-password-python.yml
(1 hunks)tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml
(1 hunks)tests/__snapshots__/python-redis-hardcoded-secret-python-snapshot.yml
(1 hunks)tests/__snapshots__/python-requests-empty-password-python-snapshot.yml
(1 hunks)tests/python/python-pyjwt-hardcoded-secret-python-test.yml
(1 hunks)tests/python/python-redis-hardcoded-secret-python-test.yml
(1 hunks)tests/python/python-requests-empty-password-python-test.yml
(1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
rules/python/security/python-redis-hardcoded-secret-python.yml
[warning] 37-37: wrong indentation: expected 20 but found 21
(indentation)
[warning] 72-72: wrong indentation: expected 20 but found 19
(indentation)
[warning] 76-76: wrong indentation: expected 20 but found 19
(indentation)
[warning] 79-79: wrong indentation: expected 21 but found 22
(indentation)
[error] 80-80: trailing spaces
(trailing-spaces)
[warning] 103-103: wrong indentation: expected 20 but found 21
(indentation)
[warning] 146-146: wrong indentation: expected 20 but found 19
(indentation)
[warning] 150-150: wrong indentation: expected 20 but found 19
(indentation)
[warning] 153-153: wrong indentation: expected 21 but found 22
(indentation)
rules/python/security/python-requests-empty-password-python.yml
[warning] 63-63: wrong indentation: expected 20 but found 19
(indentation)
[error] 78-78: trailing spaces
(trailing-spaces)
[warning] 96-96: wrong indentation: expected 20 but found 19
(indentation)
[error] 104-104: trailing spaces
(trailing-spaces)
[error] 111-111: trailing spaces
(trailing-spaces)
[warning] 129-129: wrong indentation: expected 20 but found 19
(indentation)
[error] 137-137: trailing spaces
(trailing-spaces)
[error] 329-329: trailing spaces
(trailing-spaces)
🔇 Additional comments (5)
rules/python/security/python-redis-hardcoded-secret-python.yml (1)
1-168
: LGTM for the rule implementation!
The security rule implementation is well-structured and comprehensive:
- Covers both direct Redis import and from-import scenarios
- Handles both immediate password strings and variable assignments
- Includes proper documentation with CWE reference and OWASP link
🧰 Tools
🪛 yamllint (1.35.1)
[warning] 37-37: wrong indentation: expected 20 but found 21
(indentation)
[warning] 72-72: wrong indentation: expected 20 but found 19
(indentation)
[warning] 76-76: wrong indentation: expected 20 but found 19
(indentation)
[warning] 79-79: wrong indentation: expected 21 but found 22
(indentation)
[error] 80-80: trailing spaces
(trailing-spaces)
[warning] 103-103: wrong indentation: expected 20 but found 21
(indentation)
[warning] 146-146: wrong indentation: expected 20 but found 19
(indentation)
[warning] 150-150: wrong indentation: expected 20 but found 19
(indentation)
[warning] 153-153: wrong indentation: expected 21 but found 22
(indentation)
tests/__snapshots__/python-redis-hardcoded-secret-python-snapshot.yml (1)
1-261
: LGTM for Redis test snapshots!
The test snapshots provide comprehensive coverage:
- Direct Redis import usage
- From-import Redis usage
- Both single-line and multi-line connection strings
- Various host configurations (IP addresses and domain names)
tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml (1)
1-327
: LGTM for JWT test snapshots!
The test snapshots provide comprehensive coverage:
- Direct jwt.encode usage
- Variable-based secret usage
- Aliased jwt import usage
rules/python/security/python-requests-empty-password-python.yml (1)
328-338
: Verify pattern matching for edge cases.
The rule should be tested against various edge cases to ensure it doesn't miss any empty password scenarios.
✅ Verification successful
Pattern matching for empty password scenarios is well covered
The test file tests/python/python-requests-empty-password-python-test.yml
comprehensively covers all edge cases for empty password scenarios:
-
Direct empty string literals (
""
) for all auth types:HTTPBasicAuth('user', '')
HTTPDigestAuth('user', '')
HTTPProxyAuth('user', '')
-
Variable-based empty strings for all auth types:
asd = '' HTTPBasicAuth('user', asd) HTTPDigestAuth('user', asd) HTTPProxyAuth('user', asd)
-
Both direct and fully qualified imports:
from requests.auth import HTTPBasicAuth
requests.auth.HTTPBasicAuth
The rule's pattern matching aligns perfectly with these test cases, ensuring no empty password scenarios are missed.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for test coverage of edge cases in the test files
# Look for test cases covering different scenarios
rg -A 5 "HTTPBasicAuth|HTTPDigestAuth|HTTPProxyAuth" tests/
Length of output: 31554
🧰 Tools
🪛 yamllint (1.35.1)
[error] 329-329: trailing spaces
(trailing-spaces)
rules/python/security/python-pyjwt-hardcoded-secret-python.yml (1)
435-441
: Verify rule effectiveness with real-world JWT usage patterns.
Let's validate the rule against common JWT implementation patterns.
✅ Verification successful
Let me generate additional scripts to better understand the rule's patterns and test cases.
The previous results showed JWT usage in tests, but we need to verify the rule patterns against these test cases to ensure effectiveness.
Rule effectively covers common JWT hardcoded secret patterns
The rule implementation effectively detects various JWT hardcoded secret patterns through five comprehensive matchers:
- Direct string secrets in jwt.encode/decode calls
- Variable-based secrets where the variable is assigned a hardcoded string
- Aliased JWT imports with hardcoded secrets
- Different payload and algorithm combinations
Test cases validate these scenarios:
- Hardcoded string secret:
jwt.encode(..., "123", ...)
- Variable with hardcoded secret:
key = "secret"; jwt.encode(..., key, ...)
- Aliased import with hardcoded secret:
jwt as lsadlsaldsa; lsadlsaldsa.encode(..., "123", ...)
The rule's AST patterns correctly identify these security anti-patterns while allowing valid cases where secrets are not hardcoded.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for JWT usage patterns in the codebase
# Look for JWT usage patterns
rg -A 5 "jwt\.(encode|decode)"
# Look for potential JWT imports
rg "^from.*jwt.*import|^import.*jwt"
Length of output: 5930
Script:
#!/bin/bash
# Check the rule patterns and test cases
# Look at the rule file content
cat rules/python/security/python-pyjwt-hardcoded-secret-python.yml
# Look at the test file content
cat tests/python/python-pyjwt-hardcoded-secret-python-test.yml
# Look at the snapshot file
cat tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml
Length of output: 20442
Summary by CodeRabbit
Release Notes
New Features
python-pyjwt-hardcoded-secret-python
: Warns against hardcoded secrets in JWT encoding.python-redis-hardcoded-secret-python
: Alerts for hardcoded passwords in Redis connections.python-requests-empty-password-python
: Identifies empty passwords in HTTP authentication methods.Tests