避免AI在前端暴露Supabase服务密钥
AI工具在前端查询受RLS阻止时常会错误使用service-role密钥绕过安全限制。这会导致密钥直接暴露给浏览器访客,带来数据库被篡改的风险。
DEV.to #vibecoding
SupabaseLovableBoltv0ReplitCursor
收录于 2026-11-07 · 原文发布于 Wed, 04 Nov 2026 09:40:53 +0000
要点
- 在浏览器端代码中绝对不要使用Supabase service-role密钥,必须使用anon密钥
- 将service-role密钥严格限制在服务端代码中使用(如API路由、Server Action或Edge Function)
- 当查询被RLS策略阻止时,应修复RLS策略而不是切换为service-role密钥关闭安全限制
- 如果密钥已随前端代码发布,必须立即在Supabase的 Settings → API 中重置(Rotate)密钥
坑点
- AI工具为解决RLS报错,自动在前端代码中使用了SUPABASE_SERVICE_ROLE_KEY
- 将service-role密钥打包进浏览器代码,导致访客可读取、修改或删除整个数据库
.cursorrules
- Never use SUPABASE_SERVICE_ROLE_KEY or service-role keys in browser-side code - Use anon key for all browser Supabase clients - Keep service-role keys exclusively in server-side code (API routes, server actions, edge functions) - Fix RLS policies instead of bypassing them with service-role keys when queries fail
可复制提示词
The Supabase service-role key is used in code that runs in the browser. Move every use of it to server-side code (an API route or edge function), use the anon key in the browser, and make the RLS policies allow what the app needs. Then tell me to rotate the key in Supabase.
#AI编程#安全避坑#Supabase#RLS
相关方法
内容由 AI 依据原文翻译整理,细节以原帖为准;版权归原作者。