@@ -18,7 +18,7 @@ class Messages:
18
18
def map_severity_to_sarif (severity : str ) -> str :
19
19
"""
20
20
Map Socket severity levels to SARIF levels (GitHub code scanning).
21
-
21
+
22
22
'low' -> 'note'
23
23
'medium' or 'middle' -> 'warning'
24
24
'high' or 'critical' -> 'error'
@@ -45,10 +45,13 @@ 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
48
49
file_type = Path (manifest_file ).name
49
50
logging .debug ("Processing file for line lookup: %s" , manifest_file )
50
51
51
- # (Existing logic remains unchanged, with logs added where necessary)
52
+ # ----------------------------------------------------
53
+ # 1) JSON-based manifest files
54
+ # ----------------------------------------------------
52
55
if file_type in ["package-lock.json" , "Pipfile.lock" , "composer.lock" ]:
53
56
try :
54
57
with open (manifest_file , "r" , encoding = "utf-8" ) as f :
@@ -87,9 +90,12 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
87
90
logging .error ("Error reading %s: %s" , manifest_file , e )
88
91
return 1 , f"Error reading { manifest_file } "
89
92
90
- # Text-based manifests
93
+ # ----------------------------------------------------
94
+ # 2) Text-based / line-based manifests
95
+ # ----------------------------------------------------
91
96
search_patterns = {
92
- "package.json" : rf'"{ packagename } ":\s*"{ packageversion } "' ,
97
+ # Updated pattern for package.json to allow optional '^' or '~'
98
+ "package.json" : rf'"{ packagename } ":\s*"[\^~]?{ re .escape (packageversion )} "' ,
93
99
"yarn.lock" : rf'{ packagename } @{ packageversion } ' ,
94
100
"pnpm-lock.yaml" : rf'"{ re .escape (packagename )} "\s*:\s*\{{[^}}]*"version":\s*"{ re .escape (packageversion )} "' ,
95
101
"requirements.txt" : rf'^{ re .escape (packagename )} \s*(?:==|===|!=|>=|<=|~=|\s+)?\s*{ re .escape (packageversion )} (?:\s*;.*)?$' ,
@@ -171,7 +177,7 @@ def create_security_comment_sarif(diff) -> dict:
171
177
- Accepts multiple manifest files from alert.introduced_by or alert.manifests.
172
178
- Generates one SARIF location per manifest file.
173
179
- Does NOT fall back to 'requirements.txt' if no manifest file is provided.
174
- - Adds detailed logging to validate assumptions.
180
+ - Adds detailed logging to validate our assumptions.
175
181
"""
176
182
if len (diff .new_alerts ) == 0 :
177
183
for alert in diff .new_alerts :
@@ -209,7 +215,6 @@ def create_security_comment_sarif(diff) -> dict:
209
215
if alert .introduced_by and isinstance (alert .introduced_by , list ):
210
216
for entry in alert .introduced_by :
211
217
if isinstance (entry , (list , tuple )) and len (entry ) >= 2 :
212
- # Split semicolon-separated file names.
213
218
files = [f .strip () for f in entry [1 ].split (";" ) if f .strip ()]
214
219
manifest_files .extend (files )
215
220
elif isinstance (entry , str ):
@@ -244,7 +249,7 @@ def create_security_comment_sarif(diff) -> dict:
244
249
},
245
250
}
246
251
247
- # For each manifest file, attempt to find the package declaration .
252
+ # Create a SARIF location for each manifest file .
248
253
locations = []
249
254
for mf in manifest_files :
250
255
logging .debug ("Alert %s - Processing manifest file: %s" , rule_id , mf )
0 commit comments