Skip to content

Commit b5b7b53

Browse files
author
Orlando Barrera II
committed
Testing the sarif file parsing
1 parent c5883ef commit b5b7b53

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

socketsecurity/core/messages.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Messages:
1818
def map_severity_to_sarif(severity: str) -> str:
1919
"""
2020
Map Socket severity levels to SARIF levels (GitHub code scanning).
21-
21+
2222
'low' -> 'note'
2323
'medium' or 'middle' -> 'warning'
2424
'high' or 'critical' -> 'error'
@@ -45,10 +45,13 @@ def find_line_in_file(packagename: str, packageversion: str, manifest_file: str)
4545
2) Text-based (requirements.txt, package.json, yarn.lock, etc.)
4646
- Uses compiled regex patterns to detect a match line by line
4747
"""
48+
# Extract just the file name to detect manifest type
4849
file_type = Path(manifest_file).name
4950
logging.debug("Processing file for line lookup: %s", manifest_file)
5051

51-
# (Existing logic remains unchanged, with logs added where necessary)
52+
# ----------------------------------------------------
53+
# 1) JSON-based manifest files
54+
# ----------------------------------------------------
5255
if file_type in ["package-lock.json", "Pipfile.lock", "composer.lock"]:
5356
try:
5457
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)
8790
logging.error("Error reading %s: %s", manifest_file, e)
8891
return 1, f"Error reading {manifest_file}"
8992

90-
# Text-based manifests
93+
# ----------------------------------------------------
94+
# 2) Text-based / line-based manifests
95+
# ----------------------------------------------------
9196
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)}"',
9399
"yarn.lock": rf'{packagename}@{packageversion}',
94100
"pnpm-lock.yaml": rf'"{re.escape(packagename)}"\s*:\s*\{{[^}}]*"version":\s*"{re.escape(packageversion)}"',
95101
"requirements.txt": rf'^{re.escape(packagename)}\s*(?:==|===|!=|>=|<=|~=|\s+)?\s*{re.escape(packageversion)}(?:\s*;.*)?$',
@@ -171,7 +177,7 @@ def create_security_comment_sarif(diff) -> dict:
171177
- Accepts multiple manifest files from alert.introduced_by or alert.manifests.
172178
- Generates one SARIF location per manifest file.
173179
- 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.
175181
"""
176182
if len(diff.new_alerts) == 0:
177183
for alert in diff.new_alerts:
@@ -209,7 +215,6 @@ def create_security_comment_sarif(diff) -> dict:
209215
if alert.introduced_by and isinstance(alert.introduced_by, list):
210216
for entry in alert.introduced_by:
211217
if isinstance(entry, (list, tuple)) and len(entry) >= 2:
212-
# Split semicolon-separated file names.
213218
files = [f.strip() for f in entry[1].split(";") if f.strip()]
214219
manifest_files.extend(files)
215220
elif isinstance(entry, str):
@@ -244,7 +249,7 @@ def create_security_comment_sarif(diff) -> dict:
244249
},
245250
}
246251

247-
# For each manifest file, attempt to find the package declaration.
252+
# Create a SARIF location for each manifest file.
248253
locations = []
249254
for mf in manifest_files:
250255
logging.debug("Alert %s - Processing manifest file: %s", rule_id, mf)

0 commit comments

Comments
 (0)