Skip to content

fix(trivy): initialize resource_name for cluster-scoped k8s resources#15290

Open
Volkiaa wants to merge 1 commit into
DefectDojo:bugfixfrom
Volkiaa:fix/trivy-k8s-cluster-scoped-resources
Open

fix(trivy): initialize resource_name for cluster-scoped k8s resources#15290
Volkiaa wants to merge 1 commit into
DefectDojo:bugfixfrom
Volkiaa:fix/trivy-k8s-cluster-scoped-resources

Conversation

@Volkiaa

@Volkiaa Volkiaa commented Jul 20, 2026

Copy link
Copy Markdown

Description

The kubernetes-report branch of the Trivy parser (ClusterName reports produced by trivy k8s --report all --format json) crashes with an UnboundLocalError when the first entry of the Resources array is a cluster-scoped resource (e.g. ClusterRole, ClusterRoleBinding — no Namespace key). Through /api/v2/import-scan/ this surfaces as an HTTP 500.

Root cause: in the Resources loop of dojo/tools/trivy/parser.py, resource_name is never initialized before the conditional concatenations — unlike the two twin loops just above (Vulnerabilities, Misconfigurations), which both reset service_name = "" on every iteration:

  • if the first resource has no Namespace, the if namespace: assignment is skipped and resource_name += f"{kind} / " raises UnboundLocalError;
  • if a cluster-scoped resource follows a namespaced one, it silently inherits the previous iteration's value and is imported with a corrupted service name.

The fix initializes resource_name = "" at the top of each iteration, matching the loops above.

Reproduced against a kind cluster with the current published image (defectdojo/defectdojo-django:latest, 2026-07-15):

File "dojo/tools/trivy/parser.py", line 224, in get_findings
    resource_name += f"{kind} / "
    ^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'resource_name' where it is not associated with a value

Test results

Added unittests/scans/trivy/kubernetes_cluster_scoped_resources.json — a ClusterName report whose Resources array contains a cluster-scoped resource first (crash case), then a namespaced one, then a second cluster-scoped one (carry-over case) — and test_kubernetes_cluster_scoped_resources in unittests/tools/test_trivy_parser.py asserting the three findings parse with the expected service names (ClusterRole / system:controller:bootstrap-signer, default / Deployment / redis-follower, ClusterRoleBinding / cluster-admin).

Verified: the sample raises UnboundLocalError with the current parser and parses cleanly (3 findings, correct service names) with the fix.

Documentation

No documentation change needed — one-line bugfix, no behavior change for valid inputs.

🤖 Generated with Claude Code

In the kubernetes-report branch of the Trivy parser, the Resources loop
never initializes resource_name before the conditional concatenations,
unlike the twin Vulnerabilities and Misconfigurations loops which reset
service_name = "" on every iteration.

Two consequences when a resource has no Namespace (cluster-scoped
resources such as ClusterRole or ClusterRoleBinding, as emitted by
`trivy k8s --report all --format json`):

- if the first resource in the report is cluster-scoped, the parser
  raises UnboundLocalError and /api/v2/import-scan/ returns HTTP 500;
- if a cluster-scoped resource follows a namespaced one, it silently
  inherits the previous iteration's value and gets a corrupted service
  name.

Fix by initializing resource_name = "" at the top of each iteration,
matching the two loops above. Adds a scan sample with a cluster-scoped
resource first (plus a namespaced one and a trailing cluster-scoped one
to cover the carry-over case) and a unit test asserting the parsed
service names.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant