SWC-133_encodePacked
vulnerability: Using
abi.encodePacked()
with multiple variable length arguments can, in certain situations, lead to a hash collision. Sinceabi.encodePacked()
packs all elements in order regardless of whether they’re part of an array, you can move elements between arrays and, so long as all elements are in the same order, it will return the same encoding. In a signature verification situation, an attacker could exploit this by modifying the position of elements in a previous function call to effectively bypass authorization.remediation: When using
abi.encodePacked()
, it’s crucial to ensure that a matching signature cannot be achieved using different parameters. To do so, either do not allow users access to parameters used inabi.encodePacked()
, or use fixed length arrays. Alternatively, you can simply useabi.encode()
instead.
vulnerable contract
1 | pragma solidity ^0.5.0; |
fixed_1
1 | pragma solidity ^0.5.0; |
fixed_2
1 | pragma solidity ^0.5.0; |