#1071: reject invalid XML element names in XML.toString (CWE-91)

XML.toString emitted JSONObject keys verbatim as tag names, so a key
containing '<', '>' or '/' broke out of its element and injected
arbitrary sibling structure into the output. Per #294/#123 the agreed
approach is to throw on invalid input rather than mangle it.

- Add mustBeXmlName / isXmlNameStart / isXmlNameChar implementing the
  XML 1.0 (5th ed.) Name production, code-point aware.
- Validate tagName at method entry and each key at the top of the key
  loop (skipping the cDataTagName sentinel).
- Rewrite XMLTest.shouldHandleIllegalJSONNodeNames and
  XMLConfigurationTest.shouldHandleIllegalJSONNodeNames (previously
  documenting the pass-through behaviour) to assert the throw.
- Add XMLTest.toStringRejectsElementInjectionInKey covering the #1071
  payload and an invalid caller-supplied tagName.
- Add XMLTest.toStringAcceptsValidXmlNames covering hyphen/dot/
  underscore/colon, Latin-1 letters, and the cDataTagName sentinel.

Fixes #1071. Also resolves the long-standing well-formedness question
in #166 / #294 / #308.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
Mirko Swillus
2026-07-23 14:36:42 +02:00
co-authored by Claude
parent 1795e8cf26
commit e2cfb5a641
3 changed files with 159 additions and 40 deletions
@@ -506,32 +506,31 @@ public class XMLConfigurationTest {
/**
* Possible bug:
* Illegal node-names must be converted to legal XML-node-names.
* The given example shows 2 nodes which are valid for JSON, but not for XML.
* Therefore illegal arguments should be converted to e.g. an underscore (_).
* JSON keys that are not valid XML 1.0 Names are rejected by
* XML.toString rather than being emitted as (malformed) tag names.
* See issues #166, #294, #308 and #1071.
*/
@Test
public void shouldHandleIllegalJSONNodeNames()
{
JSONObject inputJSON = new JSONObject();
inputJSON.append("123IllegalNode", "someValue1");
inputJSON.append("Illegal@node", "someValue2");
String result = XML.toString(inputJSON, null,
XMLParserConfiguration.KEEP_STRINGS);
/*
* This is invalid XML. Names should not begin with digits or contain
* certain values, including '@'. One possible solution is to replace
* illegal chars with '_', in which case the expected output would be:
* <___IllegalNode>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
*/
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
assertEquals("Length", expected.length(), result.length());
assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
assertTrue("Illegal@node", result.contains("<Illegal@node>someValue2</Illegal@node>"));
// Name may not start with a digit
try {
XML.toString(new JSONObject().append("123IllegalNode", "someValue1"),
null, XMLParserConfiguration.KEEP_STRINGS);
fail("expected JSONException for digit-leading element name");
} catch (JSONException expected) {
assertTrue("message names the offending key",
expected.getMessage().contains("123IllegalNode"));
}
// Name may not contain '@'
try {
XML.toString(new JSONObject().append("Illegal@node", "someValue2"),
null, XMLParserConfiguration.KEEP_STRINGS);
fail("expected JSONException for '@' in element name");
} catch (JSONException expected) {
assertTrue("message names the offending key",
expected.getMessage().contains("Illegal@node"));
}
}
/**