JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 56/100
Hướng nghiên cứu
Start by inspecting JSONObject.writeValue(Writer, Object, …) and JSONArray.write(…), then reproduce the direct and indirect cycles from the issue. Trace how nested values are serialized and determine how cycle handling should propagate through both methods. Done means cyclic JSONObject and JSONArray graphs produce JSONException rather than StackOverflowError, while ordinary serialization remains unchanged.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
JSONObject.toString() (and write()) recurse into nested JSONObject values without any cycle detection. If a JSONObject contains itself (directly or transitively), serialization recurses indefinitely and the JVM throws StackOverflowError.
The parsing path is protected by JSONParserConfiguration.getMaxNestingDepth(), but cycles created via put() programmatically bypass it because the cycle was never parsed.
Reproducer (org.json 20240303)
import org.json.JSONObject;
public class Repro {
public static void main(String[] args) {
JSONObject jo = new JSONObject();
jo.put("key", "value");
jo.put("self", jo); // direct self-reference
jo.toString(); // -> StackOverflowError
}
}
Indirect cycles also trigger:
JSONObject a = new JSONObject(), b = new JSONObject();
a.put("b", b);
b.put("a", a);
a.toString(); // -> StackOverflowError
JSONArray containing itself triggers the same:
JSONArray arr = new JSONArray();
arr.put("x");
arr.put(arr);
arr.toString(); // -> StackOverflowError
Why this is more than "don't construct cycles"
- Code that takes user input and walks it into a
JSONObjectmodel (deserializers, GraphQL resolvers, ORM emitters) may produce a cycle without realizing it (object graph derived from a database join, a mutually-referencing config). - The current contract is that
JSONObject.toString()returns aStringor throws a checkedJSONException. AStackOverflowErroris anError, not anException, so application try/catch blocks targetingException(or evenJSONException) won't catch it. The JVM thread crashes. - A library used in a hot serialization path that crashes on
Errorrather than throwing a typed exception is a DoS / availability issue for any process that lets this be reached.
Root cause
JSONObject.writeValue(Writer, Object, …) and JSONArray.write(…) recurse on nested values without maintaining a "seen" set. The fix is to either:
- Pass an
IdentityHashMap<Object, Boolean>of currently-being-serialized objects down throughwrite()/writeValue()and throwJSONExceptionon a cycle. - Use the same
maxNestingDepthlimit on serialization that already exists on parsing.
(1) is more precise; (2) is simpler and matches the parsing-side mitigation.
Suggested patch sketch
public Writer writeValue(Writer writer, Object value, int indentFactor, int indent,
Set<Object> seen) throws JSONException, IOException {
if (value instanceof JSONObject || value instanceof JSONArray) {
if (!seen.add(System.identityHashCode(value))) {
throw new JSONException("Cyclic reference detected during serialization");
}
try {
// existing logic, threading `seen` into recursive calls
} finally {
seen.remove(System.identityHashCode(value));
}
} else { /* unchanged */ }
}
Environment
- org.json: 20240303 (latest at time of writing)
- JDK: 21
Discovered via jqwik property-based testing on the invariant toString() either returns a String or throws JSONException (never Error). Happy to PR.
- Ngôn ngữ chính
- Java
- Star
- 4.7k
- Fork
- 2.6k
- Merge trung bình
- 6 ngày 20 giờ
- Pull request đã merge (30 ngày)
- 2
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của stleary/JSON-java
-
Fix before the next release
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
New JSONPointer tests needed Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Fix before the next release
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Tất cả issue của stleary/JSON-java
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
checkstyle/test-configs#263 ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
apache/cloudstack#14222 ·
-
[BUG]茶杯方块在取茶时会引发崩溃 Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Cannot differ own consent and managed consents in My Consents view and detailed consent view. Đang mở1.0.0-alpha2 Type/Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
wso2/dpdp-accelerator#272 ·