Skip to content

Validate XML boundary names#306

Merged
vinitkumar merged 4 commits into
masterfrom
fix/xml-boundary-issue
Jun 4, 2026
Merged

Validate XML boundary names#306
vinitkumar merged 4 commits into
masterfrom
fix/xml-boundary-issue

Conversation

@vinitkumar

@vinitkumar vinitkumar commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary by Sourcery

Validate XML names for custom roots and attribute keys to prevent malformed XML output and ensure consistent name normalization.

Bug Fixes:

  • Normalize invalid custom root element names using the existing XML-name rules before emitting raw XML bytes.
  • Reject invalid XML attribute names for custom attributes so malformed XML is not produced.

Documentation:

  • Document expected behavior for custom root name normalization and invalid custom attribute rejection in the test behavior guide.

Tests:

  • Add tests covering normalization of invalid custom root names and explicit failure on invalid custom attribute names.

@sourcery-ai

sourcery-ai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Validates XML attribute and root element names to prevent malformed XML, and aligns custom root handling with existing XML name normalization, with tests and LAT docs updated accordingly.

Flow diagram for XML attribute name validation in make_attrstring

flowchart TD
  A[make_attrstring receives attr dict] --> B{attr is empty?}
  B -- yes --> C[Return empty string]
  B -- no --> D[validate_xml_attr_names]
  D --> E{Iterate keys in attr}
  E --> F[key_is_valid_xml_attr]
  F --> G{minidom parseString succeeds?}
  G -- no --> H[Raise ValueError Invalid XML attribute name]
  G -- yes --> I[All keys valid]
  I --> J[Build attribute string and return]
Loading

Flow diagram for custom_root XML name normalization in dicttoxml

flowchart TD
  A[dicttoxml called with custom_root] --> B{root is True?}
  B -- no --> C[Call convert with parent empty]
  B -- yes --> D[Append XML declaration]
  D --> E[make_valid_xml_name with custom_root and empty attrs]
  E --> F[Receive normalized custom_root and root_attr]
  F --> G[Call convert with parent normalized custom_root]
  G --> H["Build root start tag with make_attrstring(root_attr) and namespace_str"]
  H --> I[Append closing tag using normalized custom_root]
Loading

File-Level Changes

Change Details Files
Validate XML attribute names before emitting them and reuse existing XML-name normalization for custom root names.
  • Call a new XML attribute-name validation helper at the start of attribute string rendering so invalid attribute keys raise instead of producing malformed XML.
  • Introduce key_is_valid_xml_attr using defusedxml.minidom plus an LRU cache to cheaply test whether attribute names are syntactically valid in XML.
  • Add validate_xml_attr_names to iterate over @attrs keys and reject any invalid XML attribute names with a ValueError.
  • Normalize custom_root via make_valid_xml_name so it follows the same XML name rules as object keys before building the root tag, and plumb any returned attributes through make_attrstring in the root element output.
json2xml/dicttoxml.py
Extend tests and LAT documentation to pin new behavior for custom root normalization and invalid attributes.
  • Add a test ensuring invalid custom_root values are normalized using existing XML-name rules before raw XML bytes are produced, preventing malformed root tags.
  • Add a test asserting that invalid @attrs keys now raise ValueError instead of emitting malformed XML attributes.
  • Update LAT test documentation to describe custom root name normalization and explicit rejection of invalid custom attributes.
tests/test_dict2xml.py
lat.md/tests.md

Possibly linked issues

  • #0: PR implements part of the issue: normalizing invalid custom root names and rejecting invalid @attrs XML attribute names.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f9c2bcf) to head (e50a5eb).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #306   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          552       570   +18     
=========================================
+ Hits           552       570   +18     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/test_dict2xml.py" line_range="511-519" />
<code_context>
+            b"<custom_root><key>value</key></custom_root>"
+        )
+
+    # @lat: [[tests#Conversion behavior#Invalid custom attributes are rejected]]
+    def test_dicttoxml_rejects_invalid_custom_attribute_names(self) -> None:
+        """Invalid custom attribute names should fail before dicttoxml returns malformed XML bytes."""
+        with pytest.raises(ValueError, match="Invalid XML attribute name"):
+            dicttoxml.dicttoxml(
+                {"key": {"@attrs": {"bad attr": "value"}, "@val": "payload"}},
+                root=False,
+                attr_type=False,
+            )
+
</code_context>
<issue_to_address>
**suggestion (testing):** Consider broadening attribute-name tests to cover multiple invalid and valid edge-case names

The new `test_dicttoxml_rejects_invalid_custom_attribute_names` currently exercises only one invalid attribute (`"bad attr"`). To strengthen coverage of `key_is_valid_xml_attr` / `validate_xml_attr_names`, please parameterize this test (or add more) to include:

- Several invalid names (e.g. `""`, `"1foo"`, `"foo>bar"`, something with a newline or quote).
- A few borderline valid names (e.g. `"a_b"`, `"a-b"`, `"xmlAttr"`) to confirm the validator isn’t over-restrictive.

This will better cover edge-case attribute names and the XML-based validation behavior.

```suggestion
    # @lat: [[tests#Conversion behavior#Invalid custom attributes are rejected]]
    @pytest.mark.parametrize(
        "attr_name",
        [
            "bad attr",   # space
            "",           # empty
            "1foo",       # starts with digit
            "foo>bar",    # invalid character
            "foo\nbar",   # newline
            'foo"bar',    # quote
        ],
    )
    def test_dicttoxml_rejects_invalid_custom_attribute_names(self, attr_name: str) -> None:
        """Invalid custom attribute names should fail before dicttoxml returns malformed XML bytes."""
        with pytest.raises(ValueError, match="Invalid XML attribute name"):
            dicttoxml.dicttoxml(
                {"key": {"@attrs": {attr_name: "value"}, "@val": "payload"}},
                root=False,
                attr_type=False,
            )

    @pytest.mark.parametrize(
        "attr_name",
        [
            "a_b",      # underscore
            "a-b",      # hyphen
            "xmlAttr",  # mixed case, xml-prefixed but still syntactically valid
        ],
    )
    def test_dicttoxml_accepts_valid_custom_attribute_names(self, attr_name: str) -> None:
        """Borderline-but-valid attribute names should be accepted by the XML attribute validator."""
        result = dicttoxml.dicttoxml(
            {"key": {"@attrs": {attr_name: "value"}, "@val": "payload"}},
            root=False,
            attr_type=False,
        )
        # We don't assert exact XML shape here; reaching this point means no ValueError was raised.
        assert isinstance(result, bytes)
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tests/test_dict2xml.py
@vinitkumar vinitkumar merged commit 98d299d into master Jun 4, 2026
48 checks passed
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