Jamstack + Supabase Security Blueprint

To secure a Jamstack + Supabase site, you need to: (1) enable RLS on ALL tables since Jamstack sites have no server layer, (2) use only the anon key in client-side code, (3) move anything that needs the service key into Edge Functions, (4) test RLS policies thoroughly in the SQL editor, and (5) configure Storage bucket policies for file uploads. This blueprint covers pure client-side security patterns with Edge Functions for privileged operations.

TL;DR

A Jamstack site has no server of its own, so every bit of your security comes down to RLS. Turn it on for every table and keep the policies tight. The browser only ever gets the anon key; the service key lives in Edge Functions.

Client-Side Supabase Setup Supabase

src/lib/supabase.js
import { createClient } from '@supabase/supabase-js'

// Only use the anon key on the client
export const supabase = createClient(
  import.meta.env.PUBLIC_SUPABASE_URL,
  import.meta.env.PUBLIC_SUPABASE_ANON_KEY
)

Row Level Security Supabase

Enable RLS first, then add policies. A table with RLS on and no policy returns nothing, which is a safe way to fail:

supabase/migrations/rls.sql
-- Enable RLS on all tables
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;
ALTER TABLE comments ENABLE ROW LEVEL SECURITY;

-- Public read, authenticated write
CREATE POLICY "Public read posts"
  ON posts FOR SELECT
  USING (published = true);

CREATE POLICY "Authors manage own posts"
  ON posts FOR ALL
  USING (auth.uid() = author_id)
  WITH CHECK (auth.uid() = author_id);

-- Comments: authenticated users only
CREATE POLICY "Auth users read comments"
  ON comments FOR SELECT
  USING (auth.uid() IS NOT NULL);

CREATE POLICY "Auth users create own comments"
  ON comments FOR INSERT
  WITH CHECK (auth.uid() = user_id);

Edge Function for Service Key Operations Supabase

When something genuinely needs to bypass RLS, do it here. The function checks who's calling before it touches the service key:

supabase/functions/admin-action/index.ts
import { createClient } from '@supabase/supabase-js'

Deno.serve(async (req) => {
  // Verify user auth from request
  const authHeader = req.headers.get('Authorization')
  const supabaseClient = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_ANON_KEY')!,
    { global: { headers: { Authorization: authHeader! } } }
  )

  const { data: { user } } = await supabaseClient.auth.getUser()
  if (!user) {
    return new Response('Unauthorized', { status: 401 })
  }

  // Use service key for admin operations
  const adminClient = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
  )

  // Perform privileged operation
  const { data } = await adminClient.from('posts').select('*')

  return new Response(JSON.stringify(data))
})

RLS is your only security layer. With no server in between, the browser talks to your database directly, so a table without RLS is open to anyone holding the anon key. Test each policy before launch.

Security Checklist

Pre-Launch Checklist

Alternative Stacks

Consider these related blueprints:

Check Your Jamstack + Supabase Site

Scan for missing RLS policies.

Security Blueprints

Jamstack + Supabase Security Blueprint