caelo

Caelo › Guides › Secure Supabase Edge Functions

How do you secure a Supabase Edge Function?

By Arthi Arumugam, Irth Labs · Published · Updated

Short answerCheck 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

// 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

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.

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. Act on a finding

Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.

For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.

Book the App Health Check See repair services

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