Skip to content
Smart contracts Audit finding Intermediate

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(); use safeTransferFrom() whenever possible
  • The recipient could have logic in the onERC721Received() function, which is only triggered in the safeTransferFrom() function and not in transferFrom(). A notable example of such contracts is the Sudoswap pair:
Solidity
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

https://github.com/Harpieio/contracts/blob/97083d7ce8ae9d85e29a139b1e981464ff92b89e/contracts/Vault.sol#L137

Recommendation

Use safeTransferFrom() when sending out the NFT from the vault.

Diff
- 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.

The original report as published on Sherlock, by CodingNameKiki. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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.

More by CodingNameKiki

View all

2 more disclosed reports, best first.

More from Harpie

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.