2
2
import os
3
3
import re
4
4
import json
5
+ import logging
6
+ logging .basicConfig (level = logging .DEBUG )
5
7
6
8
from pathlib import Path
7
9
from mdutils import MdUtils
11
13
12
14
13
15
class Messages :
14
-
16
+
15
17
@staticmethod
16
18
def map_severity_to_sarif (severity : str ) -> str :
17
19
"""
@@ -186,15 +188,8 @@ def get_manifest_type_url(manifest_file: str, pkg_name: str, pkg_version: str) -
186
188
@staticmethod
187
189
def create_security_comment_sarif (diff ) -> dict :
188
190
"""
189
- Create SARIF-compliant output from the diff report, including dynamic URL generation
190
- based on manifest type and improved <br/> formatting for GitHub SARIF display.
191
-
192
- This function now:
193
- - Accepts multiple manifest files from alert.introduced_by or alert.manifests.
194
- - Generates one SARIF location per manifest file.
195
- - Falls back to a default ("requirements.txt") if none is found.
191
+ Create SARIF-compliant output from the diff report.
196
192
"""
197
- # (Optional: handle scan failure based on alert.error flags)
198
193
if len (diff .new_alerts ) == 0 :
199
194
for alert in diff .new_alerts :
200
195
if alert .error :
@@ -203,18 +198,16 @@ def create_security_comment_sarif(diff) -> dict:
203
198
sarif_data = {
204
199
"$schema" : "https://json.schemastore.org/sarif-2.1.0.json" ,
205
200
"version" : "2.1.0" ,
206
- "runs" : [
207
- {
208
- "tool" : {
209
- "driver" : {
210
- "name" : "Socket Security" ,
211
- "informationUri" : "https://socket.dev" ,
212
- "rules" : []
213
- }
214
- },
215
- "results" : []
216
- }
217
- ]
201
+ "runs" : [{
202
+ "tool" : {
203
+ "driver" : {
204
+ "name" : "Socket Security" ,
205
+ "informationUri" : "https://socket.dev" ,
206
+ "rules" : []
207
+ }
208
+ },
209
+ "results" : []
210
+ }]
218
211
}
219
212
220
213
rules_map = {}
@@ -226,8 +219,7 @@ def create_security_comment_sarif(diff) -> dict:
226
219
rule_id = f"{ pkg_name } =={ pkg_version } "
227
220
severity = alert .severity
228
221
229
- # --- Determine manifest files from alert data ---
230
- # Instead of using a single manifest file, split the values.
222
+ # --- Extract manifest files ---
231
223
manifest_files = []
232
224
if alert .introduced_by and isinstance (alert .introduced_by , list ):
233
225
for entry in alert .introduced_by :
@@ -238,21 +230,21 @@ def create_security_comment_sarif(diff) -> dict:
238
230
elif hasattr (alert , 'manifests' ) and alert .manifests :
239
231
manifest_files = [mf .strip () for mf in alert .manifests .split (";" ) if mf .strip ()]
240
232
241
- # Fallback if no manifest file was determined.
233
+ # Log the extracted manifest files
234
+ logging .debug ("Alert %s manifest_files before fallback: %s" , rule_id , manifest_files )
235
+
242
236
if not manifest_files :
243
237
manifest_files = ["requirements.txt" ]
238
+ logging .debug ("Alert %s: Falling back to manifest_files: %s" , rule_id , manifest_files )
244
239
245
- # Use the first manifest for URL generation.
246
- socket_url = Messages . get_manifest_type_url ( manifest_files [ 0 ], pkg_name , pkg_version )
240
+ # Log the manifest file used for URL generation
241
+ logging . debug ( "Alert %s: Using manifest_file for URL: %s" , rule_id , manifest_files [ 0 ] )
247
242
248
- # Prepare descriptions with <br/> replacements.
249
- short_desc = (
250
- f"{ alert .props .get ('note' , '' )} <br/><br/>Suggested Action:<br/>{ alert .suggestion } "
251
- f"<br/><a href=\" { socket_url } \" >{ socket_url } </a>"
252
- )
243
+ socket_url = Messages .get_manifest_type_url (manifest_files [0 ], pkg_name , pkg_version )
244
+ short_desc = (f"{ alert .props .get ('note' , '' )} <br/><br/>Suggested Action:<br/>{ alert .suggestion } "
245
+ f"<br/><a href=\" { socket_url } \" >{ socket_url } </a>" )
253
246
full_desc = "{} - {}" .format (alert .title , alert .description .replace ('\r \n ' , '<br/>' ))
254
247
255
- # Create the rule definition if it hasn't been defined yet.
256
248
if rule_id not in rules_map :
257
249
rules_map [rule_id ] = {
258
250
"id" : rule_id ,
@@ -265,12 +257,13 @@ def create_security_comment_sarif(diff) -> dict:
265
257
},
266
258
}
267
259
268
- # Create a SARIF location for each manifest file.
260
+ # Create a SARIF location for each manifest file and log each result .
269
261
locations = []
270
262
for mf in manifest_files :
271
263
line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , mf )
272
264
if line_number < 1 :
273
- line_number = 1 # Ensure SARIF compliance.
265
+ line_number = 1
266
+ logging .debug ("Alert %s: Manifest %s, line %s: %s" , rule_id , mf , line_number , line_content )
274
267
locations .append ({
275
268
"physicalLocation" : {
276
269
"artifactLocation" : {"uri" : mf },
@@ -281,15 +274,13 @@ def create_security_comment_sarif(diff) -> dict:
281
274
}
282
275
})
283
276
284
- # Create the SARIF result for this alert with multiple locations.
285
277
result_obj = {
286
278
"ruleId" : rule_id ,
287
279
"message" : {"text" : short_desc },
288
280
"locations" : locations ,
289
281
}
290
282
results_list .append (result_obj )
291
283
292
- # Attach rules and results.
293
284
sarif_data ["runs" ][0 ]["tool" ]["driver" ]["rules" ] = list (rules_map .values ())
294
285
sarif_data ["runs" ][0 ]["results" ] = results_list
295
286
0 commit comments