Skip to content

Commit 3f9ad6f

Browse files
author
Orlando Barrera II
committed
Testing the sarif file parsing
1 parent f664b47 commit 3f9ad6f

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

socketsecurity/core/messages.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import re
44
import json
5+
import logging
6+
logging.basicConfig(level=logging.DEBUG)
57

68
from pathlib import Path
79
from mdutils import MdUtils
@@ -11,7 +13,7 @@
1113

1214

1315
class Messages:
14-
16+
1517
@staticmethod
1618
def map_severity_to_sarif(severity: str) -> str:
1719
"""
@@ -186,15 +188,8 @@ def get_manifest_type_url(manifest_file: str, pkg_name: str, pkg_version: str) -
186188
@staticmethod
187189
def create_security_comment_sarif(diff) -> dict:
188190
"""
189-
Create SARIF-compliant output from the diff report, including dynamic URL generation
190-
based on manifest type and improved <br/> formatting for GitHub SARIF display.
191-
192-
This function now:
193-
- Accepts multiple manifest files from alert.introduced_by or alert.manifests.
194-
- Generates one SARIF location per manifest file.
195-
- Falls back to a default ("requirements.txt") if none is found.
191+
Create SARIF-compliant output from the diff report.
196192
"""
197-
# (Optional: handle scan failure based on alert.error flags)
198193
if len(diff.new_alerts) == 0:
199194
for alert in diff.new_alerts:
200195
if alert.error:
@@ -203,18 +198,16 @@ def create_security_comment_sarif(diff) -> dict:
203198
sarif_data = {
204199
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
205200
"version": "2.1.0",
206-
"runs": [
207-
{
208-
"tool": {
209-
"driver": {
210-
"name": "Socket Security",
211-
"informationUri": "https://socket.dev",
212-
"rules": []
213-
}
214-
},
215-
"results": []
216-
}
217-
]
201+
"runs": [{
202+
"tool": {
203+
"driver": {
204+
"name": "Socket Security",
205+
"informationUri": "https://socket.dev",
206+
"rules": []
207+
}
208+
},
209+
"results": []
210+
}]
218211
}
219212

220213
rules_map = {}
@@ -226,8 +219,7 @@ def create_security_comment_sarif(diff) -> dict:
226219
rule_id = f"{pkg_name}=={pkg_version}"
227220
severity = alert.severity
228221

229-
# --- Determine manifest files from alert data ---
230-
# Instead of using a single manifest file, split the values.
222+
# --- Extract manifest files ---
231223
manifest_files = []
232224
if alert.introduced_by and isinstance(alert.introduced_by, list):
233225
for entry in alert.introduced_by:
@@ -238,21 +230,21 @@ def create_security_comment_sarif(diff) -> dict:
238230
elif hasattr(alert, 'manifests') and alert.manifests:
239231
manifest_files = [mf.strip() for mf in alert.manifests.split(";") if mf.strip()]
240232

241-
# Fallback if no manifest file was determined.
233+
# Log the extracted manifest files
234+
logging.debug("Alert %s manifest_files before fallback: %s", rule_id, manifest_files)
235+
242236
if not manifest_files:
243237
manifest_files = ["requirements.txt"]
238+
logging.debug("Alert %s: Falling back to manifest_files: %s", rule_id, manifest_files)
244239

245-
# Use the first manifest for URL generation.
246-
socket_url = Messages.get_manifest_type_url(manifest_files[0], pkg_name, pkg_version)
240+
# Log the manifest file used for URL generation
241+
logging.debug("Alert %s: Using manifest_file for URL: %s", rule_id, manifest_files[0])
247242

248-
# Prepare descriptions with <br/> replacements.
249-
short_desc = (
250-
f"{alert.props.get('note', '')}<br/><br/>Suggested Action:<br/>{alert.suggestion}"
251-
f"<br/><a href=\"{socket_url}\">{socket_url}</a>"
252-
)
243+
socket_url = Messages.get_manifest_type_url(manifest_files[0], pkg_name, pkg_version)
244+
short_desc = (f"{alert.props.get('note', '')}<br/><br/>Suggested Action:<br/>{alert.suggestion}"
245+
f"<br/><a href=\"{socket_url}\">{socket_url}</a>")
253246
full_desc = "{} - {}".format(alert.title, alert.description.replace('\r\n', '<br/>'))
254247

255-
# Create the rule definition if it hasn't been defined yet.
256248
if rule_id not in rules_map:
257249
rules_map[rule_id] = {
258250
"id": rule_id,
@@ -265,12 +257,13 @@ def create_security_comment_sarif(diff) -> dict:
265257
},
266258
}
267259

268-
# Create a SARIF location for each manifest file.
260+
# Create a SARIF location for each manifest file and log each result.
269261
locations = []
270262
for mf in manifest_files:
271263
line_number, line_content = Messages.find_line_in_file(pkg_name, pkg_version, mf)
272264
if line_number < 1:
273-
line_number = 1 # Ensure SARIF compliance.
265+
line_number = 1
266+
logging.debug("Alert %s: Manifest %s, line %s: %s", rule_id, mf, line_number, line_content)
274267
locations.append({
275268
"physicalLocation": {
276269
"artifactLocation": {"uri": mf},
@@ -281,15 +274,13 @@ def create_security_comment_sarif(diff) -> dict:
281274
}
282275
})
283276

284-
# Create the SARIF result for this alert with multiple locations.
285277
result_obj = {
286278
"ruleId": rule_id,
287279
"message": {"text": short_desc},
288280
"locations": locations,
289281
}
290282
results_list.append(result_obj)
291283

292-
# Attach rules and results.
293284
sarif_data["runs"][0]["tool"]["driver"]["rules"] = list(rules_map.values())
294285
sarif_data["runs"][0]["results"] = results_list
295286

0 commit comments

Comments
 (0)