question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
frangiocommented, Aug 30, 2022

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.

0reactions
elv-serbancommented, Aug 30, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found