Jelajahi Sumber

bin/tau: add some saftey checks

Dastan-glitch 4 tahun lalu
induk
melakukan
6f5bf9fb41
3 mengubah file dengan 30 tambahan dan 1 penghapusan
  1. 10 1
      bin/tau/taud/src/rpc_add.rs
  2. 5 0
      bin/tau/taud/src/rpc_get.rs
  3. 15 0
      bin/tau/taud/src/rpc_update.rs

+ 10 - 1
bin/tau/taud/src/rpc_add.rs

@@ -4,7 +4,11 @@ use serde_json::{json, Value};
 
 use darkfi::{util::Timestamp, Error};
 
-use crate::{error::TaudResult, task_info::TaskInfo, JsonRpcInterface};
+use crate::{
+    error::{TaudError, TaudResult},
+    task_info::TaskInfo,
+    JsonRpcInterface,
+};
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 struct BaseTaskInfo {
@@ -34,6 +38,11 @@ impl JsonRpcInterface {
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     pub async fn add(&self, params: Value) -> TaudResult<Value> {
         debug!(target: "tau", "JsonRpc::add() params {}", params);
+
+        if !params.is_array() {
+            return Err(TaudError::InvalidData("params is not an array".into()))
+        }
+
         let args = params.as_array().unwrap();
 
         let task: BaseTaskInfo = serde_json::from_value(args[0].clone())?;

+ 5 - 0
bin/tau/taud/src/rpc_get.rs

@@ -26,6 +26,11 @@ impl JsonRpcInterface {
     // <-- {"jsonrpc": "2.0", "result": "task", "id": 1}
     pub async fn get_task_by_id(&self, params: Value) -> TaudResult<Value> {
         debug!(target: "tau", "JsonRpc::get_task_by_id() params {}", params);
+
+        if !params.is_array() {
+            return Err(TaudError::InvalidData("params is not an array".into()))
+        }
+
         let args = params.as_array().unwrap();
 
         if args.len() != 1 {

+ 15 - 0
bin/tau/taud/src/rpc_update.rs

@@ -16,6 +16,11 @@ impl JsonRpcInterface {
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     pub async fn update(&self, params: Value) -> TaudResult<Value> {
         debug!(target: "tau", "JsonRpc::update() params {}", params);
+
+        if !params.is_array() {
+            return Err(TaudError::InvalidData("params is not an array".into()))
+        }
+
         let args = params.as_array().unwrap();
 
         if args.len() != 2 {
@@ -37,6 +42,11 @@ impl JsonRpcInterface {
         // TODO: BUG: Validate that the state string is correct and not something arbitrary
 
         debug!(target: "tau", "JsonRpc::set_state() params {}", params);
+
+        if !params.is_array() {
+            return Err(TaudError::InvalidData("params is not an array".into()))
+        }
+
         let args = params.as_array().unwrap();
 
         if args.len() != 2 {
@@ -59,6 +69,11 @@ impl JsonRpcInterface {
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     pub async fn set_comment(&self, params: Value) -> TaudResult<Value> {
         debug!(target: "tau", "JsonRpc::set_comment() params {}", params);
+
+        if !params.is_array() {
+            return Err(TaudError::InvalidData("params is not an array".into()))
+        }
+
         let args = params.as_array().unwrap();
 
         if args.len() != 2 {