From 46b5801730b6b1e07731b30fb639d917ae27462a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 20 Jul 2016 12:42:12 +0200 Subject: [PATCH] Fixing hash deserialisation (#1674) --- rpc/src/v1/tests/mocked/eth.rs | 22 ++++++++++++++++++++++ rpc/src/v1/types/hash.rs | 8 ++++++-- rpc/src/v1/types/transaction_request.rs | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index ff53be976..b27bff574 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -608,6 +608,28 @@ fn rpc_eth_send_transaction() { assert_eq!(tester.io.handle_request(&request), Some(response)); } +#[test] +fn rpc_eth_send_transaction_with_bad_to() { + let tester = EthTester::default(); + let address = tester.accounts_provider.new_account("").unwrap(); + let request = r#"{ + "jsonrpc": "2.0", + "method": "eth_sendTransaction", + "params": [{ + "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", + "to": "", + "gas": "0x76c0", + "gasPrice": "0x9184e72a000", + "value": "0x9184e72a" + }], + "id": 1 + }"#; + + let response = r#"{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params","data":null},"id":1}"#; + + assert_eq!(tester.io.handle_request(&request), Some(response.into())); +} + #[test] fn rpc_eth_send_transaction_error() { diff --git a/rpc/src/v1/types/hash.rs b/rpc/src/v1/types/hash.rs index f0606ed51..22d6a0588 100644 --- a/rpc/src/v1/types/hash.rs +++ b/rpc/src/v1/types/hash.rs @@ -116,13 +116,17 @@ macro_rules! impl_hash { type Value = $name; fn visit_str(&mut self, value: &str) -> Result where E: serde::Error { + + if value.len() != 2 + $size * 2 { + return Err(serde::Error::custom("Invalid length.")); + } + match value[2..].from_hex() { - Ok(ref v) if v.len() == $size => { + Ok(ref v) => { let mut result = [0u8; $size]; result.copy_from_slice(v); Ok($name(result)) }, - Ok(_) => Err(serde::Error::custom("Invalid length.")), _ => Err(serde::Error::custom("Invalid hex value.")) } } diff --git a/rpc/src/v1/types/transaction_request.rs b/rpc/src/v1/types/transaction_request.rs index f697144b3..12e2c81fe 100644 --- a/rpc/src/v1/types/transaction_request.rs +++ b/rpc/src/v1/types/transaction_request.rs @@ -188,6 +188,22 @@ mod tests { }); } + + #[test] + fn transaction_request_deserialize_error() { + let s = r#"{ + "from":"0xb5f7502a2807cb23615c7456055e1d65b2508625", + "to":"", + "data":"0x8595bab1", + "gas":"0x2fd618", + "gasPrice":"0x0ba43b7400" + }"#; + + let deserialized = serde_json::from_str::(s); + + assert!(deserialized.is_err(), "Should be error because to is empty"); + } + #[test] fn should_deserialize_modification() { // given