Skip to content

Fix opaque AttributeError on malformed CloudFormation templates (#2307)#10271

Open
SAY-5 wants to merge 1 commit intoaws:developfrom
SAY-5:say5/artifact-exporter-malformed-cfn-2307
Open

Fix opaque AttributeError on malformed CloudFormation templates (#2307)#10271
SAY-5 wants to merge 1 commit intoaws:developfrom
SAY-5:say5/artifact-exporter-malformed-cfn-2307

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented May 3, 2026

Fixes #2307.

When a CloudFormation template is malformed (typically inconsistent indentation that causes a section or resource body to parse as null), aws cloudformation package crashed with messages like 'NoneType' object has no attribute 'items' and 'NoneType' object has no attribute 'get' that gave the user no clue where the problem was.

This change adds a new InvalidTemplateError and guards every place in artifact_exporter.Template that previously dereferenced a template section or resource body without first checking it. Eight surfaces are covered:

  • top-level template body (empty file -> yaml_parse returns None)
  • Metadata present but null, or not a mapping
  • Resources missing, present but null, or not a mapping
  • the resource-group passed to export_resources (covers Fn::ForEach:: payloads too)
  • individual resource bodies that are null or non-mapping (the case other commenters on the issue hit: 'NoneType' object has no attribute 'get')
  • Fn::Transform handler called with a non-dict fragment
  • the handler's Parameters sub-section being null

The result: the exact malformed YAML from the original issue now produces

Cannot parse CloudFormation template: "Resources" section is empty; check that resource entries are indented under "Resources:". The template may be malformed (for example, inconsistent indentation or a missing value).

Nine regression tests are added covering the empty-template, null-Resources, null-resource-body, wrong-shape, null-Metadata, and Fn::Transform null cases.

Relation to PR #8169

PR #8169 (open since Sep 2023) addresses the same issue but only patches one surface (the Resources.items() call) and raises a generic ValueError. This PR is broader: it covers all eight surfaces I could find, raises a CFN-specific exception that fits the existing exceptions.py hierarchy, and adds tests for each surface. I have not closed #8169 - happy to leave that decision to maintainers.

Test plan

  • python -m pytest tests/unit/customizations/cloudformation/test_artifact_exporter.py - 86 passed (10 new, 76 pre-existing)
  • Manual repro of the original issue's malformed YAML now produces the new clear error
  • CI on this PR

The cloudformation package command crashed with errors like
'NoneType object has no attribute items' or '... has no attribute get'
when a template was malformed (typically inconsistent indentation that
caused a section or resource body to parse as null), instead of telling
the user where the problem was. Issue aws#2307.

Add an InvalidTemplateError that carries a clear message, and guard
every site in artifact_exporter.Template that previously dereferenced a
section or resource body without checking it. Eight surfaces are now
covered: top-level template body, Metadata, Resources, individual
resource bodies, the Resources mapping shape, the resource-group shape
in export_resources, the Fn::Transform handler, and its Parameters
sub-section.

Add nine regression tests covering the empty-template, null-Resources,
null-resource-body, wrong-shape, and Fn::Transform null cases.

Fixes aws#2307

Signed-off-by: saiasish <saiasish.cnp@gmail.com>
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.

AttributeError: 'NoneType' object has no attribute 'items' on malformed CloudFormation

1 participant