#37583 [SC-Low] Incorrect For Annotation Parsing
Submitted on Dec 9th 2024 at 20:30:36 UTC by @anatomist for Attackathon | Ethereum Protocol
Report ID: #37583
Report Type: Smart Contract
Report severity: Low
Target: https://github.com/vyperlang/vyper
Impacts:
(Compiler) Unexpected behavior
Description
Brief/Intro
For loop extracts annotation parsing and constructs ast separately for it. However, insufficient validation allows otherwise illegal code to compile, and create ambiguity in the intended behavior of contracts.
Vulnerability Details
Python for loops does not support type annotation for iterator variable. However, since vyper is a strongly typed language, it requires the type to be specified. This introduces the need for pre-parsing of for loops to extract out the type annotation, "fix" the code.
The extracted types are later manually injected back into the python parsed ast.
However, the implementation does not check that the extracted code is purely an annotation. For example, the following code extracts uint256 = 1
as the annotation, and when parsed along with dummy_target: uint256 = 1
, it also appears to be valid python syntax. However, only the type is used in code generation, and the value associated with it will be discarded.
This means users may specify arbitrary "legal looking" python code as part of the annotation and the code will still compile, which may create ambiguity in what the contract is intended to do. For example, the following code will confuse users on whether the internal function is called.
To make things worse, while the value is ignored in codegen, it is actually respected in certain semantic analysis checks (such as self recursion), which adds to the ambiguity on what the code does.
Impact Details
Ambiguity in contract behavior
Allow illegal code to compile
References
https://github.com/vyperlang/vyper/blob/e98e004235961613c3d769d4c652884b2a242608/vyper/ast/pre_parser.py#L97
https://github.com/vyperlang/vyper/blob/e98e004235961613c3d769d4c652884b2a242608/vyper/ast/parse.py#L308
Proof of Concept
Proof of Concept
Already shown in Vulnerability Details section.
Was this helpful?