Deutsch
Fix libraryMediumAuth · AI

No rate limit on login or AI endpoints

Without rate limits, bots can guess passwords and burn through your AI budget. Use Supabase Auth’s limits and CAPTCHA, and add a limiter to your own endpoints.

By Tim GeithnerPublished 4 min read

ShipRely's Security audit checks for this today.

Short answer

Endpoints that check a secret (a password, a code, an invite token) or that cost money per call (AI, e-mail, SMS) need a limit on how often one client or account may call them. Supabase Auth already limits its own sign-in and reset endpoints per IP and supports CAPTCHA; your own edge functions get nothing automatically. Add a per-user or per-key limiter in the database, check it before the expensive work, and answer 429 when it is exceeded.

Ask an AI about this page:ChatGPTClaude

On this page

An endpoint without a rate limit answers as fast and as often as anyone asks. On a login or code check, that lets a script try thousands of passwords; on an AI endpoint, it lets one account run up your bill in minutes. It is rated medium: it rarely leaks data on its own, but it makes guessing attacks and cost abuse cheap. Supabase Auth protects its own endpoints; the gap is almost always in the code you wrote yourself.

Why it matters

Two kinds of endpoints need a limit:

  • Endpoints that check a secret. Passwords, one-time codes, invite codes, “enter the PIN to open the shared board”. Without a limit, an attacker can try a long list of guesses or replay leaked password lists, known as credential stuffing. OWASP’s authentication cheat sheet lists brute force, credential stuffing and password spraying as the common automated attacks and recommends login throttling with a counter tied to the account, not only to the IP address.
  • Endpoints that cost money per call. AI requests, e-mails, SMS, image generation. OWASP’s API Security Top 10 calls this unrestricted resource consumption and recommends limiting how often a client can call an API within a time window, with stricter limits for sensitive operations.

The good news for Supabase apps: Supabase Auth applies its own limits. Its docs list per-IP limits for sign-ups and sign-ins, password reset, verification and token requests, adjustable under Authentication → Rate Limits, and it supports hCaptcha and Cloudflare Turnstile on sign-in, sign-up and reset forms. If you use supabase.auth.signInWithPassword, you already have a baseline. What has no limit by default is everything in supabase/functions/.

How to find it in your app

List the endpoints that check secrets or cost money, then look for a limit in each:

Search your repository
grep -rln "api.openai.com\|api.anthropic.com\|resend\|twilio\|sendgrid" supabase/functions
grep -rn "password\|otp\|pin\|code" supabase/functions --include=index.ts
grep -rn "check_rate_limit\|ratelimit\|429" supabase/functions

A function that appears in the first two searches but not in the third has no limit. Also check the Auth side in the dashboard: open Authentication → Rate Limits and make sure nobody raised the values for testing and forgot them, and consider turning on CAPTCHA (the Enable CAPTCHA protection toggle under Bot and Abuse Protection in the Auth settings) if your sign-up is open to everyone.

How to fix it

For sign-in, use what Supabase gives you. Keep sign-in on Supabase Auth instead of building your own password check, review the limits under Authentication → Rate Limits, and enable CAPTCHA. Your form then passes the CAPTCHA token along and shows a clear message on 429:

Safesrc/lib/auth.ts
import { supabase } from "@/integrations/supabase/client";

export async function signIn(email: string, password: string, captchaToken: string) {
  const { error } = await supabase.auth.signInWithPassword({ email, password, options: { captchaToken } });
  if (error?.status === 429) return "Too many attempts. Please wait a few minutes.";
  if (error) return "Email or password is wrong.";
  return null;
}

For your own endpoints, add a limiter in the database. This is the fixed-window limiter from our rate limiting guide. The counter table sits in a schema the Data API doesn’t expose, and only the service role may call the function:

SafeA rate limiter only the server can call
create schema if not exists private;

create table private.rate_limits (
  key text not null,
  window_start timestamptz not null,
  hits integer not null default 0,
  primary key (key, window_start)
);

create function public.check_rate_limit(_key text, _max_hits integer, _window interval)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
  _start timestamptz := date_bin(_window, now(), timestamptz '2000-01-01');
  _hits integer;
begin
  insert into private.rate_limits as r (key, window_start, hits)
  values (_key, _start, 1)
  on conflict (key, window_start)
    do update set hits = r.hits + 1
  returning r.hits into _hits;

  return _hits <= _max_hits;
end;
$$;

revoke execute on function public.check_rate_limit(text, integer, interval)
  from public, anon, authenticated;
grant execute on function public.check_rate_limit(text, integer, interval)
  to service_role;

Call it first thing in the function, keyed by the verified user, and refuse with 429 before any paid or sensitive work:

Safesupabase/functions/ai-suggest/index.ts
import { withSupabase } from "npm:@supabase/server@1";

export default {
  fetch: withSupabase({ auth: "user" }, async (_req, ctx) => {
    const { data: allowed, error } = await ctx.supabaseAdmin.rpc("check_rate_limit", {
      _key: `ai:${ctx.userClaims!.id}`,
      _max_hits: 10,
      _window: "1 minute",
    });
    if (error) return Response.json({ error: "Try again later" }, { status: 503 });
    if (!allowed) {
      return Response.json({ error: "Too many requests" }, { status: 429, headers: { "Retry-After": "60" } });
    }

    // ... the expensive work
    return Response.json({ ok: true });
  }),
};

For a custom code or PIN check, key the limit by the thing being guessed (for example pin:<board id>), so rotating IP addresses doesn’t reset it. For a daily ceiling on AI costs, combine this with a per-user quota.

Add rate limits to login and AI endpoints

Add a new Supabase migration that creates a private schema, a table private.rate_limits (key, window_start, hits, primary key key and window_start) and a security definer function public.check_rate_limit(_key text, _max_hits integer, _window interval) with search_path set to empty that counts hits per fixed window with insert on conflict do update and returns whether the count is within the limit. Revoke execute from public, anon and authenticated and grant it to service_role only.

In every edge function that calls an AI provider or sends e-mails or SMS, call check_rate_limit first with the key “ai:” plus the verified user id, 10 hits per minute, and return 429 with a Retry-After header when it returns false. In any custom code or PIN check, key the limit by the resource being guessed. Do not move sign-in away from Supabase Auth. List the endpoints you changed.

Check that the fix works

  1. Temporarily lower _max_hits to 3 and call the AI feature four times quickly as one user: the fourth call must get 429, and a second user must not be affected.
  2. Call the endpoint without a user token: it must get 401, not a counted call.
  3. On your sign-in form, try a wrong password repeatedly from your own machine: after Supabase’s limit you should see your “too many attempts” message, and with CAPTCHA enabled, requests without a token must fail.
  4. Open the Logs of the function after a burst and confirm the 429s are there, so you can spot abuse later.

We ran this SQL against Postgres with Supabase’s roles: ten calls in a minute pass, the eleventh is refused, other users are unaffected, and neither visitors nor signed-in users can call the limiter or touch its counters.

Frequently asked questions

Does Supabase rate limit sign-in for me?

For its own Auth endpoints, yes. Supabase’s docs list per-IP limits for sign-in, sign-up, password reset, verification and token requests, adjustable under Authentication → Rate Limits. They don’t cover your own edge functions or custom login code.

Should I limit by IP address or by account?

Both have a place. OWASP’s authentication guidance recommends tying failed-login counters to the account, because an attacker can rotate IP addresses. For AI endpoints, limit per signed-in user; for signed-out forms, per IP plus CAPTCHA.

Can I just limit requests in the browser?

No. A disabled button or a client-side counter only slows down your own UI. Anyone calling the endpoint directly ignores it. The limit has to sit on the server, before the expensive or sensitive work.

What status code should a limited request get?

429 Too Many Requests, ideally with a Retry-After header. Supabase Auth itself answers 429 when its limits are exceeded, so your app can treat both the same way.

Sources

  1. 1.Rate limits — Supabase Docs
  2. 2.Enable CAPTCHA Protection — Supabase Docs
  3. 3.Authentication Cheat Sheet — OWASP Cheat Sheet Series
  4. 4.API4:2023 Unrestricted Resource Consumption — OWASP API Security Top 10
  5. 5.CWE-307: Improper Restriction of Excessive Authentication Attempts — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt