mirror of
https://github.com/stleary/JSON-java.git
synced 2026-04-03 00:03:18 -04:00
Merge pull request #1046 from yuki-matsuhashi/master
Validate XML numeric character references before string construction
This commit is contained in:
@@ -158,7 +158,7 @@ public class XML {
|
|||||||
* @param cp code point to test
|
* @param cp code point to test
|
||||||
* @return true if the code point is not valid for an XML
|
* @return true if the code point is not valid for an XML
|
||||||
*/
|
*/
|
||||||
private static boolean mustEscape(int cp) {
|
static boolean mustEscape(int cp) {
|
||||||
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets
|
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets
|
||||||
*
|
*
|
||||||
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||||
|
|||||||
@@ -167,6 +167,9 @@ public class XMLTokener extends JSONTokener {
|
|||||||
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
||||||
? parseHexEntity(e)
|
? parseHexEntity(e)
|
||||||
: parseDecimalEntity(e);
|
: parseDecimalEntity(e);
|
||||||
|
if (XML.mustEscape(cp)) {
|
||||||
|
throw new JSONException("Invalid numeric character reference: &#" + e.substring(1) + ";");
|
||||||
|
}
|
||||||
return new String(new int[] {cp}, 0, 1);
|
return new String(new int[] {cp}, 0, 1);
|
||||||
}
|
}
|
||||||
Character knownEntity = entity.get(e);
|
Character knownEntity = entity.get(e);
|
||||||
|
|||||||
@@ -1468,6 +1468,42 @@ public class XMLTest {
|
|||||||
XML.toJSONObject(xmlStr);
|
XML.toJSONObject(xmlStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that out-of-range hex entities throw JSONException rather than an uncaught runtime exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void testOutOfRangeHexEntityThrowsJSONException() {
|
||||||
|
String xmlStr = "<a>�</a>";
|
||||||
|
XML.toJSONObject(xmlStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that out-of-range decimal entities throw JSONException rather than an uncaught runtime exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void testOutOfRangeDecimalEntityThrowsJSONException() {
|
||||||
|
String xmlStr = "<a>�</a>";
|
||||||
|
XML.toJSONObject(xmlStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that surrogate code point entities throw JSONException.
|
||||||
|
*/
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void testSurrogateHexEntityThrowsJSONException() {
|
||||||
|
String xmlStr = "<a>�</a>";
|
||||||
|
XML.toJSONObject(xmlStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that out-of-range numeric entities in attribute values throw JSONException.
|
||||||
|
*/
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void testOutOfRangeHexEntityInAttributeThrowsJSONException() {
|
||||||
|
String xmlStr = "<a b=\"�\"/>";
|
||||||
|
XML.toJSONObject(xmlStr);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that valid decimal numeric entity A works correctly.
|
* Tests that valid decimal numeric entity A works correctly.
|
||||||
* Should decode to character 'A'.
|
* Should decode to character 'A'.
|
||||||
|
|||||||
Reference in New Issue
Block a user