Supabase Edge Function: "blocked by CORS policy" fixed
Your app calls supabase.functions.invoke('my-function') and the browser console says Access to fetch at 'https://xyz.supabase.co/functions/v1/my-function' from origin 'https://myapp.lovable.app' has been blocked by CORS policy: Response to preflight request doesn't pass access control check. The function may even work when you test it in the dashboard. The browser asks the function for permission first, with an OPTIONS request, and the function doesn't answer it the way the browser expects.
1. The function doesn't answer the OPTIONS preflight
Before a request with an Authorization or Content-Type: application/json header, the browser sends OPTIONS. Your function has to return CORS headers for it, before any other code runs:
const corsHeaders = {
'Access-Control-Allow-Origin': 'https://myapp.lovable.app',
'Access-Control-Allow-Headers':
'authorization, x-client-info, apikey, content-type',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
};
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders });
}
// ... your code
return new Response(JSON.stringify(data), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
});
});
2. The headers are missing on the real response or on errors
The preflight passes, then the real response comes back without Access-Control-Allow-Origin, and the browser hides it. This often happens on the error path: a throw or an early return new Response('Unauthorized', { status: 401 }) without the headers. Add corsHeaders to every response, including errors, so you can see the real error message instead of a CORS error.
3. The function crashed or doesn't exist
If the function name is wrong, it isn't deployed, or it crashes on start, Supabase returns an error without your headers and the browser reports CORS. Check Edge Functions → Logs in the Supabase dashboard. A CORS error with a 404 or 500 behind it is not a CORS problem.
4. A header your app sends isn't allowed
If your code adds a custom header (for example x-user-id), it must be listed in Access-Control-Allow-Headers, or the preflight fails.
Is Access-Control-Allow-Origin: * safe?
CORS only controls which websites can read the response in a browser. It is not access control: anyone can still call your function with curl or a script. So:
- Prefer your real origins (your Lovable URL and your own domain) over
*, especially if the function uses cookies or returns private data. - Keep JWT verification on (the default), and inside the function check who the user is with
supabase.auth.getUser()before doing anything for them. - If the function calls OpenAI, Stripe or sends email, add a limit per user. Otherwise anyone who finds the URL can run up your bill, whatever your CORS headers say.
- Never return the
service_rolekey or other secrets in a response. If that happened, see what to do when the service_role key is exposed.
A prompt to paste into Lovable, Bolt or Cursor
Calling my Supabase Edge Function from the browser fails with
"blocked by CORS policy". Fix it safely:
1. Handle OPTIONS first and return CORS headers.
2. Add the same CORS headers to every response, including errors.
3. Allow only my app's origins, not "*".
4. Allow the headers supabase-js sends: authorization, x-client-info,
apikey, content-type.
5. Keep JWT verification on and check the user with auth.getUser()
before doing work. Do not put any secret keys in responses.
Want to check nothing else is exposed? LeakPatrol's free scan checks your live app for leaked keys, public files and other common mistakes in about a minute.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
Why does my Supabase Edge Function say blocked by CORS policy?
The browser sends an OPTIONS preflight first, and the function did not answer it with Access-Control-Allow-Origin and Access-Control-Allow-Headers. Handle OPTIONS at the top of the function and add the CORS headers to every response.
Why does the function work in the dashboard but not in my app?
The dashboard and curl do not enforce CORS; only browsers do. The function runs, but the browser blocks your app from reading the response.
Is Access-Control-Allow-Origin * safe for an Edge Function?
CORS is not access control: anyone can call the function outside a browser. Prefer your own origins, keep JWT verification on, and check the user inside the function.
Can a CORS error hide a different error?
Yes. If the function crashes, is not deployed or returns an error without CORS headers, the browser reports CORS. Check the Edge Function logs for the real status.
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)