NFT Security
NFT (Non-Fungible Token) contracts and marketplaces have unique vulnerabilities including metadata manipulation, signature replay, and marketplace-specific issues.
Authorised Targets Only
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.
// 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++;
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// FIXED: value no longer derives from the sequential ID, so knowing the
// next ID buys nothing. The trait is committed to before minting and
// revealed afterwards from a seed nobody could see in advance.
contract CommitRevealNFT is ERC721 {
uint256 public nextTokenId;
bytes32 public immutable revealSeed;
mapping(uint256 => bytes32) public commitments;
constructor(bytes32 seed_) ERC721("CommitReveal", "CRV") {
revealSeed = seed_;
}
function mint(bytes32 commitment) public payable {
commitments[nextTokenId] = commitment;
_mint(msg.sender, nextTokenId);
nextTokenId++;
}
function traitOf(uint256 tokenId) public view returns (uint256) {
return uint256(keccak256(abi.encodePacked(commitments[tokenId], revealSeed)));
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// FIXED: value no longer derives from the sequential ID, so knowing the
// next ID buys nothing. The trait is committed to before minting and
// revealed afterwards from a seed nobody could see in advance.
contract CommitRevealNFT is ERC721 {
uint256 public nextTokenId;
bytes32 public immutable revealSeed;
mapping(uint256 => bytes32) public commitments;
constructor(bytes32 seed_) ERC721("CommitReveal", "CRV") {
revealSeed = seed_;
}
function mint(bytes32 commitment) public payable {
commitments[nextTokenId] = commitment;
_mint(msg.sender, nextTokenId);
nextTokenId++;
}
function traitOf(uint256 tokenId) public view returns (uint256) {
return uint256(keccak256(abi.encodePacked(commitments[tokenId], revealSeed)));
}
}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.
// 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;
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// FIXED: only the token owner may set the URI, and only while the
// collection is still mutable. Freezing removes the power entirely,
// which is what buyers of a "final" collection are paying for.
contract MutableNFT is ERC721 {
mapping(uint256 => string) private _tokenURIs;
address public immutable collectionOwner;
bool public metadataFrozen;
constructor() ERC721("Mutable", "MUT") {
collectionOwner = msg.sender;
}
function setTokenURI(uint256 tokenId, string memory uri) public {
require(!metadataFrozen, "metadata frozen");
require(ownerOf(tokenId) == msg.sender, "not token owner");
_tokenURIs[tokenId] = uri;
}
// One-way switch: after this, no URI can change again
function freezeMetadata() public {
require(msg.sender == collectionOwner, "not collection owner");
metadataFrozen = true;
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// FIXED: only the token owner may set the URI, and only while the
// collection is still mutable. Freezing removes the power entirely,
// which is what buyers of a "final" collection are paying for.
contract MutableNFT is ERC721 {
mapping(uint256 => string) private _tokenURIs;
address public immutable collectionOwner;
bool public metadataFrozen;
constructor() ERC721("Mutable", "MUT") {
collectionOwner = msg.sender;
}
function setTokenURI(uint256 tokenId, string memory uri) public {
require(!metadataFrozen, "metadata frozen");
require(ownerOf(tokenId) == msg.sender, "not token owner");
_tokenURIs[tokenId] = uri;
}
// One-way switch: after this, no URI can change again
function freezeMetadata() public {
require(msg.sender == collectionOwner, "not collection owner");
metadataFrozen = true;
}
}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.
// 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!
}
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
// FIXED: _mint instead of _safeMint (there is no callback to re-enter),
// a per-transaction cap, and a guard as a backstop for any future
// external call added to this function.
contract FixedMintNFT is ERC721, ReentrancyGuard {
uint256 public nextTokenId;
uint256 public constant MAX_PER_TX = 5;
constructor() ERC721("FixedMint", "FIX") {}
function mint(uint256 quantity) public payable nonReentrant {
require(quantity > 0 && quantity <= MAX_PER_TX, "bad quantity");
for (uint256 i = 0; i < quantity; i++) {
_mint(msg.sender, nextTokenId);
nextTokenId++;
}
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
// FIXED: _mint instead of _safeMint (there is no callback to re-enter),
// a per-transaction cap, and a guard as a backstop for any future
// external call added to this function.
contract FixedMintNFT is ERC721, ReentrancyGuard {
uint256 public nextTokenId;
uint256 public constant MAX_PER_TX = 5;
constructor() ERC721("FixedMint", "FIX") {}
function mint(uint256 quantity) public payable nonReentrant {
require(quantity > 0 && quantity <= MAX_PER_TX, "bad quantity");
for (uint256 i = 0; i < quantity; i++) {
_mint(msg.sender, nextTokenId);
nextTokenId++;
}
}
}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.