HardhatDeFi
15,000 USDC
View results
Submission Details
Severity: high
Invalid

`abi.encodePacked()` should not be used with dynamic types when passing the result to a hash function such as `keccak256()`

Description: Use abi.encode() instead which will pad items to 32 bytes, which will prevent hash collisions (e.g. abi.encodePacked(0x123,0x456) => 0x123456 => abi.encodePacked(0x1,0x23456), but abi.encode(0x123,0x456) => 0x0...1230...456). Unless there is a compelling reason, abi.encode should be preferred. If there is only one argument to abi.encodePacked() it can often be cast to bytes() or bytes32() instead.
If all arguments are strings and or bytes, bytes.concat() should be used instead.

Impact:

  • Hash collisions can lead to unintended behavior or vulnerabilities, especially if the hash is used for critical operations like identifying unique items or verifying data integrity.

  • This could potentially allow attackers to manipulate the contract's logic by exploiting hash collisions.

Proof of Concept: Consider the following example where abi.encodePacked() is used:

string(abi.encodePacked("w", _collateralTokenContract.symbol()))
If _collateralTokenContract.symbol() returns a dynamic type, the concatenation might lead to collisions, as shown in the example:
abi.encodePacked(0x123,0x456) results in 0x123456
abi.encodePacked(0x1,0x23456) also results in 0x123456

Recommended Mitigation:

  1. Use abi.encode() Instead: Use abi.encode() instead of abi.encodePacked() to ensure that each argument is padded to 32 bytes, preventing hash collisions.

Example:

string(abi.encode("w", _collateralTokenContract.symbol()))
  1. Use bytes.concat() for Strings and Bytes: If all arguments are strings or bytes, consider using bytes.concat() to safely concatenate them without risk of collision.

Example:

string(bytes.concat(bytes("w"), bytes(_collateralTokenContract.symbol())))
  1. Cast Single Arguments: If there is only one argument to abi.encodePacked(), it can often be cast to bytes() or bytes32() directly.

Example:

bytes32(bytes(_collateralTokenContract.symbol()))
Updates

Lead Judging Commences

bube Lead Judge 9 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.