Skip to content

Commit 86fdc9e

Browse files
feat: add validation for declared payload length in TCP connection
Implemented checks to validate the announced payload length against the maximum message size, preventing overflow and ensuring robust error handling. This enhancement improves the stability and security of TCP connection handling.
1 parent c250fbd commit 86fdc9e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

dash-spv/src/network/connection.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,22 @@ impl TcpConnection {
542542
state.framing_buffer[22],
543543
state.framing_buffer[23],
544544
];
545-
let total_len = HEADER_LEN + length_le;
545+
// Validate announced length to prevent unbounded accumulation or overflow
546+
if length_le > dashcore::network::message::MAX_MSG_SIZE {
547+
return Err(NetworkError::ProtocolError(format!(
548+
"Declared payload length {} exceeds MAX_MSG_SIZE {}",
549+
length_le,
550+
dashcore::network::message::MAX_MSG_SIZE
551+
)));
552+
}
553+
let total_len = match HEADER_LEN.checked_add(length_le) {
554+
Some(v) => v,
555+
None => {
556+
return Err(NetworkError::ProtocolError(
557+
"Message length overflow".to_string(),
558+
));
559+
}
560+
};
546561

547562
// Ensure full frame available
548563
if state.framing_buffer.len() < total_len {

0 commit comments

Comments
 (0)