Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(misconf): parsing numbers without fraction as int #6834

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +30 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we extract this into a function? Looks like there are other callsites like this one:

return node.Decode(&r.Inner)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resource does not store a primitive value. This conversion is needed for the parameter and property of the resource.

}

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