Repository navigation
Description
Parent discovery
Tracked under #53 — Discovery: Half-Shell codebase audit and technical-debt dependency map. Split out from #64's cross-tenant audit (PR #65) as a distinct correctness question, per reviewer recommendation ("well-founded from source inspection... should be tracked as a dedicated correctness issue... outside #64's cross-tenant reference audit").
Problem
POST /api/jobs (src/app/api/jobs/route.ts) and PATCH /api/jobs/[id] (src/app/api/jobs/[id]/route.ts) both insert status_events rows using the session-scoped Supabase client (from requireApiRole), not the admin/service-role client. Neither call site checks the insert's error.
But:
supabase/migrations/202505010002_status_events.sqlcreates only a SELECT RLS policy onstatus_events— no INSERT policy has ever existed for this table.supabase/migrations/202506150001_sms_outbox_rls.sqllater appliesFORCE ROW LEVEL SECURITYtostatus_events, with a comment stating: "Writes to sms_outbox and status_events happen exclusively via the service role (admin client), which bypasses RLS by design."
If that comment's assumption is accurate for the table's actual Postgres grants, the two session-scoped inserts above should be rejected by RLS (no INSERT policy exists for the authenticated role), silently — since the code never checks the result. That would mean status_events rows are missing today for the two most common mutation paths in the app (dispatcher job creation and dispatcher/admin status changes on the board), while intake/tech-action's inserts (which do use the admin client) would be recorded fine.
This could not be verified in the sandbox that produced this finding — there is no live Supabase project available there, confirmed repeatedly across this session's work.
Scope
- Verify against the real Supabase project (this needs live DB access, which the originating sandbox doesn't have): either query
status_eventsfor rows withactor_rolein('dispatcher', 'admin')created via the board (not intake/tech-action), or attempt a session-scoped insert directly and observe whether it's rejected. - If inserts are indeed silently failing: fix by switching those two call sites to the admin client (matching the migration comment's stated intent and how
intake/tech-actionalready do it), or add an INSERT policy tostatus_eventsscoped the same way its SELECT policy is (job_id in (select id from jobs where company_id = current_company_id())) if session-scoped inserts are actually the intended design going forward. Prefer whichever requires touching fewer call sites — likely the admin-client switch, since only these two routes are affected. - Either way: start checking the insert's
errorat both call sites going forward, so a future regression here fails loudly instead of silently. - Add a regression test proving
status_eventsactually gets a row for both a dispatcher-created job and a dispatcher-updated job status, once the fix (if any) is in place.
Acceptance criteria
- Confirmed, against the real Supabase project, whether session-scoped
status_eventsinserts currently succeed or are silently rejected. - If rejected: fixed (admin client or a new INSERT policy, whichever is more consistent with the table's existing design) and covered by a regression test.
- If not rejected: documented in
docs/AUTHORIZATION.mdwhy the migration comment's assumption doesn't apply (e.g. a GRANT or role nuance), so the next person doesn't have to re-derive this. - Both call sites check the insert's
errorgoing forward, regardless of outcome. -
npm run lint,npx tsc --noEmit,npm test,npm run buildall pass.
Out of scope
- Any other RLS policy redesign beyond
status_events. - The cross-tenant resource reference audit (Audit every mutation route for unvalidated cross-tenant resource references #64, already merged) and role authorization consistency (Add a drift-proof test that role docs and route code stay in sync #63) — separate concerns.
Activity
fix: route status_events inserts through the admin client (#66)
Merge pull request #70 from diese-tech/claude/load-test-refresh-61
Merge pull request #71 from diese-tech/claude/correct-status-events-doc