Skip to content

Audit every mutation route for unvalidated cross-tenant resource references #64

Closed

Description

@diese-tech

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:

  1. 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_id used as a lookup key from a different route than the one that owns it, etc.
  2. 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 against supabase/schema.sql, the way Add behavior-level regression coverage for untested production mutation seams #58's review verified the technicians table policies — not assumed)?
  3. 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.
  4. Where RLS does cover it, no code change is needed — but note it explicitly (in docs/AUTHORIZATION.md or 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.md gets 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 build all pass.

Out of scope

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