Fix: Add transaction protection to card dealing operation#2377
Fix: Add transaction protection to card dealing operation#2377immortal71 wants to merge 3 commits intoOWASP:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds database transaction protection to the card dealing operation in the Elixir game start handler to prevent partial data corruption from database failures. It also adds minimum player validation (3+ players required) to prevent the ArithmeticError from zero-player games. However, the PR includes several unrelated files that appear to be accidental commits.
Changes:
- Wrapped card dealing and game start operations in an
Ecto.Multitransaction for atomicity - Added minimum 3-player validation before game start
- Replaced
Enum.each+Repo.insert!with transactional approach usingEnum.reduce - Added comprehensive test coverage for edge cases and transaction behavior
- Included several temporary/debug files that should not be in version control
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| copi.owasp.org/lib/copi_web/live/game_live/show.ex | Core implementation: Added Ecto.Multi transaction wrapper for atomic card dealing and game start, plus 3-player minimum validation |
| copi.owasp.org/test/copi_web/live/game_live/show_test.exs | Test coverage for player validation and transaction atomicity (missing Ecto.Query import) |
| tests/tmp_validate_yaml.py | Temporary test file - should not be committed |
| tests/tmp_inspect.py | Temporary test file - should not be committed |
| tests/tmp_get_runs.py | Temporary test file - should not be committed |
| tests/debug_get_files.py | Debug file - should not be committed |
| output.yaml | Temporary output file - should not be committed |
| copi.owasp.org/gpg_key.json | Unrelated GPG key file - appears to be accidental commit |
| @@ -0,0 +1,161 @@ | |||
| defmodule CopiWeb.GameLive.ShowTest do | |||
There was a problem hiding this comment.
Missing import statement for Ecto.Query. The test file uses the from macro on lines 116, 119, 122, and 155 but does not import Ecto.Query. This will cause a compilation error. Add import Ecto.Query after line 4 to fix this issue.
02961f9 to
60ba523
Compare
|
@immortal71 some of your commits doesn't have a verified signature. |
|
@sydseter this test keep failing any tip what am I missing here ? |
|
@immortal71 I am not sure. The code coverage is low? |
e412b50 to
ad42a46
Compare
29a0da6 to
e9e146d
Compare
|
@sydseter I think the pr is ready for review !! |
done |
cde9fb8 to
83dda08
Compare
|
I am unable to start the application locally after checking out your changes and trying to run mix phx.server I don't think this is working. |
2a7d164 to
eef4db9
Compare
|
@sydseter I have updated the pr , i think it should work now |
|
@immortal71 You have some merge conflicts. Could you please have a look at them? |
f0f8546 to
077c2dc
Compare
|
@sydseter is this good to go |
| CopiWeb.Endpoint.broadcast(topic(updated_game.id), "game:updated", updated_game) | ||
| {:noreply, assign(socket, :game, updated_game)} | ||
|
|
||
| # coveralls-ignore-start |
There was a problem hiding this comment.
Do not use coveralls ignore.
| socket | ||
| |> put_flash(:error, "Failed to start game due to a system error. Please try again.") | ||
| |> assign(:game, game)} | ||
| # coveralls-ignore-stop |
There was a problem hiding this comment.
revert the changes in this file.
- Wrap card dealing and game update in Ecto.Multi transaction - Ensures atomicity: all cards dealt or nothing committed - Add comprehensive tests for transaction behavior
077c2dc to
eea0d46
Compare
|
@sydseter how about now ?!! |
Description
This PR adds transaction protection to the card dealing operation in the game start handler to prevent database corruption from partial card dealing failures.
Problem
The current implementation deals cards to players using
Repo.insert!in a loop without transaction protection. If a database error occurs partway through (e.g., at card 30 of 52):Note: While reviewing issue #2335 (ArithmeticError fix), I realized it addresses validation but does not solve this data integrity problem.
Solution
Wrapped the entire card-dealing and game-update operation in an
Ecto.Multitransaction. This ensures:Repo.insert!with transaction-based approachChanges Made
lib/copi_web/live/game_live/show.ex
Enum.each+Repo.insert!withEcto.MultitransactionEnum.reduceto build multi operations for each cardtest/copi_web/live/game_live/show_test.exs
Testing
All existing tests pass. New tests verify:
ASVS Compliance
Addresses ASVS V2.3.3: "Verify that transactions are being used at the business logic level such that either a business logic operation succeeds in its entirety or it is rolled back to the previous correct state."
Closes #2343