Deutsch
Fix libraryHighSupabase · Edge Functions

Edge function without an auth check: who is calling your function?

A Supabase edge function that never checks its caller is a public API for everyone. How to find such functions and verify the user before any data work.

By Tim GeithnerPublished 4 min read

ShipRely's Security audit checks for this today.

Short answer

Every deployed Supabase Edge Function has a public URL. If the handler never verifies who is calling, or only checks that someone is signed in and then uses the admin client, anyone can use it to read or change data that isn’t theirs. Verify the caller’s token in the function, run queries as that user so RLS applies, and keep verify_jwt on for user-facing functions.

Ask an AI about this page:ChatGPTClaude

On this page

An edge function without an auth check runs its code for anyone who knows its URL, and every deployed function has one. When the handler never verifies who is calling, or only checks “is somebody signed in?” and then reads data with the admin client, a stranger can use your function as an API into other people’s data. It is rated high because no special skill is needed: one copied request with a changed ID is enough. It becomes critical when the function uses the secret key and returns or changes user data.

Why it matters

Your browser code talks to the database under row level security. Edge functions are different: they often create a client with the secret or service_role key, which skips RLS completely. From that moment the function itself is the only thing deciding who may see what. If it doesn’t decide, nobody does.

Two details make this easy to miss:

  • verify_jwt is not a user check. Supabase’s docs on authorization headers state that the platform check accepts publishable and secret keys too, so a caller who sends only your public key reaches the handler. The check alone, in Supabase’s words, “doesn’t authenticate a caller that sends only an API key”.
  • “Signed in” is not “allowed”. Anyone can create an account in most apps. A function that verifies the token and then loads any project by ID still lets every user read every project.

Our audit engine found exactly this in our test apps. A team app’s export-project function verified that the caller was signed in, then loaded a project and all its tasks with the service-role client by an ID from the request body. It never checked workspace membership, so any user could export any team’s project. The engine rated that instance critical, because it hands out other teams’ data through the admin client, and pointed to supabase/functions/export-project/index.ts.

How to find it in your app

Open every file in supabase/functions/*/index.ts and look for these patterns:

Search your repository
grep -rn "SERVICE_ROLE_KEY\|SUPABASE_SECRET_KEYS" supabase/functions
grep -rn "verify_jwt" supabase/config.toml supabase/config.jsonc

For each function that creates an admin client, answer three questions:

  1. Does it read the caller’s token (Authorization header) and verify it, with getClaims, getUser or the withSupabase wrapper?
  2. After that, does it check that this user may touch this record, or does it run the query as the user so RLS decides?
  3. If verify_jwt is false for it, is there another check, such as a webhook signature?

A function that answers “no” to the first two is the defect on this page. The vulnerable shape usually looks like this:

Vulnerablesupabase/functions/export-project/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";

const admin = createClient(
  Deno.env.get("SUPABASE_URL")!,
  Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
);

Deno.serve(async (req) => {
  const { projectId } = await req.json();
  // Nobody checks who is calling, and the admin client skips RLS.
  const { data } = await admin.from("projects").select("*").eq("id", projectId).single();
  return Response.json(data);
});

How to fix it

Verify the caller in the function and run the query as that user, so the same RLS policies that protect your tables protect the function too. Supabase’s current docs do this with the withSupabase wrapper from @supabase/server: with auth: "user" it rejects requests without a valid user token and hands you a client scoped to the caller.

Safesupabase/functions/export-project/index.ts
import { withSupabase } from "npm:@supabase/server@1";

export default {
  fetch: withSupabase({ auth: "user" }, async (req, ctx) => {
    const body = await req.json().catch(() => null);
    const projectId = typeof body?.projectId === "string" ? body.projectId : "";

    // ctx.supabase acts as the signed-in caller: RLS decides what they may read.
    const { data: project, error } = await ctx.supabase
      .from("projects")
      .select("id, name, description")
      .eq("id", projectId)
      .maybeSingle();
    if (error) return Response.json({ error: "Export failed" }, { status: 500 });
    if (!project) return Response.json({ error: "Not found" }, { status: 404 });

    return Response.json({ project });
  }),
};

A project of another workspace now simply returns “Not found”, because the caller’s RLS policies hide it. Use ctx.supabaseAdmin only for work that must cross RLS, and then filter by ctx.userClaims.id or check membership first. Keep verify_jwt on for this function.

If your function doesn’t use the wrapper yet, the older pattern does the same job:

Without the wrapper: verify the token yourself
import { createClient } from "npm:@supabase/supabase-js@2";

Deno.serve(async (req) => {
  const authHeader = req.headers.get("Authorization") ?? "";
  const token = authHeader.replace(/^Bearer\s+/i, "");
  if (!token) return Response.json({ error: "Not signed in" }, { status: 401 });

  // A client that sends the caller's token, so every query runs under their RLS.
  const supabase = createClient(
    Deno.env.get("SUPABASE_URL")!,
    Deno.env.get("SUPABASE_ANON_KEY")!,
    { global: { headers: { Authorization: authHeader } } },
  );

  // getClaims verifies the token's signature and expiry.
  const { data, error } = await supabase.auth.getClaims(token);
  if (error || !data) return Response.json({ error: "Not signed in" }, { status: 401 });
  const userId = data.claims.sub;

  // ... use `supabase` (not an admin client) and `userId` from here on
  return Response.json({ userId });
});
Add a caller check to an edge function

In the Supabase edge function supabase/functions/export-project/index.ts (replace with your function), verify the caller before any data access: reject requests without a valid user token with 401. Run all reads and writes with a Supabase client scoped to the caller (the withSupabase wrapper with auth: “user” from npm:@supabase/server, or createClient with the caller’s Authorization header) so row level security applies. Remove the service role client from this function unless a step really must bypass RLS; if so, first check that the caller is a member of the record’s workspace.

Keep verify_jwt enabled for this function. Members must still be able to export their own workspace’s projects. Show me the full new function and list every other function in supabase/functions that creates a client with the service role key.

The edge functions security guide covers secrets, CORS and webhooks in the same functions. A close relative of this bug is a function that reads the user ID from the body instead of the token: see edge function trusts user_id from the request body.

Check that the fix works

Test against your own project with two accounts, A and B, where B is not a member of A’s workspace:

No token

Call the function without an Authorization header, for example with curl -X POST https://your-project.supabase.co/functions/v1/export-project -H "apikey: <your publishable key>" -d '{"projectId":"<an id>"}'. It must answer 401 and return no data.

Wrong user

Sign in as B, copy B’s access token from the browser’s network tab and call the function with a project ID from A’s workspace. It must answer 404 (or 403), never the project.

Right user

Repeat as A with the same ID. The export must still work. If it doesn’t, the RLS select policy on the table is missing; the Supabase RLS guide shows the pattern.

Frequently asked questions

Isn’t verify_jwt enough to protect my edge function?

No. Supabase’s docs say the platform check also accepts the publishable and secret API keys, and the publishable key is in your site’s JavaScript. verify_jwt is a useful first gate, but the handler still has to verify the user and decide what they may touch.

My function is only called from my own app. Does it still need a check?

Yes. The URL is public and the request is easy to copy from the browser’s network tab. Anyone can call the function directly with their own body, without your frontend in between.

When is it fine to switch verify_jwt off?

For functions that are not called with a user token: webhooks from Stripe or GitHub, service-to-service calls with a secret key, and functions that are public on purpose, such as a health check. The handler must then verify the caller another way, for example with a webhook signature.

Should I use getUser or getClaims?

Both verify the token. getClaims checks the signature against your project’s public keys and is faster when the project uses asymmetric signing keys; getUser always asks the Auth server. The withSupabase wrapper from Supabase’s current docs does this check for you.

Sources

  1. 1.Securing Edge Functions — Supabase Docs
  2. 2.Authorization headers — Supabase Docs
  3. 3.Function Configuration — Supabase Docs
  4. 4.auth.getClaims() — Supabase Docs
  5. 5.CWE-306: Missing Authentication for Critical Function — MITRE
  6. 6.CWE-862: Missing Authorization — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt