10 min read

NFT

NFT Security

NFT (Non-Fungible Token) contracts and marketplaces have unique vulnerabilities including metadata manipulation, signature replay, and marketplace-specific issues.

Authorised Targets Only

Deploy these contracts on Foundry/Anvil, Hardhat, or a testnet you control. Exploiting a live collection or marketplace that holds other people's assets is theft — unauthorised exploitation is a criminal offence in most jurisdictions and may also be a securities or commodities violation.

Common NFT Vulnerabilities

All samples target solc 0.8.34 with OpenZeppelin Contracts v5. Each pair is a complete contract, so the vulnerable and fixed versions can be deployed side by side and attacked on a local chain.

1. Predictable Token IDs

A counter is not a secret — the mempool shows every mint before it lands. That only becomes a vulnerability when value is derived from the ID itself, so the fix is either to remove the derivation or to commit to the trait before revealing it.

nft-id-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: the token ID is public before the mint lands, so anything
// derived from it - rarity, a bundled trait, a reserved number - can be
// front-run by watching the mempool.
contract VulnerableNFT is ERC721 {
    uint256 public nextTokenId;

    constructor() ERC721("Vulnerable", "VULN") {}

    function mint() public payable {
        _mint(msg.sender, nextTokenId);  // Predictable!
        nextTokenId++;
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: the token ID is public before the mint lands, so anything
// derived from it - rarity, a bundled trait, a reserved number - can be
// front-run by watching the mempool.
contract VulnerableNFT is ERC721 {
    uint256 public nextTokenId;

    constructor() ERC721("Vulnerable", "VULN") {}

    function mint() public payable {
        _mint(msg.sender, nextTokenId);  // Predictable!
        nextTokenId++;
    }
}

2. Mutable Metadata

Whoever controls the URI controls what the token appears to be. An unauthenticated setter means any holder's NFT can be repointed at phishing content, and it also means a "revealed" collection can be re-rolled.

nft-metadata-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: anyone can rewrite any token's metadata at any time
contract ChangeableNFT is ERC721 {
    mapping(uint256 => string) private _tokenURIs;

    constructor() ERC721("Changeable", "CHG") {}

    function setTokenURI(uint256 tokenId, string memory uri) public {
        // No ownership check!
        _tokenURIs[tokenId] = uri;
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: anyone can rewrite any token's metadata at any time
contract ChangeableNFT is ERC721 {
    mapping(uint256 => string) private _tokenURIs;

    constructor() ERC721("Changeable", "CHG") {}

    function setTokenURI(uint256 tokenId, string memory uri) public {
        // No ownership check!
        _tokenURIs[tokenId] = uri;
    }
}

3. Reentrancy Through _safeMint

_safeMint calls onERC721Received on a contract recipient, and that call happens inside the mint loop. A recipient contract can re-enter mint() before the first loop finishes. OpenZeppelin v5 rejects a reentrant _safeMint for the same token ID, but that leaves the counter and price accounting to yours.

nft-mint-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: _safeMint calls onERC721Received on a contract recipient,
// which runs before the loop has finished. Re-entering mint() during
// that callback mints through a second loop with the same counter state.
contract ReentrableNFT is ERC721 {
    uint256 public nextTokenId;

    constructor() ERC721("Reentrable", "REENT") {}

    function mint(uint256 quantity) public payable {
        for (uint256 i = 0; i < quantity; i++) {
            _safeMint(msg.sender, nextTokenId++);
            // safeMint calls onERC721Received - reentrancy!
        }
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// VULNERABLE: _safeMint calls onERC721Received on a contract recipient,
// which runs before the loop has finished. Re-entering mint() during
// that callback mints through a second loop with the same counter state.
contract ReentrableNFT is ERC721 {
    uint256 public nextTokenId;

    constructor() ERC721("Reentrable", "REENT") {}

    function mint(uint256 quantity) public payable {
        for (uint256 i = 0; i < quantity; i++) {
            _safeMint(msg.sender, nextTokenId++);
            // safeMint calls onERC721Received - reentrancy!
        }
    }
}

Marketplace Attacks

Most marketplace losses do not come from a contract bug at all. They come from a signature that was valid when it was made and still valid months later, or from an approval the user granted once and forgot.

  • Signature replay: a listing signature is valid until it is cancelled or a nonce is spent, so it can be re-submitted — and, without a chain ID in the digest, replayed on a fork.
  • Stale listings: the OpenSea Wyvern incident in 2022 was this pattern; users who had listed early found old low-price listings still active and bought out. OpenSea has used Seaport since 2022, so review that order format rather than Wyvern's.
  • Approval scope: a marketplace needs setApprovalForAll, which is a standing right to move every token in the collection — so a compromised marketplace contract is a collection-wide drain.

Testing checklist for a marketplace integration: verify the signing digest includes chain ID, marketplace address and a per-order nonce with a deadline; confirm cancellation actually invalidates; confirm ownership and approval is re-checked at fill time rather than at listing time; confirm metadata mutability and royalty enforcement behave as documented.