diff --git a/src/client/connection/response/certificate.rs b/src/client/connection/response/certificate.rs index d78c133..6b9e774 100644 --- a/src/client/connection/response/certificate.rs +++ b/src/client/connection/response/certificate.rs @@ -31,7 +31,14 @@ impl Certificate { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::HEADER_LEN { + super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), } diff --git a/src/client/connection/response/failure/permanent.rs b/src/client/connection/response/failure/permanent.rs index e69510c..e33ea13 100644 --- a/src/client/connection/response/failure/permanent.rs +++ b/src/client/connection/response/failure/permanent.rs @@ -42,7 +42,14 @@ impl Permanent { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::super::HEADER_LEN { + super::super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), } diff --git a/src/client/connection/response/failure/temporary.rs b/src/client/connection/response/failure/temporary.rs index c9d8083..21fc2b4 100644 --- a/src/client/connection/response/failure/temporary.rs +++ b/src/client/connection/response/failure/temporary.rs @@ -42,7 +42,14 @@ impl Temporary { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::super::HEADER_LEN { + super::super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), } diff --git a/src/client/connection/response/input.rs b/src/client/connection/response/input.rs index c524b9a..37aa99f 100644 --- a/src/client/connection/response/input.rs +++ b/src/client/connection/response/input.rs @@ -21,7 +21,14 @@ impl Input { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::HEADER_LEN { + super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), } @@ -69,17 +76,19 @@ impl std::fmt::Display for Input { impl std::str::FromStr for Input { type Err = Error; fn from_str(header: &str) -> Result { - if let Some(postfix) = header.strip_prefix("10") { - return Ok(Self::Default { - header: header.to_string(), - message: message(postfix), - }); - } - if let Some(postfix) = header.strip_prefix("11") { - return Ok(Self::Sensitive { - header: header.to_string(), - message: message(postfix), - }); + if header.len() <= super::HEADER_LEN { + if let Some(postfix) = header.strip_prefix("10") { + return Ok(Self::Default { + header: header.to_string(), + message: message(postfix), + }); + } + if let Some(postfix) = header.strip_prefix("11") { + return Ok(Self::Sensitive { + header: header.to_string(), + message: message(postfix), + }); + } } Err(Error::Protocol) } diff --git a/src/client/connection/response/redirect.rs b/src/client/connection/response/redirect.rs index cc838df..b936944 100644 --- a/src/client/connection/response/redirect.rs +++ b/src/client/connection/response/redirect.rs @@ -19,7 +19,14 @@ impl Redirect { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::HEADER_LEN { + super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), } diff --git a/src/client/connection/response/success.rs b/src/client/connection/response/success.rs index fa4c236..e9c240e 100644 --- a/src/client/connection/response/success.rs +++ b/src/client/connection/response/success.rs @@ -14,7 +14,14 @@ impl Success { /// Create new `Self` from buffer include header bytes pub fn from_utf8(buffer: &[u8]) -> Result { use std::str::FromStr; - match std::str::from_utf8(buffer) { + let len = buffer.len(); + match std::str::from_utf8( + &buffer[..if len > super::HEADER_LEN { + super::HEADER_LEN + } else { + len + }], + ) { Ok(header) => Self::from_str(header), Err(e) => Err(Error::Utf8Error(e)), }