-
-
Save 4l3j4ndr0/c5a7aecf3ead3cb8da551606194b0681 to your computer and use it in GitHub Desktop.
Security SQL Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_safe_query(self, query): | |
| """Check if the SQL query is safe (read-only)""" | |
| # Convert to lowercase for case-insensitive matching | |
| query_lower = query.lower() | |
| # Check for write operations keywords | |
| unsafe_keywords = ['insert', 'update', 'delete', 'drop', 'alter', 'create', 'truncate'] | |
| for keyword in unsafe_keywords: | |
| pattern = r'^\s*' + keyword + r'\s' | |
| if re.search(pattern, query_lower): | |
| return False | |
| # Ensure query starts with SELECT or similar | |
| select_pattern = r'^\s*(select|show|describe|explain|with)\s' | |
| if not re.search(select_pattern, query_lower): | |
| return False | |
| return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment