Snowman Merkle Airdrop

AI First Flight #10
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: low
Valid

Global s_earnTimer in Snow.sol causes protocol-wide DoS on earnSnow and gets reset on every token purchase

Root + Impact

## Description
In `Snow.sol`, the free token earning mechanism is governed by a single global storage variable `uint256 private
s_earnTimer;` instead of a per-user tracking mapping (`mapping(address => uint256)`).

Furthermore, in `Snow::buySnow()`, the contract resets `s_earnTimer = block.timestamp;` whenever any user
purchases tokens.

This directly violates the protocol design specified in `README.md`:
> *"The Snow token can either be earned for free once a week, or bought at anytime, up until during the
::FARMING_DURATION is over."*

Because `s_earnTimer` is global:
1. When any user calls `earnSnow()`, all other users across the entire network are locked out from earning free
tokens for 1 week.
2. Whenever any user calls `buySnow()`, the 1-week lockout timer is reset to the current timestamp, indefinitely
starving all users from ever being able to earn free tokens.

```solidity
// Root cause in the codebase with @> marks to highlight the relevant section:
// File: src/Snow.sol

    // >>> VARIABLES
@>  uint256 private s_earnTimer;

    function buySnow(uint256 amount) external payable canFarmSnow {
        if (msg.value == (s_buyFee * amount)) {
            _mint(msg.sender, amount);
        } else {
            i_weth.safeTransferFrom(msg.sender, address(this), (s_buyFee * amount));
            _mint(msg.sender, amount);
        }

@>      s_earnTimer = block.timestamp;

        emit SnowBought(msg.sender, amount);
    }

    function earnSnow() external canFarmSnow {
@>      if (s_earnTimer != 0 && block.timestamp < (s_earnTimer + 1 weeks)) {
            revert S__Timer();
        }
        _mint(msg.sender, 1);

@>      s_earnTimer = block.timestamp;
    }

Risk

Likelihood: High

Impact: High

Proof of Concept

When an honest user calls earnSnow(), another distinct user cannot call earnSnow() until 1 full week has elapsed.
Moreover, any regular purchase via buySnow() resets s_earnTimer, preventing any user from claiming their weekly
tokens.
Add the following test to test/TestSnow.t.sol:
function test_global_timer_dos_and_buy_snow_reset() public {
// 1. Ashley earns snow at t = 0
vm.prank(ashley);
snow.earnSnow();
assertEq(snow.balanceOf(ashley), 1);

    // 2. Victory tries to earn snow immediately after Ashley; fails because timer is global
    vm.prank(victory);
    vm.expectRevert(Snow.S__Timer.selector);
    snow.earnSnow();

    // 3. Fast forward 6 days (almost 1 full week)
    vm.warp(block.timestamp + 6 days);

    // 4. Jerry buys snow, which erroneously resets s_earnTimer to current timestamp
    vm.startPrank(jerry);
    weth.approve(address(snow), FEE);
    snow.buySnow(1);
    vm.stopPrank();

    // 5. Ashley or Victory attempts to earn snow 2 days later (8 days after initial earn)
    // It should succeed since > 1 week passed since Ashley earned, but reverts because buySnow reset the timer
    vm.warp(block.timestamp + 2 days);
    vm.prank(victory);
    vm.expectRevert(Snow.S__Timer.selector);
    snow.earnSnow();
}

Recommended Mitigation

  1. Replace the single global s_earnTimer variable with a mapping mapping(address => uint256) private s_lastEarned;
    to enforce cooldowns per user.

  2. Remove the assignment s_earnTimer = block.timestamp; from buySnow(), as purchasing tokens should not interfere
    with the free earning cooldown.
    --- a/src/Snow.sol
    +++ b/src/Snow.sol
    @@ -27,7 +27,7 @@ contract Snow is ERC20, Ownable {
    error S__SnowFarmingOver();

    // >>> VARIABLES
    address private s_collector;
    
-    uint256 private s_earnTimer;
+    mapping(address => uint256) private s_lastEarned;
     uint256 public s_buyFee;
     uint256 private immutable i_farmingOver;

@@ -84,8 +84,6 @@ contract Snow is ERC20, Ownable {
             _mint(msg.sender, amount);
         }

-        s_earnTimer = block.timestamp;
-
         emit SnowBought(msg.sender, amount);
     }

     function earnSnow() external canFarmSnow {
-        if (s_earnTimer != 0 && block.timestamp < (s_earnTimer + 1 weeks)) {
+        if (s_lastEarned[msg.sender] != 0 && block.timestamp < (s_lastEarned[msg.sender] + 1 weeks)) {
             revert S__Timer();
         }
-        _mint(msg.sender, 1);
-
-        s_earnTimer = block.timestamp;
+        s_lastEarned[msg.sender] = block.timestamp;
+        _mint(msg.sender, 1);
     }
Updates

Lead Judging Commences

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

[L-02] Global Timer Reset in Snow::buySnow Denies Free Claims for All Users

## Description: The `Snow::buySnow` function contains a critical flaw where it resets a global timer `(s_earnTimer)` to the current block timestamp on every invocation. This timer controls eligibility for free token claims via `Snow::earnSnow()`, which requires 1 week to pass since the last timer reset. As a result: Any token purchase `(via buySnow)` blocks all free claims for all users for 7 days Malicious actors can permanently suppress free claims with micro-transactions Contradicts protocol documentation promising **"free weekly claims per user"** ## Impact: * **Complete Denial-of-Service:** Free claim mechanism becomes unusable * **Broken Protocol Incentives:** Undermines core user acquisition strategy * **Economic Damage:** Eliminates promised free distribution channel * **Reputation Harm:** Users perceive protocol as dishonest ```solidity function buySnow(uint256 amount) external payable canFarmSnow { if (msg.value == (s_buyFee * amount)) { _mint(msg.sender, amount); } else { i_weth.safeTransferFrom(msg.sender, address(this), (s_buyFee * amount)); _mint(msg.sender, amount); } @> s_earnTimer = block.timestamp; emit SnowBought(msg.sender, amount); } ``` ## Risk **Likelihood**: • Triggered by normal protocol usage (any purchase) • Requires only one transaction every 7 days to maintain blockage • Incentivized attack (low-cost disruption) **Impact**: • Permanent suppression of core protocol feature • Loss of user trust and adoption • Violates documented tokenomics ## Proof of Concept **Attack Scenario:** Permanent Free Claim Suppression * Attacker calls **buySnow(1)** with minimum payment * **s\_earnTimer** sets to current timestamp (T0) * All **earnSnow()** calls revert for **next 7 days** * On day 6, attacker repeats **buySnow(1)** * New timer reset (T1 = T0+6 days) * Free claims blocked until **T1+7 days (total 13 days)** * Repeat step **4 every 6 days → permanent blockage** **Test Case:** ```solidity // Day 0: Deploy contract snow = new Snow(...); // s_earnTimer = 0 // UserA claims successfully snow.earnSnow(); // Success (first claim always allowed) // Day 1: UserB buys 1 token snow.buySnow(1); // Resets global timer to day 1 // Day 2: UserA attempts claim snow.earnSnow(); // Reverts! Requires day 1+7 = day 8 // Day 7: UserC buys 1 token (day 7 < day 1+7) snow.buySnow(1); // Resets timer to day 7 // Day 8: UserA retries snow.earnSnow(); // Still reverts! Now requires day 7+7 = day 14 ``` ## Recommended Mitigation **Step 1:** Remove Global Timer Reset from `buySnow` ```diff function buySnow(uint256 amount) external payable canFarmSnow { // ... existing payment logic ... - s_earnTimer = block.timestamp; emit SnowBought(msg.sender, amount); } ``` **Step 2:** Implement Per-User Timer in `earnSnow` ```solidity // Add new state variable mapping(address => uint256) private s_lastClaimTime; function earnSnow() external canFarmSnow { // Check per-user timer instead of global if (s_lastClaimTime[msg.sender] != 0 && block.timestamp < s_lastClaimTime[msg.sender] + 1 weeks ) { revert S__Timer(); } _mint(msg.sender, 1); s_lastClaimTime[msg.sender] = block.timestamp; // Update user-specific timer emit SnowEarned(msg.sender, 1); // Add missing event } ``` **Step 3:** Initialize First Claim (Constructor) ```solidity constructor(...) { // Initialize with current timestamp to prevent immediate claims s_lastClaimTime[address(0)] = block.timestamp; } ```

Support

FAQs

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

Give us feedback!