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(engine-transactions): Prevent panic on empty input #573

Merged
merged 1 commit into from
Aug 12, 2022
Merged
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
35 changes: 34 additions & 1 deletion engine-transactions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ impl TryFrom<&[u8]> for EthTransactionKind {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes[0] == eip_2930::TYPE_BYTE {
if bytes.is_empty() {
Err(Error::EmptyInput)
} else if bytes[0] == eip_2930::TYPE_BYTE {
Ok(Self::Eip2930(eip_2930::SignedTransaction2930::decode(
&Rlp::new(&bytes[1..]),
)?))
Expand Down Expand Up @@ -181,6 +183,7 @@ impl NormalizedEthTransaction {
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Error {
UnknownTransactionType,
EmptyInput,
// Per the EIP-2718 spec 0xff is a reserved value
ReservedSentinel,
InvalidV,
Expand All @@ -200,6 +203,7 @@ impl Error {
pub fn as_str(&self) -> &'static str {
match self {
Self::UnknownTransactionType => "ERR_UNKNOWN_TX_TYPE",
Self::EmptyInput => "ERR_EMPTY_TX_INPUT",
Self::ReservedSentinel => "ERR_RESERVED_LEADING_TX_BYTE",
Self::InvalidV => "ERR_INVALID_V",
Self::EcRecover => "ERR_ECRECOVER",
Expand Down Expand Up @@ -244,3 +248,32 @@ fn vrs_to_arr(v: u8, r: U256, s: U256) -> [u8; 65] {
result[64] = v;
result
}

#[cfg(test)]
mod tests {
use super::{Error, EthTransactionKind};
use crate::{eip_1559, eip_2930};

#[test]
fn test_try_parse_empty_input() {
assert!(matches!(
EthTransactionKind::try_from([].as_ref()),
Err(Error::EmptyInput)
));

// If the first byte is present, then empty bytes will be passed in to
// the RLP parsing. Let's also check this is not a problem.
assert!(matches!(
EthTransactionKind::try_from([eip_1559::TYPE_BYTE].as_ref()),
Err(Error::RlpDecodeError(_))
));
assert!(matches!(
EthTransactionKind::try_from([eip_2930::TYPE_BYTE].as_ref()),
Err(Error::RlpDecodeError(_))
));
assert!(matches!(
EthTransactionKind::try_from([0x80].as_ref()),
Err(Error::RlpDecodeError(_))
));
}
}