GivingThanks

First Flight #28
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Invalid

abi.encodePacked() Usage with Dynamic Types Can Lead to Hash Collisions

Description

In the GivingThanks contract, the function _createTokenURI uses abi.encodePacked() to generate a JSON metadata string. While this usage does not currently introduce a vulnerability since it's used only for string concatenation, using abi.encodePacked() with dynamic types (e.g., string or bytes) can lead to potential hash collisions, especially if this data were to be hashed with keccak256(). The concern arises because abi.encodePacked() lacks delimiter information for concatenated dynamic types, which can create ambiguity in the final encoded string. This could lead to potential vulnerabilities in the future if further dynamic data is added or if the encoded result is used as a hash key.

Impact

If dynamic data were added or this approach were modified to use keccak256 for hashing the abi.encodePacked() result, it could allow for hash collisions, leading to potential exploits or logic errors. This could affect metadata uniqueness, token ownership, or the integrity of the data stored.

Proof of Concept

Consider a hypothetical case where the JSON metadata format is extended to include a string description or other dynamic content. If multiple dynamic fields are concatenated and then hashed, it could be possible to produce the same hash for different data, causing potential data integrity issues.

Example:

function _createTokenURI(string memory description, address donor, uint256 date, uint256 amount) internal pure returns (string memory) {
string memory json = string(
abi.encodePacked(
'{"description":"',
description,
'","donor":"',
Strings.toHexString(uint160(donor), 20),
'","date":"',
Strings.toString(date),
'","amount":"',
Strings.toString(amount),
'"}'
)
);
// Potential for ambiguity if `description` or other fields contain dynamic content
}

Recommended Mitigation

Avoid abi.encodePacked() for Concatenating Dynamic Types: Use abi.encode() instead to provide type separation and avoid potential ambiguity in encoding.
Enforce String Encoding Standards: If additional dynamic types are added, consider adding explicit delimiters between fields or using structured data encoding formats such as abi.encode to prevent concatenation ambiguities.
Hash Individual Fields Separately: If hashing is required, hash individual fields or use structured encoding functions.

Updates

Lead Judging Commences

n0kto Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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