> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Security and limits

> Rate limits of every authentication route, lockout after failed attempts, end-user IP forwarding, keyrings, callback hardening and accepted risks.

This page gathers the controls that protect the horizon-api authentication routes. The values come
from the code (`backend/horizon-api/lib/common/rate-limit/auth-rate-limit.config.ts`) and from the
`authenticate/security-hardening` initiative.

## How the limiter works

The limiter uses a shared counter in Upstash. Its mode is the server flag
`authenticate-rate-limit-mode` of horizon-api, edited in Horizon Enterprise → Capabilities (it needs
`feature-flag:update-security`). It replaced `AUTH_RATE_LIMIT_ENFORCE` in 2026.11.2; the variable is
read only as a transitional fallback until 2026.11.3.

| Value | Effect |
| - | - |
| `enforce` | Enforces the limits: answers `429` (recovery answers the same success body) |
| `shadow` | Counts and logs `rate_limit_blocked`, without blocking |
| `off` | The limiter does not run |

Every environment runs `shadow` from the 2026.11.2 release night and moves to `enforce` after 7 days
of logs. If the flag store cannot be read, the limiter enforces (safe default).

Policies marked **always** are enforced whatever that value says. Policies marked **silent** answer
the same as a successful request once over budget, so nothing is revealed. When Upstash does not
respond, each policy decides: **local** uses a per-instance counter and **closed** answers `503`.

IPs are stored as a hash with a daily salt. The `user` key is the hash of the presented bearer, so a
refreshed token starts with a new budget.

## Limits per route

| Policy | Limit | Window | Key | If Upstash is down | Notes |
| - | - | - | - | - | - |
| `verify-email` | 30 | 10 min | IP | local | |
| `verify-phone` | 30 | 10 min | IP | local | |
| `verify-dni` | 30 | 10 min | IP | local | |
| `validate-identity` | 10 | 10 min | IP | local | |
| `login-password` | 10 | 10 min | IP | local | plus per-account lockout |
| `login-worker` | 10 | 10 min | IP | local | |
| `login-worker-document` | 5 | 1 h | user, IP | closed | always |
| `magic-link` (consume) | 10 | 10 min | IP | closed | |
| `magic-link-request` | 5 | 1 h | e-mail, IP | closed | always, silent |
| `onboarding-magic-link-resend` | 1 | 1 h | user | local | always |
| `oauth-start` | 30 | 10 min | IP | local | |
| `oauth-welcome` | 10 | 10 min | user | local | |
| `oauth-link` | 5 | 1 h | user, IP | closed | always |
| `apple-notifications` | 20 | 10 min | Apple subject | local | never per IP |
| `password` (create or change) | 10 | 1 h | user, IP | local | always |
| `recovery` | 3 | 1 h | e-mail, IP | closed | silent |
| `reset-password` | 10 | 1 h | IP | closed | |
| `register` | 5 | 1 h | IP | local | |
| `register-patch` | 20 | 1 h | IP, user | local | |
| `resend-confirmation` | 3 | 1 h | e-mail | closed | |
| `abandon-registration` | 10 | 10 min | user | local | |
| `underage-email` | 10 | 10 min | IP, e-mail | local | always |
| `otp` | 10 | 1 h | IP | closed | |
| `profile-me` | 60 | 1 h | user | local | |
| `context-switch` | 30 | 10 min | user | local | |
| `account-login-email` | 10 | 1 h | user, IP | closed | always |
| `account-merge-assisted` | 5 | 1 h | user, IP | closed | always |
| `contract-lookup` | 5 | 1 min | IP | local | always |
| `floid-identity` | 5 | 1 h | user | local | always |
| `onboarding-identity` | 5 | 1 h | user | local | always |

`apple-notifications` is counted after the JWS is verified, keyed by `apple:` plus the subject,
because every event comes from Apple's shared egress.

## Lockout after failed attempts

With the server flag `authenticate-login-lockout` on (formerly `AUTH_LOCKOUT_ENABLED`), each account (identified by an HMAC of its e-mail) allows 5 failed
password attempts in 15 minutes. The sixth attempt inside the window answers `423` and the account
stays locked for 15 minutes. A successful sign-in clears the counter, and password recovery resets it
too. The counter lives in Upstash; without Upstash it counts per instance.

Support can unlock an account with:

```http theme={null}
POST /api/v3/auth/lockout/clear
Content-Type: application/json

{ "email": "person@example.com" }
```

It requires the `auth:unlock-lockout` permission. It answers `503` when the lock could not be
deleted, so support does not report an unlock that did not happen.

> [!NOTE]
>
> **Pending.** The lockout is turned on once the client copy has shipped. The production target is to
> have both the limiter and the lockout enforced.

## End-user IP forwarding

horizon-authenticate calls `verify-email`, `magic-link/request`, password sign-in and recovery from
server actions. All those calls leave from the same server IP. So that the per-IP limit counts each
person and not the server (ADR 0098):

1. horizon-authenticate reads the user's IP (`x-vercel-forwarded-for`, `x-real-ip`) and sends it in
   `x-auth-client-ip`, together with `x-auth-bff-token`.
2. horizon-api uses that IP **only** when `x-auth-bff-token` matches its `AUTH_BFF_SERVER_TOKEN`.

That is why `AUTH_BFF_SERVER_TOKEN` must hold **the same value** (at least 32 characters) in
horizon-api and in horizon-authenticate of each environment. portal-customer already uses it.

> [!WARNING]
>
> If it is missing or different, the whole environment shares the server IP budget: as soon as a
> handful of people sign in, `verify-email` and the magic link answer `429` for everyone.

## Keyrings

| Variable | What it seals | Rotation |
| - | - | - |
| `AUTH_PROVIDER_TOKEN_KEYRING` | Apple refresh tokens in `auth_provider_credential` (AES-256-GCM) | Add a new key as active and keep the previous one until every row is re-encrypted. A row is rewritten when its login signs in with Apple again |
| `AUTH_GOTRUE_BRIDGE_KEYRING` | The encrypted GoTrue session bridge used by v3 logout (transitional) | Add a new key; values are rewritten on sign-in or refresh and by a pass over Redis. Keep the previous one until no value uses it or 30 days, whichever is later |

They are different keyrings and are never mixed. Without `AUTH_PROVIDER_TOKEN_KEYRING`, Apple refresh
tokens are not stored and cannot be revoked when the account is deleted. The steps are in
[Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md#rotate-the-keyrings).

## Callback hardening

* **Tokens in the fragment.** Deep links carry the tokens after `#`, never in the query.
* **410 for query tokens.** A `GET` to `callback-redirect` with tokens in the query answers
  `410 OAUTH_CALLBACK_UPGRADE_REQUIRED`: always for Microsoft, and for the others when the
  `authenticate-oauth-query-tokens-accepted` flag is `false`. Usage is logged as `oauth_query_tokens_used`.
* **Provider identity.** Each callback accepts only a session that holds its provider's identity
  (Google `google`, Microsoft `azure`); `app_metadata.provider` does not count.
* **Unverified e-mail.** Without an e-mail, or with an unverified one, no profile is created: the deep
  link returns `error=unverified_email`. For Microsoft the e-mail only counts as verified through
  `email_confirmed_at` or the Azure identity of the same address.
* **Redirect allowlist.** `redirect_url` is checked against
  `config/social-login/redirects.<environment>.json`. With the `security-social-redirect-enforce`
  flag `false` it is only logged; the flag is `true` in every environment right after the 2026.11.2
  Sync (T-105).
* **No caching.** Session-bearing responses send `cache-control: no-store` and `pragma: no-cache`.

## Accepted risks

> [!NOTE]
>
> **Enumeration (ADR 0134).** `verify-email` returns `providers`, so it reveals which enabled provider
> an e-mail uses. This is accepted because the route is rate-limited and returns no profile data. A
> mail scanner that runs JavaScript can burn a magic link; that is accepted too.

## Open tasks

As of 2026-11-04 the `authenticate/security-hardening` initiative stands at 8%. The most relevant:

| Task | Priority | Status | What is missing |
| - | - | - | - |
| T-01 | p1 | review | Limit policies for the new 2026.11.3 routes |
| T-101 | p0 | in progress | `PATCH /auth/register` bound to the session JWT |
| T-102 | p0 | in progress | Anonymous `verify-email` and `verify-phone-number` return booleans only |
| T-104 | p0 | in progress | MaihueGO release that authenticates onboarding, and closing the anonymous window |
| T-105 | p0 | in progress | Per-environment `redirect_url` allowlist on every start and callback |
| T-107 | p0 | in progress | Redact URL, query and header secrets in logs |
| T-108 | p0 | todo | Purge token-bearing logs in Better Stack |
| T-111 | p1 | in progress | Limits and lockout on every public auth route |
| T-115 | p1 | todo | Alerts for lockout waves and rejected secrets |
| T-201 to T-205 | p1 | todo | Limits, client authentication, `client_secret` rotation and MFA on the IdP |

## See also

* [IdP, machines and events](https://docs.bbrands.io/maihuego/authentication/platform.md)
* [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md)
* [Rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)
* [IdP Security & Governance](https://docs.bbrands.io/oauth/security.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.