Staff can refund orders and change prices, and the app keeps no record of it. What the attacker gets: a staff member, or anyone holding a staff token, can refund a friend's order or cut a price, and nobody can ever find out who did it or when. Whoever reaches the database could also rewrite any record you did keep.
An audit log is a table the app only adds to: one row per admin action, with when (at), who, what (action) and which record. Write the row before the route's db.commit(), so the change and its row are saved together and there's never a refund without its row. For a price change, keep the old price in the action, so a reader sees what changed and can put it back.
To stop anyone from changing or removing a row, even with the database open, add triggers: SQL that the database runs by itself before every UPDATE or DELETE on the table. RAISE(ABORT, 'message') inside a trigger cancels the statement with that error, whoever sent it. Here is a trigger on another table that refuses negative prices:
db.execute("CREATE TRIGGER IF NOT EXISTS no_negative_price BEFORE UPDATE ON products "
"WHEN NEW.price < 0 BEGIN SELECT RAISE(ABORT, 'Price must be 0 or more'); END")
The check refunds order 2 as Sam, changes product 6's price as Max, reads the log, tries to UPDATE and DELETE it from its own connection, then reads it again:
POST /orders/2/refund (Sam, 09:15) → 200 {"id": 2, "refunded_at": "2026-06-01 09:15:00", "status": "refunded"}
PUT /products/6/price {"price": 18} (Max, 09:20) → 200 {"id": 6, "old_price": 22, "price": 18}
POST /orders/5/refund (no token) → 401 {"error": "Staff only"}
GET /audit → 200 [{"action": "refund", "at": "2026-06-01 09:15:00", "record": "order 2", "who": "sam@shop.example"},
{"action": "price 22 -> 18", "at": "2026-06-01 09:20:00", "record": "product 6", "who": "max@shop.example"}]
POST /check/tamper → 200 {"delete": "refused: The audit log is append-only", "update": "refused: The audit log is append-only"}
GET /audit → 200 (the same two rows)
Your task: write a function audit(who, action, record) that inserts a row into audit_log with now() as its time. Call it in refund_order and set_price before they commit. Then add two triggers that refuse every UPDATE and every DELETE on audit_log with 'The audit log is append-only'.
Assignment
Follow the Coding Tutorial to log every refund and price change in audit_log, and make the table append-only with two triggers.
Hintaudit doesn't commit: the route's db.commit() saves the change and its log row together. Each trigger is one db.execute below the CREATE TABLE line, with IF NOT EXISTS so it's safe to run every time the app starts.
Free plan: 30 tutor messages. PRO is unlimited.
