Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network messages): add limits to rejection message and reason #4687

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions zebra-network/src/protocol/external/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,47 @@ pub enum Message {
FilterClear,
}

/// The maximum size of the rejection message.
///
/// This is equivalent to `COMMAND_SIZE` in zcashd.
const MAX_REJECT_MESSAGE_LENGTH: usize = 12;
teor2345 marked this conversation as resolved.
Show resolved Hide resolved

/// The maximum size of the rejection reason.
///
/// This is equivalent to `MAX_REJECT_MESSAGE_LENGTH` in zcashd.
const MAX_REJECT_REASON_LENGTH: usize = 111;

// TODO: add tests for Error conversion and Reject message serialization (#4633)
// (Zebra does not currently send reject messages, and it ignores received reject messages.)
impl<E> From<E> for Message
where
E: Error,
{
fn from(e: E) -> Self {
let message = e
.to_string()
.escape_default()
.take(MAX_REJECT_MESSAGE_LENGTH)
.collect();
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
let reason = e
.source()
.map(ToString::to_string)
.unwrap_or_default()
.escape_default()
.take(MAX_REJECT_REASON_LENGTH)
.collect();

Message::Reject {
message: e.to_string(),
message,

// The generic case, impls for specific error types should
// use specific varieties of `RejectReason`.
ccode: RejectReason::Other,

reason: if let Some(reason) = e.source() {
reason.to_string()
} else {
String::from("")
},
reason,

// Allow this to be overridden but not populated by default, methinks.
// The hash of the rejected block or transaction.
// We don't have that data here, so the caller needs to fill it in later.
data: None,
}
}
Expand Down