Skip to content

fix(cloudformation): handle TypeError in _set_in_dict when path inval…#7511

Open
SrivatsavG wants to merge 2 commits into
bridgecrewio:mainfrom
SrivatsavG:fix/cfn-set-in-dict-typeerror
Open

fix(cloudformation): handle TypeError in _set_in_dict when path inval…#7511
SrivatsavG wants to merge 2 commits into
bridgecrewio:mainfrom
SrivatsavG:fix/cfn-set-in-dict-typeerror

Conversation

@SrivatsavG
Copy link
Copy Markdown

Title:
fix(cloudformation): handle TypeError in _set_in_dict when path invalidated by prior ref substitution

Description:

Description

Fix TypeError: 'int' object is not subscriptable crash when scanning CloudFormation templates with nested Fn::FindInMap and Ref parameters that have Number-typed defaults.

Root Cause

evaluate_default_refs() collects all Ref paths upfront via search_deep_keys, then iterates and substitutes each with the parameter's default value by mutating the template in-place. When graph conversion (convert_graph_vertices_to_definitions) injects a phantom Ref as a sibling key to Fn::FindInMap in the same dict, two Ref paths share a common ancestor. If the first substitution replaces that ancestor dict with a scalar (e.g., 1024 from a Number-typed parameter default), the second path becomes invalid — _get_from_dict tries to index into an integer and raises TypeError.

Fix

Wrap the _get_from_dict call in _set_in_dict with a TypeError catch. If a previous substitution has replaced a dict with a scalar, the current substitution is skipped. The skipped substitution is harmless — the target path was already destroyed.

Changes

  • checkov/cloudformation/context_parser.py: Add TypeError handling in _set_in_dict
  • tests/cloudformation/parser/test_cfn_yaml.py: Add regression test
  • tests/cloudformation/parser/cfn_nested_findinmap_ref.yaml: Test fixture

Testing

  • New unit test reproduces the exact crash (fails without fix, passes with fix)
  • All existing CloudFormation parser tests pass

…idated by prior ref substitution

evaluate_default_refs() collects all Ref paths upfront via search_deep_keys,
then iterates and substitutes each with the parameter's default value by
mutating the template in-place. When a substitution replaces a dict with a
scalar (e.g., a Number-typed parameter default like 1024), subsequent paths
that traverse through that dict become invalid, causing TypeError: 'int'
object is not subscriptable.

This happens when graph conversion (convert_graph_vertices_to_definitions)
injects a phantom Ref into a node that already contains a nested Ref inside
Fn::FindInMap, creating two Ref paths through the same dict.

The fix catches TypeError in _set_in_dict and skips the substitution. The
skipped substitution is harmless -- the target path was already destroyed
by the earlier substitution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant