Skip to content

Commit

Permalink
Fixed kyverno panic at JMESPath zero division (#3137)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Sinha <abhinav@nirmata.com>

Co-authored-by: Jim Bugwadia <jim@nirmata.com>

Signed-off-by: Sambhav Kothari <sambhavs.email@gmail.com>
  • Loading branch information
zeborg authored and MarcelMue committed Jan 31, 2022
1 parent eeef398 commit 68a5231
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/engine/jmespath/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ func (op1 Scalar) Multiply(op2 interface{}) (interface{}, error) {
func (op1 Quantity) Divide(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Quantity:
if v.ToDec().AsApproximateFloat64() == 0 {
return nil, fmt.Errorf(zeroDivisionError, divide)
}
var quo inf.Dec
scale := inf.Scale(math.Max(float64(op1.AsDec().Scale()), float64(v.AsDec().Scale())))
quo.QuoRound(op1.AsDec(), v.AsDec(), scale, inf.RoundDown)
return strconv.ParseFloat(quo.String(), 64)
case Scalar:
if v.float64 == 0 {
return nil, fmt.Errorf(zeroDivisionError, divide)
}
q, err := resource.ParseQuantity(fmt.Sprintf("%v", v.float64))
if err != nil {
return nil, err
Expand Down Expand Up @@ -283,6 +289,9 @@ func (op1 Scalar) Divide(op2 interface{}) (interface{}, error) {

return op1.float64 / v.float64, nil
case Quantity:
if v.ToDec().AsApproximateFloat64() == 0 {
return nil, fmt.Errorf(zeroDivisionError, divide)
}
q, err := resource.ParseQuantity(fmt.Sprintf("%v", op1.float64))
if err != nil {
return nil, err
Expand Down
88 changes: 88 additions & 0 deletions pkg/engine/jmespath/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,50 @@ func Test_Divide(t *testing.T) {
test: "divide('12s', `0`)",
err: true,
},
{
test: "divide('12s', '0s')",
err: true,
},
{
test: "divide(`12`, '0s')",
err: true,
},
{
test: "divide('12M', '0Mi')",
err: true,
},
{
test: "divide('12K', `0`)",
err: true,
},
{
test: "divide('12K', '0m')",
err: true,
},
{
test: "divide('12Ki', '0G')",
err: true,
},
{
test: "divide('12Mi', '0Gi')",
err: true,
},
{
test: "divide('12Mi', `0`)",
err: true,
},
{
test: "divide(`12`, '0Gi')",
err: true,
},
{
test: "divide(`12`, '0K')",
err: true,
},
{
test: "divide(`12`, `0`)",
err: true,
},
{
test: "divide(`25`, `2`)",
expectedResult: 12.5,
Expand Down Expand Up @@ -890,6 +934,50 @@ func Test_Modulo(t *testing.T) {
test: "modulo('12s', `0`)",
err: true,
},
{
test: "modulo('12s', '0s')",
err: true,
},
{
test: "modulo(`12`, '0s')",
err: true,
},
{
test: "modulo('12M', '0Mi')",
err: true,
},
{
test: "modulo('12K', `0`)",
err: true,
},
{
test: "modulo('12K', '0m')",
err: true,
},
{
test: "modulo('12Ki', '0G')",
err: true,
},
{
test: "modulo('12Mi', '0Gi')",
err: true,
},
{
test: "modulo('12Mi', `0`)",
err: true,
},
{
test: "modulo(`12`, '0Gi')",
err: true,
},
{
test: "modulo(`12`, '0K')",
err: true,
},
{
test: "modulo(`12`, `0`)",
err: true,
},
{
test: "modulo(`25`, `2`)",
expectedResult: 1.0,
Expand Down

0 comments on commit 68a5231

Please sign in to comment.