CidNFT : Broken tokenURI function
The tokenURI function interpolated the raw 32-byte ABI-encoded integer ID into the URI instead of its decimal string form, so token URIs contained control characters or symbols rather than readable numbers. Offchain tools reading tokenURI could break or display malformed data.
Originally disclosed on Code4rena
Submitted by horsefacts
CidNFT#tokenURI does not convert the uint256 _id argument to a string before interpolating it in the token URI:
/// @notice Get the token URI for the provided ID
/// @param _id ID to retrieve the URI for
/// @return tokenURI The URI of the queried token (path to a JSON file)
function tokenURI(uint256 _id) public view override returns (string memory) {
if (ownerOf[_id] == address(0))
// According to ERC721, this revert for non-existing tokens is required
revert TokenNotMinted(_id);
return string(abi.encodePacked(baseURI, _id, ".json"));
}
This means the raw bytes of the 32-byte ABI encoded integer _id will be interpolated into the token URI, e.g. 0x0000000000000000000000000000000000000000000000000000000000000001 for ID #1.
Most of the resulting UTF-8 strings will be malformed, incorrect, or invalid URIs. For example, token ID #1 will show up as the invisible “start of heading” control character, and ID #42 will show as the asterisk symbol *. URI-unsafe characters will break the token URIs altogether.
Impact
CidNFTtokens will have invalidtokenURIs. Offchain tools that read thetokenURIview may break or display malformed data.
Suggestion
Convert the _id to a string before calling abi.encodePacked. Latest Solmate includes a LibString helper library for this purpose:
import "solmate/utils/LibString.sol";
/// @notice Get the token URI for the provided ID
/// @param _id ID to retrieve the URI for
/// @return tokenURI The URI of the queried token (path to a JSON file)
function tokenURI(uint256 _id) public view override returns (string memory) {
if (ownerOf[_id] == address(0))
// According to ERC721, this revert for non-existing tokens is required
revert TokenNotMinted(_id);
return string(abi.encodePacked(baseURI, LibString.toString(_id), ".json"));
}
Test case
function test_InvalidTokenURI() public {
uint256 id1 = cidNFT.numMinted() + 1;
uint256 id2 = cidNFT.numMinted() + 2;
// mint id1
cidNFT.mint(new bytes[](0));
// mint id2
cidNFT.mint(new bytes[](0));
// These pass — the raw bytes '0000000000000000000000000000000000000000000000000000000000000001' are interpolated as _id.
assertEq(string(bytes(hex"7462643a2f2f626173655f7572692f00000000000000000000000000000000000000000000000000000000000000012e6a736f6e")), cidNFT.tokenURI(id1));
assertEq(string(bytes(hex"7462643a2f2f626173655f7572692f00000000000000000000000000000000000000000000000000000000000000022e6a736f6e")), cidNFT.tokenURI(id2));
// These fail - the generated string on the right is not the expected string on the left.
assertEq("tbd://base_uri/1.json", cidNFT.tokenURI(id1));
assertEq("tbd://base_uri/2.json", cidNFT.tokenURI(id2));
}
OpenCoreCH (Canto Identity) confirmed and commented:
Great catch!
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the developer passed the integer directly into abi.encodePacked, which encodes it as fixed-width binary bytes rather than converting it to a decimal string first.
Impact
CidNFT tokens will have invalid tokenURIs, and offchain tools that read the tokenURI view may break or display malformed data.
What the attacker needed
None
The fix
The ID should be converted to a string before being concatenated, for example with a string helper library, and the project confirmed the finding.
How it was found
- Mint a token to obtain a known token ID.
- Call tokenURI for that ID and compare the returned string with the expected human-readable URI.
- Observe that the returned URI contains raw encoded bytes rather than the decimal ID, so the expected URI assertion fails.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A token URI is the link that tells wallets and marketplaces where to find a token's picture and details. The code glued the token number into that link in its raw binary form rather than as ordinary digits, so the link came out garbled. Anything reading the link would get nonsense instead of a working address.
What is Misconfiguration?
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.