# How do you secure a Supabase Edge Function?

Canonical: https://caelo.sh/guides/supabase-edge-functions-security
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** Check who is calling inside the function, keep keys in Supabase secrets rather than in code, and never trust a user ID sent in the request body. Edge Functions require a valid JWT by default, but that built-in check only understands the legacy JWT keys, so Supabase now recommends turning it off and authenticating in your own code.

## What changed with verify_jwt?

Supabase's docs say: "By default, Edge Functions require a valid JWT in the authorization header." The migration guide for the new API keys adds that the built-in `verify_jwt` check "only understands the legacy JWT-based keys", and tells you to set `verify_jwt = false` for those functions "and authorize the request in your own code."

That is the risk. A function with `verify_jwt = false` and no check of its own can be called by anyone on the internet who knows its URL.

## The vulnerable pattern and the fix

Vulnerable: no auth check, trusts the body, uses the admin client

```ts
// supabase/functions/delete-account/index.ts, with verify_jwt = false
import { createClient } from "npm:@supabase/supabase-js@2";

Deno.serve(async (req) => {
  const { userId } = await req.json(); // whatever the caller sends
  const admin = createClient(
    Deno.env.get("SUPABASE_URL")!,
    Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
  );
  await admin.auth.admin.deleteUser(userId); // anyone can delete any account
  return new Response("ok");
});
```

Fixed: the user comes from the verified token, not the body

```ts
import { withSupabase } from "npm:@supabase/server@1";

export default {
  fetch: withSupabase({ auth: "user" }, async (_req, ctx) => {
    const userId = ctx.userClaims?.id; // from the verified JWT
    if (!userId) return new Response("unauthorized", { status: 401 });
    await ctx.supabaseAdmin.auth.admin.deleteUser(userId);
    return Response.json({ deleted: true });
  }),
};
```

In Supabase's docs, `withSupabase` takes an auth mode: `user` accepts a valid user JWT, `secret` accepts your secret key for server-to-server calls, and `none` accepts anyone, for functions such as webhooks that check a signature themselves. `ctx.supabase` is scoped to the caller's row level security policies; `ctx.supabaseAdmin` bypasses them, so use it only after you have decided the caller is allowed.

## Webhooks: auth none, but verify the signature

A Stripe webhook cannot send a Supabase JWT, so it needs `auth: "none"`. Supabase's docs pair that with Stripe signature verification using `stripe.webhooks.constructEventAsync` and a SubtleCrypto provider, because Deno has no synchronous Node crypto. Without that check, anyone can post a fake "payment succeeded" event. Our payments guide covers the rest.

## Fix it, step by step

1. List your functions and their settings in `supabase/config.toml`. Note every one with `verify_jwt = false`.
2. For each, decide who may call it: a signed-in user, your own server, or anyone with a valid signature.
3. Add the matching check in code, with `withSupabase` or your own JWT verification. Take the user ID from the verified token, never from the request body.
4. Move keys out of the code: `supabase secrets set STRIPE_SECRET_KEY=...`, then read them with `Deno.env.get`. Supabase says functions read a new secret immediately without a redeploy, and secret names cannot start with `SUPABASE_`.
5. Use the user-scoped client for reads and writes on the user's behalf, so row level security still applies. Reach for the admin client only for the specific step that needs it.
6. Handle CORS for browser calls as Supabase's CORS guide shows, but do not treat CORS as protection. A script or curl ignores it; the auth check is what keeps people out.

## Check it automatically

Caelo reads the files in your `supabase/functions` folder like any other code and flags provider keys written into them and service_role JWTs pasted into them. It does not check `verify_jwt` settings, whether your function authenticates the caller, or CORS. Read each function with the steps above.

- [Install Caelo: npx caelo . or the MCP server](/)
- [How Caelo compares with gitleaks, TruffleHog, Semgrep and others](/compare)

## Check your own app

1. Run the free local check: `npx caelo .` Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and `.env` files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## Questions

### Is it safe to set verify_jwt = false?

Yes, if the function then checks the caller itself or is meant to be public, like a webhook that verifies a signature. It is unsafe when nothing else checks who is calling.

### Can I use the service_role key inside an Edge Function?

Yes. Edge Functions run on the server and get `SUPABASE_SERVICE_ROLE_KEY` and `SUPABASE_SECRET_KEYS` as default secrets. Never return the key in a response and never use it to act on a user ID the caller chose.

### How should the browser call a protected function?

Use `supabase.functions.invoke` from the signed-in client. It sends the user's access token, which `withSupabase` in user mode verifies.

### Why does my function return 401 before my code runs?

The built-in JWT check rejected the request, often because the caller sent a new-style key that `verify_jwt` does not understand. Supabase recommends turning `verify_jwt` off and checking auth in code.

## Sources

- [Supabase docs: Function configuration (verify_jwt).](https://supabase.com/docs/guides/functions/function-configuration)
- [Supabase docs: Securing Edge Functions (withSupabase).](https://supabase.com/docs/guides/functions/auth)
- [Supabase docs: Migrating to the new API keys (verify_jwt and Edge Functions).](https://supabase.com/docs/guides/getting-started/migrating-to-new-api-keys)
- [Supabase troubleshooting: Edge Function 401 error response.](https://supabase.com/docs/guides/troubleshooting/edge-function-401-error-response)
- [Supabase docs: Edge Function secrets.](https://supabase.com/docs/guides/functions/secrets)
- [Supabase docs: CORS for invoking Edge Functions from the browser.](https://supabase.com/docs/guides/functions/cors)
- [Caelo source: every rule in lib/rules.js.](https://github.com/arthi-arumugam-git/caelo/blob/master/lib/rules.js)

## Related guides

- [Can users get my AI-built app's paid features for free? Checking Stripe payments](https://caelo.sh/guides/stripe-webhook-security-ai-built-app)
- [How to check your Supabase row level security is actually working](https://caelo.sh/guides/supabase-rls-check)
- [I leaked an API key or the Supabase service_role key. What do I do now?](https://caelo.sh/guides/leaked-api-key-what-to-do)
- [Supabase service_role key exposed in the frontend: what to do now](https://caelo.sh/guides/supabase-service-role-key-exposed)
- [Is Access-Control-Allow-Origin: * a security risk in my API?](https://caelo.sh/guides/cors-wildcard-ai-generated-api)
