_isApprovedOrOwner approves a null spender
See original GitHub issue_isApprovedOrOwner approves a null spender:
getApproved(tokenId) == spender
It would be safer to check for a non-null spender or at a minimum document the requirement that the caller check for it.
💻 Environment
I have tested a relatively old version but the code hasn’t changed in the latest:
- solidity 0.6.16
📝 Details
getApproved returns 0 when the approval doesn’t exist.
function getApproved(uint256 tokenId) public view returns (address) {
require(_exists(tokenId));
return _tokenApprovals[tokenId];
}
_isApprovedOrOwner
will return true if spender is address(0) and getApproved(tokenId) return 0
/**
* @dev Returns whether `spender` is allowed to manage `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
address owner = ERC721.ownerOf(tokenId);
return (spender == owner || isApprovedForAll(owner, spender) || getApproved(tokenId) == spender);
}
🔢 Code to reproduce bug
Any call with spender = address(0) will produce the problem.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
ERC 721 - OpenZeppelin Docs
Approves another address to transfer the given token ID The zero address indicates ... Returns whether the given spender can transfer a given...
Read more >() Token Tracker | PolygonScan
Token Tracker on PolygonScan shows the price of the Token $0.00, total supply 2, number of holders 1 and updated information of the...
Read more >chikn feed (FEED) Token Tracker | SnowTrace
Filtered by Token Holder (Null Address: 0x000…000) ... function approve(address spender, uint256 amount) public virtual override returns (bool) ...
Read more >MoonDAONFT (MDAO) Token Tracker | Moonbeam
MoonScan allows you to explore and search the Moonbeam blockchain for transactions, addresses, tokens, prices and other activities taking place on Moonbeam ...
Read more >Error: VM Exception while processing transaction: reverted ...
As explained in this thread, the owner has to re-approve the NFT-market contract to change the ownership on his behalf.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is an interesting case for keeping the checks against non-zero addresses in the code. It’s definitely an easy mistake for devs to directly pass the result of
ecrecover
to functions without considering that it could return zero and have undesired consequences.Personally, I would consider adding this check. I wonder how much additional runtime and deployment cost it adds, but I can’t imagine it will be too much.
Will reopen to continue the discussion.
Yes agreed. I still think it is worth adding a prerequisites or requirements section to the function comment when there is potential for mistakes like this but this is up to you.
Since there are no code changes needed, let me close this and you can decide if there is a good way to document to avoid traps.