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

rlp.decode_to_bytes doesn't raise with negative decoded len #3

Open
ClementWalter opened this issue Nov 14, 2024 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@ClementWalter
Copy link

ClementWalter commented Nov 14, 2024

Using rlp.decode_to_bytes(encoded_bytes=b'Q59:\xba\xf4\xda\x05\xb7') returns b'' because it silently slices the input data with a negative len

def decode_to_bytes(encoded_bytes: Bytes) -> Bytes:
    """
    Decodes a rlp encoded byte stream assuming that the decoded data
    should be of type `bytes`.

    Parameters
    ----------
    encoded_bytes :
        RLP encoded byte stream.

    Returns
    -------
    decoded : `ethereum.base_types.Bytes`
        RLP decoded Bytes data
    """
    if len(encoded_bytes) == 1 and encoded_bytes[0] < 0x80:
        return encoded_bytes
    elif encoded_bytes[0] <= 0xB7:
        len_raw_data = encoded_bytes[0] - 0x80

----> here len_raw_data = -47

        if len_raw_data >= len(encoded_bytes):
            raise RLPDecodingError
        raw_data = encoded_bytes[1 : 1 + len_raw_data]
        if len_raw_data == 1 and raw_data[0] < 0x80:
            raise RLPDecodingError
        return raw_data

Some other examples

  • encoded_bytes=b'~\xbc\xc5^\xbe\xff' gives len_raw_data = -2 and do return a bytes

I think that if len_raw_data >= len(encoded_bytes) should also check for negative lengths.

@SamWilsn SamWilsn transferred this issue from ethereum/execution-specs Nov 14, 2024
@SamWilsn SamWilsn added the bug Something isn't working label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants