-
Notifications
You must be signed in to change notification settings - Fork 6
Add security rules for MySQL connections in Python applications #87
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
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 several new security rules for Python applications that connect to MySQL databases. Specifically, it establishes rules to detect empty passwords and hard-coded secrets in connection strings. Each rule is accompanied by a severity level of "warning" and provides guidance on secure credential management practices. Additionally, multiple test configurations and snapshot files are created to validate these rules, ensuring that both valid and invalid connection scenarios are appropriately handled. 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: 5
🧹 Outside diff range and nitpick comments (7)
tests/python/python-mysql-empty-password-python-test.yml (1)
6-13
: Consider adding more test cases for comprehensive coverage.While the current invalid test cases cover basic scenarios, consider adding tests for:
- Connection with password=None
- Connection with password parameter omitted
- Connection using connection string format
Example additions:
invalid: - | import mysql.connector conn = mysql.connector.connect(username="abcz", passwd="") - | import mysql.connector as mysql123 def my_function(): mysql123.connect(host="localhost",user="root",passwd="",database="aaa") + - | + import mysql.connector + conn = mysql.connector.connect(username="abcz", passwd=None) + - | + import mysql.connector + conn = mysql.connector.connect(username="abcz") + - | + import mysql.connector + conn = mysql.connector.connect("mysql://user@localhost/db")tests/python/python-mysqlclient-empty-password-python-test.yml (1)
8-33
: LGTM! Comprehensive coverage of invalid cases.The invalid test cases provide excellent coverage of different connection patterns and empty password scenarios, including:
- Named parameter connections
- Positional parameter connections
- Different import styles and aliases
- Various MySQLdb connection methods
However, the
FLAGS
object in some test cases should be replaced with concrete values or environment variables to ensure test reliability.rules/python/security/python-mysql-empty-password-python.yml (1)
12-88
: Enhance pattern matching for additional empty password scenariosThe rule effectively catches direct empty string passwords but could be enhanced to detect additional scenarios:
- String concatenation resulting in empty strings
- F-strings that evaluate to empty strings
- None/null passwords
- Empty environment variables
Consider adding patterns for these scenarios to make the rule more robust. Would you like me to provide the additional pattern definitions?
tests/__snapshots__/python-mysql-empty-password-python-snapshot.yml (1)
1-107
: Add test cases for edge cases and variationsWhile the current test cases cover basic empty password scenarios, consider adding test cases for:
- Multi-line connection strings
- Connection parameters from variables
- Different string quote styles (single/double/triple quotes)
- Dictionary-based connection parameters
Would you like me to provide examples of these additional test cases?
tests/__snapshots__/python-mysql-hardcoded-secret-python-snapshot.yml (1)
4-5
: Use more secure example credentials in test casesEven in test cases, it's recommended to use obviously fake credentials (e.g., "TEST_PASSWORD_123") rather than simple strings like "abc" to prevent accidental copy-paste into production code.
- conn = mysql.connector.connect(username="abcz", passwd="abc") + conn = mysql.connector.connect(username="test_user", passwd="TEST_PASSWORD_123") - mysql123.connect(host="localhost",user="root",passwd="abc",database="aaa") + mysql123.connect(host="localhost",user="test_user",passwd="TEST_PASSWORD_123",database="test_db")Also applies to: 56-58
rules/python/security/python-mysql-hardcoded-secret-python.yml (1)
12-94
: Consider expanding pattern matching for connection strings.While the rule effectively catches hardcoded secrets in keyword arguments, it might miss secrets in connection strings (e.g.,
mysql://user:password@host/db
).Consider adding a pattern to detect connection strings:
rule: any: + - kind: string + regex: mysql:\/\/[^:]+:[^@]+@ - kind: call has: kind: attributetests/__snapshots__/python-mysqlclient-empty-password-python-snapshot.yml (1)
1-350
: Enhance test coverage with additional scenarios.While the current test cases provide good coverage for empty string passwords, consider adding:
- Tests for
None
/null
passwords- Tests with connection string formats
- Tests with environment variable usage (negative cases)
Add these test cases:
# Test None password import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd=None) # Test connection string import MySQLdb db = MySQLdb.connect("mysql://user:@localhost/db") # Test environment variable (should not trigger) import MySQLdb import os db = MySQLdb.connect(passwd=os.getenv('DB_PASSWORD'))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (9)
rules/python/security/python-mysql-empty-password-python.yml
(1 hunks)rules/python/security/python-mysql-hardcoded-secret-python.yml
(1 hunks)rules/python/security/python-mysqlclient-empty-password-python.yml
(1 hunks)tests/__snapshots__/python-mysql-empty-password-python-snapshot.yml
(1 hunks)tests/__snapshots__/python-mysql-hardcoded-secret-python-snapshot.yml
(1 hunks)tests/__snapshots__/python-mysqlclient-empty-password-python-snapshot.yml
(1 hunks)tests/python/python-mysql-empty-password-python-test.yml
(1 hunks)tests/python/python-mysql-hardcoded-secret-python-test.yml
(1 hunks)tests/python/python-mysqlclient-empty-password-python-test.yml
(1 hunks)
🔇 Additional comments (4)
tests/python/python-mysql-empty-password-python-test.yml (1)
1-5
: LGTM! Valid test case correctly demonstrates secure usage.
The valid test case properly shows a connection with a non-empty password, which is the correct pattern to test.
tests/python/python-mysql-hardcoded-secret-python-test.yml (1)
6-13
: LGTM! Invalid test cases properly identify hardcoded secrets.
The invalid test cases correctly demonstrate scenarios where passwords are hardcoded in the code.
rules/python/security/python-mysql-hardcoded-secret-python.yml (1)
1-11
: LGTM! Clear and informative rule metadata.
The rule's metadata (id, severity, message, notes) is well-structured and provides clear guidance on the security implications and remediation steps.
rules/python/security/python-mysqlclient-empty-password-python.yml (1)
1-11
: LGTM! Well-structured rule metadata.
The rule's metadata provides clear guidance on the security implications of empty passwords and references appropriate CWE/OWASP guidelines.
Summary by CodeRabbit
New Features
Bug Fixes
Tests