-
-
Notifications
You must be signed in to change notification settings - Fork 68
Open
Description
Description
The start_game event handler in GameLive.Show will crash with an ArithmeticError if a game is started without any players. This occurs because the code attempts to use modulo division rem/2 by the player count without first validating that players exist.
Location
File: copi.owasp.org/lib/copi_web/live/game_live/show.ex
Vulnerable Code
def handle_event("start_game", _, socket) do
game = socket.assigns.game
if game.started_at do
# Do nothing, game's already started
else
all_cards = Copi.Cornucopia.list_cards_shuffled(game.edition, game.suits, latest_version(game.edition))
players = game.players # Could be empty!
all_cards
|> Enum.with_index
|> Enum.each(fn({card, i}) ->
Copi.Repo.insert! %DealtCard{
card_id: card.id,
player_id: Enum.fetch!(players, rem(i, Enum.count(players))).id # ArithmeticError if count = 0
}
end)
end
endProblem Analysis
Issue 1: Division by Zero
Enum.count(players)returns0when no players have joinedrem(i, 0)throwsArithmeticError(bad argument in arithmetic expression)- This causes the LiveView process to crash
Issue 2: No Validation
- No check to ensure
game.playersis non-empty before starting - No minimum player count validation (games should require at least 2 players)
- No user-friendly error message
Reproduction Steps
- Create a new game at
/games/new - Navigate to the game page
/games/:game_idwithout adding any players - Click "Start Game" button
- Result: LiveView crashes with
ArithmeticError
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels