changeset 128:615fc5a47509

Better checking for incoming rpc requests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 02 Nov 2016 18:26:21 +0000
parents 5b113c87b6e6
children 3ae0335cfe60
files vamp-json/VampJson.h
diffstat 1 files changed, 31 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/vamp-json/VampJson.h	Wed Nov 02 14:27:42 2016 +0000
+++ b/vamp-json/VampJson.h	Wed Nov 02 18:26:21 2016 +0000
@@ -1070,7 +1070,7 @@
 private: // go private briefly for a couple of helper functions
     
     static void
-    checkTypeField(json11::Json j, std::string expected, std::string &err) {
+    checkRpcRequestType(json11::Json j, std::string expected, std::string &err) {
         if (!j["method"].is_string()) {
             err = "string expected for method";
             return;
@@ -1079,6 +1079,31 @@
             err = "expected value \"" + expected + "\" for type";
             return;
         }
+        if (!j["params"].is_null() &&
+            !j["params"].is_object()) {
+            err = "object expected for params";
+            return;
+        }
+        if (!j["id"].is_null() &&
+            !j["id"].is_number() &&
+            !j["id"].is_string()) {
+            err = "number or string expected for id";
+            return;
+        }
+        if (!j["jsonrpc"].is_null() &&
+            !j["jsonrpc"].is_string()) {
+            err = "string expected for jsonrpc";
+            return;
+        }
+        for (const auto &kv: j.object_items()) {
+            if (kv.first != "method" &&
+                kv.first != "params" &&
+                kv.first != "id" &&
+                kv.first != "jsonrpc") {
+                err = "unexpected field \"" + kv.first + "\" in rpc request object";
+                return;
+            }
+        }
     }
 
     static bool
@@ -1322,7 +1347,7 @@
     static ListRequest
     toRpcRequest_List(json11::Json j, std::string &err) {
 
-        checkTypeField(j, "list", err);
+        checkRpcRequestType(j, "list", err);
         if (failed(err)) return {};
         return toListRequest(j["params"], err);
     }
@@ -1340,7 +1365,7 @@
     static LoadRequest
     toRpcRequest_Load(json11::Json j, std::string &err) {
         
-        checkTypeField(j, "load", err);
+        checkRpcRequestType(j, "load", err);
         if (failed(err)) return {};
         return toLoadRequest(j["params"], err);
     }
@@ -1362,7 +1387,7 @@
                             const PluginHandleMapper &pmapper,
                             std::string &err) {
         
-        checkTypeField(j, "configure", err);
+        checkRpcRequestType(j, "configure", err);
         if (failed(err)) return {};
         return toConfigurationRequest(j["params"], pmapper, err);
     }
@@ -1383,7 +1408,7 @@
     toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper,
                           BufferSerialisation &serialisation, std::string &err) {
         
-        checkTypeField(j, "process", err);
+        checkRpcRequestType(j, "process", err);
         if (failed(err)) return {};
         return toProcessRequest(j["params"], pmapper, serialisation, err);
     }
@@ -1409,7 +1434,7 @@
     toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper,
                          std::string &err) {
         
-        checkTypeField(j, "finish", err);
+        checkRpcRequestType(j, "finish", err);
         if (failed(err)) return {};
         FinishRequest req;
         req.plugin = pmapper.handleToPlugin