#1071: narrow tag-name check to XML metachars only
Per review on #1072: reject only < > & " ' / in element names to close the CWE-91 injection vector, and drop the full XML 1.0 Name validation to preserve backwards compatibility for callers that emit non-well-formed but non-injecting tag names. Co-Authored-By: Claude <[email protected]> Claude-Session: https://claude.ai/code/session_01MTGnYg5v1QxaqqfDKHKTVr
This commit is contained in:
@@ -233,80 +233,27 @@ public class XML {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throw an exception if the string is not a valid XML 1.0 {@code Name}
|
* Throw an exception if the string contains an XML metacharacter
|
||||||
* (element name). Used by {@link #toString(Object)} to reject JSON keys that
|
* ({@code < > & " ' /}). Used by {@link #toString(Object)} to reject JSON
|
||||||
* would otherwise be emitted verbatim between {@code <} and {@code >} and
|
* keys that would otherwise be emitted verbatim between {@code <} and
|
||||||
* could break out of the tag context (element injection, CWE-91;
|
* {@code >} and could break out of the tag context (element injection,
|
||||||
* see issue #1071 and #294).
|
* CWE-91; see issue #1071).
|
||||||
*
|
*
|
||||||
* @param string the candidate element name
|
* @param string the candidate element name
|
||||||
* @throws JSONException if {@code string} is null, empty, or contains a
|
* @throws JSONException if {@code string} contains an XML metacharacter
|
||||||
* character outside the XML 1.0 Name production
|
|
||||||
*/
|
*/
|
||||||
static void mustBeXmlName(String string) throws JSONException {
|
static void noXmlMetachars(String string) throws JSONException {
|
||||||
if (string == null || string.isEmpty()) {
|
int length = string.length();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
char c = string.charAt(i);
|
||||||
|
if (c == '<' || c == '>' || c == '&'
|
||||||
|
|| c == '"' || c == '\'' || c == '/') {
|
||||||
throw new JSONException("'" + string
|
throw new JSONException("'" + string
|
||||||
+ "' is not a valid XML element name.");
|
+ "' contains an XML metacharacter and may not be used as an element name.");
|
||||||
}
|
|
||||||
int cp = string.codePointAt(0);
|
|
||||||
if (!isXmlNameStart(cp)) {
|
|
||||||
throw new JSONException("'" + string
|
|
||||||
+ "' is not a valid XML element name.");
|
|
||||||
}
|
|
||||||
for (int i = Character.charCount(cp); i < string.length(); i += Character.charCount(cp)) {
|
|
||||||
cp = string.codePointAt(i);
|
|
||||||
if (!isXmlNameChar(cp)) {
|
|
||||||
throw new JSONException("'" + string
|
|
||||||
+ "' is not a valid XML element name.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean inRange(int cp, int lo, int hi) {
|
|
||||||
return cp >= lo && cp <= hi;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* XML 1.0 (5th ed.) {@code NameStartChar} production.
|
|
||||||
*
|
|
||||||
* @param cp a Unicode code point
|
|
||||||
* @return true if {@code cp} may start an XML Name
|
|
||||||
*/
|
|
||||||
private static boolean isXmlNameStart(int cp) {
|
|
||||||
return cp == ':'
|
|
||||||
|| cp == '_'
|
|
||||||
|| inRange(cp, 'A', 'Z')
|
|
||||||
|| inRange(cp, 'a', 'z')
|
|
||||||
|| inRange(cp, 0xC0, 0xD6)
|
|
||||||
|| inRange(cp, 0xD8, 0xF6)
|
|
||||||
|| inRange(cp, 0xF8, 0x2FF)
|
|
||||||
|| inRange(cp, 0x370, 0x37D)
|
|
||||||
|| inRange(cp, 0x37F, 0x1FFF)
|
|
||||||
|| inRange(cp, 0x200C, 0x200D)
|
|
||||||
|| inRange(cp, 0x2070, 0x218F)
|
|
||||||
|| inRange(cp, 0x2C00, 0x2FEF)
|
|
||||||
|| inRange(cp, 0x3001, 0xD7FF)
|
|
||||||
|| inRange(cp, 0xF900, 0xFDCF)
|
|
||||||
|| inRange(cp, 0xFDF0, 0xFFFD)
|
|
||||||
|| inRange(cp, 0x10000, 0xEFFFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* XML 1.0 (5th ed.) {@code NameChar} production.
|
|
||||||
*
|
|
||||||
* @param cp a Unicode code point
|
|
||||||
* @return true if {@code cp} may appear after the first character of an XML Name
|
|
||||||
*/
|
|
||||||
private static boolean isXmlNameChar(int cp) {
|
|
||||||
return isXmlNameStart(cp)
|
|
||||||
|| cp == '-'
|
|
||||||
|| cp == '.'
|
|
||||||
|| cp == 0xB7
|
|
||||||
|| inRange(cp, '0', '9')
|
|
||||||
|| inRange(cp, 0x0300, 0x036F)
|
|
||||||
|| inRange(cp, 0x203F, 0x2040);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan the content following the named tag, attaching it to the context.
|
* Scan the content following the named tag, attaching it to the context.
|
||||||
*
|
*
|
||||||
@@ -1044,7 +991,7 @@ public class XML {
|
|||||||
String string;
|
String string;
|
||||||
|
|
||||||
if (tagName != null) {
|
if (tagName != null) {
|
||||||
mustBeXmlName(tagName);
|
noXmlMetachars(tagName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
@@ -1066,7 +1013,7 @@ public class XML {
|
|||||||
jo = (JSONObject) object;
|
jo = (JSONObject) object;
|
||||||
for (final String key : jo.keySet()) {
|
for (final String key : jo.keySet()) {
|
||||||
if (!key.equals(config.getcDataTagName())) {
|
if (!key.equals(config.getcDataTagName())) {
|
||||||
mustBeXmlName(key);
|
noXmlMetachars(key);
|
||||||
}
|
}
|
||||||
Object value = jo.opt(key);
|
Object value = jo.opt(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
|
|||||||
@@ -506,31 +506,32 @@ public class XMLConfigurationTest {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON keys that are not valid XML 1.0 Names are rejected by
|
* Possible bug:
|
||||||
* XML.toString rather than being emitted as (malformed) tag names.
|
* Illegal node-names must be converted to legal XML-node-names.
|
||||||
* See issues #166, #294, #308 and #1071.
|
* 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 (_).
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleIllegalJSONNodeNames()
|
public void shouldHandleIllegalJSONNodeNames()
|
||||||
{
|
{
|
||||||
// Name may not start with a digit
|
JSONObject inputJSON = new JSONObject();
|
||||||
try {
|
inputJSON.append("123IllegalNode", "someValue1");
|
||||||
XML.toString(new JSONObject().append("123IllegalNode", "someValue1"),
|
inputJSON.append("Illegal@node", "someValue2");
|
||||||
null, XMLParserConfiguration.KEEP_STRINGS);
|
|
||||||
fail("expected JSONException for digit-leading element name");
|
String result = XML.toString(inputJSON, null,
|
||||||
} catch (JSONException expected) {
|
XMLParserConfiguration.KEEP_STRINGS);
|
||||||
assertTrue("message names the offending key",
|
|
||||||
expected.getMessage().contains("123IllegalNode"));
|
/*
|
||||||
}
|
* This is invalid XML. Names should not begin with digits or contain
|
||||||
// Name may not contain '@'
|
* certain values, including '@'. One possible solution is to replace
|
||||||
try {
|
* illegal chars with '_', in which case the expected output would be:
|
||||||
XML.toString(new JSONObject().append("Illegal@node", "someValue2"),
|
* <___IllegalNode>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
|
||||||
null, XMLParserConfiguration.KEEP_STRINGS);
|
*/
|
||||||
fail("expected JSONException for '@' in element name");
|
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
|
||||||
} catch (JSONException expected) {
|
|
||||||
assertTrue("message names the offending key",
|
assertEquals("Length", expected.length(), result.length());
|
||||||
expected.getMessage().contains("Illegal@node"));
|
assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
|
||||||
}
|
assertTrue("Illegal@node", result.contains("<Illegal@node>someValue2</Illegal@node>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -538,29 +538,31 @@ public class XMLTest {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON keys that are not valid XML 1.0 Names are rejected by
|
* Possible bug:
|
||||||
* XML.toString rather than being emitted as (malformed) tag names.
|
* Illegal node-names must be converted to legal XML-node-names.
|
||||||
* See issues #166, #294, #308 and #1071.
|
* 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 (_).
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleIllegalJSONNodeNames()
|
public void shouldHandleIllegalJSONNodeNames()
|
||||||
{
|
{
|
||||||
// Name may not start with a digit
|
JSONObject inputJSON = new JSONObject();
|
||||||
try {
|
inputJSON.append("123IllegalNode", "someValue1");
|
||||||
XML.toString(new JSONObject().append("123IllegalNode", "someValue1"));
|
inputJSON.append("Illegal@node", "someValue2");
|
||||||
fail("expected JSONException for digit-leading element name");
|
|
||||||
} catch (JSONException expected) {
|
String result = XML.toString(inputJSON);
|
||||||
assertTrue("message names the offending key",
|
|
||||||
expected.getMessage().contains("123IllegalNode"));
|
/*
|
||||||
}
|
* This is invalid XML. Names should not begin with digits or contain
|
||||||
// Name may not contain '@'
|
* certain values, including '@'. One possible solution is to replace
|
||||||
try {
|
* illegal chars with '_', in which case the expected output would be:
|
||||||
XML.toString(new JSONObject().append("Illegal@node", "someValue2"));
|
* <___IllegalNode>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
|
||||||
fail("expected JSONException for '@' in element name");
|
*/
|
||||||
} catch (JSONException expected) {
|
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
|
||||||
assertTrue("message names the offending key",
|
|
||||||
expected.getMessage().contains("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>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -577,36 +579,26 @@ public class XMLTest {
|
|||||||
XML.toString(jo, "root");
|
XML.toString(jo, "root");
|
||||||
fail("expected JSONException for key containing XML metacharacters");
|
fail("expected JSONException for key containing XML metacharacters");
|
||||||
} catch (JSONException expected) {
|
} catch (JSONException expected) {
|
||||||
// expected: '/', '<', '>' are outside the XML Name production
|
// expected: '/', '<', '>' are rejected in element names
|
||||||
}
|
}
|
||||||
|
|
||||||
// caller-supplied tagName is validated too
|
// caller-supplied tagName is checked too
|
||||||
try {
|
try {
|
||||||
XML.toString(new JSONObject(), "bad tag");
|
XML.toString(new JSONObject(), "bad<tag");
|
||||||
fail("expected JSONException for tagName containing whitespace");
|
fail("expected JSONException for tagName containing '<'");
|
||||||
} catch (JSONException expected) {
|
} catch (JSONException expected) {
|
||||||
// expected: space is outside the XML Name production
|
// expected: '<' is rejected in element names
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// each metacharacter is rejected individually
|
||||||
* Keys that are valid XML 1.0 Names continue to serialise unchanged.
|
for (char c : new char[] {'<', '>', '&', '"', '\'', '/'}) {
|
||||||
*/
|
try {
|
||||||
@Test
|
XML.toString(new JSONObject().put("a" + c + "b", "v"));
|
||||||
public void toStringAcceptsValidXmlNames()
|
fail("expected JSONException for key containing '" + c + "'");
|
||||||
{
|
} catch (JSONException expected) {
|
||||||
JSONObject jo = new JSONObject();
|
// expected
|
||||||
jo.put("simple", "a");
|
}
|
||||||
jo.put("with-hyphen.dot_underscore", "b");
|
}
|
||||||
jo.put("ns:qualified", "c");
|
|
||||||
jo.put("élément", "d"); // Latin-1 letters
|
|
||||||
jo.put("content", "e"); // cDataTagName sentinel, emitted as text
|
|
||||||
String xml = XML.toString(jo, "root");
|
|
||||||
assertTrue(xml.contains("<simple>a</simple>"));
|
|
||||||
assertTrue(xml.contains("<with-hyphen.dot_underscore>b</with-hyphen.dot_underscore>"));
|
|
||||||
assertTrue(xml.contains("<ns:qualified>c</ns:qualified>"));
|
|
||||||
assertTrue(xml.contains("<élément>d</élément>"));
|
|
||||||
assertTrue("cDataTagName still emitted as text content", xml.contains(">e<"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user