DatingDapp

First Flight #33
Beginner FriendlyFoundrySolidityNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

tokenURI Can Be Called on Non-Existent Tokens

tokenURI Can Be Called on Non-Existent Tokens

Description:

The tokenURI function attempts to check token ownership, but it does not properly handle non-existent tokens before calling ownerOf(tokenId).

Impact:

Calling tokenURI on an invalid tokenId may cause unexpected behavior or unnecessary computation.
A missing check could result in reverting transactions where they should return a clean error.

Proof of Concept:

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
if (ownerOf(tokenId) == address(0)) { // <---- Problematic check
revert ERC721Metadata__URI_QueryFor_NonExistentToken();
}

ownerOf(tokenId) reverts if tokenId doesn't exist, which is inefficient.
The correct approach is to use _exists(tokenId), which is provided by OpenZeppelin’s ERC721 implementation.

Recommended Mitigation:

Change the tokenURI function to:

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
string memory profileName = _profiles[tokenId].name;
uint256 profileAge = _profiles[tokenId].age;
string memory imageURI = _profiles[tokenId].profileImage;
return string(
abi.encodePacked(
_baseURI(),
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"',
profileName,
'", ',
'"description":"A soulbound dating profile NFT.", ',
'"attributes": [{"trait_type": "Age", "value": ',
Strings.toString(profileAge),
"}], ",
'"image":"',
imageURI,
'"}'
)
)
)
)
);
}
This change ensures that the tokenURI function checks for token existence before proceeding with the metadata retrieval.
---
Updates

Appeal created

n0kto Lead Judge 4 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.

Support

FAQs

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