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

type mismatch when converting bytes to Bytes #4453

Open
cyberthirst opened this issue Jan 19, 2025 · 0 comments
Open

type mismatch when converting bytes to Bytes #4453

cyberthirst opened this issue Jan 19, 2025 · 0 comments
Labels
needs triage needs triage

Comments

@cyberthirst
Copy link
Collaborator

credits: @impermanentW (Whitehat) for Attackathon | Ethereum Protocol
reportid: #38693

According to the Vyper doc (see ref 1) a small Python reference implementation for conversion logic is maintained as part of Vyper’s test suite (see ref 2)

However behavior of the compiler does not match the reference when it comes to BytesM to Bytes[] conversion
Vulnerability Details

The reference specifies the BytesM_T -> Bytes_T conversion should be possible if the size of the output type is at least as big as the in type (see ref 3)

But the compiler does not implement that behavior correctly, this is done in vyper/builtins/_convert.py(see ref 4) which does not allow bytesM as an input type for to_bytes()
Impact Details

Conversions that should work according to the reference are throwing an error at compile time.

References

    https://docs.vyperlang.org/en/stable/types.html#type-conversions
    https://github.com/vyperlang/vyper/blob/v0.4.0/tests/functional/builtins/codegen/test_convert.py
    https://github.com/vyperlang/vyper/blob/e9db8d9f7486eae38f5b86531629019ad28f514e/tests/functional/builtins/codegen/test_convert.py#L83-L86
    https://github.com/vyperlang/vyper/blob/e9db8d9f7486eae38f5b86531629019ad28f514e/vyper/builtins/_convert.py#L447-L449

Proof of concept

Compile the following code using Vyper v0.4.0

#pragma version ^0.4.0

def test(test: bytes1) -> Bytes[1]:
    x: Bytes[1] = convert(test, Bytes[1])
    return x

Fails with vyper.exceptions.TypeMismatch: Can't convert bytes1 to Bytes[1] even though both _bits_of_type() are equal so the conversion should be doable according to the reference

@cyberthirst cyberthirst added the needs triage needs triage label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage needs triage
Projects
None yet
Development

No branches or pull requests

1 participant