From ea5535cc28fd09412744a8982ea2cb46d2bbb5a2 Mon Sep 17 00:00:00 2001 From: jn Date: Fri, 22 Mar 2024 02:52:59 +0700 Subject: [PATCH] fix: DisconnectReason::decode should return error on zero length list (#7284) --- crates/net/eth-wire/src/disconnect.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/net/eth-wire/src/disconnect.rs b/crates/net/eth-wire/src/disconnect.rs index 986028c51..dbf24269c 100644 --- a/crates/net/eth-wire/src/disconnect.rs +++ b/crates/net/eth-wire/src/disconnect.rs @@ -129,9 +129,14 @@ impl Decodable for DisconnectReason { // this should be a list, so decode the list header. this should advance the buffer so // buf[0] is the first (and only) element of the list. let header = Header::decode(buf)?; + if !header.list { return Err(RlpError::UnexpectedString) } + + if header.payload_length != 1 { + return Err(RlpError::ListLengthMismatch { expected: 1, got: header.payload_length }) + } } // geth rlp encodes [`DisconnectReason::DisconnectRequested`] as 0x00 and not as empty @@ -232,6 +237,14 @@ mod tests { assert!(DisconnectReason::decode(&mut &[0u8; 3][..]).is_err()) } + #[test] + fn test_reason_zero_length_list() { + let list_with_zero_length = hex::decode("c000").unwrap(); + let res = DisconnectReason::decode(&mut &list_with_zero_length[..]); + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), "unexpected list length (got 0, expected 1)") + } + #[test] fn disconnect_encoding_length() { let all_reasons = all_reasons();