Some real-world NFT tokens may support both ERC721 and ERC1155 standards, which may break InfinityExchange::_transferNFTs
The exchange contract checks for ERC721 support before ERC1155, so for tokens implementing both interfaces it transfers only one item per token ID instead of the requested quantity. A buyer paying for multiple tokens of the same ID receives only one.
Originally disclosed on Code4rena
Submitted by PwnedNoMore
Many real-world NFT tokens may support both ERC721 and ERC1155 standards, which may break InfinityExchange::_transferNFTs, i.e., transferring less tokens than expected.
For example, the asset token of The Sandbox Game, a Top20 ERC1155 token on Etherscan, supports both ERC1155 and ERC721 interfaces. Specifically, any ERC721 token transfer is regarded as an ERC1155 token transfer with only one item transferred (token address and implementation).
Assuming there is a user tries to buy two tokens of Sandbox’s ASSETs with the same token id, the actual transferring is carried by InfinityExchange::_transferNFTs which first checks ERC721 interface supports and then ERC1155.
function _transferNFTs(
address from,
address to,
OrderTypes.OrderItem calldata item
) internal {
if (IERC165(item.collection).supportsInterface(0x80ac58cd)) {
_transferERC721s(from, to, item);
} else if (IERC165(item.collection).supportsInterface(0xd9b67a26)) {
_transferERC1155s(from, to, item);
}
}
The code will go into _transferERC721s instead of _transferERC1155s, since the Sandbox’s ASSETs also support ERC721 interface. Then,
function _transferERC721s(
address from,
address to,
OrderTypes.OrderItem calldata item
) internal {
uint256 numTokens = item.tokens.length;
for (uint256 i = 0; i < numTokens; ) {
IERC721(item.collection).safeTransferFrom(from, to, item.tokens[i].tokenId);
unchecked {
++i;
}
}
}
Since the ERC721(item.collection).safeTransferFrom is treated as an ERC1155 transferring with one item (reference), there is only one item actually gets traferred.
That means, the user, who barely know the implementation details of his NFTs, will pay the money for two items but just got one.
Note that the situation of combining ERC721 and ERC1155 is prevalent and poses a great vulnerability of the exchange contract.
Proof of Concept
Check the return values of Sandbox’s ASSETs’s supportInterface, both supportInterface(0x80ac58cd) and supportInterface(0xd9b67a26) return true.
Recommended Mitigation Steps
Reorder the checks,e.g.,
function _transferNFTs(
address from,
address to,
OrderTypes.OrderItem calldata item
) internal {
if (IERC165(item.collection).supportsInterface(0xd9b67a26)) {
_transferERC1155s(from, to, item);
} else if (IERC165(item.collection).supportsInterface(0x80ac58cd)) {
_transferERC721s(from, to, item);
}
}
nneverlander (Infinity) confirmed and resolved:
When an NFT supports both 721 & 1155 interfaces, the code prefers
_transferERC721s- however this ignores the order’snumTokens. This may result in under filling NFTs for an order, at the same cost to the buyer. The warden’s recommendation would address this concern. Or maybe_transferERC721scould requirenumTokens == 1, but that approach would be limiting for this scenario. Since the buyer gets a fraction of what they paid for and it impacts a top20 1155, this seems to be a High risk issue.
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
The contract checks for ERC721 support before ERC1155, so a token supporting both interfaces is handled as ERC721, which transfers one item per token ID rather than the requested quantity.
What the attacker needed
Token supports both ERC721 and ERC1155; buyer orders multiple tokens of same ID
The fix
The checks should be reordered to test ERC1155 support before ERC721, and the project confirmed this was fixed.
How it was found
- A user buys two tokens of the same token ID from a collection that supports both ERC721 and ERC1155.
- The exchange calls _transferNFTs, which checks ERC721 support first and routes to _transferERC721s.
- The ERC721 transfer is treated by the token as an ERC1155 transfer of a single item, so only one item is moved.
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.
Some digital collectibles (NFTs) can follow two different technical standards at once. The exchange code checks for one standard first and uses a transfer method that only moves a single item, even when the buyer paid for several. So the buyer loses money because they get fewer items than they ordered.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.