ERC20.transfer()
in Snow.sol::collectFee
functionIn the Snow.sol::collectFee
function, an external ERC20 transfer()
or transferFrom()
function is called without checking its boolean return value. Standard ERC20 tokens (e.g., USDT) typically return false
on failure (e.g., insufficient balance, allowance, or other internal errors) rather than reverting. If the transfer()
or transferFrom()
call fails silently, the contract will continue execution as if the transfer was successful, leading to an incorrect state.
Funds may not be successfully transferred to s_collector
, and the function proceeds without errors, leading to loss of expected fees or mismanagement. This creates a discrepancy between the contract's perceived state and the actual state of token transfers.
To demonstrate this vulnerability, we will modify MockWETH.sol
to simulate a failed transfer()
that returns false
(as some non-standard ERC20 tokens do), and then write a Foundry test that calls collectFee()
under this condition.
Modify MockWETH.sol
:
Add the following to your MockWETH.sol
contract:
Add Test Case to TestSnow.t.sol
:
Create a new test function in your TestSnow.t.sol
file:
Reproduction Steps:
Ensure you have MockWETH.sol
, Snow.sol
, and TestSnow.t.sol
in your Foundry project.
Deploy MockWETH
and set its address as i_weth
in the Snow
constructor.
Run the test TestSnow.t.sol::testTransferFailsButNoRevertInCollectFee()
.
Expected Result: The collectFee()
function should not revert.
Actual Result: The collectFee()
function executes successfully, but the WETH
remains in the Snow
contract (weth.balanceOf(address(snow))
is still FEE
) and is not transferred to the collector
(weth.balanceOf(collector)
is still 0
), confirming the silent failure.
Always check the boolean return value of ERC20.transfer()
and transferFrom()
to ensure the operation was successful. The most robust and recommended approach is to use OpenZeppelin's SafeERC20
library, which automatically handles these checks by reverting on false
returns or insufficient allowances.
Proposed Fix in Snow.sol
:
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.