77Licensed under Fair Source 0.9
88"""
99
10+ import logging
1011import time
1112from pathlib import Path
1213from typing import Any
1314
1415from ..state import ToolResult
1516
17+ logger = logging .getLogger (__name__ )
18+
1619
1720class SecurityAdapter :
1821 """Adapter for the Security Vulnerability Scanner.
@@ -96,9 +99,34 @@ async def analyze(self) -> ToolResult:
9699 "remediation" : vuln .get ("remediation" , "" ),
97100 }
98101 findings .append (finding )
99- except Exception :
100- # Skip files that can't be scanned
102+ except (OSError , PermissionError ) as e :
103+ # File system errors - log and skip
104+ logger .warning (f"Cannot access { py_file } : { e } " )
105+ continue
106+ except UnicodeDecodeError as e :
107+ # Binary or encoding issues - log and skip
108+ logger .debug (f"Cannot decode { py_file } : { e } " )
101109 continue
110+ except (ValueError , RuntimeError , KeyError , IndexError , AttributeError ) as e :
111+ # Fail secure - treat scan failures as potential issues
112+ logger .error (f"Scanner failed on { py_file } : { e } " )
113+ findings_by_severity ["medium" ] += 1
114+ finding = {
115+ "finding_id" : f"sec_{ len (findings )} " ,
116+ "tool" : "security" ,
117+ "category" : "security" ,
118+ "severity" : "medium" ,
119+ "file_path" : str (py_file .relative_to (self .project_root )),
120+ "line_number" : None ,
121+ "code" : "SCAN_FAILURE" ,
122+ "message" : f"Security scanner failed: { type (e ).__name__ } " ,
123+ "evidence" : str (e ),
124+ "confidence" : 0.5 ,
125+ "fixable" : False ,
126+ "fix_command" : None ,
127+ "remediation" : "Manual review recommended - scanner could not complete" ,
128+ }
129+ findings .append (finding )
102130
103131 # Scan dependencies if enabled
104132 if self .scan_dependencies :
@@ -124,8 +152,39 @@ async def analyze(self) -> ToolResult:
124152 "remediation" : f"Upgrade to { vuln .get ('fix_version' , 'latest' )} " ,
125153 }
126154 findings .append (finding )
127- except Exception :
128- pass
155+ except FileNotFoundError as e :
156+ # No requirements file - log info only
157+ logger .info (f"No dependency file found: { e } " )
158+ except (
159+ ValueError ,
160+ RuntimeError ,
161+ KeyError ,
162+ IndexError ,
163+ AttributeError ,
164+ ConnectionError ,
165+ ) as e :
166+ # Fail secure - dependency scan failures are security issues
167+ logger .error (f"Dependency scanner failed: { e } " )
168+ findings_by_severity ["high" ] += 1
169+ finding = {
170+ "finding_id" : f"sec_dep_{ len (findings )} " ,
171+ "tool" : "security" ,
172+ "category" : "deps" ,
173+ "severity" : "high" ,
174+ "file_path" : "requirements.txt" ,
175+ "line_number" : None ,
176+ "code" : "DEP_SCAN_FAILURE" ,
177+ "message" : f"Dependency scanner failed: { type (e ).__name__ } " ,
178+ "evidence" : str (e ),
179+ "confidence" : 0.7 ,
180+ "fixable" : False ,
181+ "fix_command" : None ,
182+ "remediation" : "Manual dependency audit recommended - scanner could not complete" ,
183+ }
184+ findings .append (finding )
185+ except OSError as e :
186+ # File system errors - log and continue
187+ logger .warning (f"Cannot access dependency files: { e } " )
129188
130189 # Calculate score
131190 score = self ._calculate_score (findings_by_severity )
@@ -153,8 +212,20 @@ async def analyze(self) -> ToolResult:
153212 "vulnerability_scanner module not available" ,
154213 start_time ,
155214 )
215+ except OSError as e :
216+ # File system errors accessing project root
217+ logger .critical (f"File system error during security scan: { e } " )
218+ return self ._create_error_result (f"Cannot access project files: { e } " , start_time )
219+ except (AttributeError , TypeError ) as e :
220+ # Scanner API errors or invalid configuration
221+ logger .error (f"Security scanner configuration error: { e } " )
222+ return self ._create_error_result (f"Scanner configuration issue: { e } " , start_time )
156223 except Exception as e :
157- return self ._create_error_result (str (e ), start_time )
224+ # Unexpected errors - log and report
225+ logger .exception (f"Unexpected error in security scan: { e } " )
226+ return self ._create_error_result (
227+ f"Security scan failed: { type (e ).__name__ } : { e } " , start_time
228+ )
158229
159230 def _map_severity (self , severity : str ) -> str :
160231 """Map scanner severity to unified severity."""
0 commit comments