Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 0793aa8

Browse files
authored
Ensure a bad datastream cannot cause problems (#701)
* Ensure a bad datastream cannot cause problems * Formatting * Formatting
1 parent 8159c2c commit 0793aa8

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

pallets/xcmp-queue/src/lib.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,26 @@ impl<T: Config> Pallet<T> {
401401
XcmpMessageFormat::ConcatenatedEncodedBlob => {
402402
while !remaining_fragments.is_empty() {
403403
last_remaining_fragments = remaining_fragments;
404-
if let Ok(blob) = <Vec<u8>>::decode_all(&mut remaining_fragments) {
405-
let weight = max_weight - weight_used;
406-
match Self::handle_blob_message(sender, sent_at, blob, weight) {
407-
Ok(used) => weight_used = weight_used.saturating_add(used),
408-
Err(true) => {
409-
// That message didn't get processed this time because of being
410-
// too heavy. We leave it around for next time and bail.
411-
remaining_fragments = last_remaining_fragments;
412-
break
413-
},
414-
Err(false) => {
415-
// Message invalid; don't attempt to retry
416-
},
417-
}
418-
} else {
419-
debug_assert!(false, "Invalid incoming blob message data");
420-
remaining_fragments = &b""[..];
404+
match <Vec<u8>>::decode_all(&mut remaining_fragments) {
405+
Ok(blob) if remaining_fragments.len() < last_remaining_fragments.len() => {
406+
let weight = max_weight - weight_used;
407+
match Self::handle_blob_message(sender, sent_at, blob, weight) {
408+
Ok(used) => weight_used = weight_used.saturating_add(used),
409+
Err(true) => {
410+
// That message didn't get processed this time because of being
411+
// too heavy. We leave it around for next time and bail.
412+
remaining_fragments = last_remaining_fragments;
413+
break
414+
},
415+
Err(false) => {
416+
// Message invalid; don't attempt to retry
417+
},
418+
}
419+
},
420+
_ => {
421+
debug_assert!(false, "Invalid incoming blob message data");
422+
remaining_fragments = &b""[..];
423+
},
421424
}
422425
}
423426
},

pallets/xcmp-queue/src/tests.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use super::*;
1717
use cumulus_primitives_core::XcmpMessageHandler;
18-
use mock::{new_test_ext, XcmpQueue};
18+
use mock::{new_test_ext, Test, XcmpQueue};
1919

2020
#[test]
2121
fn one_message_does_not_panic() {
@@ -27,3 +27,35 @@ fn one_message_does_not_panic() {
2727
XcmpQueue::handle_xcmp_messages(messages.into_iter(), Weight::max_value());
2828
})
2929
}
30+
31+
#[test]
32+
#[should_panic = "Invalid incoming blob message data"]
33+
fn bad_message_is_handled() {
34+
new_test_ext().execute_with(|| {
35+
let bad_data = vec![
36+
1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 64, 239, 139, 0,
37+
0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 37, 0,
38+
0, 0, 0, 0, 0, 0, 16, 0, 127, 147,
39+
];
40+
InboundXcmpMessages::<Test>::insert(ParaId::from(1000), 1, bad_data);
41+
let format = XcmpMessageFormat::ConcatenatedEncodedBlob;
42+
// This should exit with an error.
43+
XcmpQueue::process_xcmp_message(1000.into(), (1, format), 10_000_000_000);
44+
});
45+
}
46+
47+
#[test]
48+
#[should_panic = "Invalid incoming blob message data"]
49+
fn other_bad_message_is_handled() {
50+
new_test_ext().execute_with(|| {
51+
let bad_data = vec![
52+
1, 1, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 64, 239,
53+
139, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0,
54+
37, 0, 0, 0, 0, 0, 0, 0, 16, 0, 127, 147,
55+
];
56+
InboundXcmpMessages::<Test>::insert(ParaId::from(1000), 1, bad_data);
57+
let format = XcmpMessageFormat::ConcatenatedEncodedBlob;
58+
// This should exit with an error.
59+
XcmpQueue::process_xcmp_message(1000.into(), (1, format), 10_000_000_000);
60+
});
61+
}

0 commit comments

Comments
 (0)