Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Restricted RLP decoding to match the RLP spec and disallow leading zeros ([#335])

[#335]: https://github.com/recmo/uint/pulls/335

## [1.11.0] - 2023-10-27

### Added
Expand Down
43 changes: 41 additions & 2 deletions src/support/alloy_rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MAX_BITS: usize = 55 * 8;

/// Allows a [`Uint`] to be serialized as RLP.
///
/// See <https://eth.wiki/en/fundamentals/rlp>
/// See <https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/>
impl<const BITS: usize, const LIMBS: usize> Encodable for Uint<BITS, LIMBS> {
#[inline]
fn length(&self) -> usize {
Expand Down Expand Up @@ -72,11 +72,24 @@ impl<const BITS: usize, const LIMBS: usize> Encodable for Uint<BITS, LIMBS> {

/// Allows a [`Uint`] to be deserialized from RLP.
///
/// See <https://eth.wiki/en/fundamentals/rlp>
/// See <https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/>
impl<const BITS: usize, const LIMBS: usize> Decodable for Uint<BITS, LIMBS> {
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, Error> {
let bytes = Header::decode_bytes(buf, false)?;

// The RLP spec states that deserialized positive integers with leading zeroes
// get treated as invalid.
//
// See:
// https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/
//
// To check this, we only need to check if the first byte is zero to make sure
// there are no leading zeros
if !bytes.is_empty() && bytes[0] == 0 {
return Err(Error::LeadingZero);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RLP spec explicitly says

integers must be represented in big-endian binary form with no leading zeroes (thus making the integer value zero equivalent to the empty byte array).

meaning that the old accepting behaviour was not according to spec and a bug. Fixing bugs is not a breaking change.

There's 'robustness principle' argument that could be made to make this error optionally a warning (i.e. introduce a non-strict mode). But that seems more effort than it's worth. Unless someone volunteers the strict mode should be default.


Self::try_from_be_slice(bytes).ok_or(Error::Overflow)
}
}
Expand Down Expand Up @@ -148,4 +161,30 @@ mod test {
});
});
}

#[test]
fn test_invalid_uints() {
// these are non-canonical because they have leading zeros
assert_eq!(
U256::decode(&mut &hex!("820000")[..]),
Err(Error::LeadingZero)
);
// 00 is not a valid uint
// See https://github.com/ethereum/go-ethereum/blob/cd2953567268777507b1ec29269315324fb5aa9c/rlp/decode_test.go#L118
assert_eq!(U256::decode(&mut &hex!("00")[..]), Err(Error::LeadingZero));
// these are non-canonical because they can fit in a single byte, i.e.
// 0x7f, 0x33
assert_eq!(
U256::decode(&mut &hex!("8100")[..]),
Err(Error::NonCanonicalSingleByte)
);
assert_eq!(
U256::decode(&mut &hex!("817f")[..]),
Err(Error::NonCanonicalSingleByte)
);
assert_eq!(
U256::decode(&mut &hex!("8133")[..]),
Err(Error::NonCanonicalSingleByte)
);
}
}
19 changes: 17 additions & 2 deletions src/support/fastrlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MAX_BITS: usize = 55 * 8;

/// Allows a [`Uint`] to be serialized as RLP.
///
/// See <https://eth.wiki/en/fundamentals/rlp>
/// See <https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/>
impl<const BITS: usize, const LIMBS: usize> Encodable for Uint<BITS, LIMBS> {
#[inline]
fn length(&self) -> usize {
Expand Down Expand Up @@ -72,16 +72,31 @@ impl<const BITS: usize, const LIMBS: usize> Encodable for Uint<BITS, LIMBS> {

/// Allows a [`Uint`] to be deserialized from RLP.
///
/// See <https://eth.wiki/en/fundamentals/rlp>
/// See <https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/>
impl<const BITS: usize, const LIMBS: usize> Decodable for Uint<BITS, LIMBS> {
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, DecodeError> {
// let bytes = Header::decode_bytes(buf, false)?;
let header = Header::decode(buf)?;
if header.list {
return Err(DecodeError::UnexpectedList);
}

let bytes = &buf[..header.payload_length];
*buf = &buf[header.payload_length..];

// The RLP spec states that deserialized positive integers with leading zeroes
// get treated as invalid.
//
// See:
// https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/
//
// To check this, we only need to check if the first byte is zero to make sure
// there are no leading zeros
if !bytes.is_empty() && bytes[0] == 0 {
return Err(DecodeError::LeadingZero);
}

Self::try_from_be_slice(bytes).ok_or(DecodeError::Overflow)
}
}
Expand Down