Files
hackerspace.sh/lib/actions/comms.ts
T
Moheeb Zara f5d670477b fix(security): close member self-escalation + harden after full audit
Full-application audit (5 parallel read-only audits). Two agent
"blockers" were false alarms and verified as already-correct. Real
findings fixed:

- Self-escalation (BLOCKER): migration 024 + schema.sql section 16
  extend prevent_member_self_role_change to also block a
  non-privileged member changing their own tier_id (self tier
  upgrade) and onboarding_completed_at (skipping required
  code-of-conduct via a raw PostgREST call). finishOnboarding and
  skipOnboarding now set onboarding_completed_at through the service
  client AFTER their server-side required-steps check, so the
  legitimate path is unaffected.
- Cross-tenant write scoping: added .eq('space_id', member.space_id)
  to updateForumThread, deleteForumThread, editComment, deleteComment,
  deleteChannel, renameChannel (RLS-only -> defence-in-depth).
- generateInviteCode: crypto.getRandomValues, widened to 8 chars
  (was Math.random, ~21 bits, enumerable under the join rate limit).
- SafeMarkdown: dropped iframe and the global style attribute
  (no unsandboxed embeds / UI-redress); markdown + safe HTML stay.
- /settings: server-side admin redirect before fetching space and
  integration config (was client-hidden only).
- app/(app)/layout.tsx: export const dynamic = 'force-dynamic'.
- Bounded queries: loadComments .limit(500), tasks .limit(2000).

Backlog from the audit is tracked in HANDOFF (import Zod schemas,
types/database.ts + docs regen, email lowercasing, unauthorized-
handling consistency, KB-modal ACL hookup).

271/271 tests pass. Build clean. Migrations 023 + 024 idempotent,
apply via deploy.sh on next deploy.
2026-05-15 17:35:11 -07:00

94 lines
3.0 KiB
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import { createClient } from '@/lib/supabase/server'
import { requireMember, requireMemberWithRole, parseInput } from '@/lib/auth-helpers'
import { createChannelSchema, uuidSchema } from '@/lib/validations'
export async function createChannel(input: {
name: string
description?: string | null
channel_type?: 'general' | 'area' | 'ops' | 'project'
}) {
const v = parseInput(createChannelSchema, input)
if (!v.ok) return { error: v.error }
const supabase = await createClient()
const auth = await requireMember(supabase)
if (!auth.ok) return { error: auth.error }
const { member } = auth
const { data: { user } } = await supabase.auth.getUser()
if (!user) return { error: 'Not authenticated' }
// Uniqueness check: channel names must be unique within a space.
const { data: existing } = await supabase
.from('comms_channels')
.select('id')
.eq('space_id', member.space_id)
.eq('name', v.data.name)
.maybeSingle()
if (existing) return { error: `A channel named "${v.data.name}" already exists.` }
const { data, error } = await supabase
.from('comms_channels')
.insert({
space_id: member.space_id,
name: v.data.name,
description: v.data.description ?? null,
channel_type: v.data.channel_type ?? 'general',
created_by: user.id,
is_default: false,
})
.select('id')
.single()
if (error) return { error: error.message }
revalidatePath('/comms')
return { id: data.id }
}
export async function deleteChannel(channelId: string) {
const v = parseInput(uuidSchema, channelId)
if (!v.ok) return { error: 'Invalid channel ID' }
const supabase = await createClient()
const auth = await requireMember(supabase)
if (!auth.ok) return { error: auth.error }
const { member } = auth
// Default channels are protected at the RLS layer too.
const { error, count } = await supabase
.from('comms_channels')
.delete({ count: 'exact' })
.eq('id', v.data)
.eq('space_id', member.space_id)
if (error) return { error: error.message }
if (count === 0) return { error: 'Cannot delete this channel' }
revalidatePath('/comms')
return { success: true as const }
}
export async function renameChannel(channelId: string, name: string, description?: string | null) {
const idCheck = parseInput(uuidSchema, channelId)
if (!idCheck.ok) return { error: 'Invalid channel ID' }
const v = parseInput(createChannelSchema, { name, description })
if (!v.ok) return { error: v.error }
const supabase = await createClient()
const auth = await requireMember(supabase)
if (!auth.ok) return { error: auth.error }
const { member } = auth
const { error } = await supabase
.from('comms_channels')
.update({ name: v.data.name, description: v.data.description ?? null })
.eq('id', idCheck.data)
.eq('space_id', member.space_id)
if (error) return { error: error.message }
revalidatePath('/comms')
return { success: true as const }
}