Skip to content
Business logic Audit finding Intermediate

LSP8CompatibleERC721 ’s approve() deviates from ERC-721 specification

The approve() function in LSP8CompatibleERC721 only allows the token owner to call it, whereas the ERC-721 specification also permits an authorised operator of the owner to call it. This deviation could cause incompatibility with protocols that rely on ERC-721 behaviour, such as NFT exchanges where a router contract approved via setApprovalForAll() needs to grant approvals.

MiloTruck Follow

Originally disclosed on Code4rena

Submitted by MiloTruck

The LSP8CompatibleERC721 contract is a wrapper around LSP8, which is meant to function similarly to ERC-721 tokens. One of its implemented functions is ERC-721’s approve():

LSP8CompatibleERC721.sol#L155-L158

    function approve(address operator, uint256 tokenId) public virtual {
        authorizeOperator(operator, bytes32(tokenId));
        emit Approval(tokenOwnerOf(bytes32(tokenId)), operator, tokenId);
    }

As approve() calls authorizeOperator() from the LSP8IdentifiableDigitalAssetCore contract, only the owner of tokenId is allowed to call approve():

LSP8IdentifiableDigitalAssetCore.sol#L105-L113

    function authorizeOperator(
        address operator,
        bytes32 tokenId
    ) public virtual {
        address tokenOwner = tokenOwnerOf(tokenId);

        if (tokenOwner != msg.sender) {
            revert LSP8NotTokenOwner(tokenOwner, tokenId, msg.sender);
        }

However, the implementation above deviates from the ERC-721 specification, which mentions that an “authorized operator of the current owner” should also be able to call approve():

    /// @notice Change or reaffirm the approved address for an NFT
    /// @dev The zero address indicates there is no approved address.
    ///  Throws unless `msg.sender` is the current NFT owner, or an authorized
    ///  operator of the current owner.
    /// @param _approved The new approved NFT controller
    /// @param _tokenId The NFT to approve
    function approve(address _approved, uint256 _tokenId) external payable;

This means, anyone who is an approved operator for tokenId’s owner through setApprovalForAll() should also be able to grant approvals. An example of such behaviour can be seen in Openzeppelin’s ERC721 implementation:

ERC721.sol#L121-L123

        if (_msgSender() != owner && !isApprovedForAll(owner, _msgSender())) {
            revert ERC721InvalidApprover(_msgSender());
        }

Impact

As LSP8CompatibleERC721’s approve() functions differently from ERC-721, protocols that rely on this functionality will be incompatible with LSP8 tokens that inherit from LSP8CompatibleERC721.

For example, in an NFT exchange, users might be required to call setApprovalForAll() for the protocol’s router contract. The router then approves a swap contract, which transfers the NFT from the user to the recipient using transferFrom().

Additionally, developers that expect LSP8CompatibleERC721 to behave exactly like ERC-721 tokens might introduce bugs in their contracts, due to the difference in approve().

Recommended Mitigation

Modify approve() to allow approved operators for tokenId’s owner to grant approvals:

function approve(address operator, uint256 tokenId) public virtual {
    bytes32 tokenIdBytes = bytes32(tokenId);
    address tokenOwner = tokenOwnerOf(tokenIdBytes);

    if (tokenOwner != msg.sender && !isApprovedForAll(tokenOwner, msg.sender)) {
        revert LSP8NotTokenOwner(tokenOwner, tokenIdBytes, msg.sender);
    }

    if (operator == address(0)) {
        revert LSP8CannotUseAddressZeroAsOperator();
    }

    if (tokenOwner == operator) {
        revert LSP8TokenOwnerCannotBeOperator();
    }

    bool isAdded = _operators[tokenIdBytes].add(operator);
    if (!isAdded) revert LSP8OperatorAlreadyAuthorized(operator, tokenIdBytes);

    emit AuthorizedOperator(operator, tokenOwner, tokenIdBytes);
    emit Approval(tokenOwner, operator, tokenId);
}

Assessed type

ERC721

skimaharvey (LUKSO) disputed and commented:

However, the implementation above deviates from the ERC-721 specification, which mentions that an “authorized operator of the current owner” should also be able to call approve():

@MiloTruck - can you please point me to it in the standard because I could not find anything stipulating that?

MiloTruck (warden) commented:

@skimaharvey - It’s mentioned in the @dev natspec comment above approve() in the ERC-721 interface under specification:

    ///  Throws unless `msg.sender` is the current NFT owner, or an authorized
    ///  operator of the current owner.

You can refer to the code block in the issue above as well.

skimaharvey (LUKSO) confirmed and commented:

NVM read it too fast. Seems valid 🙏.


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

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 approve() function calls authorizeOperator(), which only checks if the caller is the token owner, ignoring approved operators for all tokens.

Impact

Protocols that rely on ERC-721 approve() behaviour, such as NFT exchanges where a router contract approved via setApprovalForAll() needs to grant approvals, will be incompatible with LSP8 tokens that inherit from LSP8CompatibleERC721.

What the attacker needed

Token owner has approved an operator via setApprovalForAll; protocol relies on ERC-721 approve() behaviour

The fix

Modify approve() to allow approved operators for the token owner to grant approvals, as shown in the recommended mitigation.

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.

ERC-721 is a standard for NFTs on Ethereum. The standard says that if you have given someone permission to manage all your NFTs (via setApprovalForAll), that person should also be able to approve others to transfer a specific NFT. In this implementation, only the owner can do that, which breaks compatibility with some applications.

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.

More by MiloTruck

View all

35 more disclosed reports, best first.

More from LUKSO

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.