Repository navigation
Description
Parent discovery
Tracked under #53 — Discovery: Half-Shell codebase audit and technical-debt dependency map. Scopes the repo owner's "security patching especially around separate tenants and permissions" item.
Why this is its own issue, distinct from #63
#63 checks that each route's role requirement (requireApiRole([...])) matches what's documented — "is dispatcher allowed to call this route at all." This issue is about a different, narrower failure mode that #58's review caught directly in production code: a route can correctly require dispatcher/admin, and still accept a client-supplied ID for a different resource (a technician_id, a customer_id, a quote_id passed somewhere unexpected) without confirming that resource belongs to the caller's own company before writing it. POST /api/jobs's technician_id was exactly this bug: right role, wrong-tenant resource, no ownership check. Nothing about #58's fix or #63's role-matrix check would find a second instance of this same pattern elsewhere — this issue exists to go looking on purpose.
Scope
For every mutation route (POST/PATCH/DELETE) under src/app/api/**, not just the ones already covered by #58/#60/#62 test-wise:
- Identify every place a route accepts a client-supplied ID that references another table (a foreign key target) —
technician_id,customer_id,quote_id,job_idused as a lookup key from a different route than the one that owns it, etc. - For each one, confirm: is that referenced resource's ownership (company_id match) validated before it's written or acted on — either by an explicit app-level check (the pattern Add behavior-level regression coverage for untested production mutation seams #58 just added to
POST /api/jobs) or by RLS actually enforcing it (confirmed againstsupabase/schema.sql, the way Add behavior-level regression coverage for untested production mutation seams #58's review verified thetechnicianstable policies — not assumed)? - Where neither an app-level check nor an RLS policy actually covers it, that's a finding: fix it with the same minimal, targeted pattern used in Add behavior-level regression coverage for untested production mutation seams #58 (company-scoped lookup before use, reject if it doesn't resolve), with a red-before-fix regression test proving the cross-tenant case.
- Where RLS does cover it, no code change is needed — but note it explicitly (in
docs/AUTHORIZATION.mdor inline) so the next person doesn't have to re-derive that from the schema like this session just did.
This is a source-reading audit first, test-writing second — the deliverable per route is "confirmed safe (cite why)" or "fixed (with a regression test)," not blanket new test coverage for its own sake.
Acceptance criteria
- Every mutation route's client-supplied foreign-resource IDs are enumerated and checked against actual company-scoping (app-level or confirmed-real RLS, not assumed).
- Every finding is either fixed (minimal, targeted, with a red-before-fix regression test) or explicitly documented as RLS-covered with the specific policy cited.
-
docs/AUTHORIZATION.mdgets a short section summarizing the audit's outcome, mirroring the "POST /api/jobs — technician tenant ownership" note Add behavior-level regression coverage for untested production mutation seams #58 added. -
npm run lint,npx tsc --noEmit,npm test,npm run buildall pass.
Out of scope
- Role authorization consistency (Add a drift-proof test that role docs and route code stay in sync #63).
- Per-route behavioral test coverage for its own sake (Add behavior-level regression coverage for untested production mutation seams #58/Harden payment provider tests, especially the live Square integration #60/Add behavior-level test coverage for the remaining admin CRUD routes #62 already own that) — this issue only adds tests where an actual cross-tenant gap is found.
- Redesigning RLS policies wholesale — only fixing/documenting the specific gaps this audit finds.
Activity