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: disallow leading zeros in RLP decoding #335

Merged
merged 10 commits into from
Oct 31, 2023
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
Rjected marked this conversation as resolved.
Show resolved Hide resolved
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 @@

/// 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 @@

/// 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);

Check warning on line 97 in src/support/fastrlp.rs

View check run for this annotation

Codecov / codecov/patch

src/support/fastrlp.rs#L97

Added line #L97 was not covered by tests
}

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