7 security bugs AI agents write in Next.js + Supabase + Stripe apps (and the rules that stop them)
AI coding agents are great at getting a Next.js + Supabase + Stripe app working . They're much less reliable at getting it safe . The bugs below all pass a quick click-through test, which is exactly why they ship. Every "fixed" snippet here is taken from a small reference app I built with Next.js 16, @supabase/ssr and the Stripe Node SDK, so you can copy them with some confidence. At the end…
The AI coding agents excel at building Next.js, Supabase, and Stripe applications, but they struggle with ensuring the security of these applications. Each of the five bugs listed below has passed a simple click-through test, which is why they were shipped to production. The following fixes are taken from a small reference app built with Next.js 16, Supabase, and the Stripe Node SDK, allowing you to copy them with confidence.
1. Row-Level Security (RLS) enabled, but no protection:
AI agents love to create an empty query that passes the RLS check and provides no protection. To secure your application, set up a policy that grants access only to the signed-in user, and avoid writing policies for tables that your server should only write to. For example:
```
alter table public.subscriptions enable row level security;
create policy "read own subscriptions" on public.subscriptions for select to authenticated using (( select auth.uid() ) = user_id);
```
To find existing vulnerabilities, run the following query:
```
select tablename, policyname from pg_policies where qual = true;
```
Rule: Ensure every table in the public schema has RLS enabled, and never use `using (true)`. If a query returns no rows, fix the policy instead of changing the keys.
2. Using the service-role key to fix empty results:
When RLS correctly returns no rows, AI agents often replace the user's key with the secret/service-role key, effectively bypassing RLS. To maintain security, create two clearly named clients: one acting as the signed-in user (RLS applies) and another for trusted server code such as webhooks. Use the latter only in webhook handlers and background jobs, never in code that runs on behalf of a user. Rule: The admin client should only be used in webhook handlers and background jobs, never in user-facing code.
3. Trusting getSession() on the server:
The `supabase.auth.getSession()` method reads the cookie without re-validating it with Supabase Auth. On the server, you should properly check the user before performing any sensitive actions. Instead of using `getSession()`, use `getUser()` or `getClaims()` for server-side authorization. Rule: Use `getUser()` or `getClaims()` for server-side authorization, never `getSession()`.
4. Parsing the Stripe webhook body before verifying it:
When verifying a Stripe webhook, AI agents might incorrectly parse the JSON payload before checking the signature, causing the signature check to fail. To fix this issue, read the raw request text and verify the signature before processing the payload. Exclude the webhook path from any auth middleware or proxy to prevent redirection to a login page. Rule: Webhook handlers should read `request.text()` and verify the signature before touching the payload.
5. Non-idempotent webhooks:
Stripe retries deliveries and may send events more than once. To handle this, record each event ID with a primary key and ignore duplicates. This ensures that each event is processed only once, regardless of the number of retries. Rule: Record each event ID with a primary key on the server and ignore duplicate events to ensure idempotent processing.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to