Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 add checks to avoid panicking when decoding proofs with sp-trie #257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions primitives/file-key-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions primitives/file-key-verifier/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ impl<const H_LENGTH: usize, const CHUNK_SIZE: u64, const SIZE_TO_CHALLENGES: u64
.try_into()
.map_err(|_| ProvenFileKeyError::FingerprintAndTrieHashMismatch)?;

// 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 self.proof.encoded_nodes.iter() {
if encoded_node.len() < 2 {
return Err(ProvenFileKeyError::FailedToDecodeChunkFromProof);
}
}

// This generates a partial trie based on the proof and checks that the root hash matches the `expected_root`.
let (memdb, root) = self
.proof
Expand Down
16 changes: 16 additions & 0 deletions primitives/forest-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ where
return Err("No challenges provided.".into());
}

// 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.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.to_memory_db(Some(root.into())).map_err(|_| {
"Failed to convert proof to memory DB, root doesn't match with expected."
Expand Down Expand Up @@ -235,6 +243,14 @@ where
return Err("Root is empty.".into());
}

// 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.encoded_nodes.iter() {
if encoded_node.len() < 2 {
return Err("Invalid encoded node in the proof.".into());
}
}

// TODO: Understand why `CompactProof` cannot be used directly to construct memdb and modify a partial trie. (it fails with error IncompleteDatabase)
// Convert compact proof to `sp_trie::StorageProof` in order to access the trie nodes.
let (storage_proof, mut root) = proof
Expand Down
Loading