Skip to content

Commit

Permalink
Handle malformed message with truncated tag bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
int08h committed Mar 20, 2018
1 parent a029e50 commit f1f4af2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub enum Error {
/// Invalid number of tags specified
InvalidNumTags(u32),

/// Could not convert bytes to message because bytes were too short
MessageTooShort,

/// Otherwise invalid request
InvalidRequest,
}
Expand Down
15 changes: 14 additions & 1 deletion src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ impl RtMessage {

let mut buf = [0; 4];
for _ in 0..num_tags {
msg.read_exact(&mut buf).unwrap();
if msg.read_exact(&mut buf).is_err() {
return Err(Error::MessageTooShort);
}

let tag = Tag::from_wire(&buf)?;

if let Some(last_tag) = tags.last() {
if tag <= *last_tag {
return Err(Error::TagNotStrictlyIncreasing(tag));
}
}

tags.push(tag);
}

Expand All @@ -111,6 +115,7 @@ impl RtMessage {
let value = bytes[(header_end + value_start)..(header_end + value_end)].to_vec();
rt_msg.add_field(tag, &value)?;
}

Ok(rt_msg)
}

Expand Down Expand Up @@ -376,4 +381,12 @@ mod test {
RtMessage::from_bytes(&bytes).unwrap();
}

#[test]
#[should_panic(expected="MessageTooShort")]
fn from_bytes_too_few_bytes_for_tags() {
// Header says two tags (8 bytes) but truncate first tag at 2 bytes
let bytes = &[0x02, 0, 0, 0, 4, 0, 0, 0, 0, 0];
RtMessage::from_bytes(bytes).unwrap();
}

}

0 comments on commit f1f4af2

Please sign in to comment.