`XML.unescape` rejects the character references `XML.escape` produces for control characters
还没有人认领这个 Issue。
评估
调研方向
Start by reading XML.escape and XMLTokener.unescapeEntity; the stack trace identifies XMLTokener.java as the failing entry point, and the report also names XML.toJSONObject as an affected path. Check the existing XML tests for escaping and parsing, then add coverage for round-tripping the listed control characters. Done means the escape/unescape pair agrees and XML.toJSONObject can read back the reported string value.
由索引模型根据 Issue 内容生成。
描述
This bug was found by an automated program we have running that writes tests with hegel (but I have personally reviewed it). More details at the bottom, but the short version is that I am not personally affected by this bug, and if that's not welcome then please feel free to disregard it.
Escaping a string that contains U+0001, a form feed, U+007F, U+0085, a lone surrogate or U+FFFF with XML.escape gives a numeric character reference that XML.unescape then refuses with "Invalid numeric character reference"; a tab in the same position comes back unchanged. The same pair of calls sits under XML.toString and XML.toJSONObject, so a JSONObject whose string value contains a form feed converts to XML that the library cannot read back:
import org.json.JSONObject;
import org.json.XML;
public class Main {
public static void main(String[] args) {
for (String s : new String[] {"a\tb", "a\u0001b", "a\fb", "a\u007fb", "a\u0085b", "a\ud83db", "a\uffffb"}) {
String escaped = XML.escape(s);
System.out.print("escape -> " + escaped.replace("\t", "\\t") + " unescape -> ");
try {
System.out.println(XML.unescape(escaped).equals(s) ? "same string" : "different string");
} catch (Exception e) {
System.out.println(e);
}
}
String xml = XML.toString(new JSONObject().put("msg", "page\fbreak"));
System.out.println(xml);
System.out.println(XML.toJSONObject(xml));
}
}
Output:
escape -> a\tb unescape -> same string
escape -> ab unescape -> org.json.JSONException: Invalid numeric character reference: 
escape -> ab unescape -> org.json.JSONException: Invalid numeric character reference: 
escape -> ab unescape -> org.json.JSONException: Invalid numeric character reference: 
escape -> a…b unescape -> org.json.JSONException: Invalid numeric character reference: …
escape -> a�b unescape -> org.json.JSONException: Invalid numeric character reference: �
escape -> ab unescape -> org.json.JSONException: Invalid numeric character reference: 
<msg>pagebreak</msg>
Exception in thread "main" org.json.JSONException: Invalid numeric character reference: 
at org.json.XMLTokener.unescapeEntity(XMLTokener.java:171)
at org.json.XMLTokener.nextEntity(XMLTokener.java:148)
at org.json.XMLTokener.nextContent(XMLTokener.java:117)
at org.json.XML.parse(XML.java:422)
at org.json.XML.toJSONObject(XML.java:821)
at org.json.XML.toJSONObject(XML.java:907)
at org.json.XML.toJSONObject(XML.java:706)
at Main.main(Main.java:17)
For every one of these characters XML.escape writes an &#x…; reference and XML.unescape (and so XML.toJSONObject) throws on it. Whether escape should stop producing these references or unescape should accept them, the two ends of the pair disagree, and XML.toJSONObject(XML.toString(o)) fails for any object with such a character in a string value.
Tested on org.json:json 20260814 and on current master (4f822e0).
As mentioned at the top, this was found by an automated program that writes property-based tests for various open source projects using hegel, and the report was reviewed by hand before filing. We've also potentially found (but not yet hand validated) 17 other bugs in json-java. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/java/json-java. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.
- 主要语言
- Java
- 星标
- 4.7k
- 派生
- 2.6k
- 平均合并
- 4 天 11 小时
- 30 天内合并 PR
- 7
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
stleary/JSON-java 的其他 Issue
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`可能重新可做 @XIAYM-gh 于 46 天前认领,目前没有进行中的 PR。 未关闭Fix before the next release
难度 2/5 1-3 小时 新手友好度 78/100
-
Awaiting further input from submitter No changes at this time
难度 3/5 1-2 天 新手友好度 45/100
-
Investigating
难度 5/5 一周以上 新手友好度 45/100
-
Active discussion
难度 5/5 一周以上 新手友好度 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles可能重新可做 关联的 PR 已关闭且未合并。 未关闭Active discussion
难度 4/5 3-5 天 新手友好度 56/100
查看 stleary/JSON-java 的全部 Issue
相似的 Issue
-
[Bug] dubbo.protocol.triple.max-message-size is only read from OS environment variables可能已有人在做 @CCCLL 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
hierynomus/sshj#1070 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
intellij-elixir/intellij-elixir#4492 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
quarkiverse/quarkus-openapi-generator#1796 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 66/100
quarkiverse/quarkus-jooq#324 ·
维护者通常 1 天内回复