Commit bbe8913
committed
Security: Add transaction wrapper to prevent TOCTOU vulnerabilities
Wrap role-based data access in a database transaction to eliminate
Time-of-Check to Time-of-Use (TOCTOU) race conditions.
Changes:
1. Update entity_api coaching relationship functions to accept ConnectionTrait
- find_by_organization_with_user_names: &DatabaseConnection -> &impl ConnectionTrait
- find_by_user_and_organization_with_user_names: &DatabaseConnection -> &impl ConnectionTrait
- Now compatible with both database connections and transactions
2. Wrap domain::coaching_relationship function in transaction
- Begin transaction before role check
- Execute all queries within transaction scope
- Commit transaction after data retrieval
- Ensures atomic snapshot of database state
Security Benefits:
- Prevents race condition where user roles change between check and data access
- Provides REPEATABLE READ isolation level guarantees (PostgreSQL default)
- Ensures consistent view of user permissions and accessible data
- Eliminates window for privilege escalation during query execution
The transaction ensures that the role check and coaching relationship
retrieval see the same consistent snapshot of the database, preventing
scenarios where a user's admin privileges are revoked mid-query but they
still receive admin-level data.
All existing tests pass with no behavioral changes.1 parent 9a58f54 commit bbe8913
File tree
2 files changed
+26
-6
lines changed- domain/src
- entity_api/src
2 files changed
+26
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
35 | 47 | | |
36 | | - | |
| 48 | + | |
37 | 49 | | |
38 | 50 | | |
39 | 51 | | |
| |||
44 | 56 | | |
45 | 57 | | |
46 | 58 | | |
47 | | - | |
| 59 | + | |
48 | 60 | | |
49 | 61 | | |
50 | | - | |
| 62 | + | |
51 | 63 | | |
52 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
53 | 73 | | |
54 | 74 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
0 commit comments