From a52d92cffc39e24c4abc5b6f120aa8531ce9b2d1 Mon Sep 17 00:00:00 2001 From: Tobi Demeco Date: Thu, 14 Nov 2024 15:39:34 -0300 Subject: [PATCH] fix: :bug: add checks to avoid panicking when decoding proofs with `sp-trie` --- primitives/file-key-verifier/src/lib.rs | 8 ++++++++ primitives/file-key-verifier/src/types.rs | 8 ++++++++ primitives/forest-verifier/src/lib.rs | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/primitives/file-key-verifier/src/lib.rs b/primitives/file-key-verifier/src/lib.rs index 8d4c27d54..3e84bb42d 100644 --- a/primitives/file-key-verifier/src/lib.rs +++ b/primitives/file-key-verifier/src/lib.rs @@ -80,6 +80,14 @@ where .try_into() .map_err(|_| "Failed to convert fingerprint to a hasher output.")?; + // Basic sanity check to make sure the received compact proof won't panic + // TODO: remove this after [this PR](https://github.com/paritytech/polkadot-sdk/pull/6486) is merged. + for encoded_node in proof.proof.encoded_nodes.iter() { + if encoded_node.len() < 2 { + return Err("Invalid encoded node in the proof.".into()); + } + } + // This generates a partial trie based on the proof and checks that the root hash matches the `expected_root`. let (memdb, root) = proof .proof diff --git a/primitives/file-key-verifier/src/types.rs b/primitives/file-key-verifier/src/types.rs index 2057ce288..1d652bb16 100644 --- a/primitives/file-key-verifier/src/types.rs +++ b/primitives/file-key-verifier/src/types.rs @@ -62,6 +62,14 @@ impl