Skip to content

Commit

Permalink
fix overflow in frame decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 3, 2021
1 parent cebb3df commit e5fcc1f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes


## [codec-0.7.4] - 2021-12-03

* Fix overflow in frame decoder

## [0.5.7] - 2021-12-02

* Add memory pools support
Expand Down
2 changes: 1 addition & 1 deletion codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-amqp-codec"
version = "0.7.3"
version = "0.7.4"
description = "AMQP 1.0 Protocol Codec"
authors = ["Nikolay Kim <[email protected]>", "Max Gortman <[email protected]>", "Mike Yagley <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
6 changes: 4 additions & 2 deletions codec/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ pub enum AmqpParseError {
#[derive(Debug, Display, From, Clone)]
pub enum AmqpCodecError {
ParseError(AmqpParseError),
#[display(fmt = "bytes left unparsed at the frame trail")]
#[display(fmt = "Bytes left unparsed at the frame trail")]
UnparsedBytesLeft,
#[display(fmt = "max inbound frame size exceeded")]
#[display(fmt = "Max inbound frame size exceeded")]
MaxSizeExceeded,
#[display(fmt = "Invalid inbound frame size")]
InvalidFrameSize,
}

#[derive(Debug, Display, From, Clone)]
Expand Down
21 changes: 21 additions & 0 deletions codec/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl<T: Decode + Encode> Decoder for AmqpCodec<T> {
if self.max_size != 0 && size > self.max_size {
return Err(AmqpCodecError::MaxSizeExceeded);
}
if size <= 4 {
return Err(AmqpCodecError::InvalidFrameSize);
}
self.state.set(DecodeState::Frame(size - 4));
src.advance(4);

Expand Down Expand Up @@ -162,3 +165,21 @@ impl Encoder for ProtocolIdCodec {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

use crate::AmqpFrame;

#[test]
fn test_decode() -> Result<(), AmqpCodecError> {
let mut data = BytesMut::from(b"\0\0\0\0\0\0\0\0\0\x06AC@A\0S$\xc0\x01\0B".as_ref());

let codec = AmqpCodec::<AmqpFrame>::new();
let res = codec.decode(&mut data);
assert!(matches!(res, Err(AmqpCodecError::InvalidFrameSize)));

Ok(())
}
}

0 comments on commit e5fcc1f

Please sign in to comment.