Skip to content

Commit a126e10

Browse files
authored
fix(misconf): skip Rego errors with a nil location (#6666)
1 parent 6a72dd4 commit a126e10

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pkg/iac/rego/load.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (s *Scanner) fallbackChecks(compiler *ast.Compiler) {
184184
}
185185

186186
compiler.Errors = lo.Filter(compiler.Errors, func(e *ast.Error, _ int) bool {
187-
return !lo.Contains(excludedFiles, e.Location.File)
187+
return e.Location == nil || !lo.Contains(excludedFiles, e.Location.File)
188188
})
189189
}
190190

@@ -219,6 +219,9 @@ func (s *Scanner) prunePoliciesWithError(compiler *ast.Compiler) error {
219219
}
220220

221221
for _, e := range compiler.Errors {
222+
if e.Location == nil {
223+
continue
224+
}
222225
s.debug.Log("Error occurred while parsing: %s, %s", e.Location.File, e.Error())
223226
delete(s.policies, e.Location.File)
224227
}

pkg/iac/rego/load_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package rego_test
33
import (
44
"bytes"
55
"embed"
6+
"fmt"
67
"io"
78
"strings"
89
"testing"
910
"testing/fstest"
1011

1112
checks "github.com/aquasecurity/trivy-checks"
13+
"github.com/open-policy-agent/opa/ast"
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416

@@ -208,3 +210,40 @@ deny {
208210
})
209211
}
210212
}
213+
214+
func Test_FallbackErrorWithoutLocation(t *testing.T) {
215+
fsys := fstest.MapFS{
216+
"schemas/fooschema.json": {
217+
Data: []byte(`{
218+
"$schema": "http://json-schema.org/draft-07/schema#",
219+
"type": "object",
220+
"properties": {
221+
"foo": {
222+
"type": "string"
223+
}
224+
}
225+
}`),
226+
},
227+
}
228+
229+
for i := 0; i < ast.CompileErrorLimitDefault+1; i++ {
230+
src := `# METADATA
231+
# schemas:
232+
# - input: schema["fooschema"]
233+
package builtin.test%d
234+
235+
deny {
236+
input.evil == "foo bar"
237+
}`
238+
fsys[fmt.Sprintf("policies/my-check%d.rego", i)] = &fstest.MapFile{
239+
Data: []byte(fmt.Sprintf(src, i)),
240+
}
241+
}
242+
243+
scanner := rego.NewScanner(
244+
types.SourceDockerfile,
245+
options.ScannerWithEmbeddedPolicies(false),
246+
)
247+
err := scanner.LoadPolicies(false, false, fsys, []string{"."}, nil)
248+
assert.Error(t, err)
249+
}

0 commit comments

Comments
 (0)