Repository navigation
fix: Do not error out on invalid rego policies if any - #2670
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue where the policy evaluation system incorrectly treats invalid rego policies as errors when they produce no results. The change allows the system to handle invalid policies gracefully by returning empty results instead of failing with an error.
- Removes error handling that treated nil scan results as failures
- Updates test expectations to verify that invalid policies return empty results rather than errors
- Removes redundant test cases that were testing the same error condition
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/policy/policy.go | Removes the error condition that was triggered when scan results were nil |
| pkg/policy/policy_test.go | Updates test expectations and removes duplicate test cases for invalid policy handling |
Comments suppressed due to low confidence (1)
pkg/policy/policy_test.go:549
- The test case expects empty results for an invalid policy, but there's no assertion to verify that no error is returned. Consider adding an explicit check that expectedError is empty or nil to ensure the fix is properly validated.
results: []Result{}, // passing an invalid policy should not return any results but not an error
| @@ -300,10 +300,6 @@ func (p *Policies) Eval(ctx context.Context, resource client.Object, inputs ...[ | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
There was a problem hiding this comment.
Returning a nil scanResult without any validation could cause nil pointer dereferences in calling code. Consider returning an empty slice or struct instead of nil when no results are found.
Suggested change
| } | |
| } | |
| if scanResult == nil { | |
| scanResult = scan.Results{} | |
| } |
simar7
marked this pull request as ready for review
July 25, 2025 03:56
afdesk
approved these changes
Jul 25, 2025
Contributor
|
LGTM! $ helm install trivy-operator aquasecurity/trivy-operator --create-namespace --namespace trivy-system \
--set image.repository=afdesk/trivy-operator --set image.tag=fix-invalid-logic \
--set operator.scanJobTTL=30m --set trivy.resources.limits.memory=1Gi |
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
If there are no results returned upon processing an invalid policy, it should not be treated as an error as there are simply no errors that have actually taken place.
Checklist