-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsecurity_check.py
More file actions
93 lines (75 loc) · 2.92 KB
/
security_check.py
File metadata and controls
93 lines (75 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
🔒 Security Verification Script
Checks for hardcoded API keys and sensitive information before Git push
"""
import os
import re
import glob
def scan_for_api_keys():
"""Scan all Python files for potential hardcoded API keys"""
# Patterns to look for
api_key_patterns = [
r'api_key\s*=\s*["\'][A-Za-z0-9_-]{20,}["\']',
r'API_KEY\s*=\s*["\'][A-Za-z0-9_-]{20,}["\']',
r'AIzaSy[A-Za-z0-9_-]{33}', # Google API keys
r'sk-[A-Za-z0-9]{48}', # OpenAI API keys
r'xoxb-[A-Za-z0-9-]{50,}', # Slack tokens
]
# Files to scan
python_files = glob.glob("**/*.py", recursive=True)
issues_found = []
for file_path in python_files:
if "fraud_env" in file_path or "__pycache__" in file_path:
continue
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
for line_num, line in enumerate(content.split('\n'), 1):
for pattern in api_key_patterns:
if re.search(pattern, line):
issues_found.append({
'file': file_path,
'line': line_num,
'content': line.strip(),
'pattern': pattern
})
except Exception as e:
print(f"⚠️ Could not scan {file_path}: {e}")
return issues_found
def main():
print("🔒 Security Scan: Checking for hardcoded API keys...")
print("=" * 60)
issues = scan_for_api_keys()
if issues:
print(f"🚨 SECURITY ALERT: Found {len(issues)} potential issues:")
print()
for issue in issues:
print(f"📁 File: {issue['file']}")
print(f"📍 Line {issue['line']}: {issue['content']}")
print(f"🔍 Pattern: {issue['pattern']}")
print("-" * 40)
print()
print("🛡️ ACTIONS NEEDED:")
print("1. Move API keys to .env file")
print("2. Use os.getenv() to load from environment")
print("3. Add .env to .gitignore")
print("4. Re-run this script to verify fixes")
return False
else:
print("✅ Security scan passed!")
print("🔒 No hardcoded API keys found")
print("✨ Safe to push to GitHub")
# Check if .env is in .gitignore
try:
with open('.gitignore', 'r') as f:
gitignore_content = f.read()
if '.env' in gitignore_content:
print("✅ .env file is properly ignored by Git")
else:
print("⚠️ Add .env to .gitignore file")
except FileNotFoundError:
print("⚠️ Create .gitignore file and add .env")
return True
if __name__ == "__main__":
main()