Files
hackerspace.sh/scripts/017_governance_rls_hardening.sql
Moheeb Zara ea8d7a5f91 feat: governance kernel, deployment hardening, local-dev stack
Adds proposals + proposal_votes + policies + incidents + incident_updates
tables with full RLS, async voting flow, dismissal-appeal pathway,
versioned policies with provenance, and an anonymous-incident token mode.
Adds per-space configurable areas, financial visibility settings, member
self-profile + COI disclosure, and a mission statement that renders in
the app footer.

Reorganises lib/actions.ts into per-domain files (tasks, projects,
members, payments, contacts, knowledge-base, secrets, area-leads,
settings, imports, proposals, incidents, policies, areas) plus a barrel.
Adds lib/permissions.ts and lib/auth-helpers.ts so action bodies stop
duplicating the user-to-member-to-role check. Wires every action that has
a Zod schema; introduces a flexibleDateTime preprocessor so HTML date
pickers no longer hit "Invalid datetime".

Hardens RLS: drops NOT NULL on space_members.user_id for offline members,
adds a privilege-escalation trigger that blocks self-edits to role / tier
/ status / approved / has_card_access / space_id, closes four
cross-tenant holes in the governance kernel, and adds a partial unique
index so two admins cannot concurrently activate two drafts of the same
policy slug.

Ships every deployment artifact: Dockerfile, docker-compose,
.do/app.yaml, .env.example, /api/health, plus a managed-Supabase guide
(docs/DEPLOYMENT.md), an end-to-end self-hosted Droplet guide
(docs/DEPLOY_DO_SELFHOSTED.md), and a Supabase-CLI local-dev quickstart
(docs/LOCAL_DEV.md). Replaces middleware.ts with proxy.ts for Next.js 16.
Adds scripts/schema.sql as the canonical idempotent full deploy plus
fourteen numbered incremental migrations.

Adds scripts/seed.sql so a fresh local DB renders something instead of
empty pages: a demo space, five members across all roles, a v1 code of
conduct, three proposals in different states with seven votes already
cast, an open incident, a project, three tasks, a KB entry, and matching
activity-log rows.

Replaces v0-generated test scaffolding with 266 real tests across
__tests__/{actions,auth-helpers,components,governance,permissions,
security,utils,validations}.test.ts. Adds react-markdown + remark-gfm
for proposal / incident / policy body rendering. Deletes four dead v0
fix-* scripts, the unused components/app-shell.tsx, and the duplicate
app/auth/login/page.tsx.

Routes (31 total): /api/health, /api/paypal/sync, /auth/callback, /,
/login, /signup, /signup/confirm, /dashboard, /tasks, /projects,
/members, /payments, /financials, /contacts, /ops, /ops/[id], /ops/new,
/proposals, /proposals/[id], /proposals/new, /incidents, /incidents/[id],
/incidents/new, /policies, /policies/[slug], /policies/new, /profile,
/recruitment, /settings, /comms, /import.

Full pass-by-pass history is in docs/HANDOFF.md; the roadmap is in
docs/GOVERNANCE_FEATURES.md.
2026-05-14 04:52:11 -07:00

108 lines
3.9 KiB
SQL

-- =============================================================================
-- 017: Governance RLS hardening
-- =============================================================================
-- Pass 5's Tier 1 RLS allowed several cross-tenant edge cases when a user is
-- a member of more than one space. The app's `requireMember()` uses `.single()`
-- which makes multi-space practically impossible at the app layer, but RLS
-- should be airtight regardless.
--
-- Bugs being closed (defense in depth):
-- 1. proposal_votes.votes_insert / votes_update accepted a vote where
-- member_id was from space A but the proposal was in space B.
-- 2. incidents.incidents_insert let the reporter set reporter_id to any
-- space_members row they own, including one in a different space.
-- 3. incident_updates.incident_updates_insert did not validate author_id.
--
-- Also adds a partial unique index that prevents two `active` policy versions
-- for the same slug — fixing a race when two admins concurrently activate
-- different drafts.
--
-- Idempotent: drops policies / indexes first.
-- =============================================================================
-- 1. proposal_votes: require member's space match the proposal's space.
DROP POLICY IF EXISTS votes_insert ON public.proposal_votes;
CREATE POLICY votes_insert ON public.proposal_votes FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1
FROM public.proposals p
JOIN public.space_members m ON m.id = proposal_votes.member_id
WHERE p.id = proposal_votes.proposal_id
AND m.user_id = auth.uid()
AND m.space_id = p.space_id
AND p.status = 'open'
AND p.voting_opens_at <= now()
AND p.voting_closes_at > now()
)
);
DROP POLICY IF EXISTS votes_update ON public.proposal_votes;
CREATE POLICY votes_update ON public.proposal_votes FOR UPDATE
USING (
EXISTS (
SELECT 1
FROM public.proposals p
JOIN public.space_members m ON m.id = proposal_votes.member_id
WHERE p.id = proposal_votes.proposal_id
AND m.user_id = auth.uid()
AND m.space_id = p.space_id
AND p.status = 'open'
AND p.voting_closes_at > now()
)
);
-- 2. incidents: reporter_id, when set, must be a space_members row owned by
-- the caller AND in the same space as the incident.
DROP POLICY IF EXISTS incidents_insert ON public.incidents;
CREATE POLICY incidents_insert ON public.incidents FOR INSERT
WITH CHECK (
space_id IN (SELECT public.get_user_space_ids(auth.uid()))
AND (
reporter_id IS NULL
OR EXISTS (
SELECT 1 FROM public.space_members m
WHERE m.id = incidents.reporter_id
AND m.user_id = auth.uid()
AND m.space_id = incidents.space_id
)
)
);
-- 3. incident_updates: author_id, when set, must be a space_members row
-- owned by the caller AND in the same space as the incident.
DROP POLICY IF EXISTS incident_updates_insert ON public.incident_updates;
CREATE POLICY incident_updates_insert ON public.incident_updates FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.incidents i
WHERE i.id = incident_updates.incident_id
AND (
public.user_has_role_in_space(auth.uid(), i.space_id, ARRAY['admin','board'])
OR i.reporter_id IN (
SELECT m.id FROM public.space_members m
WHERE m.user_id = auth.uid() AND m.space_id = i.space_id
)
)
)
AND (
author_id IS NULL
OR EXISTS (
SELECT 1 FROM public.space_members m, public.incidents i
WHERE i.id = incident_updates.incident_id
AND m.id = incident_updates.author_id
AND m.user_id = auth.uid()
AND m.space_id = i.space_id
)
)
);
-- 4. Exactly one active policy version per (space, slug).
DROP INDEX IF EXISTS public.policies_one_active_per_slug;
CREATE UNIQUE INDEX policies_one_active_per_slug
ON public.policies (space_id, slug)
WHERE status = 'active';