The MondrianWallet
contract suffers from a vulnerability related to the distribution and randomness of the non-fungible tokens (NFTs) it mints. The randomness is compromised because the token ID
is used to assign the art, which is not random and allows users to predict which art they will receive based on the token ID. Additionally, the distribution is flawed because, despite having only four art options, the contract uses the %10
operation, leading to an uneven distribution among the art options.
The vulnerability arises from the tokenURI
function, which determines the art of the NFT based on the modulo operation tokenId % 10
(each user based on tokenId
can predict the ART they will receive, and due to the use of operation %10 the distribution of NFTs will be bad because there are only four types of ART). This operation results in a non-random and bad distribution of NFTs, where distribution is predictable and 30% of tokens are associated with each of the first three art options (ART_ONE
, ART_TWO
, and ART_THREE
each 10%), while 70% of tokens are associated with ART_FOUR
.
To prove the vulnerability in the MondrianWallet contract regarding the poor distribution and randomness of NFTs, we first need to introduce a dummy mint
function. This function, defined as mint, will allow minting of NFTs by incrementing a token ID counter
. The function will look like this:
Next, we write a test to mint
a series of NFTs and verify the assigned art URIs
. The test, written in JavaScript using the Hardhat framework, will demonstrate the predictable and uneven distribution of the NFTs. By minting 10 tokens, we will be able to observe that the distribution is not random. Specifically, we can predict which art will be assigned to each token based on the token ID modulo 10. The code for the test is as follows:
The analysis of the test results demonstrates that the distribution of art among the NFTs is bad and not random. Based on the modulo arithmetic, tokens that have IDs
ending with 3 through 9 are always assigned the same art URI (ART_FOUR), showing a 70% concentration. Meanwhile, the other art options are assigned only 30% of the time collectively (ART_ONE
, ART_TWO
and ART_THREE
each 10%). This predictable and uneven distribution proves that the current implementation of the tokenURI
function does not provide a fair or random assignment of art to the NFTs.
The impact of this vulnerability is significant in terms of user trust and the perceived value of the NFTs. The highly predictable and uneven distribution results in most users receiving the same art option (ART_FOUR
), diminishing the rarity and desirability of the NFTs. This unfair assignment could lead to user dissatisfaction and a loss of trust in the protocol, as the NFTs minted do not reflect a fair or random allocation of art, which is essential for maintaining the integrity and appeal of the NFT collection.
Manual code review
Hardhat
To solve the identified vulnerabilities, consider using RANDOMIZER.AI
, which provides robust and secure randomness. This solution works on both Ethereum
and ZkSync
, ensuring a consistent and fair distribution of art assignments. By integrating RANDOMIZER.AI
, the assignment of art pieces to tokens can be reliably randomized at the time of minting, improving the overall security and functionality of the NFT distribution process.
To interact with RANDOMIZER.AI
, we will add the following interface:
To improve the distribution and randomness of the assigned art the following code should be added to the contract. The _assignArt
function should be called at the time of minting to assign art to the token, ensuring each token receives an art piece immediately upon creation. The assigned art should be stored in a mapping(uint256 => string) called IdAndItsArt
, guaranteeing consistent and retrievable art for each token ID. The tokenURI
function should be updated to retrieve the assigned art from this mapping, making it deterministic and ensuring it always returns the correct art.
By generating a random value and performing an operation %4 on the random value, we solved the issue of bad randomness and uneven distribution of art among NFTs. This ensures a more secure, fair, and predictable assignment of art to each minted token.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.