Skip to content

Commit

Permalink
fix(collator): better ignore invalid messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jan 30, 2025
1 parent 4d7870a commit e18ee50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
52 changes: 12 additions & 40 deletions collator/src/mempool/impls/std_impl/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use everscale_types::boc::Boc;
use everscale_types::models::MsgInfo;
use everscale_types::prelude::Load;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tycho_util::bc::ExtMsgRepr;
use tycho_util::metrics::HistogramGuard;

use crate::mempool::impls::std_impl::deduplicator::Deduplicator;
Expand Down Expand Up @@ -55,7 +56,10 @@ impl Parser {

let all_bytes_blake = payloads
.into_par_iter()
.map(|bytes| (<[u8; 32]>::from(blake3::hash(&bytes)), bytes))
.filter_map(|bytes| {
(bytes.len() <= ExtMsgRepr::MAX_BOC_SIZE)
.then(|| (<[u8; 32]>::from(blake3::hash(&bytes)), bytes))
})
.collect::<Vec<_>>();

let uniq_bytes_blake = all_bytes_blake
Expand Down Expand Up @@ -102,46 +106,14 @@ impl Parser {
}

fn parse_message_bytes(message: &Bytes) -> Option<Arc<ExternalMessage>> {
let cell = match Boc::decode(message) {
Ok(cell) => cell,
Err(e) => {
// TODO: should handle errors properly?
tracing::error!(
target: tracing_targets::MEMPOOL_ADAPTER,
"Failed to deserialize bytes into cell. Error: {e:?}"
);
return None;
}
};

let mut slice = match cell.as_slice() {
Ok(slice) => slice,
Err(e) => {
tracing::error!(
target: tracing_targets::MEMPOOL_ADAPTER,
"Failed to make slice from cell. Error: {e:?}"
);
return None;
}
};
let cell = Boc::decode(message).ok()?;
if cell.is_exotic() || cell.level() != 0 || cell.repr_depth() > ExtMsgRepr::MAX_REPR_DEPTH {
return None;
}

let info = match MsgInfo::load_from(&mut slice) {
Ok(MsgInfo::ExtIn(message)) => message,
Ok(info) => {
tracing::error!(
target: tracing_targets::MEMPOOL_ADAPTER,
?info,
"Bad message. Unexpected message variant"
);
return None;
}
Err(e) => {
tracing::error!(
target: tracing_targets::MEMPOOL_ADAPTER,
"Bad cell. Failed to deserialize to ExtInMsgInfo. Err: {e:?}"
);
return None;
}
let mut cs = cell.as_slice_allow_pruned();
let MsgInfo::ExtIn(info) = MsgInfo::load_from(&mut cs).ok()? else {
return None;
};
Some(Arc::new(ExternalMessage { cell, info }))
}
Expand Down
7 changes: 7 additions & 0 deletions util/src/bc/ext_msg_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ impl ExtMsgRepr {
// Decode BOC.
let msg_root = Boc::decode(bytes)?;

// Cell must not contain any suspicious pruned branches not wrapped into merkle stuff.
if msg_root.level() != 0 {
return Err(InvalidExtMsg::TooBigLevel);
}

// Apply limits to the cell depth.
if msg_root.repr_depth() > Self::MAX_REPR_DEPTH {
return Err(InvalidExtMsg::DepthExceeded);
Expand Down Expand Up @@ -167,6 +172,8 @@ pub enum InvalidExtMsg {
BocSizeExceeded,
#[error("invalid message BOC")]
BocError(#[from] everscale_types::boc::de::Error),
#[error("too big root cell level")]
TooBigLevel,
#[error("max cell repr depth exceeded")]
DepthExceeded,
#[error("invalid message")]
Expand Down

0 comments on commit e18ee50

Please sign in to comment.