Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: high
Valid

Reentrancy Vulnerability in `refund()` function

Summary

The refund() function in the PuppyRaffle contract is vulnerable to a reentrancy attack. This vulnerability could be exploited to steal all of the funds in the contract.

Vulnerability Details

The refund() function works as follows:

  1. The player's address is retrieved from the players array at the specified index.

  2. A require statement checks that the player address is equal to the caller's address.

  3. Another require statement checks that the player address is not already refunded or is not active.

  4. The player's entrance fee is sent back to them using the sendValue() function.

  5. The player's address is set to address(0) in the players array.

  6. A RaffleRefunded event is emitted.

The vulnerability is that the players array is updated after the sendValue() call is made. This means that if the attacker is able to reenter the refund function before the update to players mapping has completed, the attacker can steal all of the funds in the contract.

Proof of Concept

function testRefundForReentrnacy() public {
address[] memory players = new address[](3);
players[0] = playerOne;
players[1] = playerTwo;
players[2] = address(this);
puppyRaffle.enterRaffle{value: entranceFee * players.length}(players);
vm.prank(address(this));
puppyRaffle.refund(puppyRaffle.getActivePlayerIndex(address(this)));
assertEq(address(puppyRaffle).balance, 0);
}
receive() external payable {
if(address(puppyRaffle).balance == 0) return;
vm.prank(address(this));
puppyRaffle.refund(puppyRaffle.getActivePlayerIndex(address(this)));
}

Impact

If this vulnerability is exploited, the attacker could steal all of the funds in the PuppyRaffle contract.

Tools Used

Foundry

Recommendations

Follows the CEI pattern and update the players mapping before the external call is being made

Updates

Lead Judging Commences

Hamiltonite Lead Judge about 2 years ago
Submission Judgement Published
Validated
Assigned finding tags:

reentrancy-in-refund

reentrancy in refund() function

Support

FAQs

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

Give us feedback!