Skip to content

Commit

Permalink
fix(misconf): parsing numbers without fraction as int (#6834)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored Jun 5, 2024
1 parent 0bcfedb commit 8141a13
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/iac/scanners/cloudformation/parser/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func getIntrinsicTag(tag string) string {
}
}

func abortIntrinsic(property *Property, msg string, components ...string) (*Property, bool) {
func abortIntrinsic(property *Property, _ string, _ ...string) (*Property, bool) {
//
return property, false
}
19 changes: 18 additions & 1 deletion pkg/iac/scanners/cloudformation/parser/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ func (p *Parameter) UnmarshalYAML(node *yaml.Node) error {
}

func (p *Parameter) UnmarshalJSONWithMetadata(node jfather.Node) error {
return node.Decode(&p.inner)

var inner parameterInner

if err := node.Decode(&inner); err != nil {
return err
}

// jfather parses Number without fraction as int64
// https://github.com/liamg/jfather/blob/4ef05d70c05af167226d3333a4ec7d8ac3c9c281/parse_number.go#L33-L42
switch v := inner.Default.(type) {
case int64:
inner.Default = int(v)
default:
inner.Default = v
}

p.inner = inner
return nil
}

func (p *Parameter) Type() cftypes.CfType {
Expand Down
47 changes: 47 additions & 0 deletions pkg/iac/scanners/cloudformation/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,52 @@ Resources:
assert.Equal(t, "testbucket", bucketNameProp.AsString())
}
}
}

func TestJsonWithNumbers(t *testing.T) {
src := `
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"SomeIntParam": {
"Type": "Number",
"Default": 1
},
"SomeFloatParam": {
"Type": "Number",
"Default": 1.1
}
},
"Resources": {
"SomeResource": {
"Type": "Test::Resource",
"Properties": {
"SomeIntProp": 1,
"SomeFloatProp": 1.1
}
}
}
}
`

fsys := testutil.CreateFS(t, map[string]string{
"main.json": src,
})

files, err := New().ParseFS(context.TODO(), fsys, ".")

require.NoError(t, err)
require.Len(t, files, 1)

file := files[0]

assert.Equal(t, 1, file.Parameters["SomeIntParam"].Default())
assert.Equal(t, 1.1, file.Parameters["SomeFloatParam"].Default())

res := file.GetResourcesByType("Test::Resource")
assert.NotNil(t, res)
assert.Len(t, res, 1)

assert.Equal(t, 1, res[0].GetProperty("SomeIntProp").AsIntValue().Value())
assert.Equal(t, 0, res[0].GetProperty("SomeFloatProp").AsIntValue().Value())
}
16 changes: 13 additions & 3 deletions pkg/iac/scanners/cloudformation/parser/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ import (
func setPropertyValueFromJson(node jfather.Node, propertyData *PropertyInner) error {

switch node.Kind() {

case jfather.KindNumber:
propertyData.Type = cftypes.Float64
return node.Decode(&propertyData.Value)
var val any
if err := node.Decode(&val); err != nil {
return err
}
switch v := val.(type) {
case float64:
propertyData.Type = cftypes.Float64
propertyData.Value = v
case int64:
propertyData.Type = cftypes.Int
propertyData.Value = int(v)
}
return nil
case jfather.KindBoolean:
propertyData.Type = cftypes.Bool
return node.Decode(&propertyData.Value)
Expand Down

0 comments on commit 8141a13

Please sign in to comment.