handle when hcl parser panics

Apparently `\` followed by a digit number can cause hcl parser to panic!
Will fix in hcl library, but using a hammer to squash any similar issue
here.
This commit is contained in:
Mahmood Ali
2020-08-24 20:35:58 -04:00
parent 3dcf60a61e
commit 1479f353c0
2 changed files with 26 additions and 1 deletions

View File

@@ -238,7 +238,7 @@ func Parse(rules string) (*Policy, error) {
}
// Attempt to parse
if err := hcl.Decode(p, rules); err != nil {
if err := hclDecode(p, rules); err != nil {
return nil, fmt.Errorf("Failed to parse ACL Policy: %v", err)
}
@@ -312,3 +312,15 @@ func Parse(rules string) (*Policy, error) {
}
return p, nil
}
// hclDecode wraps hcl.Decode function but handles any unexpected panics
func hclDecode(p *Policy, rules string) (err error) {
defer func() {
if rerr := recover(); rerr != nil {
err = fmt.Errorf("invalid acl policy: %v", rerr)
}
}()
err = hcl.Decode(p, rules)
return err
}

View File

@@ -327,3 +327,16 @@ func TestParse(t *testing.T) {
})
}
}
func TestParse_BadInput(t *testing.T) {
inputs := []string{
`namespace "\500" {}`,
}
for i, c := range inputs {
t.Run(fmt.Sprintf("%d: %v", i, c), func(t *testing.T) {
_, err := Parse(c)
assert.Error(t, err)
})
}
}