11import json
2- import os
3- import re
4- import json
52import logging
6- logging .basicConfig (level = logging .DEBUG )
7- 3+ import re
84from pathlib import Path
5+ 96from mdutils import MdUtils
107from prettytable import PrettyTable
118
129from socketsecurity .core .classes import Diff , Issue , Purl
1310
11+ log = logging .getLogger ("socketcli" )
1412
1513class Messages :
1614
@@ -46,21 +44,21 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
4644 - Uses regex patterns to detect a match line by line
4745 """
4846 file_type = Path (manifest_file ).name
49- logging .debug ("Processing file for line lookup: %s" , manifest_file )
47+ log .debug ("Processing file for line lookup: %s" , manifest_file )
5048
5149 if file_type in ["package-lock.json" , "Pipfile.lock" , "composer.lock" ]:
5250 try :
5351 with open (manifest_file , "r" , encoding = "utf-8" ) as f :
5452 raw_text = f .read ()
55- logging .debug ("Read %d characters from %s" , len (raw_text ), manifest_file )
53+ log .debug ("Read %d characters from %s" , len (raw_text ), manifest_file )
5654 data = json .loads (raw_text )
5755 packages_dict = (
5856 data .get ("packages" )
5957 or data .get ("default" )
6058 or data .get ("dependencies" )
6159 or {}
6260 )
63- logging .debug ("Found package keys in %s: %s" , manifest_file , list (packages_dict .keys ()))
61+ log .debug ("Found package keys in %s: %s" , manifest_file , list (packages_dict .keys ()))
6462 found_key = None
6563 found_info = None
6664 for key , value in packages_dict .items ():
@@ -72,16 +70,16 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
7270 if found_key and found_info :
7371 needle_key = f'"{ found_key } ":'
7472 lines = raw_text .splitlines ()
75- logging .debug ("Total lines in %s: %d" , manifest_file , len (lines ))
73+ log .debug ("Total lines in %s: %d" , manifest_file , len (lines ))
7674 for i , line in enumerate (lines , start = 1 ):
7775 if needle_key in line :
78- logging .debug ("Found match at line %d in %s: %s" , i , manifest_file , line .strip ())
76+ log .debug ("Found match at line %d in %s: %s" , i , manifest_file , line .strip ())
7977 return i , line .strip ()
8078 return 1 , f'"{ found_key } ": { found_info } '
8179 else :
8280 return 1 , f"{ packagename } { packageversion } (not found in { manifest_file } )"
8381 except (FileNotFoundError , json .JSONDecodeError ) as e :
84- logging .error ("Error reading %s: %s" , manifest_file , e )
82+ log .error ("Error reading %s: %s" , manifest_file , e )
8583 return 1 , f"Error reading { manifest_file } "
8684
8785 # For pnpm-lock.yaml, use a special regex pattern.
@@ -114,15 +112,15 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
114112 }
115113 searchstring = search_patterns .get (file_type , rf'{ re .escape (packagename )} .*{ re .escape (packageversion )} ' )
116114
117- logging .debug ("Using search pattern for %s: %s" , file_type , searchstring )
115+ log .debug ("Using search pattern for %s: %s" , file_type , searchstring )
118116 try :
119117 with open (manifest_file , 'r' , encoding = "utf-8" ) as file :
120118 lines = [line .rstrip ("\n " ) for line in file ]
121- logging .debug ("Total lines in %s: %d" , manifest_file , len (lines ))
119+ log .debug ("Total lines in %s: %d" , manifest_file , len (lines ))
122120 for line_number , line_content in enumerate (lines , start = 1 ):
123121 line_main = line_content .split (";" , 1 )[0 ].strip ()
124122 if re .search (searchstring , line_main , re .IGNORECASE ):
125- logging .debug ("Match found at line %d in %s: %s" , line_number , manifest_file , line_content .strip ())
123+ log .debug ("Match found at line %d in %s: %s" , line_number , manifest_file , line_content .strip ())
126124 return line_number , line_content .strip ()
127125 except FileNotFoundError :
128126 return 1 , f"{ manifest_file } not found"
@@ -172,8 +170,8 @@ def create_security_comment_sarif(diff) -> dict:
172170 - For alerts with multiple manifest files, generates an individual SARIF result for each file.
173171 - Appends the manifest file name to the rule ID and name to make each result unique.
174172 - Does NOT fall back to 'requirements.txt' if no manifest file is provided.
175- - Adds detailed logging to validate our assumptions.
176-
173+ - Adds detailed log to validate our assumptions.
174+
177175 """
178176 if len (diff .new_alerts ) == 0 :
179177 for alert in diff .new_alerts :
@@ -204,7 +202,7 @@ def create_security_comment_sarif(diff) -> dict:
204202 base_rule_id = f"{ pkg_name } =={ pkg_version } "
205203 severity = alert .severity
206204
207- logging .debug ("Alert %s - introduced_by: %s, manifests: %s" , base_rule_id , alert .introduced_by , getattr (alert , 'manifests' , None ))
205+ log .debug ("Alert %s - introduced_by: %s, manifests: %s" , base_rule_id , alert .introduced_by , getattr (alert , 'manifests' , None ))
208206 manifest_files = []
209207 if alert .introduced_by and isinstance (alert .introduced_by , list ):
210208 for entry in alert .introduced_by :
@@ -216,21 +214,21 @@ def create_security_comment_sarif(diff) -> dict:
216214 elif hasattr (alert , 'manifests' ) and alert .manifests :
217215 manifest_files = [mf .strip () for mf in alert .manifests .split (";" ) if mf .strip ()]
218216
219- logging .debug ("Alert %s - extracted manifest_files: %s" , base_rule_id , manifest_files )
217+ log .debug ("Alert %s - extracted manifest_files: %s" , base_rule_id , manifest_files )
220218 if not manifest_files :
221- logging .error ("Alert %s: No manifest file found; cannot determine file location." , base_rule_id )
219+ log .error ("Alert %s: No manifest file found; cannot determine file location." , base_rule_id )
222220 continue
223221
224- logging .debug ("Alert %s - using manifest_files for processing: %s" , base_rule_id , manifest_files )
222+ log .debug ("Alert %s - using manifest_files for processing: %s" , base_rule_id , manifest_files )
225223
226224 # Create an individual SARIF result for each manifest file.
227225 for mf in manifest_files :
228- logging .debug ("Alert %s - Processing manifest file: %s" , base_rule_id , mf )
226+ log .debug ("Alert %s - Processing manifest file: %s" , base_rule_id , mf )
229227 socket_url = Messages .get_manifest_type_url (mf , pkg_name , pkg_version )
230228 line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , mf )
231229 if line_number < 1 :
232230 line_number = 1
233- logging .debug ("Alert %s: Manifest %s, line %d: %s" , base_rule_id , mf , line_number , line_content )
231+ log .debug ("Alert %s: Manifest %s, line %d: %s" , base_rule_id , mf , line_number , line_content )
234232
235233 # Create a unique rule id and name by appending the manifest file.
236234 unique_rule_id = f"{ base_rule_id } ({ mf } )"
@@ -271,7 +269,7 @@ def create_security_comment_sarif(diff) -> dict:
271269 sarif_data ["runs" ][0 ]["results" ] = results_list
272270
273271 return sarif_data
274-
272+ 275273 @staticmethod
276274 def create_security_comment_json (diff : Diff ) -> dict :
277275 scan_failed = False
0 commit comments