Skip to content

Confirm whether session-scoped status_events inserts are silently rejected by RLS #66

Closed

Description

@diese-tech

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.sql creates only a SELECT RLS policy on status_events — no INSERT policy has ever existed for this table.
  • supabase/migrations/202506150001_sms_outbox_rls.sql later applies FORCE ROW LEVEL SECURITY to status_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

  1. Verify against the real Supabase project (this needs live DB access, which the originating sandbox doesn't have): either query status_events for rows with actor_role in ('dispatcher', 'admin') created via the board (not intake/tech-action), or attempt a session-scoped insert directly and observe whether it's rejected.
  2. 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-action already do it), or add an INSERT policy to status_events scoped 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.
  3. Either way: start checking the insert's error at both call sites going forward, so a future regression here fails loudly instead of silently.
  4. Add a regression test proving status_events actually 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_events inserts 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.md why 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 error going forward, regardless of outcome.
  • npm run lint, npx tsc --noEmit, npm test, npm run build all pass.

Out of scope

Activity

  1. added 2 commits that reference this issue on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions