Skip to content

Commit e18c540

Browse files
committed
fix: avoid loading peer-supplied exception classes
Use a generic exception when reading v0.3 JSON-RPC error data. Add regression coverage for class initialization and constructor invocation. Based on a fix and test proposed by Chaitanya Pawar.
1 parent 8101f3a commit e18c540

2 files changed

Lines changed: 52 additions & 25 deletions

File tree

‎compat-0.3/spec/src/main/java/org/a2aproject/sdk/compat03/json/JsonUtil_v0_3.java‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ OffsetDateTime read(JsonReader in) throws java.io.IOException {
255255
* <li>"message": The exception message</li>
256256
* </ul>
257257
* <p>
258-
* <b>Deserialization:</b> Reads the JSON and reconstructs the Throwable using reflection to find
259-
* a constructor that accepts a String message parameter. If no such constructor exists or if
260-
* instantiation fails, returns a generic {@link RuntimeException} with the message.
258+
* <b>Deserialization:</b> Returns a generic {@link RuntimeException} with the message.
259+
* The type is informational and is not used to load or instantiate a class.
261260
*
262261
* @see Throwable
263262
*/
@@ -284,15 +283,12 @@ Throwable read(JsonReader in) throws java.io.IOException {
284283
return null;
285284
}
286285

287-
String type = null;
288286
String message = null;
289287

290288
in.beginObject();
291289
while (in.hasNext()) {
292290
String fieldName = in.nextName();
293291
switch (fieldName) {
294-
case "type" ->
295-
type = in.nextString();
296292
case "message" ->
297293
message = in.nextString();
298294
default ->
@@ -301,25 +297,6 @@ Throwable read(JsonReader in) throws java.io.IOException {
301297
}
302298
in.endObject();
303299

304-
// Try to reconstruct the Throwable
305-
if (type != null) {
306-
try {
307-
Class<?> throwableClass = Class.forName(type);
308-
if (Throwable.class.isAssignableFrom(throwableClass)) {
309-
// Try to find a constructor that takes a String message
310-
try {
311-
var constructor = throwableClass.getConstructor(String.class);
312-
return (Throwable) constructor.newInstance(message);
313-
} catch (NoSuchMethodException e) {
314-
// No String constructor, return a generic RuntimeException
315-
return new RuntimeException(message);
316-
}
317-
}
318-
} catch (Exception e) {
319-
// If we can't reconstruct the exact type, return a generic RuntimeException
320-
return new RuntimeException(message);
321-
}
322-
}
323300
return new RuntimeException(message);
324301
}
325302
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.a2aproject.sdk.compat03.json;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
6+
7+
import org.a2aproject.sdk.compat03.spec.JSONRPCError_v0_3;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ThrowableDeserializationSecurityTest {
11+
private static boolean nonThrowableInitialized;
12+
13+
public static class NonThrowable {
14+
static {
15+
nonThrowableInitialized = true;
16+
}
17+
}
18+
19+
public static class CustomThrowable extends Exception {
20+
public CustomThrowable(String message) {
21+
super(message);
22+
}
23+
}
24+
25+
@Test
26+
void peerControlledTypeDoesNotInitializeNonThrowable() throws JsonProcessingException_v0_3 {
27+
assertFalse(nonThrowableInitialized);
28+
29+
JSONRPCError_v0_3 error = deserializeError(NonThrowable.class.getName());
30+
31+
assertFalse(nonThrowableInitialized, "The peer-selected class must not be initialized");
32+
assertEquals("owned", assertInstanceOf(RuntimeException.class, error.getData()).getMessage());
33+
}
34+
35+
@Test
36+
void peerControlledThrowableIsNotInstantiated() throws JsonProcessingException_v0_3 {
37+
JSONRPCError_v0_3 error = deserializeError(CustomThrowable.class.getName());
38+
39+
RuntimeException data = assertInstanceOf(RuntimeException.class, error.getData());
40+
assertEquals(RuntimeException.class, data.getClass());
41+
assertEquals("owned", data.getMessage());
42+
}
43+
44+
private JSONRPCError_v0_3 deserializeError(String type) throws JsonProcessingException_v0_3 {
45+
String json = """
46+
{"code":-32603,"message":"boom","data":{"type":"%s","message":"owned","__throwable":true}}
47+
""".formatted(type);
48+
return JsonUtil_v0_3.fromJson(json, JSONRPCError_v0_3.class);
49+
}
50+
}

0 commit comments

Comments
 (0)