Supabase Production Audit: A Step-by-Step Guide to Security, Drift, and Cost Optimization
Supabase is a powerful open-core alternative to traditional database-as-a-service (DBaaS) platforms, offering PostgreSQL compatibility, real-time capabilities, and a robust API layer. However, as your production environment scales, ensuring security, compliance, and operational efficiency becomes increasingly complex. This guide provides a structured approach to auditing your Supabase project for security risks, configuration drift, unused objects, and cost inefficiencies—all while minimizing production impact.
Why Audit Your Supabase Production Environment?
Auditing your Supabase project is critical for several reasons:
- Security Risks: Misconfigured roles, exposed APIs, or outdated dependencies can introduce vulnerabilities. A recent community discussion highlights concerns around auditability and drift detection in production environments.
- Configuration Drift: Over time, manual changes or misaligned deployments can lead to inconsistencies between environments. Detecting drift early prevents production outages or security gaps.
- Unused Objects: Orphaned tables, functions, or storage buckets accumulate costs and clutter your schema. Identifying and cleaning them up improves performance and reduces expenses.
- Cost Optimization: Supabase’s pay-as-you-go model means inefficient resource usage (e.g., over-provisioned databases or unused storage) directly impacts your budget.
Step 1: Inventory Your Supabase Project
Before diving into audits, create a baseline inventory of your Supabase project. This step ensures you have a complete understanding of your environment before identifying issues.
1.1 List All Databases and Schemas
Use the Supabase REST API or CLI to enumerate databases and schemas:
curl -X GET "https://.supabase.co/rest/v1/database" -H "apikey: "
This will return metadata about your databases, including schema names and object counts. Document these for later comparison.
1.2 Document Roles and Permissions
Roles define access control in Supabase. Audit roles using:
curl -X GET "https://.supabase.co/rest/v1/database/roles" -H "apikey: "
Check for:
- Unused roles (e.g., legacy roles with no active members).
- Overly permissive roles (e.g., roles granting `SELECT` on sensitive tables to public users).
- Roles with no explicit membership (potential security gaps).
1.3 Capture API Keys and Secrets
List all API keys and service roles associated with your project:
curl -X GET "https://.supabase.co/rest/v1/database/keys" -H "apikey: "
Review for:
- Keys with no rotation history (security risk).
- Keys used in deprecated or insecure contexts (e.g., embedded in client-side code).
Step 2: Security Audit
Security is the foundation of any production audit. Focus on misconfigurations, exposed data, and compliance gaps.
2.1 Check for Exposed Data
Supabase’s real-time capabilities and public APIs can inadvertently expose sensitive data. Verify:
- Public Table Access: Ensure no tables are publicly readable or writable. Use:
curl -X GET "https://.supabase.co/rest/v1/database/tables" -H "apikey: " | jq '.[] | select(.is_public == true)' - Row-Level Security (RLS): Confirm RLS policies are enabled on tables containing PII or sensitive data. Test policies with:
SELECT * FROM your_table WHERE NOT rls_enabled();
2.2 Review Authentication and Authorization
Audit your auth system for:
- Default or weak passwords (e.g., `postgres` user with no password).
- Unused or inactive users (check `auth.users` table).
- JWT validation misconfigurations (e.g., missing `iss` or `aud` claims).
2.3 Scan for Vulnerable Dependencies
Supabase relies on PostgreSQL and third-party extensions. Use tools like Snyk or Grype to scan for outdated or vulnerable packages in your database extensions.
Step 3: Configuration Drift Detection
Drift occurs when your production environment diverges from your intended state (e.g., Git-managed schema or Terraform templates). Detecting drift early prevents cascading failures.
3.1 Compare Against Git-Managed Schema
If your schema is version-controlled (e.g., in a Git repo), compare it against the live database:
# Export live schema
pg_dump --schema-only --no-owner --no-privileges -h -U postgres -f live_schema.sql
# Diff against Git-managed schema
git diff --no-index live_schema.sql your_git_schema.sql
3.2 Use Supabase CLI for Schema Validation
The Supabase CLI includes tools to validate your project against a reference schema:
supabase db diff --from --to main
This highlights changes between branches or tags.
3.3 Monitor for Unauthorized Changes
Enable Supabase’s audit logging to track all schema changes. Query logs for:
- Changes made outside CI/CD pipelines.
- Unintended `ALTER TABLE` or `DROP` operations.
Step 4: Identify Unused Objects
Unused objects (tables, functions, extensions) bloat your database, increase costs, and create maintenance overhead.
4.1 Query for Unused Tables
Identify tables with no recent activity:
SELECT table_name, last_analyze, n_live_tup, n_dead_tup
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY n_live_tup = 0, last_analyze ASC;
Tables with `n_live_tup = 0` and no recent `last_analyze` are likely unused.
4.2 Audit Database Functions
Check for unused functions:
SELECT routine_name, routine_schema, last_analyze
FROM information_schema.routines
WHERE routine_type = 'FUNCTION'
ORDER BY last_analyze ASC;
4.3 Review Storage Usage
Supabase’s storage is billed by usage. Audit large or unused objects:
curl -X GET "https://.supabase.co/storage/v1/object/list?bucket=public" -H "apikey: " | jq '.[] | select(.size > 10485760)'
Step 5: Cost Optimization
Supabase’s pricing is usage-based, so optimizing costs requires visibility into resource consumption.
5.1 Analyze Database Query Patterns
Use PostgreSQL’s statistics collector to identify slow or resource-intensive queries:
SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;
Optimize or rewrite queries with high `total_time`.
5.2 Right-Size Your Database
Review Supabase’s sizing recommendations and compare against your current usage. Adjust plans if over-provisioned.
5.3 Clean Up Unused Storage
Delete large or obsolete files from Supabase Storage:
curl -X DELETE "https://.supabase.co/storage/v1/object/" -H "apikey: "
Step 6: Safe Remediation
Remediating issues in production requires caution. Follow these best practices:
6.1 Use Blue-Green Deployments
For schema changes, deploy to a staging environment first. Supabase supports migrations to sync changes safely.
6.2 Automate Audits with CI/CD
Integrate audits into your CI pipeline. Example workflow:
- Run schema diffs on every PR.
- Scan for vulnerabilities in dependencies.
- Alert on unused objects or cost spikes.
6.3 Document Changes
Maintain a changelog for all remediation steps, including:
- Who made the change.
- Why the change was necessary.
- Impact on production (e.g., downtime, data migration).
When to Seek Expert Help
While the steps above provide a foundation, complex environments may require specialized expertise. If your audit reveals:
- Critical security vulnerabilities (e.g., exposed admin APIs).
- Widespread configuration drift requiring remediation.
- Cost inefficiencies tied to unclear usage patterns.
- A need for continuous governance and intelligence.
An enterprise intelligence and governed engineering platform can provide:
- Automated, repeatable audits with actionable insights.
- Expert-led remediation for complex drift or security issues.
- Continuous monitoring to prevent future problems.
- Cost optimization strategies tailored to your workload.
By combining your technical expertise with governed engineering practices, you can ensure your Supabase environment remains secure, efficient, and aligned with your business goals.
Apply this to your environment.
If this problem exists in your estate, we can review the current architecture and determine whether an assessment, pilot or engineering engagement makes sense.
Book a Technical Discovery