CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

g14wxz/realtime-channel-authorization

Secures Supabase Realtime private channels via RLS policies on the realtime.messages table.

Quality

97%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Passed

No findings from the security scan

Overview
Quality
Evals
Security
Files

SKILL.mdskills/realtime-channel-authorization/

name:
realtime-channel-authorization
description:
Configures private Realtime channels with RLS-backed authorization on the realtime.messages table. Enforces tenant-scoped Presence and Broadcast security. Use when implementing private realtime channels, realtime authorization, presence security, broadcast security, or securing the realtime messages table.

Pre-Conditions

  • Supabase project MUST have Realtime enabled.
  • The supabase-mcp-verification tile MUST be installed and passing (provides MCP-based schema verification tooling).
  • The tenant-isolation-rls tile MUST be installed to provide tenant scoping patterns (supplies the JWT claim conventions and RLS helper functions referenced in Phase 2).
  • Caller MUST provide the channel naming scheme and tenant identifier column.

Execution Phases

Phase 1 — Define Channel Naming Convention

  1. Establish a multiplexed topic pattern: {resource}:{tenant_id}:{sub_topic}.
  2. NEVER allow open-ended dynamic channel creation. All channel names MUST follow the defined pattern.
  3. Document the allowed channel prefixes in the migration comments.

Phase 2 — Enable RLS on realtime.messages

  1. Run ALTER TABLE realtime.messages ENABLE ROW LEVEL SECURITY.
  2. Create a SELECT policy scoped to the authenticated role:
    CREATE POLICY "tenant_realtime_access" ON realtime.messages
    FOR SELECT TO authenticated
    USING (
      extension ->> 'tenant_id' = (SELECT auth.jwt() ->> 'tenant_id')
    );
  3. Create a matching INSERT policy for Broadcast/Presence writes:
    CREATE POLICY "tenant_realtime_insert" ON realtime.messages
    FOR INSERT TO authenticated
    WITH CHECK (
      extension ->> 'tenant_id' = (SELECT auth.jwt() ->> 'tenant_id')
    );
  4. HALT if RLS cannot be enabled on realtime.messages.

Phase 3 — Configure Private Channels

  1. Set channel access level to private in the client subscription config:
    const channel = supabase.channel(`orders:${tenantId}:updates`, {
      config: { private: true },
    });
    channel.subscribe((status) => {
      if (status === 'SUBSCRIBED') console.log('Connected to private channel');
    });
  2. NEVER use public access level for channels carrying tenant-specific data.

Phase 4 — Scope Presence and Broadcast

  1. Include tenant_id in Presence track payloads:
    channel.track({ tenant_id: tenantId, user_id: userId, online_at: new Date().toISOString() });
  2. Include the tenant identifier in Broadcast event payloads:
    channel.send({
      type: 'broadcast',
      event: 'order_updated',
      payload: { tenant_id: tenantId, order_id: orderId, status: 'shipped' },
    });
  3. Tenant-scoped filtering is enforced server-side automatically by the RLS policies defined in Phase 2 — clients only receive events whose extension->>'tenant_id' matches their JWT claim.

Phase 5 — Apply Migration

  1. Package all SQL into a numbered migration file.
  2. Apply via supabase db push or apply_migration.
  3. HALT on failure.

Verification Report

  1. Confirm RLS is enabled on realtime.messages via pg_tables query.
  2. Confirm policies exist with tenant scoping by querying pg_policies.
  3. Test: subscribe to a private channel with a valid JWT — MUST succeed.
  4. Test: subscribe to a private channel with a mismatched tenant JWT — MUST be rejected.
  5. Report: channel pattern, policies created count, test results.

skills

realtime-channel-authorization

tile.json