CCyberwiki

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

  1. Enable row level security on every table. Every table in the public schema can be reached with the anon key. Enable row level security on each table in the SQL editor:1
    alter table public.orders enable row level security;
    Prevents: Insecure direct object reference
  2. 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
  3. 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 key SUPABASE_SERVICE_ROLE_KEY, without the prefix, and use it only in route handlers and server actions.Prevents: Privileged key in client-side code
  4. Store keys in Vercel, not in the repository. Add each key under the project's Settings, Environment Variables. Confirm that .gitignore contains .env* before the first commit.Prevents: Exposed credentials in source code repositories
  5. 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
  6. 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
  7. 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 USING or as parameters, never joined into the text.Prevents: SQL injection
  8. 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
  9. 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
  10. 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
  11. 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.
    // 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' },
          ],
        }];
      },
    };
    Prevents: Cross-site scripting

Verification in SecFlash

Table 1. SecFlash issues that confirm each step.
StepActionIssueSecFlash page
1Enable row level security on every tableSF-CLD-501Cloud
2Add a policy for each permitted actionSF-CLD-503Cloud
3Keep the service role key on the serverSF-SEC-724Security
4Store keys in Vercel, not in the repositorySF-SEC-702Security
5Enable push protectionSF-CLD-605Cloud
6Check ownership in every routeSF-EXP-501Exposure
7Pass values as parameters in raw SQLSF-SEC-704Security
8Keep storage buckets privateSF-CLD-515Cloud
9Limit sign-in attemptsSF-SEC-727Security
10Enable two-factor authentication on every account
11Send security headersSF-SEC-101Security

See also

References

  1. a b "Row Level Security". Supabase Docs. Supabase.
  2. back "How to use environment variables in Next.js". Next.js Docs. Vercel.