Skip to content

Commit e02d6ff

Browse files
authored
fix: replace expect() panics with error handling in check_filters_data (GHSA-6wqr) (#277)
1 parent b770a63 commit e02d6ff

2 files changed

Lines changed: 40 additions & 27 deletions

File tree

light-client-lib/src/protocols/filter/block_filter.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,50 @@ impl FilterProtocol {
4444
&self,
4545
block_filters: packed::BlockFilters,
4646
limit: usize,
47-
) -> Vec<packed::Byte32> {
47+
) -> Result<Vec<packed::Byte32>, Status> {
4848
let start_number: BlockNumber = block_filters.start_number().unpack();
4949
let reader = GCSFilterReader::new(SipHasher24Builder::new(0, 0), M, P);
5050
let script_hashes = self
5151
.storage
5252
.get_scripts_hash(start_number + limit as BlockNumber);
53-
block_filters
54-
.filters()
55-
.into_iter()
56-
.take(limit)
57-
.enumerate()
58-
.filter_map(|(index, block_filter)| {
59-
let mut input = Cursor::new(block_filter.raw_data());
60-
if reader
61-
.match_any(&mut input, &mut script_hashes.iter().map(|v| v.as_slice()))
62-
.expect("GCSFilterReader#match_any should be ok")
63-
{
64-
let block_hash = block_filters
65-
.block_hashes()
66-
.get(index)
67-
.expect("checked index");
68-
info!("check_filters_data matched, block_hash: {:#x}", block_hash);
69-
Some(block_hash)
70-
} else {
71-
trace!(
72-
"check_filters_data not matched, block_hash: {:#x}",
73-
block_filters.block_hashes().get(index).expect("msg")
74-
);
75-
None
53+
let mut matched = Vec::new();
54+
for (index, block_filter) in block_filters.filters().into_iter().take(limit).enumerate() {
55+
let mut input = Cursor::new(block_filter.raw_data());
56+
let is_match = reader
57+
.match_any(&mut input, &mut script_hashes.iter().map(|v| v.as_slice()))
58+
.map_err(|e| {
59+
let errmsg = format!("GCSFilterReader#match_any failed: {}", e);
60+
StatusCode::MalformedProtocolMessage.with_context(errmsg)
61+
})?;
62+
if is_match {
63+
let block_hash = match block_filters.block_hashes().get(index) {
64+
Some(h) => h,
65+
None => {
66+
let errmsg = format!(
67+
"block_hashes index {} out of bounds (len: {})",
68+
index,
69+
block_filters.block_hashes().len()
70+
);
71+
return Err(StatusCode::MalformedProtocolMessage.with_context(errmsg));
72+
}
73+
};
74+
info!("check_filters_data matched, block_hash: {:#x}", block_hash);
75+
matched.push(block_hash);
76+
} else {
77+
match block_filters.block_hashes().get(index) {
78+
Some(h) => trace!("check_filters_data not matched, block_hash: {:#x}", h),
79+
None => {
80+
let errmsg = format!(
81+
"block_hashes index {} out of bounds (len: {})",
82+
index,
83+
block_filters.block_hashes().len()
84+
);
85+
return Err(StatusCode::MalformedProtocolMessage.with_context(errmsg));
86+
}
7687
}
77-
})
78-
.collect()
88+
}
89+
}
90+
Ok(matched)
7991
}
8092

8193
async fn should_ask(&self, immediately: bool) -> bool {

light-client-lib/src/protocols/filter/components/block_filters_process.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ impl<'a> BlockFiltersProcess<'a> {
206206
parent_block_filter_hash = current_hash;
207207
}
208208

209-
let possible_match_blocks = self.filter.check_filters_data(block_filters, limit);
209+
let possible_match_blocks =
210+
return_if_failed!(self.filter.check_filters_data(block_filters, limit));
210211
let possible_match_blocks_len = possible_match_blocks.len();
211212
trace!(
212213
"peer {}, matched blocks: {}",

0 commit comments

Comments
 (0)