File size: 3,067 Bytes
0c9b99b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Security check script to ensure no sensitive data is present before pushing to repositories.
"""

import os
import re
from pathlib import Path

def check_for_sensitive_data():
    """
    Scans project files for potential sensitive data patterns.
    """
    sensitive_patterns = [
        r'token\s*=\s*["\'][^"\']+["\']',  # token = "value"
        r'password\s*=\s*["\'][^"\']+["\']',  # password = "value"  
        r'api_key\s*=\s*["\'][^"\']+["\']',  # api_key = "value"
        r'secret\s*=\s*["\'][^"\']+["\']',  # secret = "value"
        r'hf_[a-zA-Z0-9]{34}',  # HuggingFace token pattern
        r'ghp_[a-zA-Z0-9]{36}',  # GitHub personal access token
        r'sk-[a-zA-Z0-9]{48}',  # OpenAI API key pattern
    ]
    
    files_to_check = [
        '*.py', '*.md', '*.txt', '*.json', '*.yaml', '*.yml'
    ]
    
    issues_found = []
    project_root = Path('.')
    
    for pattern in files_to_check:
        for file_path in project_root.rglob(pattern):
            # Skip hidden files and cache directories
            if any(part.startswith('.') for part in file_path.parts):
                continue
            if '__pycache__' in str(file_path):
                continue
                
            try:
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    
                for line_num, line in enumerate(content.splitlines(), 1):
                    # Skip comment lines and pattern definitions
                    if line.strip().startswith('#') or 'r\'' in line:
                        continue
                        
                    for sensitive_pattern in sensitive_patterns:
                        if re.search(sensitive_pattern, line, re.IGNORECASE):
                            issues_found.append({
                                'file': str(file_path),
                                'line': line_num,
                                'pattern': sensitive_pattern,
                                'content': line.strip()
                            })
            except (UnicodeDecodeError, PermissionError):
                # Skip binary files or files we can't read
                continue
    
    return issues_found

def main():
    print("πŸ” Scanning for sensitive data in project files...")
    
    issues = check_for_sensitive_data()
    
    if not issues:
        print("βœ… No sensitive data patterns detected!")
        print("βœ… Project is safe to push to public repositories.")
        return True
    else:
        print(f"⚠️  Found {len(issues)} potential sensitive data issues:")
        for issue in issues:
            print(f"  πŸ“ {issue['file']}:{issue['line']}")
            print(f"     Pattern: {issue['pattern']}")
            print(f"     Content: {issue['content'][:80]}{'...' if len(issue['content']) > 80 else ''}")
            print()
        
        print("❌ Please review and remove sensitive data before pushing!")
        return False

if __name__ == "__main__":
    main()