Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rpc decoding to reject extra data #208

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ impl Request {
let mut s = RlpStream::new();
s.begin_list(2);
s.append(&id.as_bytes());
s.begin_list(distances.len());
for distance in distances {
s.append(&distance);
}
s.append_list(&distances);
buf.extend_from_slice(&s.out());
buf
}
Expand Down Expand Up @@ -307,8 +304,9 @@ impl Message {
}

let msg_type = data[0];
let data = &data[1..];

let rlp = rlp::Rlp::new(&data[1..]);
let rlp = rlp::Rlp::new(data);

let list_len = rlp.item_count().and_then(|size| {
if size < 2 {
Expand All @@ -318,6 +316,12 @@ impl Message {
}
})?;

// verify there is no extra data
let payload_info = rlp.payload_info()?;
if data.len() != payload_info.header_len + payload_info.value_len {
return Err(DecoderError::RlpInconsistentLengthAndData);
}

let id = RequestId::decode(rlp.val_at::<Vec<u8>>(0)?)?;

let message = match msg_type {
Expand Down Expand Up @@ -746,4 +750,23 @@ mod tests {

assert_eq!(request, decoded);
}

#[test]
fn reject_extra_data() {
let data = [6, 194, 0, 75];
let msg = Message::decode(&data).unwrap();
assert_eq!(
msg,
Message::Response(Response {
id: RequestId(vec![0]),
body: ResponseBody::Talk { response: vec![75] }
})
);

let data2 = [6, 193, 0, 75, 252];
Message::decode(&data2).expect_err("should reject extra data");

let data3 = [6, 194, 0, 75, 252];
Message::decode(&data3).expect_err("should reject extra data");
}
}