🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Auth

Supabase Edge Function callable by anyone (the anon key passes verify_jwt)

LovableBoltCursorReplitv0

Published

Weakness type: CWE-306 (Missing Authentication for Critical Functionality), CWE-862 (Missing Authorization)

The error

verify_jwt is on, but my Edge Function still runs for logged-out users / anyone can call https://<project-ref>.supabase.co/functions/v1/my-function with curl using the key from my JavaScript bundle / my send-email, AI or credits function was spammed and ran up a bill / a security review says an Edge Function has no authentication / the function returns other users' data or changes their records / I assumed 'Enforce JWT verification' meant only signed-in users can call it

Why it happens

Supabase Edge Functions have a platform check called verify_jwt that is on by default. It looks at the Authorization header before your code runs and returns 401 if the header is missing, malformed or signed with a different key. Many AI-built apps treat that as 'only my logged-in users can call this function', but it is not. Supabase's API keys guide lists the legacy anon key as a long-lived JWT, and the platform check validates legacy JWTs, so a request carrying only the anon key passes. supabase-js does this for you: when nobody is signed in, the client falls back to sending the legacy anon key as the Bearer token. Projects on the newer keys are not safer by default either. Publishable keys are not JWTs, but Supabase says that for migration compatibility verify_jwt accepts publishable and secret keys on either header, and that passing the check does not authenticate the caller. Both the anon key and the publishable key are meant to be public: they ship in your web or mobile bundle, so anyone can copy them from DevTools and call your function from a script. So when Lovable, Bolt, Cursor, Replit or v0 writes a function that sends emails, calls OpenAI, grants credits or reads data with the service role client, and never checks who the user is, anyone on the internet can trigger it. It gets worse when the function uses a service role (or secret key) client, because Supabase notes that client bypasses Row Level Security, so a function that does not filter by the caller's id returns every user's rows.

How to fix it

  1. Test it the way an attacker would. Copy your project URL and anon or publishable key from your site's JavaScript (DevTools > Sources or Network) and call the function with no user token: curl -i -X POST https://<project-ref>.supabase.co/functions/v1/<name> -H "apikey: <public key>" -H "Authorization: Bearer <same key if it starts with eyJ>" -H "Content-Type: application/json" -d '{}'. If it does the work instead of returning 401, the function is open to anyone.
  2. List every Edge Function and decide who should be allowed to call it: a signed-in user (most app features), your own backend or cron (service-to-service), an external provider such as Stripe (webhook), or genuinely anyone (a health check). Write that next to each function name before changing code.
  3. For user-facing functions, check the user inside the function, not just at the platform. Supabase's current pattern is the @supabase/server package: wrap the handler in withSupabase({ auth: 'user' }, ...), which only accepts a valid user JWT on the Authorization header and gives you ctx.userClaims plus ctx.supabase, a client scoped to that user's RLS policies. In older code built with createClient, read the Authorization header, call supabase.auth.getUser(token), and return 401 if there is no user. Leave verify_jwt on for these functions as a first layer.
  4. Use the caller's identity for every read and write. Prefer ctx.supabase (or a client created with the user's Authorization header) so RLS applies. If you must use ctx.supabaseAdmin or a service role / secret key client, Supabase warns it bypasses Row Level Security, so filter every query by the caller's id from the verified claims and never by a user_id sent in the request body.
  5. For cron jobs, pg_net calls and other backend callers, require a secret key instead of a user: verify_jwt = false for that function in supabase/config.toml and withSupabase({ auth: 'secret' }), which accepts only the project's secret key named default (or 'secret:<name>' for a specific key). Keep secret keys on servers only; Supabase blocks them in browsers with 401, but they still work from scripts, so a leaked one must be rotated.
  6. For external webhooks (Stripe, GitHub, Resend), set verify_jwt = false and use auth: 'none', then verify the provider's signature inside the handler before doing anything. Supabase's docs say auth: 'none' disables every credential check, so the signature check is the only thing protecting the function.
  7. Protect expensive functions even for signed-in users: anyone can create an account. Add a per-user rate limit (Supabase has an Upstash Redis rate limiting example keyed on the user id), cap spend on paid APIs such as OpenAI, and consider a CAPTCHA such as Cloudflare Turnstile on public forms that trigger a function.
  8. Re-run the curl test with no user token (expect 401), with a real user's access token (expect success), and with one user's token asking for another user's data (expect 401, 403 or 404). Add these as automated tests so the next AI edit cannot quietly remove the check.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

Security fix: my Supabase Edge Functions can be called by anyone, because verify_jwt also accepts the public anon key (a JWT) and publishable keys. Do not just toggle verify_jwt. 1) List every function in supabase/functions and tell me who should call each one: signed-in user, backend/cron, external webhook, or public. 2) For signed-in user functions, wrap the handler with withSupabase({ auth: 'user' }) from npm:@supabase/server@1 (or, if we use createClient, read the Authorization header, call supabase.auth.getUser(token) and return 401 when there is no user). Use the RLS-scoped client for queries. 3) Anywhere a service role or secret key client is used, filter every query by the verified caller id, never by a user_id from the request body. 4) For cron or backend callers set verify_jwt = false in supabase/config.toml for that function and use auth: 'secret'. 5) For Stripe or other webhooks set verify_jwt = false, use auth: 'none' and verify the provider signature first. 6) Add a per-user rate limit to functions that send email or call paid APIs. 7) Never put the service role or secret key in frontend code. 8) Give me curl commands to prove: no token returns 401, a real user works, and user A cannot read user B's data. Show the diffs and config changes.

Error codes this fixes

FAQ

Doesn't verify_jwt mean only logged-in users can call my Edge Function?

No. It only checks that the Authorization header carries a token the platform accepts. The legacy anon key is a valid long-lived JWT, and for migration compatibility the check also accepts publishable and secret keys. Supabase says passing the check does not authenticate the caller, so check for a real user inside the function.

Is the Supabase anon key a JWT?

Yes. Supabase's API keys guide lists anon and service_role as legacy long-lived JWTs. The newer publishable (sb_publishable_) and secret (sb_secret_) keys are short strings, not JWTs, and belong in the apikey header.

How do I make an Edge Function only work for signed-in users?

Wrap it with withSupabase({ auth: 'user' }) from @supabase/server, which accepts only a valid user JWT and gives you the user's claims and an RLS-scoped client. In older createClient code, call supabase.auth.getUser() with the token from the Authorization header and return 401 when there is no user.

Can someone call my Edge Function from outside my app?

Yes. Your project URL and anon or publishable key are in your frontend bundle by design, so anyone can copy them and call the function with curl. Treat every function as a public URL and authorize the caller in code.

Should I turn verify_jwt off?

Only for functions that are called without a user token, such as Stripe webhooks (verify the signature instead) or cron jobs (require a secret key). Keep it on for user-facing functions, but do not rely on it alone.

Why was my OpenAI or email Edge Function spammed?

If the function only relies on verify_jwt, anyone with your public key can call it in a loop. Require a signed-in user, add a per-user rate limit and set spending limits on the paid API.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.