Use DecodeLimit for decoding XCM messages (#605)

* Use DecodeLimit for decoding XCM messages

* Use decode_all_with_depth_limit in appropriate places

* Use decode_all while decoding byte vector

* cargo fmt
This commit is contained in:
Keith Yeung
2021-09-19 05:29:34 -07:00
committed by GitHub
parent 85bde07641
commit 1aab3349cc
34 changed files with 82 additions and 61 deletions
+16 -6
View File
@@ -31,7 +31,7 @@ mod mock;
#[cfg(test)]
mod tests;
use codec::{Decode, Encode};
use codec::{Decode, DecodeAll, DecodeLimit, Encode};
use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, ChannelStatus, GetChannelInfo, MessageSendError,
ParaId, XcmpMessageFormat, XcmpMessageHandler, XcmpMessageSource,
@@ -44,7 +44,7 @@ use rand_chacha::{
use scale_info::TypeInfo;
use sp_runtime::{traits::Hash, RuntimeDebug};
use sp_std::{convert::TryFrom, prelude::*};
use xcm::{latest::prelude::*, VersionedXcm, WrapVersion};
use xcm::{latest::prelude::*, VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH};
pub use pallet::*;
@@ -249,7 +249,11 @@ impl<T: Config> Pallet<T> {
let have_active = s[index].4 > s[index].3;
let appended = have_active &&
<OutboundXcmpMessages<T>>::mutate(recipient, s[index].4 - 1, |s| {
if XcmpMessageFormat::decode(&mut &s[..]) != Ok(format) {
if XcmpMessageFormat::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &s[..],
) != Ok(format)
{
return false
}
if s.len() + data.len() > max_message_size {
@@ -371,7 +375,10 @@ impl<T: Config> Pallet<T> {
XcmpMessageFormat::ConcatenatedVersionedXcm => {
while !remaining_fragments.is_empty() {
last_remaining_fragments = remaining_fragments;
if let Ok(xcm) = VersionedXcm::<T::Call>::decode(&mut remaining_fragments) {
if let Ok(xcm) = VersionedXcm::<T::Call>::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut remaining_fragments,
) {
let weight = max_weight - weight_used;
match Self::handle_xcm_message(sender, sent_at, xcm, weight) {
Ok(used) => weight_used = weight_used.saturating_add(used),
@@ -394,7 +401,7 @@ impl<T: Config> Pallet<T> {
XcmpMessageFormat::ConcatenatedEncodedBlob => {
while !remaining_fragments.is_empty() {
last_remaining_fragments = remaining_fragments;
if let Ok(blob) = <Vec<u8>>::decode(&mut remaining_fragments) {
if let Ok(blob) = <Vec<u8>>::decode_all(&mut remaining_fragments) {
let weight = max_weight - weight_used;
match Self::handle_blob_message(sender, sent_at, blob, weight) {
Ok(used) => weight_used = weight_used.saturating_add(used),
@@ -588,7 +595,10 @@ impl<T: Config> XcmpMessageHandler for Pallet<T> {
for (sender, sent_at, data) in iter {
// Figure out the message format.
let mut data_ref = data;
let format = match XcmpMessageFormat::decode(&mut data_ref) {
let format = match XcmpMessageFormat::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut data_ref,
) {
Ok(f) => f,
Err(_) => {
debug_assert!(false, "Unknown XCMP message format. Silently dropping message");