How do I secure Edge Functions?
Last checked 31 Aug 2026
Short answer
Verify the caller's identity and role inside the function itself, validate all input, use secrets rather than hard-coded keys, and don't assume the front end already checked permissions.
Why Edge Functions need their own checks
An Edge Function is a public HTTP endpoint. Anyone who finds its URL can call it directly, bypassing whatever your front end normally shows or hides. Treat every Edge Function as if it will be called by someone who skipped your UI entirely.
What to check inside the function
- Authentication — verify the caller's JWT/session; reject unauthenticated requests unless the function is genuinely meant to be public.
- Authorisation — if the action is role-gated (e.g. admin-only), check the caller's role against your
user_rolestable inside the function, not just on the client. - Input validation — validate and sanitise every parameter; don't trust that the front end only ever sends well-formed data.
- Secrets — read API keys from the server-side secrets store, never hard-code them in the function file.
- Rate limiting — for functions that are expensive or abusable (email sending, AI calls), add basic throttling per user or IP.
A prompt that helps
Review my Edge Functions and confirm each one checks the caller's authentication and role before doing anything privileged, validates its inputs, and doesn't hard-code any secrets.
What usually goes wrong
A function written to be called from one specific admin page ends up reachable by anyone, because the "it's only called from the admin page" assumption isn't actually enforced inside the function — the URL is public regardless of which page links to it. See /questions/how-do-i-protect-admin-pages and /questions/how-do-i-rate-limit-an-api for the related patterns.
Sources
Was this answer helpful?
Related questions
- AnswerHow do I check Supabase RLS?Ask Lovable to list every table and its row-level security status and policies in plain language — Lovable Cloud users don't have Supabase dashboard access, so this has to go through chat.
- AnswerHow do I prevent spam registrations?Verify email addresses on sign-up, add a CAPTCHA to the registration form, rate-limit sign-up attempts per IP, and check the token server-side before creating an account.
- AnswerHow do I stop bots?Add a CAPTCHA (e.g. Cloudflare Turnstile) to public forms, validate inputs server-side, and rate-limit submissions — client-side-only checks like a hidden field are easy for real bots to bypass.
- AnswerWhere should secrets be stored?In your project's server-side secrets store, used only inside Edge Functions — never in front-end code, client-side environment variables, or committed to your Git repository.