Short answer
If an edge function reads userId from the JSON body and then queries with the service_role or secret key, the caller decides whose data they get. Anyone with your public key can send another user’s ID and read or change that user’s rows. Take the user ID from the verified token instead, and query as that user so row level security applies.
Ask an AI about this page:ChatGPTClaude
On this page
An edge function that reads userId from the request body and then loads data with the admin key lets the caller decide whose data comes back. Send someone else’s ID and you get their rows. It is rated high: it needs no special tool and no bug in the database, because the function itself hands out any user’s data past your row level security. The only thing an attacker needs is the other user’s ID, and those leak more easily than people think. This is a textbook insecure direct object reference.
Why it matters
The pattern looks harmless because the frontend always sends the right ID:
await supabase.functions.invoke("list-trips", { body: { userId: user.id } });But the request body is written by whoever sends the request. Anyone can open the network tab, copy this call and replace the ID. On the server side, the function then does this:
import { createClient } from "npm:@supabase/supabase-js@2";
Deno.serve(async (req) => {
// "The platform already verified the JWT" - but nobody reads it.
const { userId } = await req.json();
const admin = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
);
const { data } = await admin.from("trips").select("*").eq("user_id", userId);
return Response.json({ trips: data ?? [] });
});The service_role key bypasses RLS, as Supabase’s own docs on edge function secrets point out. So the only filter left is .eq("user_id", userId), and the attacker controls userId.
The comment in that code is the second trap. Turning on verify_jwt feels like authentication, but Supabase’s docs on authorization headers say the platform check also accepts the publishable key. That key is in your site’s JavaScript, so a visitor without an account passes the check too.
Our audit engine found exactly this in our test apps. A travel app’s list-trips function took userId from the body and read that user’s trips, including private notes, with the service-role client, while a comment claimed the platform had already verified the user. The engine reported it as a high finding (CWE-639), pointed to supabase/functions/list-trips/index.ts and explained why verify_jwt does not count as a user check here.
How to find it in your app
Search your functions for IDs that come from the request and reach a query:
grep -rn "userId\|user_id\|ownerId\|accountId" supabase/functions --include=index.ts
grep -rn "functions.invoke" src | grep -i "user"A hit is a defect when both are true: the value comes from req.json(), a query parameter or a header you set yourself, and it is used in a query that runs with the secret or service_role key. The same goes for workspaceId, teamId or orgId: if the body says which tenant to load and nobody checks membership, it is the same bug one level up. The multi-tenant data isolation guide covers that variant.
How to fix it
Take the user from the verified token, never from the body, and let RLS do the filtering. With the withSupabase wrapper that Supabase’s docs describe, the verified identity is in ctx.userClaims, and ctx.supabase queries as that user:
import { withSupabase } from "npm:@supabase/server@1";
export default {
fetch: withSupabase({ auth: "user" }, async (_req, ctx) => {
// The user id comes from the verified token, never from the body.
const userId = ctx.userClaims!.id;
const { data, error } = await ctx.supabase
.from("trips")
.select("id, destination, starts_on, ends_on, notes")
.eq("user_id", userId)
.order("starts_on");
if (error) return Response.json({ error: "Could not load trips" }, { status: 500 });
return Response.json({ trips: data ?? [] });
}),
};Two layers now protect the data: the ID comes from the token, and even if someone changed the filter, the RLS owner policy on trips would still hide other users’ rows. Remove userId from the frontend call as well, so nobody is tempted to use it again.
If a step really needs the admin client, for example to read a column users may not select, pin it to the caller:
import { withSupabase } from "npm:@supabase/server@1";
export default {
fetch: withSupabase({ auth: "user" }, async (_req, ctx) => {
const userId = ctx.userClaims!.id;
// Admin client only because the work must cross RLS: always pin it to the caller.
const { data } = await ctx.supabaseAdmin
.from("trips")
.select("id, is_premium")
.eq("user_id", userId);
return Response.json({ trips: data ?? [] });
}),
};In the Supabase edge function supabase/functions/list-trips/index.ts (replace with your function), stop reading userId from the request body. Verify the caller’s token (the withSupabase wrapper with auth: “user” from npm:@supabase/server, or getClaims on a client created with the caller’s Authorization header) and use the user id from the verified token. Run the query with the caller-scoped client so row level security applies; use the service role client only if a step must bypass RLS, and then filter by the verified user id.
Remove userId from the body in every frontend call to this function. A signed-in user must still see their own trips. List every other edge function that reads a user, workspace or account id from the request and uses it with the service role key.
Check that the fix works
Use two accounts in your own project, A and B:
- Sign in as A and call the function from your app. A’s trips must load as before.
- Copy that request from the network tab and send it again with B’s user ID in the body, still with A’s token. The response must contain only A’s trips (the body ID is ignored now).
- Send the request with only your publishable key and no user token. It must answer 401.
- Search the function once more:
userIdfromreq.json()must no longer appear.
If step 1 now returns an empty list, the trips table is missing its select policy; the Supabase RLS guide shows how to add one. For functions that don’t check the caller at all, see edge function without an auth check.
Frequently asked questions
User IDs are random UUIDs. Can anyone really guess them?
Guessing is hard, but finding them often isn’t. IDs show up in URLs, shared links, team member lists, comments, public profiles and logs. Security must not depend on an ID staying secret; the server has to check who is asking.
verify_jwt is on for my function. Doesn’t that prove who the user is?
No. The platform check only says that some valid credential was sent, and Supabase’s docs state it also accepts the publishable key. It doesn’t tie the request to the userId in your body. Your handler has to read the user from the token itself.
May an admin still act for another user?
Yes, but then the function must check that the caller is an admin, from app_metadata or a roles table, before it uses a userId from the body. Never take that decision from the body or from user_metadata.
Sources
- 1.Securing Edge Functions — Supabase Docs
- 2.Authorization headers — Supabase Docs
- 3.Environment variables — Supabase Docs
- 4.CWE-639: Authorization Bypass Through User-Controlled Key — MITRE
- 5.A01 Broken Access Control — OWASP Top 10