速率限制
Rate limits protect your services from abuse
Supabase Auth 会对身份验证端点执行速率限制,以防止滥用。一些速率限制是可以自定义的,你可以在你的项目中 身份验证 > 速率限制 进行配置。
🌐 Supabase Auth enforces rate limits on authentication endpoints to prevent abuse. Some rate limits are customizable, and you can configure them in your project Authentication > Rate Limits.
你也可以用管理 API 来管理速率限制:
🌐 You can also manage rate limits using the Management API:
1# Get your access token from https://supabase.com/dashboard/account/tokens2export SUPABASE_ACCESS_TOKEN="your-access-token"3export PROJECT_REF="your-project-ref"45# Get current rate limits6curl -X GET "https://api.supabase.com/v1/projects/$PROJECT_REF/config/auth" \7 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \8 | jq 'to_entries | map(select(.key | startswith("rate_limit_"))) | from_entries'910# Update rate limits11curl -X PATCH "https://api.supabase.com/v1/projects/$PROJECT_REF/config/auth" \12 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \13 -H "Content-Type: application/json" \14 -d '{15 "rate_limit_anonymous_users": 10,16 "rate_limit_email_sent": 10,17 "rate_limit_sms_sent": 10,18 "rate_limit_verify": 10,19 "rate_limit_token_refresh": 10,20 "rate_limit_otp": 10,21 "rate_limit_web3": 1022 }'速率限制行为 #
🌐 Rate limit behavior
Supabase Auth 对受 IP 地址限制的端点操作使用令牌桶算法。
🌐 Supabase Auth uses a token bucket algorithm for endpoint operations that are limited by IP address.
每个桶的最大容量是 30 个请求。当桶满时,可以在短时间内允许最多 30 个请求的短暂突发。一旦桶空了,请求就会被限速,直到令牌重新填充。限速就是定义桶重新填充的速度。
🌐 Each bucket has a maximum capacity of 30 requests. When the bucket is full, brief bursts of up to 30 requests can be allowed in a short period. Once the bucket empties, requests are rate limited until tokens refill. The rate limit defines the rate at which the bucket is refilled.
这意味着闲置的客户端可以容忍短时间的流量高峰,但持续超过速率限制的请求会被拒绝。当超过速率限制时,会返回 429 请求过多 错误。
🌐 This means a client that has been idle will tolerate a brief spike in traffic, but sustained request above the rate limit are denied. When rate limits are exceeded, a 429 Too Many Requests error is returned.
下表显示了身份验证端点的速率限制配额和其他详细信息。
🌐 The table below shows the rate limit quotas and additional details for authentication endpoints.
| 操作 | 路径 | 限制条件 | 可自定义 | 限额 |
|---|---|---|---|---|
| 触发邮件发送的端点 | /auth/v1/signup /auth/v1/recover /auth/v1/user | 项目范围内请求总和 | 仅自定义 SMTP | 2 每小时内使用内置邮件提供商发送的邮件数量。你只能通过自定义 SMTP 设置来更改这个限制。此速率限制仅在调用此端点更新用户邮箱地址时应用于 /auth/v1/user。 |
| 发送一次性密码(OTP) | /auth/v1/otp | 全项目合并请求总和 | 是 | 默认每小时发送 30 个 OTP。 |
| 发送 OTP 或魔法链接 | /auth/v1/otp | 用户的最后请求 | 是 | 默认为 60 seconds 窗口,在此之后才允许该用户发起新的请求。 |
| 注册确认请求 | /auth/v1/signup | 用户的最后一次请求 | 是 | 默认值为 60 seconds ,在允许同一用户进行新请求之前的时间窗口。 |
| 密码重置请求 | /auth/v1/recover | 用户的最后一次请求 | 是 | 默认情况下为 60 seconds 窗口,在同一用户可以再次请求之前。 |
| 验证请求 | /auth/v1/verify | IP 地址 | 否 | 360 每小时请求数(突发可达 30 请求) |
| 令牌刷新请求 | /auth/v1/token | IP 地址 | 否 | 1800 每小时请求次数(突发情况下可达 30 次请求) |
| 创建或验证多因素认证(MFA)挑战 | /auth/v1/factors/:id/challenge /auth/v1/factors/:id/verify | IP 地址 | 否 | 15 次请求每小时(突发可达 次请求) |
| 匿名登录 | /auth/v1/signup | IP 地址 | 否 | 每小时 30 次请求(突发可达 30 次请求)。只有在调用此端点时未在请求体中提供邮箱或手机号时,速率限制才会生效。 |
IP地址转发 #
🌐 IP address forwarding
默认情况下,Supabase Auth 使用客户端的 IP 地址来进行速率限制。在某些情况下,比如使用服务端框架或在项目前面放置代理时,可能需要转发终端用户的 IP 地址,以避免根据服务端客户端的地址被限流。要在 Supabase Auth 中使用转发的 IP 地址进行速率限制,请将 Sb-Forwarded-For 头设置为终端用户的 IP 地址,并使用 密钥 API 发起请求。可公开的 API 密钥和旧版 anon/service_role API 密钥不支持此操作。
🌐 By default, Supabase Auth uses the IP address of the client for rate limiting. In certain cases, such as when using server-side frameworks or proxies in front of a project, it may be necessary to forward the end-user IP address to avoid being rate limited based on the address of the server-side client. To use a forwarded IP address for rate limiting in Supabase Auth, set the Sb-Forwarded-For header to the end-user IP address and make a request with a secret API key. Publishable API keys and legacy anon/service_role API keys are not supported.
新项目必须明确启用 IP 地址转发。你可以在项目的速率限制设置中的 IP 地址转发 部分启用此功能,路径为 认证 > 速率限制。
🌐 IP address forwarding must be explicitly enabled for new projects. You can enable this feature in your project under the IP Address Forwarding section of your project's rate limit settings at Authentication > Rate Limits.
你也可以使用管理 API 启用 IP 地址转发:
🌐 You can also enable IP address forwarding using the management API:
1# Get your access token from https://supabase.com/dashboard/account/tokens2export SUPABASE_ACCESS_TOKEN="your-access-token"3export PROJECT_REF="your-project-ref"45# Update IP address forwarding settings6curl -X PATCH "https://api.supabase.com/v1/projects/$PROJECT_REF/config/auth" \7 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \8 -H "Content-Type: application/json" \9 -d '{10 "security_sb_forwarded_for_enabled": true11 }' \12 | jq '.security_sb_forwarded_for_enabled'一旦启用 IP 地址转发,就可以使用 Supabase SDK 设置 Sb-Forwarded-For 头了:
🌐 Once IP address forwarding is enabled, set the Sb-Forwarded-For header using the Supabase SDK:
1import { createServerClient } from '@supabase/ssr'23const supabase = createServerClient(4 'https://<your-project-id>.supabase.co',5 '<your-secret-key>', // Key should start with sb_secret6 {7 global: {8 headers: {9 'sb-forwarded-for': request.headers.get('x-forwarded-for'),10 },11 },12 }13)