Spaces:
Sleeping
Sleeping
fix: simplify and improve code analysis patterns
Browse files- Replace complex regex with simpler string matching for SQL injection detection
- Fix hardcoded password/secret detection patterns
- Improve off-by-one and type coercion bug detection
- Test all patterns locally - all pass
- Now correctly analyzes: SQL injection, hardcoded secrets, API keys, logic errors, and style issues
main.py
CHANGED
|
@@ -56,18 +56,23 @@ async def review_code(request: ReviewRequest):
|
|
| 56 |
def analyze_code_locally(code: str, lang: str) -> dict:
|
| 57 |
"""Pattern-based code analysis - production would use LLM"""
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
# === SECURITY BUGS (Highest Priority) ===
|
| 60 |
-
|
|
|
|
| 61 |
return {
|
| 62 |
"type": "bug",
|
| 63 |
"category": "security",
|
| 64 |
"severity": 5,
|
| 65 |
"line_hint": 3,
|
| 66 |
-
"comment": "SQL injection:
|
| 67 |
"fix": "Use parameterized queries: cursor.execute('SELECT * FROM users WHERE id = ?', (uid,))"
|
| 68 |
}
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
return {
|
| 72 |
"type": "bug",
|
| 73 |
"category": "security",
|
|
@@ -77,17 +82,19 @@ def analyze_code_locally(code: str, lang: str) -> dict:
|
|
| 77 |
"fix": "Use environment variables: password = os.getenv('DB_PASSWORD')"
|
| 78 |
}
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
return {
|
| 82 |
"type": "bug",
|
| 83 |
"category": "security",
|
| 84 |
"severity": 4,
|
| 85 |
"line_hint": 1,
|
| 86 |
-
"comment": "Hardcoded API key
|
| 87 |
"fix": "api_key = os.getenv('API_KEY')\nif not api_key:\n raise ValueError('API_KEY not set')"
|
| 88 |
}
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
return {
|
| 92 |
"type": "bug",
|
| 93 |
"category": "security",
|
|
@@ -98,27 +105,30 @@ def analyze_code_locally(code: str, lang: str) -> dict:
|
|
| 98 |
}
|
| 99 |
|
| 100 |
# === LOGIC BUGS ===
|
| 101 |
-
|
|
|
|
| 102 |
return {
|
| 103 |
"type": "bug",
|
| 104 |
"category": "logic",
|
| 105 |
"severity": 3,
|
| 106 |
"line_hint": 2,
|
| 107 |
-
"comment": "Infinite loop detected. Loop never
|
| 108 |
-
"fix": "Add break condition
|
| 109 |
}
|
| 110 |
|
| 111 |
-
|
|
|
|
| 112 |
return {
|
| 113 |
"type": "bug",
|
| 114 |
"category": "logic",
|
| 115 |
"severity": 3,
|
| 116 |
"line_hint": 2,
|
| 117 |
"comment": "Off-by-one error: range(len(x) - 1) skips the last element.",
|
| 118 |
-
"fix": "Use
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
return {
|
| 123 |
"type": "bug",
|
| 124 |
"category": "logic",
|
|
@@ -128,26 +138,29 @@ def analyze_code_locally(code: str, lang: str) -> dict:
|
|
| 128 |
"fix": "Use: if items: instead of if len(items) != 0:"
|
| 129 |
}
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
# === DEFAULT: NO BUGS ===
|
| 153 |
return {
|
|
|
|
| 56 |
def analyze_code_locally(code: str, lang: str) -> dict:
|
| 57 |
"""Pattern-based code analysis - production would use LLM"""
|
| 58 |
|
| 59 |
+
code_lower = code.lower()
|
| 60 |
+
code_no_space = code.lower().replace(' ', '')
|
| 61 |
+
|
| 62 |
# === SECURITY BUGS (Highest Priority) ===
|
| 63 |
+
# SQL Injection: f-string + SQL keywords + variable interpolation
|
| 64 |
+
if ('f"' in code or "f'" in code) and any(sql in code_lower for sql in ['select', 'insert', 'delete', 'update', 'from', 'where']) and '{' in code and '}' in code:
|
| 65 |
return {
|
| 66 |
"type": "bug",
|
| 67 |
"category": "security",
|
| 68 |
"severity": 5,
|
| 69 |
"line_hint": 3,
|
| 70 |
+
"comment": "SQL injection: f-string with variable interpolation in SQL query. User input flows directly to database.",
|
| 71 |
"fix": "Use parameterized queries: cursor.execute('SELECT * FROM users WHERE id = ?', (uid,))"
|
| 72 |
}
|
| 73 |
|
| 74 |
+
# Hardcoded password
|
| 75 |
+
if re.search(r'(password|passwd|pwd)\s*[=:]\s*["\'][^"\']+["\']', code, re.I):
|
| 76 |
return {
|
| 77 |
"type": "bug",
|
| 78 |
"category": "security",
|
|
|
|
| 82 |
"fix": "Use environment variables: password = os.getenv('DB_PASSWORD')"
|
| 83 |
}
|
| 84 |
|
| 85 |
+
# Hardcoded API keys / secrets
|
| 86 |
+
if re.search(r'(api_key|apikey|secret_key|private_key|token|access_token)\s*[=:]\s*["\'][a-zA-Z0-9_\-]+["\']', code, re.I):
|
| 87 |
return {
|
| 88 |
"type": "bug",
|
| 89 |
"category": "security",
|
| 90 |
"severity": 4,
|
| 91 |
"line_hint": 1,
|
| 92 |
+
"comment": "Hardcoded API key or secret detected. Move to environment variables.",
|
| 93 |
"fix": "api_key = os.getenv('API_KEY')\nif not api_key:\n raise ValueError('API_KEY not set')"
|
| 94 |
}
|
| 95 |
|
| 96 |
+
# Code execution vulnerabilities
|
| 97 |
+
if re.search(r'\b(eval|exec)\s*\(', code) or 'pickle.load' in code or 'yaml.load' in code:
|
| 98 |
return {
|
| 99 |
"type": "bug",
|
| 100 |
"category": "security",
|
|
|
|
| 105 |
}
|
| 106 |
|
| 107 |
# === LOGIC BUGS ===
|
| 108 |
+
# Infinite loops
|
| 109 |
+
if ('while true' in code_no_space or 'while(true)' in code_no_space or 'for(;;)' in code_no_space) and 'break' not in code_lower:
|
| 110 |
return {
|
| 111 |
"type": "bug",
|
| 112 |
"category": "logic",
|
| 113 |
"severity": 3,
|
| 114 |
"line_hint": 2,
|
| 115 |
+
"comment": "Infinite loop detected. Loop condition never becomes false.",
|
| 116 |
+
"fix": "Add a break condition or change the loop condition."
|
| 117 |
}
|
| 118 |
|
| 119 |
+
# Off-by-one errors: range(len(x) - 1)
|
| 120 |
+
if re.search(r'range\s*\(\s*len\s*\([^)]+\)\s*-\s*1\s*\)', code):
|
| 121 |
return {
|
| 122 |
"type": "bug",
|
| 123 |
"category": "logic",
|
| 124 |
"severity": 3,
|
| 125 |
"line_hint": 2,
|
| 126 |
"comment": "Off-by-one error: range(len(x) - 1) skips the last element.",
|
| 127 |
+
"fix": "Use range(len(items)) or simply: for item in items:"
|
| 128 |
}
|
| 129 |
|
| 130 |
+
# Redundant length checks
|
| 131 |
+
if re.search(r'if\s+len\s*\([^)]+\)\s*[!=><]=*\s*0', code):
|
| 132 |
return {
|
| 133 |
"type": "bug",
|
| 134 |
"category": "logic",
|
|
|
|
| 138 |
"fix": "Use: if items: instead of if len(items) != 0:"
|
| 139 |
}
|
| 140 |
|
| 141 |
+
# Type coercion bugs in JavaScript
|
| 142 |
+
if lang == 'javascript':
|
| 143 |
+
# Loose equality (== instead of ===)
|
| 144 |
+
if re.search(r'==\s|==[\'\"]|==\d|\s==', code) and not re.search(r'===', code):
|
| 145 |
+
return {
|
| 146 |
+
"type": "bug",
|
| 147 |
+
"category": "logic",
|
| 148 |
+
"severity": 2,
|
| 149 |
+
"line_hint": 3,
|
| 150 |
+
"comment": "Use strict equality (===) instead of ==. Prone to type coercion bugs.",
|
| 151 |
+
"fix": "Replace == with === and != with !=="
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
# var instead of const/let
|
| 155 |
+
if re.search(r'\bvar\s+\w+\s*[=;]', code):
|
| 156 |
+
return {
|
| 157 |
+
"type": "bug",
|
| 158 |
+
"category": "style",
|
| 159 |
+
"severity": 2,
|
| 160 |
+
"line_hint": 1,
|
| 161 |
+
"comment": "Use const/let instead of var. var has scope issues.",
|
| 162 |
+
"fix": "Replace var with const (or let if reassignment needed)"
|
| 163 |
+
}
|
| 164 |
|
| 165 |
# === DEFAULT: NO BUGS ===
|
| 166 |
return {
|