supabase-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSupabase Best Practices
Supabase 最佳实践
Comprehensive security and performance optimization guide for Supabase applications with Clerk authentication integration. Contains 40+ rules across 10 categories, prioritized by impact to guide secure development and code review.
集成Clerk身份验证的Supabase应用程序全面安全与性能优化指南。涵盖10个类别共40+条规则,按影响优先级排序,指导安全开发与代码审查。
When to Apply
适用场景
Reference these guidelines when:
- Setting up a new Supabase project
- Integrating Clerk authentication with Supabase
- Writing Row Level Security (RLS) policies
- Designing database schemas
- Implementing real-time features
- Configuring Storage buckets
- Writing Edge Functions
- Reviewing code for security issues
在以下场景中参考本指南:
- 搭建新的Supabase项目
- 将Clerk身份验证与Supabase集成
- 编写行级安全(Row Level Security, RLS)策略
- 设计数据库架构
- 实现实时功能
- 配置存储桶
- 编写Edge Functions
- 审查代码中的安全问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Row Level Security | CRITICAL | |
| 2 | Clerk Integration | CRITICAL | |
| 3 | Database Security | HIGH | |
| 4 | Authentication Patterns | HIGH | |
| 5 | API Security | HIGH | |
| 6 | Storage Security | MEDIUM-HIGH | |
| 7 | Realtime Security | MEDIUM | |
| 8 | Edge Functions | MEDIUM | |
| 9 | Testing | MEDIUM | |
| 10 | Security | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 行级安全(Row Level Security) | 关键(CRITICAL) | |
| 2 | Clerk集成 | 关键(CRITICAL) | |
| 3 | 数据库安全 | 高(HIGH) | |
| 4 | 认证模式 | 高(HIGH) | |
| 5 | API安全 | 高(HIGH) | |
| 6 | 存储安全 | 中高(MEDIUM-HIGH) | |
| 7 | 实时安全 | 中(MEDIUM) | |
| 8 | Edge Functions | 中(MEDIUM) | |
| 9 | 测试 | 中(MEDIUM) | |
| 10 | 安全 | 中(MEDIUM) | |
Quick Reference
快速参考
1. Row Level Security (CRITICAL)
1. 行级安全(关键)
- - Always enable RLS on public schema tables
rls-always-enable - - Wrap auth functions with (SELECT ...) for performance
rls-wrap-functions-select - - Add indexes on columns used in RLS policies
rls-add-indexes - - Specify roles with TO authenticated clause
rls-specify-roles - - Use SECURITY DEFINER functions for complex policies
rls-security-definer - - Minimize joins in RLS policies
rls-minimize-joins - - Use explicit auth.uid() checks
rls-explicit-auth-check - - Use RESTRICTIVE policies for additional constraints
rls-restrictive-policies
- - 始终为公共架构表启用RLS
rls-always-enable - - 用(SELECT ...)包装认证函数以提升性能
rls-wrap-functions-select - - 为RLS策略中使用的列添加索引
rls-add-indexes - - 通过TO authenticated子句指定角色
rls-specify-roles - - 使用SECURITY DEFINER函数实现复杂策略
rls-security-definer - - 尽量减少RLS策略中的关联操作
rls-minimize-joins - - 使用显式的auth.uid()检查
rls-explicit-auth-check - - 使用RESTRICTIVE策略添加额外约束
rls-restrictive-policies
2. Clerk Integration (CRITICAL)
2. Clerk集成(关键)
- - Use Third-Party Auth integration (not JWT templates)
clerk-setup-third-party - - Use accessToken callback for server-side clients
clerk-client-server-side - - Use useSession() hook for client-side clients
clerk-client-client-side - - Configure role: authenticated claim in Clerk
clerk-role-claim - - Use organization claims for multi-tenant RLS
clerk-org-policies - - Enforce MFA with RESTRICTIVE policies
clerk-mfa-policies - - Never use deprecated JWT template integration
clerk-no-jwt-templates
- - 使用第三方认证集成(而非JWT模板)
clerk-setup-third-party - - 为服务器端客户端使用accessToken回调
clerk-client-server-side - - 为客户端使用useSession()钩子
clerk-client-client-side - - 在Clerk中配置role: authenticated声明
clerk-role-claim - - 使用组织声明实现多租户RLS
clerk-org-policies - - 通过RESTRICTIVE策略强制启用MFA
clerk-mfa-policies - - 切勿使用已弃用的JWT模板集成
clerk-no-jwt-templates
3. Database Security (HIGH)
3. 数据库安全(高)
- - Use versioned migrations for schema changes
db-migrations-versioned - - Follow proper schema design patterns
db-schema-design - - Implement proper indexing strategy
db-indexes-strategy - - Always use foreign key constraints
db-foreign-keys - - Secure trigger functions properly
db-triggers-security - - Use SECURITY INVOKER for views
db-views-security-invoker
- - 使用版本化迁移管理架构变更
db-migrations-versioned - - 遵循正确的架构设计模式
db-schema-design - - 实施合理的索引策略
db-indexes-strategy - - 始终使用外键约束
db-foreign-keys - - 妥善保护触发器函数
db-triggers-security - - 为视图使用SECURITY INVOKER
db-views-security-invoker
4. Authentication Patterns (HIGH)
4. 认证模式(高)
- - Always validate JWT claims
auth-jwt-claims-validation - - Treat user_metadata as untrusted
auth-user-metadata-safety - - Use app_metadata for authorization
auth-app-metadata-authorization - - Implement proper session management
auth-session-management
- - 始终验证JWT声明
auth-jwt-claims-validation - - 将user_metadata视为不可信数据
auth-user-metadata-safety - - 使用app_metadata进行授权
auth-app-metadata-authorization - - 实施合理的会话管理
auth-session-management
5. API Security (HIGH)
5. API安全(高)
- - Always filter queries even with RLS
api-filter-queries - - Use publishable keys correctly
api-publishable-keys - - Never expose service role key to client
api-service-role-server-only
- - 即使启用RLS也始终过滤查询
api-filter-queries - - 正确使用可发布密钥
api-publishable-keys - - 切勿向客户端暴露服务角色密钥
api-service-role-server-only
6. Storage Security (MEDIUM-HIGH)
6. 存储安全(中高)
- - Enable RLS on storage.objects
storage-rls-policies - - Configure bucket-level security
storage-bucket-security - - Use signed URLs for private files
storage-signed-urls
- - 为storage.objects启用RLS
storage-rls-policies - - 配置桶级安全
storage-bucket-security - - 为私有文件使用签名URL
storage-signed-urls
7. Realtime Security (MEDIUM)
7. 实时安全(中)
- - Use private channels for sensitive data
realtime-private-channels - - RLS policies apply to realtime
realtime-rls-authorization - - Clean up subscriptions on unmount
realtime-cleanup-subscriptions
- - 为敏感数据使用私有频道
realtime-private-channels - - RLS策略适用于实时功能
realtime-rls-authorization - - 在卸载时清理订阅
realtime-cleanup-subscriptions
8. Edge Functions (MEDIUM)
8. Edge Functions(中)
- - Always verify JWT in edge functions
edge-verify-jwt - - Handle CORS properly
edge-cors-handling - - Use secrets for sensitive data
edge-secrets-management
- - 始终在Edge Functions中验证JWT
edge-verify-jwt - - 妥善处理CORS
edge-cors-handling - - 使用密钥管理敏感数据
edge-secrets-management
9. Testing (MEDIUM)
9. 测试(中)
- - Test RLS policies with pgTAP
test-pgtap-rls - - Isolate tests properly
test-isolation - - Use test helper functions
test-helpers
- - 使用pgTAP测试RLS策略
test-pgtap-rls - - 妥善隔离测试
test-isolation - - 使用测试辅助函数
test-helpers
10. Security (MEDIUM)
10. 安全(中)
- - Validate all inputs before processing
security-validate-inputs - - Regularly run Security Advisor checks
security-audit-advisors
- - 处理前验证所有输入
security-validate-inputs - - 定期运行Security Advisor检查
security-audit-advisors
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
references/rules/rls-always-enable.md
references/rules/clerk-setup-third-party.md
references/rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- When NOT to use the pattern
- Reference links to official documentation
阅读单个规则文件获取详细说明与代码示例:
references/rules/rls-always-enable.md
references/rules/clerk-setup-third-party.md
references/rules/_sections.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 不适用该模式的场景
- 官方文档参考链接
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
references/supabase-guidelines.md如需查看包含所有扩展规则的完整指南:
references/supabase-guidelines.md