is passed to the getWord function, which will return the element of the wordsList corresponding to the id passed as parameter.
However the first element of the list (”Acai”) will be returned for 2 distinct values of id respectively 0 and and 1.
This endangers the randomness of the function and could lead to potential exploits.
For id == 0, this function will return wordsList[id] i.e. wordsList[0] i.e. ”Acai”.
For id==1, this function will return wordsList[id - 1] i.e. wordsList[0] i.e. ”Acai”.
This means elements from wordsList don’t have the same probability of being picked, which is bad practice for a randomizing function. This behavior should be fixed.
This will ensure all words in wordsList have an equal probability of being picked.
Alternatively, the returnIndex should at least use a require statement and a comment to describe function’s behavior and inputs to avoid confusion of the user. Such statement would look like
require(id <= 100, “id should be lesser or equal to 100”);
Lines of code
https://github.com/code-423n4/2023-10-nextgen/blob/main/smart-contracts/XRandoms.sol#L40-L43
Vulnerability details
Impact
In the
randomWord()
function, the numberis passed to the
getWord
function, which will return the element of thewordsList
corresponding to theid
passed as parameter. However the first element of the list (”Acai”
) will be returned for 2 distinct values ofid
respectively 0 and and 1. This endangers the randomness of the function and could lead to potential exploits.Proof of Concept
Let’s have a look at the
getWord
functionFor
id == 0
, this function will returnwordsList[id]
i.e.wordsList[0]
i.e.”Acai”
. Forid==1
, this function will returnwordsList[id - 1]
i.e.wordsList[0]
i.e.”Acai”
. This means elements fromwordsList
don’t have the same probability of being picked, which is bad practice for a randomizing function. This behavior should be fixed.Tools Used
Visual Studio / Manual Review
Recommended Mitigation Steps
In the
getWord
function, we should replaceWith
This will ensure all words in
wordsList
have an equal probability of being picked.Alternatively, the
returnIndex
should at least use arequire
statement and a comment to describe function’s behavior and inputs to avoid confusion of the user. Such statement would look likeAssessed type
Other