Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: medium
Invalid

Predictable TokenID Generation in EggHuntGame Contract

Summary

The tokenID generation mechanism in the EggHuntGame contract currently suffers from significant vulnerabilities, including predictable sequencing, centralised control, and collision risks. These issues expose the system to frontrunning attacks, manipulation, and potential duplication of tokenIDs. A comprehensive improvement plan has been proposed to address these flaws by implementing cryptographic ID generation, burn tracking, decentralised validation layers, and enhanced interface support.

Vulnerability Details

1. Predictable Sequencing

The current implementation uses a linear counter (eggCounter) to assign tokenIDs. This approach makes future tokenIDs predictable, enabling attackers to frontrun valuable metadata assignments.

2. Centralised Control

The reliance on a single counter creates a dependency bottleneck and introduces risks of manipulation or overflow attacks.

3. Collision Risks

There is no mechanism to prevent duplicate tokenIDs from multiple sources or to track burned tokens, leading to potential reuse of retired IDs.

Impact

Security Risks

  • Frontrunning Attacks: Predictable tokenIDs allow attackers to preemptively claim tokens with desirable metadata.

  • Manipulation: Centralised control of the counter increases the risk of malicious interference.

  • Duplicate Tokens: Lack of collision protection can result in duplicate or reused tokenIDs.

Functional Limitations

  • No Burn Tracking: Burned tokens can be unintentionally resurrected due to missing tracking mechanisms.

  • Inconsistent Validation: Without decentralised validation layers, the system is vulnerable to arbitrary ID generation across contracts.

Tools Used

Manual review

Recommendations

1. Cryptographic ID Generation

Replace the sequential counter-based ID assignment with a cryptographically secure mechanism that incorporates multiple entropy sources such as block randomness, previous block hash, recipient address, contract address, and sequence number.

Implementation Example:

function generateTokenId(address player) internal returns (uint256) {
bytes32 hash = keccak256(abi.encodePacked(
block.prevrandao,
uint256(blockhash(block.number - 1)),
player,
address(this),
eggCounter
));
eggCounter++;
return uint256(hash);
}

Benefits:

  • Unpredictable tokenID sequences

  • Mitigates metadata frontrunning risks

  • Ensures unique per-player/block combinations

2. Burn Tracking and Re-mint Protection

Introduce a mapping (_burnedTokens) to permanently track burned tokenIDs and prevent their reuse.

Implementation Example:

mapping(uint256 => bool) private _burnedTokens;
function _burn(uint256 tokenId) internal virtual override {
super._burn(tokenId);
_burnedTokens[tokenId] = true;
}
function mintEgg(address to, uint256 tokenId) external {
require(!_exists(tokenId), "ERC721: token already minted");
require(!_burnedTokens[tokenId], "TokenID permanently retired");
_mint(to, tokenId);
}

Benefits:

  • Prevents resurrection of burned tokens

  • Ensures unique ownership of tokenIDs

3. Decentralised Validation Layer

Implement cross-contract validation by requiring proof of ID creation using cryptographic parameters shared between contracts.

Implementation Example:

mapping(uint256 => bytes32) public idCreationProof;
function mintEgg(address to, uint256 tokenId) external {
bytes32 expectedProof = keccak256(abi.encodePacked(
block.prevrandao,
uint256(blockhash(block.number - 1)),
to,
msg.sender,
totalSupply + 1
));
require(idCreationProof[tokenId] == expectedProof, "Invalid ID proof");
_mint(to, tokenId);
idCreationProof[tokenId] = bytes32(0); // Invalidate proof
}

Benefits:

  • Ensures system-wide consistency in ID generation

  • Prevents arbitrary ID creation across contracts

4. Enhanced Interface Support

Adopt the ERC721Enumerable extension for standardised enumeration and robust tracking of tokens.

Implementation Example:

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract EggstravaganzaNFT is ERC721Enumerable {
// Built-in enumeration functions like totalSupply(), tokenByIndex(), etc.
}

Benefits:

  • Improved interoperability with external systems

  • Eliminates need for manual totalSupply tracking

  • Provides built-in duplicate prevention mechanisms

Updates

Lead Judging Commences

m3dython Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Design choice

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.