feat(rls): Phase 1 #5 — Goal RLS + callsite fixes#133
Merged
Conversation
- Migration 20260530000000: enable RLS + FORCE on Goal, composite index on (organizationId, id) — note: (organizationId, status) already exists; SELECT/INSERT/UPDATE/DELETE policies for app_user, grants to app_user + admin_user - goals/route.ts: wrap findMany(GET) and create(POST) with withOrgContext - goals/[goalId]/route.ts: loadGoal() uses withAdminBypass (pre-auth); GET findUnique (incl. CompanyMission JOIN), PATCH update, DELETE count + delete all wrapped with withOrgContext(goal.organizationId) - agents/[agentId]/goals/route.ts: goal existence check uses withAdminBypass (agent's orgId not in scope for this check) - lib/templates/template-engine.ts: goal.create in importTemplate wrapped with withOrgContext(organizationId) — called from API routes only Cross-table JOIN: GET /goals/[goalId] includes mission (CompanyMission, already RLS-enforced) — both sub-queries run in the same withOrgContext transaction, same app.current_org_id. Safe. RLS_ENFORCEMENT_ENABLED=false; wrapping is a no-op until flag flip.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
20260530000000_rls_phase1_goal: enables RLS + FORCE onGoal, adds composite index on(organizationId, id)(existing(organizationId, status)index is separate and untouched), SELECT/INSERT/UPDATE/DELETE policies forapp_user, grants toapp_user+admin_usergoals/route.ts(GET findMany, POST create) →withOrgContextgoals/[goalId]/route.ts→loadGoal()useswithAdminBypass(pre-auth lookup); GET/PATCH/DELETE usewithOrgContext(goal.organizationId)agents/[agentId]/goals/route.ts→ goal existence check useswithAdminBypass(agent's orgId not in scope)lib/templates/template-engine.ts→goal.createinimportTemplatewrapped withwithOrgContext(organizationId)— called from API routes only, not workersRLS_ENFORCEMENT_ENABLED=false— allwithOrgContextwrapping is a no-op until flag flip.Cross-table JOIN analysis
GET /api/goals/[goalId]includesmission: { select: { id, statement } }(Goal → CompanyMission). Both tables now have RLS. This findUnique runs insidewithOrgContext(prisma, goal.organizationId, ...)— both the Goal query and the CompanyMission sub-query execute in the same$transactionwith the sameapp.current_org_id. Safe. ✓goal-context.tsaccesses Goal viaprisma.agentGoalLink.findMany({ include: { goal } })— not aprisma.goal.*call; addressed when AgentGoalLink is migrated.Test plan
pnpm precheck— TS ✅ vitest ✅ ESLint ✅ (4119 tests, 307 files)SELECT COUNT(*) FROM "Goal"asapp_userwithoutapp.current_org_id— must return 0 rowsapp.current_org_idto a valid org ID — must return only that org's goalsGET/POST /api/goals,GET/PATCH/DELETE /api/goals/<id>, and template import work with flag off🤖 Generated with Claude Code