Skip to content

Instantly share code, notes, and snippets.

@4l3j4ndr0
Created March 23, 2025 17:58
Show Gist options
  • Select an option

  • Save 4l3j4ndr0/c5a7aecf3ead3cb8da551606194b0681 to your computer and use it in GitHub Desktop.

Select an option

Save 4l3j4ndr0/c5a7aecf3ead3cb8da551606194b0681 to your computer and use it in GitHub Desktop.
Security SQL Validation
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