Skip to content

fix(deploy-on-aws): resolve AttributeError from ET.Element type annotations in diagram scripts - #273

Open
iamharshtita wants to merge 1 commit into
awslabs:mainfrom
iamharshtita:fix/defusedxml-element-type-annotations
Open

iamharshtita wants to merge 1 commit into
awslabs:mainfrom
iamharshtita:fix/defusedxml-element-type-annotations

Conversation

@iamharshtita

Copy link
Copy Markdown

Summary

defusedxml.ElementTree wraps only the parsing functions (parse, fromstring, iterparse) — it does not expose Element or ElementTree as attributes. Using ET.Element and ET.ElementTree as type annotations causes an AttributeError at import time on Python 3.10+, where annotations are evaluated eagerly. This crashes all four diagram post-processing scripts before any function is called, affecting every agent platform (Claude Code, Codex, Cursor, Kiro).

The fix imports Element and ElementTree from xml.etree.ElementTree for type annotations only. The defusedxml import is retained for all actual XML parsing calls (ET.parse, ET.fromstring, ET.iterparse) where the Bandit B314/B405 and Semgrep XXE security requirements apply. The Element and ElementTree classes are not parsers and carry no XXE risk.

Related

Changes

  • scripts/lib/fix_step_badges.py — add from xml.etree.ElementTree import Element, ElementTree, replace all ET.ElementElement and ET.ElementTreeElementTree
  • scripts/lib/fix_icon_colors.py — same fix
  • scripts/lib/fix_nesting.py — same fix
  • scripts/lib/post_process_drawio.py — same fix

validate_drawio.py and drawio_url.py are unaffected — they use only ET.parse, ET.fromstring, ET.ParseError, and ET.iterparse which are all valid defusedxml exports.

All 4 scripts import cleanly after the fix, fix_step_badges.py runs successfully against example diagrams, and all 125 existing tests pass.

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

…ations

defusedxml.ElementTree does not expose Element or ElementTree — it wraps
only the parsing functions (parse, fromstring, iterparse) for security.
Using ET.Element as a type annotation causes an AttributeError at import
time on Python 3.10+ where annotations are evaluated eagerly, crashing
all diagram post-processing scripts before any function is called.

This affects all agent platforms (Claude Code, Codex, Cursor, Kiro) since
the crash is in the Python scripts themselves, not the agent runtime.

Fix: import Element and ElementTree from xml.etree.ElementTree for type
annotations only. The defusedxml import is retained for all actual XML
parsing calls (ET.parse, ET.fromstring, ET.iterparse) which is where the
Bandit B314/B405 and Semgrep XXE security requirements apply. The Element
and ElementTree classes carry no parsing security risk.

Affected scripts:
- scripts/lib/fix_step_badges.py
- scripts/lib/fix_icon_colors.py
- scripts/lib/fix_nesting.py
- scripts/lib/post_process_drawio.py

Validated: all 4 scripts import cleanly, fix_step_badges runs against
example diagrams, and all 125 existing tests pass.

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of the
project license.
@iamharshtita
iamharshtita requested review from a team as code owners September 11, 2026 22:01
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