DAT-15Recommended
Know your isolation level
Write down the isolation level each database runs at. Raise it for each transaction where a check and a write must see the same data.
How to do it
PostgreSQL defaults to Read Committed, MySQL's InnoDB to Repeatable Read, and SQLite runs writes one at a time. For "check, then write" logic, pick one of three fixes. Make the write itself conditional, lock the rows with SELECT … FOR UPDATE, or use SERIALIZABLE. Higher levels make the database cancel transactions that would conflict, so pair them with Retry transactions that fail on a conflict. The race conditions rule shows the attack that a weak level allows.
Examples
SQL example
-- PostgreSQL: expect the level the settings file names
SHOW default_transaction_isolation;
-- MySQL
SELECT @@transaction_isolation;
-- One transaction that needs more
BEGIN ISOLATION LEVEL SERIALIZABLE;
How to check
The database settings in the repository name the default level, and a test compares it with the query below. A second test forces the race. Two connections each begin a transaction and run the check. Only then, after a barrier, both run the write. Exactly one commits, and the other retries or fails with a clear error.
Sources
Related rules
- DAT-81 Retry transactions that fail on a conflict in Data and databases
- APS-53 Make each change happen exactly once in Application security
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/transactions-and-concurrency/isolation-levels">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.