Skip to content

Commit

Permalink
Add input validation to getRuleInfo to prevent panic (#14501)
Browse files Browse the repository at this point in the history
* return error from getRuleInfo if rule contains empty slice to prevent panic

* add changelog entry
  • Loading branch information
ccapurso committed Mar 24, 2022
1 parent deb38e0 commit 6c396c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/14501.txt
@@ -0,0 +1,3 @@
```release-note:bug
core: Fix panic caused by parsing policies with empty slice values.
```
5 changes: 5 additions & 0 deletions helper/random/parser.go
Expand Up @@ -126,6 +126,11 @@ func getRuleInfo(rule map[string]interface{}) (data ruleInfo, err error) {
if err != nil {
return data, fmt.Errorf("unable to get rule data: %w", err)
}

if len(slice) == 0 {
return data, fmt.Errorf("rule info cannot be empty")
}

data = ruleInfo{
ruleType: key,
data: slice[0],
Expand Down
9 changes: 9 additions & 0 deletions helper/random/parser_test.go
Expand Up @@ -297,6 +297,15 @@ func TestParser_ParsePolicy(t *testing.T) {
expected: StringGenerator{},
expectErr: true,
},
"config value with empty slice": {
registry: defaultRuleNameMapping,
rawConfig: `
rule {
n = []
}`,
expected: StringGenerator{},
expectErr: true,
},
}

for name, test := range tests {
Expand Down

0 comments on commit 6c396c1

Please sign in to comment.