#37582 [SC-Low] Incorrect HexString Parsing Leads To Compilation Error Or Type Confusion
Was this helpful?
Was this helpful?
Submitted on Dec 9th 2024 at 20:26:34 UTC by @anatomist for
Report ID: #37582
Report Type: Smart Contract
Report severity: Low
Target: https://github.com/vyperlang/vyper
Impacts:
(Compiler) Incorrect bytecode generation leading to incorrect behavior
(Compiler) Unexpected behavior
(Compiler) Semantic analysis errors
Hex string pre-parsing without considering position adjustments may result in mismatched offsets, and eventually cause compiler failure or type confusions.
Vyper supports several custom syntax, and one of those are HexString, written in the x'01234'
syntax, and treated as Bytes[]
literal values. Since HexString is not legal python syntax, a pre-parsing pass must be made to "fix" the code before passing it to python.ast module for parsing. The fixing rule is relatively simple, it just strips the leading x
, and track occurances in hex_string_locations
.
Later when visiting ast nodes, the hex_string_locations
are looked up and used to annotate ast nodes as Bytes
type instead of Str
type.
However, it is overlooked the pre-parsing of HexString may
Get placed in a location where there were prior code adjustments that changes "fixed" occurance locations
HexString consumes leading x
and may change "fixed" code locations itself.
This has several implications. The immediate consequence is compilation failure. For example, the following code fails to compile while it should
Slightly more consequential, code that should fail to compile can now compile fine, and suffer a type confusion + incorrect value interpretation at the same time. For example, the following code shouldn't compile, but it does, and it incorrectly passes "6161"
as second argument to FooBar.test
instead of b"\x61\x61"
.
Additionally, the state machine of HexString pre-parser is also slightly flawed, and allows code such as the following to compile.
Compilation failure for legal code
Potential type confusion when coupled with a user mistake in type specification
Allow illegal code to compile
https://github.com/vyperlang/vyper/blob/e98e004235961613c3d769d4c652884b2a242608/vyper/ast/pre_parser.py#L136
https://github.com/vyperlang/vyper/blob/e98e004235961613c3d769d4c652884b2a242608/vyper/ast/pre_parser.py#L282
https://github.com/vyperlang/vyper/blob/e98e004235961613c3d769d4c652884b2a242608/vyper/ast/parse.py#L398
Already shown in Vulnerability Details section.