Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/separate auth rate limits into its own page (#18265)

Joshen Lim8be40f1fdbe7
diff --git a/studio/components/interfaces/Auth/RateLimits/RateLimits.tsx b/studio/components/interfaces/Auth/RateLimits/RateLimits.tsx
new file mode 100644
index 00000000000..b6d588e2e8e
--- /dev/null
+++ b/studio/components/interfaces/Auth/RateLimits/RateLimits.tsx
@@ -0,0 +1,340 @@
+import { zodResolver } from '@hookform/resolvers/zod'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
+import Link from 'next/link'
+import { useEffect } from 'react'
+import { useForm } from 'react-hook-form'
+import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ Button,
+ FormControl_Shadcn_,
+ FormField_Shadcn_,
+ FormItem_Shadcn_,
+ FormMessage_Shadcn_,
+ Form_Shadcn_,
+ IconAlertCircle,
+ Input_Shadcn_,
+} from 'ui'
+import * as z from 'zod'
+
+import AlertError from 'components/ui/AlertError'
+import {
+ FormActions,
+ FormHeader,
+ FormPanel,
+ FormSection,
+ FormSectionContent,
+ FormSectionLabel,
+} from 'components/ui/Forms'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import { useAuthConfigQuery } from 'data/auth/auth-config-query'
+import { useAuthConfigUpdateMutation } from 'data/auth/auth-config-update-mutation'
+import { useCheckPermissions } from 'hooks'
+import { isSmtpEnabled } from '../SmtpForm/SmtpForm.utils'
+import toast from 'react-hot-toast'
+
+const RateLimits = () => {
+ const formId = 'auth-rate-limits-form'
+ const { ref: projectRef } = useParams()
+ const canUpdateConfig = useCheckPermissions(PermissionAction.UPDATE, 'custom_config_gotrue')
+
+ const {
+ data: authConfig,
+ error,
+ isLoading,
+ isError,
+ isSuccess,
+ } = useAuthConfigQuery({ projectRef })
+ const { mutate: updateAuthConfig, isLoading: isUpdatingConfig } = useAuthConfigUpdateMutation({
+ onSuccess: () => {
+ toast.success('Rate limits successfully updated')
+ },
+ onError: (error) => {
+ toast.error(`Failed to update rate limits: ${error.message}`)
+ },
+ })
+
+ const canUpdateEmailLimit = authConfig?.EXTERNAL_EMAIL_ENABLED && isSmtpEnabled(authConfig)
+ const canUpdateSMSRateLimit = authConfig?.EXTERNAL_PHONE_ENABLED && !authConfig?.SMS_AUTOCONFIRM
+
+ const FormSchema = z.object({
+ RATE_LIMIT_TOKEN_REFRESH: z.coerce.number().min(0, 'Must be not be lower than 0'),
+ RATE_LIMIT_VERIFY: z.coerce.number().min(0, 'Must be not be lower than 0'),
+ RATE_LIMIT_EMAIL_SENT: z.coerce
+ .number()
+ .min(0, 'Must be not be lower than 0')
+ .max(32767, 'Must not be more than 32,767 an hour'),
+ RATE_LIMIT_SMS_SENT: z.coerce.number().min(0, 'Must be not be lower than 0'),
+ })
+
+ const form = useForm<z.infer<typeof FormSchema>>({
+ resolver: zodResolver(FormSchema),
+ defaultValues: {
+ RATE_LIMIT_TOKEN_REFRESH: 0,
+ RATE_LIMIT_VERIFY: 0,
+ RATE_LIMIT_EMAIL_SENT: 0,
+ RATE_LIMIT_SMS_SENT: 0,
+ },
+ })
+
+ const onSubmit = (data: z.infer<typeof FormSchema>) => {
+ if (!projectRef) return console.error('Project ref is required')
+
+ // [Joshen] Temporarily removing RATE_LIMIT_TOKEN_REFRESH and RATE_LIMIT_VERIFY until BE supports it
+ const payload: Partial<z.infer<typeof FormSchema>> = {}
+ if (data.RATE_LIMIT_EMAIL_SENT !== authConfig?.RATE_LIMIT_EMAIL_SENT) {
+ payload.RATE_LIMIT_EMAIL_SENT = data.RATE_LIMIT_EMAIL_SENT
+ }
+ if (data.RATE_LIMIT_SMS_SENT !== authConfig?.RATE_LIMIT_SMS_SENT) {
+ payload.RATE_LIMIT_SMS_SENT = data.RATE_LIMIT_SMS_SENT
+ }
+ updateAuthConfig({ projectRef, config: payload }, { onSuccess: () => form.reset(data) })
+ }
+
+ useEffect(() => {
+ if (isSuccess) {
+ form.reset({
+ RATE_LIMIT_TOKEN_REFRESH: 30,
+ RATE_LIMIT_VERIFY: 30,
+ RATE_LIMIT_EMAIL_SENT: authConfig.RATE_LIMIT_EMAIL_SENT,
+ RATE_LIMIT_SMS_SENT: authConfig.RATE_LIMIT_SMS_SENT,
+ })
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [isSuccess])
+
+ return (
+ <div>
+ <FormHeader
+ title="Rate Limits"
+ description="Safeguard against bursts of incoming traffic to prevent abuse and maximize stability"
+ docsUrl="https://supabase.com/docs/guides/platform/going-into-prod#rate-limiting-resource-allocation--abuse-prevention"
+ />
+
+ {isError && <AlertError subject="Failed to retrieve auth config rate limits" error={error} />}
+
+ {isLoading && <GenericSkeletonLoader />}
+
+ {isSuccess && (
+ <Form_Shadcn_ {...form}>
+ <form id={formId} className="space-y-4" onSubmit={form.handleSubmit(onSubmit)}>
+ <FormPanel
+ footer={
+ <div className="flex py-4 px-8">
+ <FormActions
+ form={formId}
+ isSubmitting={isUpdatingConfig}
+ hasChanges={form.formState.isDirty}
+ handleReset={() => form.reset()}
+ disabled={!canUpdateConfig}
+ helper={
+ !canUpdateConfig
+ ? 'You need additional permissions to update authentication settings'
+ : undefined
+ }
+ />
+ </div>
+ }
+ >
+ <FormSection
+ id="email-sent"
+ header={
+ <FormSectionLabel
+ description={
+ <p className="text-foreground-light text-sm">
+ Number of emails that can be sent per hour from your project
+ </p>
+ }
+ >
+ Rate limit for sending emails
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false}>
+ <FormField_Shadcn_
+ control={form.control}
+ name="RATE_LIMIT_EMAIL_SENT"
+ render={({ field }) => (
+ <FormItem_Shadcn_>
+ <FormControl_Shadcn_>
+ <Input_Shadcn_ disabled={!canUpdateEmailLimit} type="number" {...field} />
+ </FormControl_Shadcn_>
+ <FormMessage_Shadcn_ />
+ {!canUpdateEmailLimit && (
+ <Alert_Shadcn_>
+ <IconAlertCircle strokeWidth={1.5} />
+ <AlertTitle_Shadcn_>
+ {!authConfig.EXTERNAL_EMAIL_ENABLED &&
+ 'Enable email-based logins to update this configuration'}
+ {!isSmtpEnabled(authConfig) &&
+ 'Custom SMTP provider is required to update this configuration'}
+ </AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ <p className="!leading-tight">
+ {!authConfig.EXTERNAL_EMAIL_ENABLED &&
+ 'Head over to the providers page to enable email provider before updating your rate limit'}
+ {!isSmtpEnabled(authConfig) &&
+ 'The built-in email service has a fixed rate limit. You will need to set up your own custom SMTP provider to update your email rate limit'}
+ </p>
+ <Link
+ passHref
+ href={
+ !authConfig.EXTERNAL_EMAIL_ENABLED
+ ? `/project/${projectRef}/auth/providers`
+ : `/project/${projectRef}/settings/auth`
+ }
+ >
+ <Button asChild type="default" className="mt-2">
+ <a>
+ {!authConfig.EXTERNAL_EMAIL_ENABLED
+ ? 'View providers configuration'
+ : 'View SMTP settings'}
+ </a>
+ </Button>
+ </Link>
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ )}
+ </FormItem_Shadcn_>
+ )}
+ />
+ </FormSectionContent>
+ </FormSection>
+
+ <FormSection
+ id="sms-sent"
+ header={
+ <FormSectionLabel
+ description={
+ <p className="text-foreground-light text-sm">
+ Number of SMS messages that can be sent per hour from your project
+ </p>
+ }
+ >
+ Rate limit for sending SMS messages
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false}>
+ <FormField_Shadcn_
+ control={form.control}
+ name="RATE_LIMIT_SMS_SENT"
+ render={({ field }) => (
+ <FormItem_Shadcn_>
+ <FormControl_Shadcn_>
+ <Input_Shadcn_
+ disabled={!canUpdateSMSRateLimit}
+ type="number"
+ {...field}
+ />
+ </FormControl_Shadcn_>
+ <FormMessage_Shadcn_ />
+ {!canUpdateSMSRateLimit && (
+ <Alert_Shadcn_>
+ <IconAlertCircle strokeWidth={1.5} />
+ <AlertTitle_Shadcn_>
+ Enable phone-based logins to update this configuration
+ </AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ <p className="!leading-tight">
+ Head over to the providers page to enable phone provider and phone
+ confirmations before updating your rate limit
+ </p>
+ <Link passHref href={`/project/${projectRef}/auth/providers`}>
+ <Button asChild type="default" className="mt-2">
+ <a>View providers configuration</a>
+ </Button>
+ </Link>
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ )}
+ </FormItem_Shadcn_>
+ )}
+ />
+ </FormSectionContent>
+ </FormSection>
+
+ {/* [Joshen] The bottom 2 fields are hidden for now until BE support is ready */}
+ {/* <FormSection
+ id="token-refresh"
+ header={
+ <FormSectionLabel
+ description={
+ <p className="text-foreground-light text-sm">
+ Number of sessions that can be refreshed in a 5 minute interval
+ </p>
+ }
+ >
+ Rate limit for token refreshes
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false}>
+ <FormField_Shadcn_
+ control={form.control}
+ name="RATE_LIMIT_TOKEN_REFRESH"
+ render={({ field }) => (
+ <FormItem_Shadcn_>
+ <FormControl_Shadcn_>
+ <Input_Shadcn_ disabled type="number" {...field} />
+ </FormControl_Shadcn_>
+ {field.value > 0 && (
+ <>
+ <p className="text-foreground-lighter text-sm">
+ This is equivalent to {field.value * 12} requests per hour
+ </p>
+ </>
+ )}
+ <FormMessage_Shadcn_ />
+ </FormItem_Shadcn_>
+ )}
+ />
+ </FormSectionContent>
+ </FormSection> */}
+
+ {/* <FormSection
+ id="verify"
+ header={
+ <FormSectionLabel
+ description={
+ <p className="text-foreground-light text-sm">
+ Number of OTP/Magic link verifications that can be made in a 5 minute
+ interval
+ </p>
+ }
+ >
+ Rate limit for token verifications
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false}>
+ <FormField_Shadcn_
+ control={form.control}
+ name="RATE_LIMIT_VERIFY"
+ render={({ field }) => (
+ <FormItem_Shadcn_>
+ <FormControl_Shadcn_>
+ <Input_Shadcn_ disabled type="number" {...field} />
+ </FormControl_Shadcn_>
+ {field.value > 0 && (
+ <p className="text-foreground-lighter text-sm">
+ This is equivalent to {field.value * 12} requests per hour
+ </p>
+ )}
+ <FormMessage_Shadcn_ />
+ </FormItem_Shadcn_>
+ )}
+ />
+ </FormSectionContent>
+ </FormSection> */}
+ </FormPanel>
+ </form>
+ </Form_Shadcn_>
+ )}
+ </div>
+ )
+}
+
+export default RateLimits
diff --git a/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx b/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
index e8c22dec3b3..bbf5def35e5 100644
--- a/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
+++ b/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
@@ -309,15 +309,6 @@ const SmtpForm = () => {
actions={<span className="mr-3 text-foreground-lighter">seconds</span>}
disabled={!canUpdateConfig}
/>
- <InputNumber
- name="RATE_LIMIT_EMAIL_SENT"
- id="RATE_LIMIT_EMAIL_SENT"
- min={0}
- label="Rate limit for sending emails"
- descriptionText="How many emails can be sent per hour."
- actions={<span className="mr-3 text-foreground-lighter">emails per hour</span>}
- disabled={!canUpdateConfig}
- />
<Input
name="SMTP_USER"
id="SMTP_USER"
diff --git a/studio/components/layouts/AuthLayout/AuthLayout.utils.ts b/studio/components/layouts/AuthLayout/AuthLayout.utils.ts
index c5782accffc..4dd7dd8aae3 100644
--- a/studio/components/layouts/AuthLayout/AuthLayout.utils.ts
+++ b/studio/components/layouts/AuthLayout/AuthLayout.utils.ts
@@ -25,6 +25,12 @@ export const generateAuthMenu = (ref: string): ProductMenuGroup[] => {
url: `/project/${ref}/auth/providers`,
items: [],
},
+ {
+ name: 'Rate Limits',
+ key: 'rate-limits',
+ url: `/project/${ref}/auth/rate-limits`,
+ items: [],
+ },
{
name: 'Email Templates',
key: 'templates',
diff --git a/studio/components/ui/Forms/FormHeader.tsx b/studio/components/ui/Forms/FormHeader.tsx
index c1ed34ea1ca..b6a709aba4e 100644
--- a/studio/components/ui/Forms/FormHeader.tsx
+++ b/studio/components/ui/Forms/FormHeader.tsx
@@ -1,17 +1,36 @@
+import Link from 'next/link'
import ReactMarkdown from 'react-markdown'
+import { Button, IconExternalLink } from 'ui'
-const FormHeader = ({ title, description }: { title: string; description?: string }) => {
+const FormHeader = ({
+ title,
+ description,
+ docsUrl,
+}: {
+ title: string
+ description?: string
+ docsUrl?: string
+}) => {
return (
- <div className="mb-6">
- <h3 className="text-foreground mb-2 text-xl">
- <ReactMarkdown unwrapDisallowed disallowedElements={['p']}>
- {title}
- </ReactMarkdown>
- </h3>
- {description && (
- <div className="text-foreground-lighter text-sm">
- <ReactMarkdown>{description}</ReactMarkdown>
- </div>
+ <div className="mb-6 flex items-center justify-between">
+ <div className="space-y-1">
+ <h3 className="text-foreground text-xl">
+ <ReactMarkdown unwrapDisallowed disallowedElements={['p']}>
+ {title}
+ </ReactMarkdown>
+ </h3>
+ {description && (
+ <div className="text-foreground-lighter text-sm">
+ <ReactMarkdown>{description}</ReactMarkdown>
+ </div>
+ )}
+ </div>
+ {docsUrl !== undefined && (
+ <Link passHref href="/">
+ <Button asChild type="default" icon={<IconExternalLink />}>
+ <a>Documentation</a>
+ </Button>
+ </Link>
)}
</div>
)
diff --git a/studio/pages/project/[ref]/auth/rate-limits.tsx b/studio/pages/project/[ref]/auth/rate-limits.tsx
new file mode 100644
index 00000000000..4cbcbe2759c
--- /dev/null
+++ b/studio/pages/project/[ref]/auth/rate-limits.tsx
@@ -0,0 +1,28 @@
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+import RateLimits from 'components/interfaces/Auth/RateLimits/RateLimits'
+import { AuthLayout } from 'components/layouts'
+import { FormsContainer } from 'components/ui/Forms'
+import NoPermission from 'components/ui/NoPermission'
+import { useCheckPermissions } from 'hooks'
+import { NextPageWithLayout } from 'types'
+
+const PageLayout: NextPageWithLayout = () => {
+ const canReadAuthSettings = useCheckPermissions(PermissionAction.READ, 'custom_config_gotrue')
+
+ if (!canReadAuthSettings) {
+ return <NoPermission isFullPage resourceText="access your project's auth rate limit settings" />
+ } else {
+ return (
+ <FormsContainer>
+ <RateLimits />
+ </FormsContainer>
+ )
+ }
+}
+
+PageLayout.getLayout = (page) => {
+ return <AuthLayout>{page}</AuthLayout>
+}
+
+export default PageLayout
diff --git a/studio/stores/authConfig/schema/AuthProviders/AuthProvidersFormValidation.tsx b/studio/stores/authConfig/schema/AuthProviders/AuthProvidersFormValidation.tsx
index 1df8fc716e7..970437a8b93 100644
--- a/studio/stores/authConfig/schema/AuthProviders/AuthProvidersFormValidation.tsx
+++ b/studio/stores/authConfig/schema/AuthProviders/AuthProvidersFormValidation.tsx
@@ -84,11 +84,6 @@ const PROVIDER_PHONE = {
{ label: 'Twilio Verify', value: 'twilio_verify', icon: 'twilio-icon.svg' },
],
},
- RATE_LIMIT_SMS_SENT: {
- type: 'number',
- title: 'Rate limit for sending SMS messages',
- description: 'How many SMS messages can be sent per hour',
- },
// Twilio
SMS_TWILIO_ACCOUNT_SID: {