@@ -174,10 +174,13 @@ def get_manifest_type_url(manifest_file: str, pkg_name: str, pkg_version: str) -
174
174
@staticmethod
175
175
def create_security_comment_sarif (diff ) -> dict :
176
176
"""
177
- Create a SARIF-compliant JSON object for alerts. This function now:
178
- - Accepts multiple manifest files (from alert.introduced_by or alert.manifests)
177
+ Create SARIF-compliant output from the diff report, including dynamic URL generation
178
+ based on manifest type and improved <br/> formatting for GitHub SARIF display.
179
+
180
+ This function now:
181
+ - Accepts multiple manifest files from alert.introduced_by or alert.manifests.
179
182
- Generates one SARIF location per manifest file.
180
- - Supports various language-specific manifest types .
183
+ - Falls back to a default ("requirements.txt") if none is found .
181
184
"""
182
185
# (Optional: handle scan failure based on alert.error flags)
183
186
if len (diff .new_alerts ) == 0 :
@@ -216,7 +219,7 @@ def create_security_comment_sarif(diff) -> dict:
216
219
if alert .introduced_by and isinstance (alert .introduced_by , list ):
217
220
for entry in alert .introduced_by :
218
221
if isinstance (entry , list ) and len (entry ) >= 2 :
219
- manifest_files .append (entry [1 ])
222
+ manifest_files .append (entry [1 ]. strip () )
220
223
elif isinstance (entry , str ):
221
224
manifest_files .extend ([m .strip () for m in entry .split (";" ) if m .strip ()])
222
225
elif hasattr (alert , 'manifests' ) and alert .manifests :
@@ -229,12 +232,14 @@ def create_security_comment_sarif(diff) -> dict:
229
232
# Use the first manifest for URL generation.
230
233
socket_url = Messages .get_manifest_type_url (manifest_files [0 ], pkg_name , pkg_version )
231
234
235
+ # Prepare the short and full descriptions.
232
236
short_desc = (
233
237
f"{ alert .props .get ('note' , '' )} <br/><br/>Suggested Action:<br/>"
234
238
f"{ alert .suggestion } <br/><a href=\" { socket_url } \" >{ socket_url } </a>"
235
239
)
236
240
full_desc = "{} - {}" .format (alert .title , alert .description .replace ('\r \n ' , '<br/>' ))
237
241
242
+ # Create the rule definition if it hasn't been defined yet.
238
243
if rule_id not in rules_map :
239
244
rules_map [rule_id ] = {
240
245
"id" : rule_id ,
@@ -247,11 +252,12 @@ def create_security_comment_sarif(diff) -> dict:
247
252
},
248
253
}
249
254
255
+ # Create a SARIF location for each manifest file.
250
256
locations = []
251
257
for mf in manifest_files :
252
258
line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , mf )
253
259
if line_number < 1 :
254
- line_number = 1
260
+ line_number = 1 # Ensure SARIF compliance.
255
261
locations .append ({
256
262
"physicalLocation" : {
257
263
"artifactLocation" : {"uri" : mf },
@@ -262,13 +268,15 @@ def create_security_comment_sarif(diff) -> dict:
262
268
}
263
269
})
264
270
271
+ # Create the SARIF result for this alert.
265
272
result_obj = {
266
273
"ruleId" : rule_id ,
267
274
"message" : {"text" : short_desc },
268
275
"locations" : locations ,
269
276
}
270
277
results_list .append (result_obj )
271
278
279
+ # Attach the collected rules and results.
272
280
sarif_data ["runs" ][0 ]["tool" ]["driver" ]["rules" ] = list (rules_map .values ())
273
281
sarif_data ["runs" ][0 ]["results" ] = results_list
274
282
0 commit comments