mirror of
https://github.com/aljazceru/notedeck.git
synced 2025-12-22 18:54:19 +01:00
check message length before prefix comparisons
This commit is contained in:
@@ -78,6 +78,11 @@ impl<'a> RelayMessage<'a> {
|
|||||||
return Err(Error::Empty);
|
return Err(Error::Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure we can inspect the begning of the message below ...
|
||||||
|
if msg.len() < 12 {
|
||||||
|
return Err(Error::DecodeFailed("message too short".into()));
|
||||||
|
}
|
||||||
|
|
||||||
// Notice
|
// Notice
|
||||||
// Relay response format: ["NOTICE", <message>]
|
// Relay response format: ["NOTICE", <message>]
|
||||||
if msg.len() >= 12 && &msg[0..=9] == "[\"NOTICE\"," {
|
if msg.len() >= 12 && &msg[0..=9] == "[\"NOTICE\"," {
|
||||||
@@ -159,6 +164,16 @@ mod tests {
|
|||||||
fn test_handle_various_messages() -> Result<()> {
|
fn test_handle_various_messages() -> Result<()> {
|
||||||
let tests = vec![
|
let tests = vec![
|
||||||
// Valid cases
|
// Valid cases
|
||||||
|
(
|
||||||
|
// shortest valid message
|
||||||
|
r#"["EOSE","x"]"#,
|
||||||
|
Ok(RelayMessage::eose("x")),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
// also very short
|
||||||
|
r#"["NOTICE",""]"#,
|
||||||
|
Ok(RelayMessage::notice("")),
|
||||||
|
),
|
||||||
(
|
(
|
||||||
r#"["NOTICE","Invalid event format!"]"#,
|
r#"["NOTICE","Invalid event format!"]"#,
|
||||||
Ok(RelayMessage::notice("Invalid event format!")),
|
Ok(RelayMessage::notice("Invalid event format!")),
|
||||||
@@ -197,11 +212,11 @@ mod tests {
|
|||||||
),
|
),
|
||||||
(
|
(
|
||||||
r#"["EOSE"]"#,
|
r#"["EOSE"]"#,
|
||||||
Err(Error::DecodeFailed("unrecognized message type".into())),
|
Err(Error::DecodeFailed("message too short".into())),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
r#"["NOTICE"]"#,
|
r#"["NOTICE"]"#,
|
||||||
Err(Error::DecodeFailed("unrecognized message type".into())),
|
Err(Error::DecodeFailed("message too short".into())),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
r#"["NOTICE": 404]"#,
|
r#"["NOTICE": 404]"#,
|
||||||
|
|||||||
Reference in New Issue
Block a user