Use `safeTransferFrom()` instead of `transferFrom()` for outgoing erc721 transfers
The vault contract used transferFrom() instead of safeTransferFrom() when sending ERC721 tokens to the caller, so a recipient contract that cannot handle ERC721s could lose the NFT. The report argues this could cause loss of NFTs if the recipient is unable to handle them.
CodingNameKiki Follow with millers.planet, 0xNazgul and 14 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-harpie-judging/tree/main/001-M
Found by
CodingNameKiki, millers.planet, 0xNazgul, cccz, Bnke0x0, Chom, Waze, IEatBabyCarrots, TomJ, Tomo, hickuphh3, pashov, sach1r0, Sm4rty, IllIllI, chainNue, Dravee
Summary
It is recommended to use safeTransferFrom() instead of transferFrom() when transferring ERC721s out of the vault.
Vulnerability Detail
The transferFrom() method is used instead of safeTransferFrom(), which I assume is a gas-saving measure. I however argue that this isn’t recommended because:
- OpenZeppelin’s documentation discourages the use of
transferFrom(); usesafeTransferFrom()whenever possible - The recipient could have logic in the
onERC721Received()function, which is only triggered in thesafeTransferFrom()function and not intransferFrom(). A notable example of such contracts is the Sudoswap pair:
function onERC721Received(
address,
address,
uint256 id,
bytes memory
) public virtual returns (bytes4) {
IERC721 _nft = nft();
// If it's from the pair's NFT, add the ID to ID set
if (msg.sender == address(_nft)) {
idSet.add(id);
}
return this.onERC721Received.selector;
}
- It helps ensure that the recipient is indeed capable of handling ERC721s.
Impact
While unlikely because the recipient is the function caller, there is the potential loss of NFTs should the recipient is unable to handle the sent ERC721s.
Code Snippet
Recommendation
Use safeTransferFrom() when sending out the NFT from the vault.
- IERC721(_erc721Address).transferFrom(address(this), msg.sender, _id);
+ IERC721(_erc721Address).safeTransferFrom(address(this), msg.sender, _id);
Note that the vault would have to inherit the IERC721Receiver contract if the change is applied to Transfer.sol as well.
Harpie Team
Added safeTransferFrom in withdraw function. Fix here.
Lead Senior Watson
Makes sense to be compatible with contracts as recipients. Confirmed fix.
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 vault used transferFrom() instead of safeTransferFrom(), skipping the onERC721Received check that ensures the recipient can handle ERC721 tokens.
Impact
There is a potential loss of NFTs if the recipient contract is unable to handle the sent ERC721s, though this is unlikely because the recipient is the function caller.
What the attacker needed
Recipient is a contract unable to handle ERC721
The fix
The vault was fixed by using safeTransferFrom() in the withdraw function.
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.
When a smart contract sends an NFT to another contract, it should use a special safe transfer function that checks the receiver can handle NFTs. The vault used a plain transfer function that skips this check, so an NFT could get stuck if the receiver contract cannot process it.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.