fix(trivy): initialize resource_name for cluster-scoped k8s resources#15290
Open
Volkiaa wants to merge 1 commit into
Open
fix(trivy): initialize resource_name for cluster-scoped k8s resources#15290Volkiaa wants to merge 1 commit into
Volkiaa wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The kubernetes-report branch of the Trivy parser (
ClusterNamereports produced bytrivy k8s --report all --format json) crashes with anUnboundLocalErrorwhen the first entry of theResourcesarray is a cluster-scoped resource (e.g.ClusterRole,ClusterRoleBinding— noNamespacekey). Through/api/v2/import-scan/this surfaces as an HTTP 500.Root cause: in the
Resourcesloop ofdojo/tools/trivy/parser.py,resource_nameis never initialized before the conditional concatenations — unlike the two twin loops just above (Vulnerabilities,Misconfigurations), which both resetservice_name = ""on every iteration:Namespace, theif namespace:assignment is skipped andresource_name += f"{kind} / "raisesUnboundLocalError;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):Test results
Added
unittests/scans/trivy/kubernetes_cluster_scoped_resources.json— aClusterNamereport whoseResourcesarray contains a cluster-scoped resource first (crash case), then a namespaced one, then a second cluster-scoped one (carry-over case) — andtest_kubernetes_cluster_scoped_resourcesinunittests/tools/test_trivy_parser.pyasserting 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
UnboundLocalErrorwith 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