Skip to content

Commit 8141a13

Browse files
authored
fix(misconf): parsing numbers without fraction as int (#6834)
1 parent 0bcfedb commit 8141a13

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

pkg/iac/scanners/cloudformation/parser/intrinsics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func getIntrinsicTag(tag string) string {
100100
}
101101
}
102102

103-
func abortIntrinsic(property *Property, msg string, components ...string) (*Property, bool) {
103+
func abortIntrinsic(property *Property, _ string, _ ...string) (*Property, bool) {
104104
//
105105
return property, false
106106
}

pkg/iac/scanners/cloudformation/parser/parameter.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@ func (p *Parameter) UnmarshalYAML(node *yaml.Node) error {
2727
}
2828

2929
func (p *Parameter) UnmarshalJSONWithMetadata(node jfather.Node) error {
30-
return node.Decode(&p.inner)
30+
31+
var inner parameterInner
32+
33+
if err := node.Decode(&inner); err != nil {
34+
return err
35+
}
36+
37+
// jfather parses Number without fraction as int64
38+
// https://github.com/liamg/jfather/blob/4ef05d70c05af167226d3333a4ec7d8ac3c9c281/parse_number.go#L33-L42
39+
switch v := inner.Default.(type) {
40+
case int64:
41+
inner.Default = int(v)
42+
default:
43+
inner.Default = v
44+
}
45+
46+
p.inner = inner
47+
return nil
3148
}
3249

3350
func (p *Parameter) Type() cftypes.CfType {

pkg/iac/scanners/cloudformation/parser/parser_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,52 @@ Resources:
370370
assert.Equal(t, "testbucket", bucketNameProp.AsString())
371371
}
372372
}
373+
}
374+
375+
func TestJsonWithNumbers(t *testing.T) {
376+
src := `
377+
{
378+
"AWSTemplateFormatVersion": "2010-09-09",
379+
"Parameters": {
380+
"SomeIntParam": {
381+
"Type": "Number",
382+
"Default": 1
383+
},
384+
"SomeFloatParam": {
385+
"Type": "Number",
386+
"Default": 1.1
387+
}
388+
},
389+
"Resources": {
390+
"SomeResource": {
391+
"Type": "Test::Resource",
392+
"Properties": {
393+
"SomeIntProp": 1,
394+
"SomeFloatProp": 1.1
395+
}
396+
}
397+
}
398+
}
399+
`
400+
401+
fsys := testutil.CreateFS(t, map[string]string{
402+
"main.json": src,
403+
})
404+
405+
files, err := New().ParseFS(context.TODO(), fsys, ".")
406+
407+
require.NoError(t, err)
408+
require.Len(t, files, 1)
409+
410+
file := files[0]
411+
412+
assert.Equal(t, 1, file.Parameters["SomeIntParam"].Default())
413+
assert.Equal(t, 1.1, file.Parameters["SomeFloatParam"].Default())
414+
415+
res := file.GetResourcesByType("Test::Resource")
416+
assert.NotNil(t, res)
417+
assert.Len(t, res, 1)
373418

419+
assert.Equal(t, 1, res[0].GetProperty("SomeIntProp").AsIntValue().Value())
420+
assert.Equal(t, 0, res[0].GetProperty("SomeFloatProp").AsIntValue().Value())
374421
}

pkg/iac/scanners/cloudformation/parser/util.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ import (
1313
func setPropertyValueFromJson(node jfather.Node, propertyData *PropertyInner) error {
1414

1515
switch node.Kind() {
16-
1716
case jfather.KindNumber:
18-
propertyData.Type = cftypes.Float64
19-
return node.Decode(&propertyData.Value)
17+
var val any
18+
if err := node.Decode(&val); err != nil {
19+
return err
20+
}
21+
switch v := val.(type) {
22+
case float64:
23+
propertyData.Type = cftypes.Float64
24+
propertyData.Value = v
25+
case int64:
26+
propertyData.Type = cftypes.Int
27+
propertyData.Value = int(v)
28+
}
29+
return nil
2030
case jfather.KindBoolean:
2131
propertyData.Type = cftypes.Bool
2232
return node.Decode(&propertyData.Value)

0 commit comments

Comments
 (0)