1
1
import json
2
- import os
3
- import re
4
- import json
5
2
import logging
6
- logging .basicConfig (level = logging .DEBUG )
7
-
3
+ import re
8
4
from pathlib import Path
5
+
9
6
from mdutils import MdUtils
10
7
from prettytable import PrettyTable
11
8
12
9
from socketsecurity .core .classes import Diff , Issue , Purl
13
10
11
+ log = logging .getLogger ("socketcli" )
14
12
15
13
class Messages :
16
14
@@ -46,21 +44,21 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
46
44
- Uses regex patterns to detect a match line by line
47
45
"""
48
46
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 )
50
48
51
49
if file_type in ["package-lock.json" , "Pipfile.lock" , "composer.lock" ]:
52
50
try :
53
51
with open (manifest_file , "r" , encoding = "utf-8" ) as f :
54
52
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 )
56
54
data = json .loads (raw_text )
57
55
packages_dict = (
58
56
data .get ("packages" )
59
57
or data .get ("default" )
60
58
or data .get ("dependencies" )
61
59
or {}
62
60
)
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 ()))
64
62
found_key = None
65
63
found_info = None
66
64
for key , value in packages_dict .items ():
@@ -72,16 +70,16 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
72
70
if found_key and found_info :
73
71
needle_key = f'"{ found_key } ":'
74
72
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 ))
76
74
for i , line in enumerate (lines , start = 1 ):
77
75
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 ())
79
77
return i , line .strip ()
80
78
return 1 , f'"{ found_key } ": { found_info } '
81
79
else :
82
80
return 1 , f"{ packagename } { packageversion } (not found in { manifest_file } )"
83
81
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 )
85
83
return 1 , f"Error reading { manifest_file } "
86
84
87
85
# 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)
114
112
}
115
113
searchstring = search_patterns .get (file_type , rf'{ re .escape (packagename )} .*{ re .escape (packageversion )} ' )
116
114
117
- logging .debug ("Using search pattern for %s: %s" , file_type , searchstring )
115
+ log .debug ("Using search pattern for %s: %s" , file_type , searchstring )
118
116
try :
119
117
with open (manifest_file , 'r' , encoding = "utf-8" ) as file :
120
118
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 ))
122
120
for line_number , line_content in enumerate (lines , start = 1 ):
123
121
line_main = line_content .split (";" , 1 )[0 ].strip ()
124
122
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 ())
126
124
return line_number , line_content .strip ()
127
125
except FileNotFoundError :
128
126
return 1 , f"{ manifest_file } not found"
@@ -172,8 +170,8 @@ def create_security_comment_sarif(diff) -> dict:
172
170
- For alerts with multiple manifest files, generates an individual SARIF result for each file.
173
171
- Appends the manifest file name to the rule ID and name to make each result unique.
174
172
- 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
+
177
175
"""
178
176
if len (diff .new_alerts ) == 0 :
179
177
for alert in diff .new_alerts :
@@ -204,7 +202,7 @@ def create_security_comment_sarif(diff) -> dict:
204
202
base_rule_id = f"{ pkg_name } =={ pkg_version } "
205
203
severity = alert .severity
206
204
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 ))
208
206
manifest_files = []
209
207
if alert .introduced_by and isinstance (alert .introduced_by , list ):
210
208
for entry in alert .introduced_by :
@@ -216,21 +214,21 @@ def create_security_comment_sarif(diff) -> dict:
216
214
elif hasattr (alert , 'manifests' ) and alert .manifests :
217
215
manifest_files = [mf .strip () for mf in alert .manifests .split (";" ) if mf .strip ()]
218
216
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 )
220
218
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 )
222
220
continue
223
221
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 )
225
223
226
224
# Create an individual SARIF result for each manifest file.
227
225
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 )
229
227
socket_url = Messages .get_manifest_type_url (mf , pkg_name , pkg_version )
230
228
line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , mf )
231
229
if line_number < 1 :
232
230
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 )
234
232
235
233
# Create a unique rule id and name by appending the manifest file.
236
234
unique_rule_id = f"{ base_rule_id } ({ mf } )"
@@ -271,7 +269,7 @@ def create_security_comment_sarif(diff) -> dict:
271
269
sarif_data ["runs" ][0 ]["results" ] = results_list
272
270
273
271
return sarif_data
274
-
272
+
275
273
@staticmethod
276
274
def create_security_comment_json (diff : Diff ) -> dict :
277
275
scan_failed = False
0 commit comments