fix(cloudformation): handle TypeError in _set_in_dict when path inval…#7511
Open
SrivatsavG wants to merge 2 commits into
Open
fix(cloudformation): handle TypeError in _set_in_dict when path inval…#7511SrivatsavG wants to merge 2 commits into
SrivatsavG wants to merge 2 commits into
Conversation
…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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title:
fix(cloudformation): handle TypeError in _set_in_dict when path invalidated by prior ref substitution
Description:
Description
Fix
TypeError: 'int' object is not subscriptablecrash when scanning CloudFormation templates with nestedFn::FindInMapandRefparameters that haveNumber-typed defaults.Root Cause
evaluate_default_refs()collects allRefpaths upfront viasearch_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 phantomRefas a sibling key toFn::FindInMapin the same dict, two Ref paths share a common ancestor. If the first substitution replaces that ancestor dict with a scalar (e.g.,1024from aNumber-typed parameter default), the second path becomes invalid —_get_from_dicttries to index into an integer and raisesTypeError.Fix
Wrap the
_get_from_dictcall in_set_in_dictwith aTypeErrorcatch. 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: AddTypeErrorhandling in_set_in_dicttests/cloudformation/parser/test_cfn_yaml.py: Add regression testtests/cloudformation/parser/cfn_nested_findinmap_ref.yaml: Test fixtureTesting