This Proof of Concept (PoC) demonstrates a potential reentrancy vulnerability in the PuppyRaffle::refund() smart contract, which can allow an attacker to manipulate the contract's state and potentially exploit it to withdraw funds that should be reserved for the winners of the raffle. The PoC uses an Attack contract to exploit this vulnerability.
The vulnerability arises from the combination of two functions in the PuppyRaffle contract: refund(uint256 playerIndex) and enterRaffle(address[] memory newPlayers). The specific issue lies in the order of operations in the refund function, where funds are sent back to the caller before modifying the players array, allowing a potential reentrancy attack.
Attack Contract
Test in Foundry:
The impact of this vulnerability can be significant. An attacker can repeatedly enter the raffle using the enterRaffle function of the Attack contract, thereby increasing the number of times they appear in the players array. Subsequently, they can invoke the refund function from the Attack contract, resulting in the attacker receiving a refund for each entry, effectively causing a loss of funds that should be reserved for legitimate raffle participants.
VsCode
Slither
Foundry
To mitigate this reentrancy vulnerability, it is recommended to adjust the order of operations within the refund function in the PuppyRaffle contract. Funds should only be sent back to the caller after the modification of the players array to prevent the reentrancy attack.
reentrancy in refund() function
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.