Skip to Content
Texfolia Docs

Public auth abuse protection

Auth is configured in apps/web/src/lib/auth.ts using Better Auth's built-in flows only. The login page supports GitHub, Google, GitLab, and Microsoft, plus a 6-digit email code. The code expires in 5 minutes, is stored hashed, and allows 3 attempts. Verifying it signs in an existing account or creates a verified one. Stripe customers are created at checkout, not when the account is created, so a code request never reaches Stripe. The login page reads Better Auth's better-auth.last_used_login_method cookie after mount and shows a Last used badge on the matching provider or on Continue with email. The cookie is not copied into the database.

All paths below are relative to /api/auth. This inventory covers the unauthenticated HTTP routes in Better Auth 1.7.15 and the configured plugins.

Method and pathSide effects and controls
POST /email-otp/send-verification-otpSends a sign-in code when type is sign-in. Requires Turnstile. Three requests per IP and path in 60 seconds. Known and unknown addresses get the same response and an email. Does not create the user. The sender throws for any other OTP type, so no mail goes out.
POST /sign-in/email-otpChecks the code, then opens a session. Creates a verified user when the address is new. The code works once. A wrong code returns INVALID_OTP. After 3 wrong codes the next check returns TOO_MANY_ATTEMPTS. An expired code returns OTP_EXPIRED. Three requests per IP and path in 60 seconds.
POST /send-verification-emailSends a verification link to an unverified account. Requires Turnstile. Unknown and verified addresses get the same response and no email. Changing email still uses this link for the new address.
GET /verify-emailCompletes an email verification with a signed, single-use token that expires after one hour. Uses the default rate limit.
POST /sign-in/socialStarts OAuth or verifies a provider ID token. Three requests per IP and path in 10 seconds.
GET/POST /callback/:idCompletes OAuth using provider proof and the OAuth state flow. Uses the default rate limit.
GET /get-session, POST /sign-out, GET /errorSession lookup/refresh, session removal, or error redirect. No email or customer creation. Uses the default rate limit, except that /get-session has none.
GET /delete-user/callbackCompletes an account deletion from its confirmation email. Requires a session and that user's stored token. Redirects to Account settings without deleting while the account is the only owner of an organization. Uses the default rate limit.
POST /stripe/webhookRequires a valid Stripe webhook signature. Has no rate limit.

The default rate limit is 100 requests per IP and path in 10 seconds. The email OTP plugin tightens code send and code sign-in to 3 requests per IP and path in 60 seconds. Limits run before CAPTCHA verification, including on invalid and missing-token requests. Counters live in the rate_limit Postgres table, so every Worker isolate shares them. Turnstile is required for every public email sender.

cf-connecting-ip is the only configured client IP header. Cloudflare supplies this header in deployed Workers; caller-supplied x-forwarded-for values cannot select a different bucket. Direct local requests without the header use Better Auth's development loopback fallback.

The remaining organization, billing, account, and session-management endpoints require a session and their applicable authorization checks. In particular, /organization/invite-member requires organization permission before sending an invitation, and only owners invite owners and admins. /delete-user emails a confirmation link and is refused while the account is the only owner of an organization. Subscription changes require account ownership or an organization owner/admin role. /change-email also requires Turnstile, which runs before the session check.

Session cache

session.cookieCache keeps a signed copy of the session and user in a cookie for five minutes, so most requests do not read the session from Postgres. A session that was revoked, or whose user was deleted, keeps working for up to five minutes where only the cookie is checked. These always read the database:

  • Project access and roles, on every request.
  • WebSocket upgrades, through requestPrincipal. Reading the database there also sets no cookie, which would make TanStack Start rebuild the 101 response and fail the upgrade.
  • /organization/*, /subscription/*, and session revocation routes (/revoke-session, /revoke-other-sessions, /revoke-sessions), whose requests lose the cached cookie in beforeAuth.
  • Better Auth's own account, email, and deletion endpoints.

Disabled routes

disabledPaths keeps OAuth tokens on the server by disabling /get-access-token and /refresh-token.

Turnstile configuration

TURNSTILE_HOSTNAMES is a comma-separated list of exact hostnames, without schemes, ports, paths, or wildcards. Whitespace is trimmed and names are lowercased. If the variable is absent, the hostname of BETTER_AUTH_URL is used. Empty or malformed lists fail during auth configuration. Local development allows localhost. The production value is configured under env.production in apps/web/wrangler.jsonc.

Every token must report an allowed hostname and action auth-email. The auth pages, account settings, and the server share the action constant in roles.ts. Missing tokens, failed verification, missing/mismatched hostname or action, duplicate/expired tokens, and provider outages fail before any email is sent. Turnstile's Siteverify service enforces token expiry and single use.

Code requests return the same response body whether or not an account exists. Both addresses receive a code. The account is created only when the code is verified. CAPTCHA failures and rate-limit responses do not include account information. These are response-body guarantees. Local tests do not establish constant timing.

Verification

From the repository root:

bun test apps/web/src/lib/auth.test.ts apps/web/src/lib/auth-abuse.test.ts

auth-abuse.test.ts uses the production auth options, Better Auth's HTTP handler, a memory adapter, captured email delivery, and mocked external Turnstile/Stripe boundaries. It checks valid/missing/invalid tokens, hostname and action enforcement, provider failures, generic responses, rate limits, path normalization, one-time sign-in codes, attempt limits, and that creating an account from a code never reaches Stripe. No external provider requests are made.

On this page