@@ -82,11 +82,7 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
82
82
needle_key = f'"{ found_key } ":' # e.g. "node_modules/axios":
83
83
needle_version = f'"version": "{ packageversion } "'
84
84
lines = raw_text .splitlines ()
85
- < << << << HEAD
86
85
best_line = 1
87
- == == == =
88
- best_line = - 1
89
- > >> >> >> 7 ddb4537518fa762da7ebebff2044ce71e720f3c
90
86
snippet = None
91
87
92
88
for i , line in enumerate (lines , start = 1 ):
@@ -101,17 +97,10 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
101
97
else :
102
98
return 1 , f'"{ found_key } ": { found_info } '
103
99
else :
104
- << << << < HEAD
105
100
return 1 , f"{ packagename } { packageversion } (not found in { manifest_file } )"
106
101
107
102
except (FileNotFoundError , json .JSONDecodeError ):
108
103
return 1 , f"Error reading { manifest_file } "
109
- == == == =
110
- return - 1 , f"{ packagename } { packageversion } (not found in { manifest_file } )"
111
-
112
- except (FileNotFoundError , json .JSONDecodeError ):
113
- return - 1 , f"Error reading { manifest_file } "
114
- >> >> >> > 7 ddb4537518fa762da7ebebff2044ce71e720f3c
115
104
116
105
# ----------------------------------------------------
117
106
# 2) Text-based / line-based manifests
@@ -153,7 +142,6 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
153
142
for line_number , line_content in enumerate (lines , start = 1 ):
154
143
# For Python conditional dependencies, ignore everything after first ';'
155
144
line_main = line_content .split (";" , 1 )[0 ].strip ()
156
- << << << < HEAD
157
145
158
146
# Use a case-insensitive regex search
159
147
if re .search (searchstring , line_main , re .IGNORECASE ):
@@ -203,36 +191,6 @@ def create_security_comment_sarif(diff) -> dict:
203
191
Create SARIF-compliant output from the diff report, including dynamic URL generation
204
192
based on manifest type and improved <br/> formatting for GitHub SARIF display.
205
193
"""
206
- == == == =
207
-
208
- # Use a case-insensitive regex search
209
- if re .search (searchstring , line_main , re .IGNORECASE ):
210
- return line_number , line_content .strip ()
211
-
212
- except FileNotFoundError :
213
- return - 1 , f"{ manifest_file } not found"
214
- except Exception as e :
215
- return - 1 , f"Error reading { manifest_file } : { e } "
216
-
217
- return - 1 , f"{ packagename } { packageversion } (not found)"
218
-
219
- @staticmethod
220
- def create_security_comment_sarif (diff : Diff ) -> dict :
221
- """
222
- Create SARIF-compliant output from the diff report, including line references
223
- and a link to the Socket docs in the fullDescription. Also converts any \r \n
224
- into <br/> so they render properly in GitHub's SARIF display.
225
- """
226
- # Check if there's a blocking error in new alerts
227
- scan_failed = False
228
- if len (diff .new_alerts ) == 0 :
229
- for alert in diff .new_alerts :
230
- if alert .error :
231
- scan_failed = True
232
- break
233
-
234
- # Basic SARIF skeleton
235
- > >> >> >> 7 ddb4537518fa762da7ebebff2044ce71e720f3c
236
194
sarif_data = {
237
195
"$schema" : "https://json.schemastore.org/sarif-2.1.0.json" ,
238
196
"version" : "2.1.0" ,
@@ -259,27 +217,11 @@ def create_security_comment_sarif(diff: Diff) -> dict:
259
217
rule_id = f"{ pkg_name } =={ pkg_version } "
260
218
severity = alert .severity
261
219
262
- < << << << HEAD
263
220
# Generate the correct URL for the alert based on manifest type
264
- == == == =
265
- # Convert any \r\n in short desc to <br/> so they display properly
266
- short_desc_raw = f"{ alert .props .get ('note' , '' )} \r \n \r \n Suggested Action:\r \n { alert .suggestion } "
267
- short_desc = short_desc_raw .replace ("\r \n " , "<br/>" )
268
-
269
- # Build link to Socket docs, e.g. "https://socket.dev/npm/package/foo/alerts/1.2.3"
270
- socket_url = f"https://socket.dev/npm/package/{ pkg_name } /alerts/{ pkg_version } "
271
-
272
- # Also convert \r\n in the main description to <br/>, then append the Socket docs link
273
- base_desc = alert .description .replace ("\r \n " , "<br/>" )
274
- full_desc_raw = f"{ alert .title } - { base_desc } <br/>{ socket_url } "
275
-
276
- # Identify the manifest file and line
277
- >> >> >> > 7 ddb4537518fa762da7ebebff2044ce71e720f3c
278
221
introduced_list = alert .introduced_by
279
222
manifest_file = introduced_list [0 ][1 ] if introduced_list and isinstance (introduced_list [0 ], list ) else alert .manifests or "requirements.txt"
280
223
socket_url = Messages .get_manifest_type_url (manifest_file , pkg_name , pkg_version )
281
224
282
- < << << << HEAD
283
225
# Prepare descriptions with <br/> replacements
284
226
short_desc = f"{ alert .props .get ('note' , '' )} <br/><br/>Suggested Action:<br/>{ alert .suggestion } "
285
227
full_desc = f"{ alert .title } - { alert .description .replace ('\r \n ' , '<br/>' )} \r \n <a href=\" { socket_url } \" >{ socket_url } </a>"
@@ -290,33 +232,19 @@ def create_security_comment_sarif(diff: Diff) -> dict:
290
232
line_number = 1 # Ensure SARIF compliance
291
233
292
234
# Create the rule if not already defined
293
- == == == =
294
- line_number , line_content = Messages .find_line_in_file (pkg_name , pkg_version , manifest_file )
295
-
296
- # If not already defined, create a rule for this package
297
- > >> >> >> 7 ddb4537518fa762da7ebebff2044ce71e720f3c
298
235
if rule_id not in rules_map :
299
236
rules_map [rule_id ] = {
300
237
"id" : rule_id ,
301
238
"name" : f"{ pkg_name } =={ pkg_version } " ,
302
239
"shortDescription" : {"text" : f"Alert generated for { rule_id } by Socket Security" },
303
- << << << < HEAD
304
240
"fullDescription" : {"text" : full_desc },
305
241
"helpUri" : socket_url ,
306
- == == == =
307
- "fullDescription" : {"text" : full_desc_raw },
308
- "helpUri" : alert .url ,
309
- > >> >> >> 7 ddb4537518fa762da7ebebff2044ce71e720f3c
310
242
"defaultConfiguration" : {
311
243
"level" : Messages .map_severity_to_sarif (severity )
312
244
},
313
245
}
314
246
315
- << << << < HEAD
316
247
# Add the SARIF result
317
- == == == =
318
- # Create a SARIF "result" referencing the line where we found the match
319
- >> >> >> > 7 ddb4537518fa762da7ebebff2044ce71e720f3c
320
248
result_obj = {
321
249
"ruleId" : rule_id ,
322
250
"message" : {"text" : short_desc },
@@ -334,11 +262,7 @@ def create_security_comment_sarif(diff: Diff) -> dict:
334
262
}
335
263
results_list .append (result_obj )
336
264
337
- < << << << HEAD
338
265
# Attach rules and results
339
- == == == =
340
- # Attach our rules and results to the SARIF data
341
- >> >> >> > 7 ddb4537518fa762da7ebebff2044ce71e720f3c
342
266
sarif_data ["runs" ][0 ]["tool" ]["driver" ]["rules" ] = list (rules_map .values ())
343
267
sarif_data ["runs" ][0 ]["results" ] = results_list
344
268
0 commit comments