jnan77 / jsonrpc4j

Automatically exported from code.google.com/p/jsonrpc4j
0 stars 0 forks source link

Non-compliant response to request containing malformed JSON #50

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Submit a JSON-RPC request that is actually malformed JSON, e.g. with 
mismatched braces, or perhaps un-quoted property (id:1, instead of "id":1)

What is the expected output? What do you see instead?
The JSON-RPC 2.0 spec specifies a JSON response even in this case, with a 
specific error code (-32700).
Instead, jsonrpc4j throws an exception, and the servlet container ends up 
outputting HTML including a stack trace.

What version of the product are you using? On what operating system?
0.27-SNAPSHOT

Please provide any additional information below.

The following change implements the expected behavior:

Index: jsonrpc4j/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java
===================================================================
--- jsonrpc4j/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java 
(revision 238683)
+++ jsonrpc4j/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java 
(working copy)
@@ -29,6 +29,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

+import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.JsonNode;
@@ -215,9 +216,17 @@
         * @param ops the {@link OutputStream}
         * @throws IOException on error
         */
-       public void handle(InputStream ips, OutputStream ops)
-               throws IOException {
-               handleNode(mapper.readTree(new NoCloseInputStream(ips)), ops);
+       public void handle(InputStream ips, OutputStream ops) throws 
IOException {
+
+               JsonNode jsonNode = null;
+               try {
+                       jsonNode = mapper.readTree(new NoCloseInputStream(ips));
+               } catch (JsonParseException e) {
+                       writeAndFlushValue(ops, createErrorResponse("jsonrpc", 
"null", -32700, "Parse error", null));
+                       return;
+               }
+
+               handleNode(jsonNode, ops);
        }

        /**

Original issue reported on code.google.com by smend...@gmail.com on 13 Feb 2013 at 7:29

GoogleCodeExporter commented 8 years ago
And the test:
Index: jsonrpc4j/src/test/java/com/googlecode/jsonrpc4j/JsonRpcServerTest.java
===================================================================
--- jsonrpc4j/src/test/java/com/googlecode/jsonrpc4j/JsonRpcServerTest.java     
(revision 238683)
+++ jsonrpc4j/src/test/java/com/googlecode/jsonrpc4j/JsonRpcServerTest.java     
(working copy)
@@ -1,6 +1,7 @@
 package com.googlecode.jsonrpc4j;

-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;

 import java.io.ByteArrayOutputStream;

@@ -382,7 +383,17 @@
                assertTrue(json.get("id").isNull());
        }

+       @Test
+       public void callParseErrorJson() throws Exception {
+               jsonRpcServerAnnotatedParam.handle(new 
ClassPathResource("jsonRpcParseErrorTest.json").getInputStream(), baos);

+               String response = baos.toString(JSON_ENCODING);
+               JsonNode json = mapper.readTree(response);
+
+               // Invalid parameters
+               assertEquals(-32700, json.get("error").get("code").asInt());
+       }
+
        // Service and service interfaces used in test

        private interface ServiceInterface {

Original comment by smend...@gmail.com on 13 Feb 2013 at 7:32

GoogleCodeExporter commented 8 years ago
thanks, applied the patch

Original comment by brian.di...@gmail.com on 16 Feb 2013 at 8:09