Securing a Next.js and Supabase application
In a web application built with Next.js, deployed on Vercel and using Supabase for its database and sign-in, the anon key reaches every table in the public schema until row level security is turned on, so the database comes first. Each step names the attack it prevents, and Table 1 gives the SecFlash issue that confirms it.
Procedure
- Enable row level security on every table. Every table in the
publicschema can be reached with the anon key. Enable row level security on each table in the SQL editor:1
Prevents: Insecure direct object referencealter table public.orders enable row level security; - Add a policy for each permitted action. With row level security on and no policy, the table returns no rows to anyone using the anon key. Write select, insert, update and delete policies that compare
auth.uid()with the row's owner column.1Prevents: Insecure direct object reference - Keep the service role key on the server. Next.js inlines variables whose names start with
NEXT_PUBLIC_into the JavaScript sent to browsers.2 Name the keySUPABASE_SERVICE_ROLE_KEY, without the prefix, and use it only in route handlers and server actions.Prevents: Privileged key in client-side code - Store keys in Vercel, not in the repository. Add each key under the project's Settings, Environment Variables. Confirm that
.gitignorecontains.env*before the first commit.Prevents: Exposed credentials in source code repositories - Enable push protection. In the GitHub repository's Settings, Advanced Security, turn on push protection under secret scanning. A push that contains a recognised key is then rejected.Prevents: Exposed credentials in source code repositories
- Check ownership in every route. Each route handler and server action that takes an ID must also filter on the signed-in user, and return 404 when nothing matches.Prevents: Insecure direct object reference
- Pass values as parameters in raw SQL. The Supabase client builds safe queries by default. In database functions and other raw SQL, pass values with
USINGor as parameters, never joined into the text.Prevents: SQL injection - Keep storage buckets private. Make a bucket public only when every file in it is meant for everyone, and remove any policy that lets everyone list a bucket's files.Prevents: Public cloud storage bucket
- Limit sign-in attempts. Review the limits under Authentication, Rate Limits in Supabase. For the application's own API routes, add a rate limiting rule in the Vercel firewall.Prevents: Brute-force attack
- Enable two-factor authentication on every account. The Vercel, Supabase, GitHub and domain registrar accounts each control the application. Enable two-factor authentication on each of them.Prevents: Phishing
- Send security headers. Add them in
next.config.js. A Content-Security-Policy needs the list of hosts the site loads resources from; add it as a separate step.
Prevents: Cross-site scripting// next.config.js module.exports = { async headers() { return [{ source: '/:path*', headers: [ { key: 'Strict-Transport-Security', value: 'max-age=31536000' }, { key: 'X-Content-Type-Options', value: 'nosniff' }, { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' }, { key: 'X-Frame-Options', value: 'DENY' }, ], }]; }, };
Verification in SecFlash
| Step | Action | Issue | SecFlash page |
|---|---|---|---|
| 1 | Enable row level security on every table | SF-CLD-501 | Cloud |
| 2 | Add a policy for each permitted action | SF-CLD-503 | Cloud |
| 3 | Keep the service role key on the server | SF-SEC-724 | Security |
| 4 | Store keys in Vercel, not in the repository | SF-SEC-702 | Security |
| 5 | Enable push protection | SF-CLD-605 | Cloud |
| 6 | Check ownership in every route | SF-EXP-501 | Exposure |
| 7 | Pass values as parameters in raw SQL | SF-SEC-704 | Security |
| 8 | Keep storage buckets private | SF-CLD-515 | Cloud |
| 9 | Limit sign-in attempts | SF-SEC-727 | Security |
| 10 | Enable two-factor authentication on every account | ||
| 11 | Send security headers | SF-SEC-101 | Security |
See also
- Common types of attack on web applications
- Row level security
- HTTP security headers
- Security checklist before launch
- Category: Supabase
References
- a b "Row Level Security". Supabase Docs. Supabase.
- back "How to use environment variables in Next.js". Next.js Docs. Vercel.
External links
- OWASP Secure Headers Project, OWASP Foundation