The contract is vulnerable to a Re-entrancy attack and can drain the contract
High - A bad actor could steal all the funds from the contract
Copy the Re-entrancy contract example
Import the contract in the testfile
launch the custom test
We add any number of players in the raffle
We launch the attack
Now the raffle should not have any funds left
Create a contract under the test folder Reentrancy.sol and paste the following code:
Copy and paste the following functions into PuppyRaffleTest.t.sol:
With this measure we can eliminate the risk of blocking the raffle if the winner is a smart contract (if it doesn't implement a fallback function), and at the same time we eliminate the ability of an actor to exploit this.
enterRaffle()As the Contract is already imported the Address library from Oppenzeppelin we can easily implement this check.
We can simplify this by adding this line using Address for address; because we are using this library for address payable we cannot use it for normal addresses, we need to add it to work with address also.
Restrain the entry to contracts could be sufficient, but for best practices we can restructure the code for best practices.
The Re-entrancy attack is possible here because the state registering 'the right' of funds is modified after sending the funds. Making it possible to a smart contract to execute code upon receiving the funds, and execute repetitively refund(). We will need to change the place of two lines of code in the refund() function to eliminate this possibility of the attack.
In the last three last lines of the function
We need to put the line payable(msg.sender).sendValue(entranceFee); after this line players[playerIndex] = address(0);.
After modifying the contract with this changes the test should failed, meaning that the attack couldn't take place.
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.