Skip to content

Commit 0914971

Browse files
author
Orlando Barrera II
committed
Improved the sarif file parsing
1 parent 1683f7e commit 0914971

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

socketsecurity/core/messages.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ def get_manifest_type_url(manifest_file: str, pkg_name: str, pkg_version: str) -
174174
@staticmethod
175175
def create_security_comment_sarif(diff) -> dict:
176176
"""
177-
Create a SARIF-compliant JSON object for alerts. This function now:
178-
- Accepts multiple manifest files (from alert.introduced_by or alert.manifests)
177+
Create SARIF-compliant output from the diff report, including dynamic URL generation
178+
based on manifest type and improved <br/> formatting for GitHub SARIF display.
179+
180+
This function now:
181+
- Accepts multiple manifest files from alert.introduced_by or alert.manifests.
179182
- Generates one SARIF location per manifest file.
180-
- Supports various language-specific manifest types.
183+
- Falls back to a default ("requirements.txt") if none is found.
181184
"""
182185
# (Optional: handle scan failure based on alert.error flags)
183186
if len(diff.new_alerts) == 0:
@@ -216,7 +219,7 @@ def create_security_comment_sarif(diff) -> dict:
216219
if alert.introduced_by and isinstance(alert.introduced_by, list):
217220
for entry in alert.introduced_by:
218221
if isinstance(entry, list) and len(entry) >= 2:
219-
manifest_files.append(entry[1])
222+
manifest_files.append(entry[1].strip())
220223
elif isinstance(entry, str):
221224
manifest_files.extend([m.strip() for m in entry.split(";") if m.strip()])
222225
elif hasattr(alert, 'manifests') and alert.manifests:
@@ -229,12 +232,14 @@ def create_security_comment_sarif(diff) -> dict:
229232
# Use the first manifest for URL generation.
230233
socket_url = Messages.get_manifest_type_url(manifest_files[0], pkg_name, pkg_version)
231234

235+
# Prepare the short and full descriptions.
232236
short_desc = (
233237
f"{alert.props.get('note', '')}<br/><br/>Suggested Action:<br/>"
234238
f"{alert.suggestion}<br/><a href=\"{socket_url}\">{socket_url}</a>"
235239
)
236240
full_desc = "{} - {}".format(alert.title, alert.description.replace('\r\n', '<br/>'))
237241

242+
# Create the rule definition if it hasn't been defined yet.
238243
if rule_id not in rules_map:
239244
rules_map[rule_id] = {
240245
"id": rule_id,
@@ -247,11 +252,12 @@ def create_security_comment_sarif(diff) -> dict:
247252
},
248253
}
249254

255+
# Create a SARIF location for each manifest file.
250256
locations = []
251257
for mf in manifest_files:
252258
line_number, line_content = Messages.find_line_in_file(pkg_name, pkg_version, mf)
253259
if line_number < 1:
254-
line_number = 1
260+
line_number = 1 # Ensure SARIF compliance.
255261
locations.append({
256262
"physicalLocation": {
257263
"artifactLocation": {"uri": mf},
@@ -262,13 +268,15 @@ def create_security_comment_sarif(diff) -> dict:
262268
}
263269
})
264270

271+
# Create the SARIF result for this alert.
265272
result_obj = {
266273
"ruleId": rule_id,
267274
"message": {"text": short_desc},
268275
"locations": locations,
269276
}
270277
results_list.append(result_obj)
271278

279+
# Attach the collected rules and results.
272280
sarif_data["runs"][0]["tool"]["driver"]["rules"] = list(rules_map.values())
273281
sarif_data["runs"][0]["results"] = results_list
274282

0 commit comments

Comments
 (0)