`XML.unescape` rejects the character references `XML.escape` produces for control characters
まだ誰も着手していません。
評価
調査の方向性
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時間
- マージ済み PR(30日)
- 7
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
stleary/JSON-java のほかの issue
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`再び着手できるかも @XIAYM-gh が 48 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンFix before the next release
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)対応中かも @Lyin-OTTF が今日担当しました。 オープンFix before the next release
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
Parsing large object tree keeps a separate copy of every key in every object対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンInvestigating
難易度 5/5 1週間以上 初心者へのやさしさ 45/100
-
Active discussion
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンActive discussion
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
stleary/JSON-java の issue をすべて見る
似ている issue
-
enhancement web-ui
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
jenkinsci/jenkins#27583 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/rocketmq-dashboard#7302 ·
メンテナーはふだん 3 日以内に返信
-
area: android good first issue help wanted testing
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Gautam-aman/ResQNet#56 ·
メンテナーはふだん 1 日以内に返信
-
documentation hermes-queue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
chrisalunlloyd2-sudo/mindpalace#294 ·
メンテナーはふだん 1 日以内に返信