@@ -45,13 +45,10 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
45
45
2) Text-based (requirements.txt, package.json, yarn.lock, etc.)
46
46
- Uses compiled regex patterns to detect a match line by line
47
47
"""
48
- # Extract just the file name to detect manifest type
49
48
file_type = Path (manifest_file ).name
50
- logging .debug ("Processing file: %s" , manifest_file )
49
+ logging .debug ("Processing file for line lookup : %s" , manifest_file )
51
50
52
- # ----------------------------------------------------
53
- # 1) JSON-based manifest files
54
- # ----------------------------------------------------
51
+ # (Existing logic remains unchanged, with logs added where necessary)
55
52
if file_type in ["package-lock.json" , "Pipfile.lock" , "composer.lock" ]:
56
53
try :
57
54
with open (manifest_file , "r" , encoding = "utf-8" ) as f :
@@ -64,7 +61,7 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
64
61
or data .get ("dependencies" )
65
62
or {}
66
63
)
67
- logging .debug ("Found package keys: %s" , list (packages_dict .keys ()))
64
+ logging .debug ("Found package keys in %s : %s" , manifest_file , list (packages_dict .keys ()))
68
65
found_key = None
69
66
found_info = None
70
67
for key , value in packages_dict .items ():
@@ -81,7 +78,7 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
81
78
logging .debug ("Total lines in %s: %d" , manifest_file , len (lines ))
82
79
for i , line in enumerate (lines , start = 1 ):
83
80
if (needle_key in line ) or (needle_version in line ):
84
- logging .debug ("Match found at line %d in %s: %s" , i , manifest_file , line .strip ())
81
+ logging .debug ("Found match at line %d in %s: %s" , i , manifest_file , line .strip ())
85
82
return i , line .strip ()
86
83
return 1 , f'"{ found_key } ": { found_info } '
87
84
else :
@@ -90,9 +87,7 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
90
87
logging .error ("Error reading %s: %s" , manifest_file , e )
91
88
return 1 , f"Error reading { manifest_file } "
92
89
93
- # ----------------------------------------------------
94
- # 2) Text-based / line-based manifests
95
- # ----------------------------------------------------
90
+ # Text-based manifests
96
91
search_patterns = {
97
92
"package.json" : rf'"{ packagename } ":\s*"{ packageversion } "' ,
98
93
"yarn.lock" : rf'{ packagename } @{ packageversion } ' ,
@@ -176,6 +171,7 @@ def create_security_comment_sarif(diff) -> dict:
176
171
- Accepts multiple manifest files from alert.introduced_by or alert.manifests.
177
172
- Generates one SARIF location per manifest file.
178
173
- Does NOT fall back to 'requirements.txt' if no manifest file is provided.
174
+ - Adds detailed logging to validate assumptions.
179
175
"""
180
176
if len (diff .new_alerts ) == 0 :
181
177
for alert in diff .new_alerts :
@@ -206,27 +202,31 @@ def create_security_comment_sarif(diff) -> dict:
206
202
rule_id = f"{ pkg_name } =={ pkg_version } "
207
203
severity = alert .severity
208
204
209
- # --- Extract manifest files from alert data ---
205
+ # Log raw alert data for manifest extraction.
210
206
logging .debug ("Alert %s - introduced_by: %s, manifests: %s" , rule_id , alert .introduced_by , getattr (alert , 'manifests' , None ))
207
+
211
208
manifest_files = []
212
209
if alert .introduced_by and isinstance (alert .introduced_by , list ):
213
210
for entry in alert .introduced_by :
214
- # Accept lists or tuples
215
211
if isinstance (entry , (list , tuple )) and len (entry ) >= 2 :
216
- # Split the second element if it contains semicolons
212
+ # Split semicolon-separated file names.
217
213
files = [f .strip () for f in entry [1 ].split (";" ) if f .strip ()]
218
214
manifest_files .extend (files )
219
215
elif isinstance (entry , str ):
220
216
manifest_files .extend ([m .strip () for m in entry .split (";" ) if m .strip ()])
221
217
elif hasattr (alert , 'manifests' ) and alert .manifests :
222
218
manifest_files = [mf .strip () for mf in alert .manifests .split (";" ) if mf .strip ()]
223
219
220
+ logging .debug ("Alert %s - extracted manifest_files: %s" , rule_id , manifest_files )
221
+
224
222
if not manifest_files :
225
223
logging .error ("Alert %s: No manifest file found; cannot determine file location." , rule_id )
226
224
continue # Skip this alert if no manifest is provided
227
225
228
- logging .debug ("Alert %s using manifest_files: %s" , rule_id , manifest_files )
226
+ logging .debug ("Alert %s - using manifest_files for processing: %s" , rule_id , manifest_files )
227
+
229
228
# Use the first manifest for URL generation.
229
+ logging .debug ("Alert %s - Using file for URL generation: %s" , rule_id , manifest_files [0 ])
230
230
socket_url = Messages .get_manifest_type_url (manifest_files [0 ], pkg_name , pkg_version )
231
231
short_desc = (f"{ alert .props .get ('note' , '' )} <br/><br/>Suggested Action:<br/>{ alert .suggestion } "
232
232
f"<br/><a href=\" { socket_url } \" >{ socket_url } </a>" )
@@ -244,9 +244,10 @@ def create_security_comment_sarif(diff) -> dict:
244
244
},
245
245
}
246
246
247
- # Create a SARIF location for each manifest file .
247
+ # For each manifest file, attempt to find the package declaration .
248
248
locations = []
249
249
for mf in manifest_files :
250
+ logging .debug ("Alert %s - Processing manifest file: %s" , rule_id , mf )
250
251
line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , mf )
251
252
if line_number < 1 :
252
253
line_number = 1 # Ensure SARIF compliance.
0 commit comments