Build and secure Supabase database applications
A Supabase skill covering RLS security traps, CLI/MCP usage, and the correct execute_sql-then-migrate workflow for schema changes.
Why it matters
Implement Supabase features correctly and securely across database, auth, storage, and edge functions while avoiding common pitfalls like RLS misconfigurations, JWT security issues, and API exposure vulnerabilities.
Outcomes
What it gets done
Configure Row Level Security policies with proper authentication and authorization checks
Set up database tables with correct Data API exposure and role permissions
Implement secure authentication flows using supabase-js and SSR integrations
Debug and verify Supabase implementations against current changelog and documentation
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-supabase | bash Overview
Supabase
This skill covers Supabase development - database, auth, edge functions, storage, RLS - with a security checklist naming specific silent-failure traps (views bypassing RLS, UPDATE needing a SELECT policy, SECURITY DEFINER exposure, storage upsert grants) and the correct execute_sql-then-migrate schema change workflow. Use it for any Supabase task - database, auth, edge functions, realtime, storage, vectors, cron, queues, or client library integration.
What it does
Covers any task involving Supabase - Database, Auth, Edge Functions, Realtime, Storage, Vectors, Cron, Queues, and client libraries (supabase-js, @supabase/ssr) across Next.js, React, SvelteKit, Astro, and Remix. Six core principles anchor it: verify against the live changelog and docs rather than training data, since function signatures and config change between versions; verify every fix with a test query; stop and reconsider after 2-3 failed attempts rather than looping on the same command; check Data API exposure settings when a SQL-created table is unexpectedly inaccessible (separate from RLS, which controls row visibility once a table is already reachable); enable RLS on every table in any exposed schema (including public by default), with policies matching the real access model rather than a copy-pasted auth.uid() pattern; and run a security checklist for anything touching auth, RLS, views, storage, or user data. That checklist names specific Supabase traps: never use user_metadata/raw_user_meta_data in authorization decisions since it's user-editable and reaches auth.jwt() (use app_metadata/raw_app_meta_data instead); deleting a user doesn't invalidate existing tokens, so sign out or revoke sessions explicitly; never expose the service_role key in public clients (any Next.js NEXT_PUBLIC_ var reaches the browser); views bypass RLS by default (use security_invoker = true on Postgres 15+, or revoke anon/authenticated access on older versions); an UPDATE needs a SELECT policy too or it silently returns 0 rows with no error; auth.role() is deprecated in favor of the TO clause, and specifically breaks when anonymous sign-ins are enabled since anon users also carry the authenticated Postgres role; TO authenticated alone is authentication without authorization (a BOLA/IDOR risk) and must be paired with an ownership predicate; UPDATE policies need both USING and WITH CHECK or a user can reassign a row's ownership; SECURITY DEFINER functions bypass RLS entirely and should never be used just to silence a permission error, and any such function left in public is callable by every role by default unless moved to a non-exposed schema with its own auth.uid() check; and storage upsert specifically needs INSERT, SELECT, and UPDATE grants together, since INSERT alone lets uploads work but silently fails on file replacement.
create policy "example" on table_name for select
to authenticated
using ( (select auth.uid()) = user_id );
CLI usage always discovers commands via --help rather than guessing, since the CLI structure changes between versions; known gotchas include supabase db query needing CLI v2.79.0+ (fall back to MCP execute_sql or psql), supabase db advisors needing v2.81.3+ (fall back to MCP get_advisors), and always generating new migration files via supabase migration new <name> rather than inventing a filename. The MCP server (https://mcp.supabase.com/mcp) is troubleshot in order: check reachability (a 401 without a token means it's up), verify .mcp.json points at the right URL, then complete the OAuth 2.1 auth flow if tools still aren't visible. Documentation lookup prefers the MCP search_docs tool first, then fetching any docs page as markdown by appending .md, then web search as a last resort. For schema changes, execute_sql (MCP) or supabase db query (CLI) run SQL directly for free iteration without migration history entries - apply_migration is explicitly wrong for this since it writes a history entry on every call, locking in whatever SQL was passed first. Once ready to commit: run advisors and fix issues, re-check the security checklist for views/functions/triggers/storage changes, generate the migration with supabase db pull <name> --local --yes, then verify with supabase migration list --local.
When to use - and when NOT to
Use it for any Supabase task - database, auth, edge functions, realtime, storage, vectors, cron, queues, client library integration, or auth issues like login/logout flows.
Inputs and outputs
Input is a Supabase task (schema change, RLS policy, auth flow, CLI/MCP setup issue). Output is verified-against-current-docs implementation: correctly-scoped RLS policies, a clean migration generated only after iterating with execute_sql/db query, and an advisors-clean, security-checklist-passed change.
Integrations
Uses the Supabase CLI (--help-discovered commands), the Supabase MCP server (search_docs, execute_sql, get_advisors) with OAuth 2.1 authentication, and Supabase's own changelog and docs (fetchable as markdown) as the source of truth over training data.
Who it's for
Developers working with Supabase who want to avoid its specific silent-failure security traps (RLS on views, UPDATE-needs-SELECT, SECURITY DEFINER exposure, storage upsert grants) and use the correct iterate-then-migrate schema workflow instead of locking in a first-draft migration.
Source README
Supabase
When to Use
Use when doing ANY task involving Supabase. Triggers: Supabase products (Database, Auth, Edge Functions, Realtime, Storage, Vectors, Cron, Queues); client libraries and SSR integrations (supabase-js, @supabase/ssr) in Next.js, React, SvelteKit, Astro, Remix; auth issues (login, logout,...
Core Principles
1. Supabase changes frequently - verify against changelog and current docs before implementing.
Do not rely on training data for Supabase features. Function signatures, config.toml settings, and API conventions change between versions.
First, fetch https://supabase.com/changelog.md (a lightweight summary index - not a heavy pull), scan for breaking-change tags relevant to your task, and follow the linked page for any that apply. Then look up the relevant topic using the documentation access methods below.
2. Verify your work.
After implementing any fix, run a test query to confirm the change works. A fix without verification is incomplete.
3. Recover from errors, don't loop.
If an approach fails after 2-3 attempts, stop and reconsider. Try a different method, check documentation, inspect the error more carefully, and review relevant logs when available. Supabase issues are not always solved by retrying the same command, and the answer is not always in the logs, but logs are often worth checking before proceeding.
4. Exposing tables to the Data API: Depending on the user's Data API settings, newly created tables may not be automatically exposed via the Data (REST) API. If this is the case, anon and authenticated roles will need to be explicitly granted access.
Note that this is separate from RLS, which controls which rows are visible once a table is accessible, not whether the table is accessible at all.
When a user reports a SQL-created table is unexpectedly inaccessible, check their Data API settings and whether the roles have been granted access via explicit GRANT SQL. When granting public (anon/authenticated) access, always enable RLS too. See Exposing a Table to the Data API for the full setup workflow.
5. RLS in exposed schemas.
Enable RLS on every table in any exposed schema, which includes public by default. This is critical in Supabase because tables in exposed schemas can be reachable through the Data API when the anon/authenticated roles have access (see Exposing a Table to the Data API). For private schemas, prefer RLS as defense in depth. After enabling RLS, create policies that match the actual access model rather than defaulting every table to the same auth.uid() pattern.
6. Security checklist.
When working on any Supabase task that touches auth, RLS, views, storage, or user data, run through this checklist. These are Supabase-specific security traps that silently create vulnerabilities:
Auth and session security
- Never use
user_metadataclaims in JWT-based authorization decisions. In Supabase,raw_user_meta_datais user-editable and can appear inauth.jwt(), so it is unsafe for RLS policies or any other authorization logic. Store authorization data inraw_app_meta_data/app_metadatainstead. - Deleting a user does not invalidate existing access tokens. Sign out or revoke sessions first, keep JWT expiry short for sensitive apps, and for strict guarantees validate
session_idagainstauth.sessionson sensitive operations. - If you use
app_metadataorauth.jwt()for authorization, remember JWT claims are not always fresh until the user's token is refreshed.
- Never use
API key and client exposure
- Never expose the
service_roleor secret key in public clients. Prefer publishable keys for frontend code. Legacyanonkeys are only for compatibility. In Next.js, anyNEXT_PUBLIC_env var is sent to the browser.
- Never expose the
RLS, views, and privileged database code
- Views bypass RLS by default. In Postgres 15 and above, use
CREATE VIEW ... WITH (security_invoker = true). In older versions of Postgres, protect your views by revoking access from theanonandauthenticatedroles, or by putting them in an unexposed schema. - UPDATE requires a SELECT policy. In Postgres RLS, an UPDATE needs to first SELECT the row. Without a SELECT policy, updates silently return 0 rows - no error, just no change.
auth.role()is deprecated - use theTOclause instead. Supabase has deprecatedauth.role()in favour of specifying the target role directly on the policy withTO authenticatedorTO anon. Beyond deprecation,auth.role() = 'authenticated'breaks silently when anonymous sign-ins are enabled, because anonymous users carry theauthenticatedPostgres role and pass the check regardless of whether the user is genuinely signed in.-- Deprecated (do not use) create policy "example" on table_name for select using ( auth.role() = 'authenticated' );TO authenticatedalone is authentication without authorization (BOLA / IDOR). UsingTO authenticatedonly checks the role - it does not restrict which rows a user can access. The correct pattern combinesTO authenticatedwith an ownership predicate inUSING:create policy "example" on table_name for select to authenticated using ( (select auth.uid()) = user_id );- UPDATE policies require both
USINGandWITH CHECK. WithoutWITH CHECK, a user can reassign a row'suser_idto another user:create policy "example" on table_name for update to authenticated using ( (select auth.uid()) = user_id ) with check ( (select auth.uid()) = user_id ); SECURITY DEFINERfunctions bypass RLS. ASECURITY DEFINERfunction runs with its creator's privileges - typically a role withbypassrls(e.g.,postgres). Never addSECURITY DEFINERto resolve a permission error; it silently removes access control without fixing the underlying cause. PreferSECURITY INVOKER.SECURITY DEFINERfunctions inpublicare callable by all roles. Postgres grantsEXECUTEtoPUBLICby default for every new function, so anySECURITY DEFINERfunction inpublicis a public API endpoint callable byanonandauthenticated(which inherit fromPUBLIC) without any additional grant. WhenSECURITY DEFINERis genuinely needed (e.g., bypassing RLS on an internal lookup table), keep the function in a non-exposed schema, always include anauth.uid()check in the function body, and runsupabase db advisorsafter making changes.
- Views bypass RLS by default. In Postgres 15 and above, use
Storage access control
- Storage upsert requires INSERT + SELECT + UPDATE. Granting only INSERT allows new uploads but file replacement (upsert) silently fails. You need all three.
Dependency and supply-chain security
- Always pin package versions and commit lockfiles when installing Supabase packages (
supabase-js,@supabase/ssr,supabase-py, etc.). See the npm security guide for the full checklist.
- Always pin package versions and commit lockfiles when installing Supabase packages (
For any security concern not covered above, fetch the Supabase product security index: https://supabase.com/docs/guides/security/product-security.md
Supabase CLI
Always discover commands via --help - never guess. The CLI structure changes between versions.
supabase --help # All top-level commands
supabase <group> --help # Subcommands (e.g., supabase db --help)
supabase <group> <command> --help # Flags for a specific command
Supabase CLI Known gotchas:
supabase db queryrequires CLI v2.79.0+ → use MCPexecute_sqlorpsqlas fallbacksupabase db advisorsrequires CLI v2.81.3+ → use MCPget_advisorsas fallback- When you need a new migration SQL file, always create it with
supabase migration new <name>first. Never invent a migration filename or rely on memory for the expected format.
Version check and upgrade: Run supabase --version to check. For CLI changelogs and version-specific features, consult the CLI documentation or GitHub releases.
Supabase MCP Server
For setup instructions, server URL, and configuration, see the MCP setup guide.
Troubleshooting connection issues - follow these steps in order:
Check if the server is reachable:
curl -so /dev/null -w "%{http_code}" https://mcp.supabase.com/mcp
A401is expected (no token) and means the server is up. Timeout or "connection refused" means it may be down.Check
.mcp.jsonconfiguration:
Verify the project root has a valid.mcp.jsonwith the correct server URL. If missing, create one pointing tohttps://mcp.supabase.com/mcp.Authenticate the MCP server:
If the server is reachable and.mcp.jsonis correct but tools aren't visible, the user needs to authenticate. The Supabase MCP server uses OAuth 2.1 - tell the user to trigger the auth flow in their agent, complete it in the browser, and reload the session.
Supabase Documentation
Before implementing any Supabase feature, find the relevant documentation. Use these methods in priority order:
- MCP
search_docstool (preferred - returns relevant snippets directly) - Fetch docs pages as markdown - any docs page can be fetched by appending
.mdto the URL path. - Web search for Supabase-specific topics when you don't know which page to look at.
Making and Committing Schema Changes
To make schema changes, use execute_sql (MCP) or supabase db query (CLI). These run SQL directly on the database without creating migration history entries, so you can iterate freely and generate a clean migration when ready.
Do NOT use apply_migration to change a local database schema - it writes a migration history entry on every call, which means you can't iterate, and supabase db diff / supabase db pull will produce empty or conflicting diffs. If you use it, you'll be stuck with whatever SQL you passed on the first try.
When ready to commit your changes to a migration file:
- Run advisors →
supabase db advisors(CLI v2.81.3+) or MCPget_advisors. Fix any issues. - Review the Security Checklist above if your changes involve views, functions, triggers, or storage.
- Generate the migration →
supabase db pull <descriptive-name> --local --yes - Verify →
supabase migration list --local
Reference Guides
- Skill Feedback → references/skill-feedback.md
MUST read when the user reports that this skill gave incorrect guidance or is missing information.
Limitations
- Use this skill only when the task clearly matches its upstream product or API scope.
- Verify commands, API behavior, pricing, quotas, credentials, and deployment effects against current official documentation before making changes.
- Do not treat generated examples as a substitute for environment-specific tests, security review, or user approval for destructive or costly actions.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.