Field note / 03
Verify passphrases before consuming a view
Two viewers racing for the last read—or one script guessing passphrases—will break a self-destructing note store unless verification runs before consumption inside a conditional SQL gate.
Nov 2026 · 3 min read · from GateKeep
Gate ordering matters #
When a link caps out after one view, the intuitive implementation reads the note row, decrypts or checks the passphrase in memory, and decrements the counter at the end. Under concurrent requests, two callers both read views_left = 1 and both unlock the payload.
Flipping the mistake naively—decrementing views_left up front—introduces a worse bug: anyone sending bad guesses can intentionally burn every remaining view before the real recipient opens the link.
GET /notes/:id
└─ exists ─▶ not burned ─▶ not expired ─▶ verify passphrase ─▶ conditional UPDATE (views_left > 0)
└─ last read ─▶ erase ciphertext + queue HMAC webhook
Files and webhooks follow the lock #
- Conditional atomic consume: only after the passphrase hash verifies does PostgreSQL execute a single guarded
UPDATErequiringviews_left > 0andexpires_at > now(). The losing request receives zero rows and sees the note as burned. - Ciphertext erasure & private buckets: AES-256-GCM note bodies are wiped on the final read. Attachments remain in a private Supabase bucket and mint short-lived signed URLs only after every Postgres gate clears.
- Transactional webhook outbox: burn events insert into
webhook_deliveriesin the same transaction and drain viaFOR UPDATE SKIP LOCKEDworkers with HMAC signatures.
If a wrong passphrase can mutate the counter, an unauthenticated loop can destroy every unread secret without reading a single byte.