-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudformation): add support for the length function
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package parser | ||
|
||
import "github.com/aquasecurity/defsec/pkg/scanners/cloudformation/cftypes" | ||
|
||
func ResolveLength(property *Property) (*Property, bool) { | ||
if !property.isFunction() { | ||
return property, true | ||
} | ||
|
||
val := property.AsMap()["Fn::Length"] | ||
if val.IsList() { | ||
return property.deriveResolved(cftypes.Int, val.Len()), true | ||
} else if val.IsMap() { | ||
resolved, _ := val.resolveValue() | ||
|
||
if resolved.IsList() { | ||
return property.deriveResolved(cftypes.Int, resolved.Len()), true | ||
} | ||
return resolved, false | ||
} | ||
|
||
return property, false | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/defsec/pkg/scanners/cloudformation/cftypes" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ResolveLength_WhenPropIsArray(t *testing.T) { | ||
prop := &Property{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::Length": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.List, | ||
Value: []*Property{ | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Int, | ||
Value: 1, | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "IntParameter", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
resolved, ok := ResolveIntrinsicFunc(prop) | ||
require.True(t, ok) | ||
require.True(t, resolved.IsInt()) | ||
require.Equal(t, 2, resolved.AsInt()) | ||
} | ||
|
||
func Test_ResolveLength_WhenPropIsIntrinsicFunction(t *testing.T) { | ||
fctx := &FileContext{ | ||
Parameters: map[string]*Parameter{ | ||
"SomeParameter": { | ||
inner: parameterInner{ | ||
Type: "string", | ||
Default: "a|b|c|d", | ||
}, | ||
}, | ||
}, | ||
} | ||
prop := &Property{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::Length": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::Split": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.List, | ||
Value: []*Property{ | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "|", | ||
}, | ||
}, | ||
{ | ||
ctx: fctx, | ||
Inner: PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Ref": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "SomeParameter", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
resolved, ok := ResolveIntrinsicFunc(prop) | ||
require.True(t, ok) | ||
require.True(t, resolved.IsInt()) | ||
require.Equal(t, 4, resolved.AsInt()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters