fix(trivy_operator): fix compliance severity logic and checkID comparison#14359
fix(trivy_operator): fix compliance severity logic and checkID comparison#14359SergK wants to merge 1 commit intoDefectDojo:bugfixfrom
Conversation
…ison
Fix two bugs in the Trivy Operator parser:
1. compliance_handler.py: The severity selection logic was inverted -
it attempted to use check_severity when it was empty (causing KeyError)
and fell back to result_severity when check_severity was present.
Fixed to correctly prefer check-level severity, falling back to
result-level severity when the check has no severity.
2. checks_handler.py: The checkID comparison used integer 0 instead of
string "0", so `check_id != 0` was always True (string vs int).
This caused bogus reference URLs and vulnerability IDs to be
generated for checks without a checkID. Also changed from
`check.get("checkID", "0")` to `check.get("checkID") or "0"` to
handle explicit null values in JSON.
Signed-off-by: Sergiy Kulanov <sergiy_kulanov@epam.com>
|
@SergK Thanks. Could you assess the possible impact on deduplication? |
|
@valentijnscholten As far as I can see from the code, the Trivy Operator parser uses hash-code based deduplication configured in "Trivy Operator Scan": ["title", "severity", "vulnerability_ids", "description"],
For the For the severity fix ( That said, this is a one-time correction — the old severity values were simply wrong (inverted logic), so having them re-deduplicate with the correct severity is the right outcome. After the first reimport post-upgrade, everything settles and stays stable. |
Description
Fix two bugs in the Trivy Operator parser.
Bug 1: Compliance severity logic is inverted (
compliance_handler.py)The severity selection logic is inverted — when
check_severityis present, the code discards it and usesresult_severityinstead. Whencheck_severityis empty, the code tries to use it, causing aKeyErrorcrash.Example — the existing
cis_benchmark.jsontest fixture already proves this:When
check_severityis empty (e.g., a compliance pass sentinel), the original code crashes:Fix: Use check-level severity when present, fall back to result-level otherwise:
Reference: In the Trivy Operator Go source (compliance_types.go:237-240), when a control has zero misconfigurations, a
ComplianceCheck{Success: true}sentinel is created with empty severity — confirming the empty-severity path is reachable.Bug 2: checkID string/int comparison (
checks_handler.py)Three related issues with
checkIDhandling:check_id != 0— string vs int comparison (alwaysTruein Python):check.get("checkID", "0")— doesn't guard againstnull: Python'sdict.get(key, default)only uses the default when the key is absent. If the JSON contains"checkID": null, Python returnsNone, not"0".if check_id:— string"0"is truthy: When checkID is missing (defaulted to"0"), a bogus vulnerability ID is still generated becausebool("0")isTruein Python.Fix:
check.get("checkID") or "0"for null-safety,!= "0"for proper string comparison.Reference: In the Trivy Operator Go source (config_audit_types.go:111),
checkIDis defined asstring(not*string), so Go serializes it as""notnull. The CRD schema marks it as required. Theor "0"guard is defensive for manual JSON uploads to DefectDojo.Test results
test_cis_benchmark— first finding now correctly asserts"Critical"(check-level) instead of"High"(result-level). The test data incis_benchmark.jsonalready hadcheck.severity = "CRITICAL"andresult.severity = "HIGH"— the original assertion matched the buggy output.test_compliance_severity_logic— two checks: one withseverity="MEDIUM"(should use it), one withseverity=""(should fall back to result's"HIGH")test_configauditreport_missing_checkid— check with nocheckIDfield: verifies references is""(no bogus URL)ruff checkpassesDocumentation
No documentation changes needed — bug fixes to existing parser behavior.
Checklist
bugfixbranch.