@@ -13,6 +13,7 @@ import (
13
13
"github.com/open-policy-agent/opa/ast"
14
14
"github.com/open-policy-agent/opa/rego"
15
15
"github.com/open-policy-agent/opa/storage"
16
+ "github.com/open-policy-agent/opa/util"
16
17
17
18
"github.com/aquasecurity/trivy/pkg/iac/debug"
18
19
"github.com/aquasecurity/trivy/pkg/iac/framework"
@@ -161,7 +162,7 @@ func (s *Scanner) SetParentDebugLogger(l debug.Logger) {
161
162
s .debug = l .Extend ("rego" )
162
163
}
163
164
164
- func (s * Scanner ) runQuery (ctx context.Context , query string , input interface {} , disableTracing bool ) (rego.ResultSet , []string , error ) {
165
+ func (s * Scanner ) runQuery (ctx context.Context , query string , input ast. Value , disableTracing bool ) (rego.ResultSet , []string , error ) {
165
166
166
167
trace := (s .traceWriter != nil || s .tracePerResult ) && ! disableTracing
167
168
@@ -180,7 +181,7 @@ func (s *Scanner) runQuery(ctx context.Context, query string, input interface{},
180
181
}
181
182
182
183
if input != nil {
183
- regoOptions = append (regoOptions , rego .Input (input ))
184
+ regoOptions = append (regoOptions , rego .ParsedInput (input ))
184
185
}
185
186
186
187
instance := rego .New (regoOptions ... )
@@ -342,6 +343,14 @@ func isPolicyApplicable(staticMetadata *StaticMetadata, inputs ...Input) bool {
342
343
return false
343
344
}
344
345
346
+ func parseRawInput (input any ) (ast.Value , error ) {
347
+ if err := util .RoundTrip (& input ); err != nil {
348
+ return nil , err
349
+ }
350
+
351
+ return ast .InterfaceToValue (input )
352
+ }
353
+
345
354
func (s * Scanner ) applyRule (ctx context.Context , namespace , rule string , inputs []Input , combined bool ) (scan.Results , error ) {
346
355
347
356
// handle combined evaluations if possible
@@ -354,7 +363,12 @@ func (s *Scanner) applyRule(ctx context.Context, namespace, rule string, inputs
354
363
qualified := fmt .Sprintf ("data.%s.%s" , namespace , rule )
355
364
for _ , input := range inputs {
356
365
s .trace ("INPUT" , input )
357
- if ignored , err := s .isIgnored (ctx , namespace , rule , input .Contents ); err != nil {
366
+ parsedInput , err := parseRawInput (input .Contents )
367
+ if err != nil {
368
+ s .debug .Log ("Error occurred while parsing input: %s" , err )
369
+ continue
370
+ }
371
+ if ignored , err := s .isIgnored (ctx , namespace , rule , parsedInput ); err != nil {
358
372
return nil , err
359
373
} else if ignored {
360
374
var result regoResult
@@ -364,7 +378,7 @@ func (s *Scanner) applyRule(ctx context.Context, namespace, rule string, inputs
364
378
results .AddIgnored (result )
365
379
continue
366
380
}
367
- set , traces , err := s .runQuery (ctx , qualified , input . Contents , false )
381
+ set , traces , err := s .runQuery (ctx , qualified , parsedInput , false )
368
382
if err != nil {
369
383
return nil , err
370
384
}
@@ -388,9 +402,15 @@ func (s *Scanner) applyRuleCombined(ctx context.Context, namespace, rule string,
388
402
if len (inputs ) == 0 {
389
403
return nil , nil
390
404
}
405
+
406
+ parsed , err := parseRawInput (inputs )
407
+ if err != nil {
408
+ return nil , fmt .Errorf ("failed to parse input: %w" , err )
409
+ }
410
+
391
411
var results scan.Results
392
- qualified := fmt . Sprintf ( "data.%s.%s" , namespace , rule )
393
- if ignored , err := s .isIgnored (ctx , namespace , rule , inputs ); err != nil {
412
+
413
+ if ignored , err := s .isIgnored (ctx , namespace , rule , parsed ); err != nil {
394
414
return nil , err
395
415
} else if ignored {
396
416
for _ , input := range inputs {
@@ -402,7 +422,8 @@ func (s *Scanner) applyRuleCombined(ctx context.Context, namespace, rule string,
402
422
}
403
423
return results , nil
404
424
}
405
- set , traces , err := s .runQuery (ctx , qualified , inputs , false )
425
+ qualified := fmt .Sprintf ("data.%s.%s" , namespace , rule )
426
+ set , traces , err := s .runQuery (ctx , qualified , parsed , false )
406
427
if err != nil {
407
428
return nil , err
408
429
}
0 commit comments