Skip to content

Commit

Permalink
fix: reject invalid binary event (resolve #357) (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhx-12243 authored Aug 14, 2024
1 parent 5f19731 commit 618cb98
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions socketioxide/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,21 @@ impl<'a> TryFrom<Str> for Packet<'a> {
fn try_from(value: Str) -> Result<Self, Self::Error> {
let chars = value.as_bytes();
// It is possible to parse the packet from a byte slice because separators are only ASCII
let mut i = 1;
let index = (b'0'..=b'6')
.contains(&chars[0])
.then_some(chars[0])
.ok_or(Error::InvalidPacketType)?;

// Move the cursor to skip the payload count if it is a binary packet
if index == b'5' || index == b'6' {
while chars.get(i) != Some(&b'-') {
i += 1;
}
i += 1;
}
let mut i = if index == b'5' || index == b'6' {
chars
.iter()
.position(|x| *x == b'-')
.ok_or(Error::InvalidPacketType)?
+ 1
} else {
1
};

let start_index = i;
// Custom nsps will start with a slash
Expand Down Expand Up @@ -919,4 +921,12 @@ mod test {
let packet = Packet::bin_ack("/", json!("data"), vec![Bytes::from_static(&[1])], 54);
assert_eq!(packet.get_size_hint(), 5);
}

#[test]
fn packet_reject_invalid_binary_event() {
let payload = "5invalid".to_owned();
let err = Packet::try_from(payload).unwrap_err();

assert!(matches!(err, Error::InvalidPacketType));
}
}

0 comments on commit 618cb98

Please sign in to comment.