---
title: Security
description: What the package protects against, and how.
---

# Security

| Threat | Protection |
| --- | --- |
| Stolen database | Sessions, API tokens and e-mail links are stored as SHA-256 hashes. Recovery codes are an HMAC with a key from `secret`, so the short codes cannot be brute-forced from a dump alone. Passwords use argon2id. TOTP secrets are encrypted with AES-256-GCM. |
| XSS reading the session | The cookie is `HttpOnly`. |
| Cookie set by a subdomain | On https the cookie is `__Host-`: bound to this host, `Secure`, `Path=/`, no `Domain`. |
| CSRF | `SameSite=Lax`, plus an `Origin` check (and `Sec-Fetch-Site`) on every function that changes something and on sign-out. Changes over GET are refused. |
| OAuth CSRF and code theft | `state`, PKCE (S256) and a Google `nonce`, kept in an encrypted cookie that works for 10 minutes. |
| Open redirects | `next` must be a path on this site. `//host`, `https://app//host`, `\`, control characters and other origins fall back to the default page. |
| Account pre-registration | A Google/GitHub login with a verified e-mail takes over an unverified account with that e-mail, and removes everything that gave access to it: password, sessions, tokens, linked accounts, 2FA. Unverified users cannot link providers. A verification link signs in only the browser that signed up. |
| Stolen session cookie | Adding a way back in (linking a provider, an API token, an e-mail change, a first password, a new authenticator) needs a sign-in in the last 15 minutes or the password. Linking and new tokens e-mail the user. An e-mail change applies only when opened while signed in as the user. A password reset ends sessions, tokens and pending e-mail changes. |
| User enumeration | Same answer and timing for wrong passwords and unknown e-mails. Reset and resend always answer ok. With verification on, sign-up answers the same for new and used e-mails. E-mails go out in the background, so their sending time does not show in the answer. |
| Brute force | Rate limits per e-mail and per IP, stored in Postgres so they hold across instances. 5 tries per two-factor step, and 10 per user in 15 minutes across steps. |
| Lockout | Only failed sign-ins count per e-mail, so the owner can sign in while someone guesses. New reset requests do not cancel older links. |
| Replayed codes and links | Each TOTP code, recovery code and e-mail link works once. A new verification or e-mail change link makes the older one stop working. |
| Leaked API token | Tokens only read through the package, and only in their own workspace when they have one. They cannot change the account, sessions, 2FA, tokens or workspaces. They can expire and be revoked. |
| Dev login in production | Off when `NODE_ENV=production` and on any public host name. |

## Rate limits

| Action | Limit |
| --- | --- |
| Sign-in | 10 failures per e-mail and 50 per IP in 15 min |
| Sign-up | 10 per IP per hour |
| Reset and verification e-mails | 3 per e-mail per hour |
| OAuth callbacks | 30 per IP in 5 min |
| Password confirmation in settings | 10 per user in 15 min |
| Two-factor settings | 10 per user in 15 min |
| Two-factor codes at sign-in | 5 per step; 10 per user in 15 min; 30 per user a day |
| Invites | 50 per user per hour; 100 per workspace a day |

The client IP comes from the last `x-forwarded-for` entry (the one your nearest proxy added), then `x-real-ip`, then the socket address from the adapter. When no IP is known, per-IP limits are skipped rather than shared by everyone, and the package logs a warning once in production. Set `ip` when your proxy chain has more than one hop, or when your proxy passes client headers through unchanged.

## Cryptography

Only built-in primitives: `node:crypto` (randomBytes, SHA-256, HMAC, HKDF, AES-GCM, timingSafeEqual) and argon2id from Bun or Node. Each purpose gets its own key, derived from `secret` with HKDF.

## What you do

- Keep `secret` secret and long (`bunx kaarel-auth secret`). Changing it ends running OAuth flows and makes saved TOTP secrets unreadable.
- Serve the app over https.
- Set `trustedOrigins` only for origins you control.
