Require every API route to use authedRoute or publicRoute [ABD-21]

Merged
FR
6d ago
+194-66

What & why

Third step of ABD-21. Stacked on #721 (base: abd-21-cookie-routes-authed-route); merge that first.

After #720 and #721, most API routes export through authedRoute, but nothing requires it: a new route that forgets auth still passes lint, because no-api-incomplete-signup.grit only flagged the wrong helper (getUserWithMfa), not a missing one. This makes the wrapper mandatory.

  • publicRoute(handler) (@/lib/http/authed-route): opens an anonymous viewer frame and never authenticates. Used by the deliberately public routes (waitlist, invites/validate, username/check, share-invites, embed/rooms/[roomId]) and the CORS preflight OPTIONS handlers.
  • Admin routes admin/stats, admin/users, admin/users/[id] move onto authedRoute({ credentials: "session" }) and keep their admin check in the handler. Behaviour change: a signed-out caller now gets 401 instead of 403.
  • api-route-wrappers.test.ts (new, unit): fails for a handler exported any way other than authedRoute(...) / publicRoute(...), a public route missing from its reviewed list, or a route file resolving the user itself (getUserWithMfa, getCompletedSignupUser, authenticateRequest, withViewer, …).
  • Deleted no-api-incomplete-signup.grit and its biome.json override; the new test covers it and more. The isolation suite's runtime viewer-frame check stays.
  • token-route-access.test.ts now only scans files that import authedRoute (public routes import publicRoute from the same module).
  • Agent guidance (AGENTS.md, app/src/app/AGENTS.md, add-authed-page skill, withViewer doc) describes the new rule.

Thread: https://ampcode.com/threads/T-01a1e700-7a74-76b5-85a8-6c3a6faaad6c

Checklist

  • User-facing change → added/updated a PostHog event (and a funnel/insight for a new flow), or N/A — N/A, internal refactor
  • User-facing or service change → docs updated (README features/roadmap/services, .env.example, in-app help, CONTRIBUTING.md, onboarding checklist), or N/A — N/A for user docs; agent guidance updated as above
  • New error paths report via captureServerException / the error boundary, or N/A — N/A, no new error paths
  • New behavior is covered by tests, or N/A — api-route-wrappers.test.ts is the new guardrail; existing route tests cover the moved routes
  • User-facing change → demo video attached (record-pr-demo skill); refactor through a user flow → parity run, or N/A — N/A, API auth plumbing only
  • If this fixes a recurring defect, considered a guardrail via the learn skill, or N/A — this is the guardrail: it replaces a narrower grit rule with a test covering every route

Notes

Verified locally:

  • bun run check:fix clean; full unit suite passes (4,462 tests); integration project 659/659 (placeholder Supabase env values).
  • Mutation checks on api-route-wrappers.test.ts: a bare handler, withViewer alone, an export async function GET, an unlisted publicRoute, a direct getCompletedSignupUser reference, and an unwrapped aliased export each fail it.
  • token-route-access.test.ts still fails for an aliased authedRoute import and for a session route switched to tokenScope: "read".
  • Replayed Next's generated route-signature check against all 80 route handlers: passes.