Skip to content

Commit 75d9bb2

Browse files
twistlock: defend against compliances being null (#13318)
* twistlock: defende against compliances being null * twistlock: defende against compliances being null
1 parent 924ba7f commit 75d9bb2

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

docs/content/en/open_source/contributing/how-to-write-a-parser.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ Good example:
166166
finding.cwe = data["mykey"]
167167
```
168168

169+
```python
170+
finding.cwe = data.get("mykey", 123)
171+
```
172+
173+
```python
174+
some_list = data.get("key_of_the_list") or []
175+
```
176+
177+
The finale example guards against cases where `key_of_the_list` is present, but `null`.
178+
179+
169180
### Parsing of CVSS vectors
170181

171182
Data can have `CVSS` vectors or scores. Defect Dojo use the `cvss` module provided by RedHat Security.

dojo/tools/twistlock/parser.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,14 @@ def parse_json(self, json_output):
177177

178178
def get_items(self, tree, test):
179179
items = {}
180-
if "results" in tree:
180+
results = tree.get("results") or []
181+
if results:
181182
# Extract image metadata for impact field (Item 3)
182-
result = tree["results"][0]
183+
result = results[0]
183184
image_metadata = self.build_image_metadata(result)
184185

185186
# Parse vulnerabilities
186-
vulnerabilityTree = result.get("vulnerabilities", [])
187+
vulnerabilityTree = result.get("vulnerabilities") or []
187188
for node in vulnerabilityTree:
188189
item = get_item(node, test, image_metadata)
189190
unique_key = node["id"] + str(
@@ -194,7 +195,7 @@ def get_items(self, tree, test):
194195
items[unique_key] = item
195196

196197
# Parse compliance findings
197-
complianceTree = result.get("compliances", [])
198+
complianceTree = result.get("compliances") or []
198199
for node in complianceTree:
199200
item = get_compliance_item(node, test, image_metadata)
200201
# Create unique key for compliance findings - prefer ID if available
@@ -326,11 +327,14 @@ def get_compliance_item(compliance, test, image_metadata=""):
326327

327328

328329
def convert_severity(severity):
329-
if severity.lower() == "important":
330+
if not severity:
331+
return "Info"
332+
sev_lower = severity.lower()
333+
if sev_lower == "important":
330334
return "High"
331-
if severity.lower() == "moderate":
335+
if sev_lower == "moderate":
332336
return "Medium"
333-
if severity.lower() in {"information", "informational", ""}:
337+
if sev_lower in {"information", "informational", ""}:
334338
return "Info"
335339
return severity.title()
336340

0 commit comments

Comments
 (0)