Merge pull request #1093 from Lyin-OTTF/fix/xml-serialize-escape-whitespace
Fix TAB/LF/CR escape when serializing to XML
This commit is contained in:
@@ -156,34 +156,45 @@ public class XML {
|
||||
|
||||
/**
|
||||
* @param cp code point to test
|
||||
* @return true if the code point is not valid for an XML
|
||||
* @return true if the code point must be escaped when serializing to XML
|
||||
*/
|
||||
static boolean mustEscape(int cp) {
|
||||
// Every ISO control character must be written as a numeric character
|
||||
// reference when serializing. This includes #x9 (tab), #xA (LF) and
|
||||
// #xD (CR): literal occurrences of these characters are subject to
|
||||
// whitespace normalization by conforming XML parsers, so they can
|
||||
// only survive a round trip as 	, and (see #1092).
|
||||
//
|
||||
// This method is only about the serialization path. Numeric character
|
||||
// references found while parsing are validated with
|
||||
// isValidXmlCharacter(int) (see #1059).
|
||||
return Character.isISOControl(cp) || !isValidXmlCharacter(cp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cp code point to test
|
||||
* @return true if the code point can be written as a numeric character
|
||||
* reference in a document generated by this class
|
||||
*/
|
||||
static boolean isValidXmlCharacter(int cp) {
|
||||
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets
|
||||
*
|
||||
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||
*
|
||||
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
|
||||
*/
|
||||
// isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F)
|
||||
// all ISO control characters are out of range except tabs and new lines
|
||||
return (Character.isISOControl(cp)
|
||||
&& cp != 0x9
|
||||
&& cp != 0xA
|
||||
&& cp != 0xD
|
||||
) || !(
|
||||
// Valid character range per W3C XML 1.0 spec:
|
||||
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||
// Previously omitted #x9/#xA/#xD, causing unescape(" ") etc.
|
||||
// to reject valid LF/TAB/CR as illegal (see #1059)
|
||||
cp == 0x9
|
||||
|| cp == 0xA
|
||||
|| cp == 0xD
|
||||
|| (cp >= 0x20 && cp <= 0xD7FF)
|
||||
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
||||
|| (cp >= 0x10000 && cp <= 0x10FFFF)
|
||||
)
|
||||
;
|
||||
// The C1 control characters (#x7F-#x9F) fall inside [#x20-#xD7FF] and
|
||||
// are therefore legal XML 1.0 characters, but this class has always
|
||||
// rejected &#NN; references to them while parsing. Keeping them
|
||||
// rejected here keeps the parse result of this fix identical to the
|
||||
// previous release; accepting them would be a separate behaviour
|
||||
// change (see #1093).
|
||||
if (Character.isISOControl(cp)) {
|
||||
return cp == 0x9 || cp == 0xA || cp == 0xD;
|
||||
}
|
||||
return (cp >= 0x20 && cp <= 0xD7FF)
|
||||
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
||||
|| (cp >= 0x10000 && cp <= 0x10FFFF);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -167,7 +167,11 @@ public class XMLTokener extends JSONTokener {
|
||||
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
||||
? parseHexEntity(e)
|
||||
: parseDecimalEntity(e);
|
||||
if (XML.mustEscape(cp)) {
|
||||
// Validate with isValidXmlCharacter, not with XML.mustEscape:
|
||||
// mustEscape also escapes TAB/LF/CR for the serialization path
|
||||
// (#1092), while the numeric references 	, and are
|
||||
// valid XML and must keep being accepted here (see #1059).
|
||||
if (!XML.isValidXmlCharacter(cp)) {
|
||||
throw new JSONException("Invalid numeric character reference: &#" + e.substring(1) + ";");
|
||||
}
|
||||
return new String(new int[] {cp}, 0, 1);
|
||||
|
||||
Reference in New Issue
Block a user