Skip to content

Commit 1dfdb40

Browse files
authored
Validate if a map is actually a map (#2669)
1 parent 5b2d325 commit 1dfdb40

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/cfnlint/rules/resources/properties/ValuePrimitiveType.py

+26
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ def check_value(self, value, path, **kwargs):
192192

193193
return matches
194194

195+
def _check_map(self, m, path):
196+
matches = []
197+
if isinstance(m, dict):
198+
if len(m) == 1:
199+
for k, v in m.items():
200+
if k == "Fn::If":
201+
if isinstance(v, list) and len(v) == 3:
202+
matches.extend(
203+
self._check_map(v[1], path[:] + ["Fn::If", 1])
204+
)
205+
matches.extend(
206+
self._check_map(v[2], path[:] + ["Fn::If", 2])
207+
)
208+
else:
209+
matches.append(
210+
RuleMatch(
211+
path,
212+
"Map must be an object of key-value pairs",
213+
)
214+
)
215+
return matches
216+
195217
def check(self, cfn, properties, specs, spec_type, path):
196218
"""Check itself"""
197219
matches = []
@@ -201,6 +223,10 @@ def check(self, cfn, properties, specs, spec_type, path):
201223
primitive_type = specs.get(prop).get("PrimitiveType")
202224
if not primitive_type:
203225
primitive_type = specs.get(prop).get("PrimitiveItemType")
226+
if specs.get(prop).get("Type") == "Map":
227+
matches.extend(
228+
self._check_map(properties.get(prop), path[:] + [prop])
229+
)
204230
if specs.get(prop).get("Type") in ["List", "Map"]:
205231
item_type = specs.get(prop).get("Type")
206232
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Conditions:
3+
IsUsEast1: !Equals [!Ref AWS::Region, "us-east-1"]
4+
Resources:
5+
ExampleLambda:
6+
Properties:
7+
Code:
8+
ZipFile: |
9+
exports.handler = async (event) => {
10+
console.log('Hello World');
11+
return 'Hello World';
12+
};
13+
Environment:
14+
Variables:
15+
- Key: A
16+
Value: B
17+
- C: d
18+
Handler: src/index.handler
19+
Runtime: nodejs18.x
20+
Role: arn:aws:iam::123456789012:role/MyRole
21+
Type: AWS::Lambda::Function
22+
ExampleLambda1:
23+
Properties:
24+
Code:
25+
ZipFile: |
26+
exports.handler = async (event) => {
27+
console.log('Hello World');
28+
return 'Hello World';
29+
};
30+
Environment:
31+
Variables:
32+
Fn::If:
33+
- IsUsEast1
34+
- - Key: A
35+
Value: B
36+
- C: d
37+
- !Ref AWS::NoValue
38+
Handler: src/index.handler
39+
Runtime: nodejs18.x
40+
Role: arn:aws:iam::123456789012:role/MyRole
41+
Type: AWS::Lambda::Function

test/unit/rules/resources/properties/test_value_primitive_type.py

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def test_template_config(self):
4646
4,
4747
)
4848

49+
def test_bad_map_values(self):
50+
"""Test strict false"""
51+
self.helper_file_negative(
52+
"test/fixtures/templates/bad/resources/properties/primitive_types_map.yaml",
53+
2,
54+
)
55+
4956
def test_file_negative_nist_high_main(self):
5057
"""Generic Test failure"""
5158
self.helper_file_rule_config(

0 commit comments

Comments
 (0)