⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
fix(auth): resolve the client IP behind a reverse proxy (#624)
Croff1ab827d16dfc
diff --git a/.env.example b/.env.exampleindex 4a73d3a1..0dd8a0d7 100644--- a/.env.example+++ b/.env.example@@ -42,6 +42,11 @@ BETTER_AUTH_SECRET=change-me-please-generate-a-real-secret # openssl rand -base# (a host-only cookie) when API_URL is on the web's host; set only for multi-label# TLDs or deep subdomains.# COOKIE_DOMAIN=.example.com+# Client IP for the sign-in rate limit (docs/self-hosting.md, "Client IP"). By default+# X-Forwarded-For, taken only when it holds one address. Name another header your proxy+# sets itself, or list the proxies to skip from the right of an X-Forwarded-For chain.+# CLIENT_IP_HEADER=x-real-ip+# TRUSTED_PROXIES=10.0.0.5,172.18.0.0/16# ── API (Elysia) ──API_PORT=3000diff --git a/charts/itsaplan/README.md b/charts/itsaplan/README.mdindex cc5b7d11..212bb652 100644--- a/charts/itsaplan/README.md+++ b/charts/itsaplan/README.md@@ -184,6 +184,8 @@ bot:| `api.env.API_URL` | Public API origin | `""` || `api.env.APP_URL` | Public web origin | `""` || `api.env.COOKIE_DOMAIN` | Cookie domain override | (unset) |+| `api.env.CLIENT_IP_HEADER` | Header the client IP for the rate limit is read from | (unset, `X-Forwarded-For`) |+| `api.env.TRUSTED_PROXIES` | Proxies skipped in an `X-Forwarded-For` chain (CSV) | (unset) || `api.env.GIT_WEBHOOK_BASE_URL` | Public origin for incoming Git webhooks | (unset, uses `API_URL`) || `api.env.S3_BUCKET` | S3 bucket name | `""` || `api.env.S3_REGION` | S3 region | `us-east-1` |diff --git a/charts/itsaplan/templates/configmap.yaml b/charts/itsaplan/templates/configmap.yamlindex 3a61b160..28190b7d 100644--- a/charts/itsaplan/templates/configmap.yaml+++ b/charts/itsaplan/templates/configmap.yaml@@ -14,6 +14,12 @@ data:{{- with .Values.api.env.COOKIE_DOMAIN }}COOKIE_DOMAIN: {{ . | quote }}{{- end }}+ {{- with .Values.api.env.CLIENT_IP_HEADER }}+ CLIENT_IP_HEADER: {{ . | quote }}+ {{- end }}+ {{- with .Values.api.env.TRUSTED_PROXIES }}+ TRUSTED_PROXIES: {{ . | quote }}+ {{- end }}{{- with .Values.api.env.GIT_WEBHOOK_BASE_URL }}GIT_WEBHOOK_BASE_URL: {{ . | quote }}{{- end }}diff --git a/charts/itsaplan/values.yaml b/charts/itsaplan/values.yamlindex 36f84430..60d225b9 100644--- a/charts/itsaplan/values.yaml+++ b/charts/itsaplan/values.yaml@@ -40,6 +40,9 @@ api:API_URL: ""APP_URL: ""# COOKIE_DOMAIN: ""+ # Where the client IP for the rate limit is read; unset reads X-Forwarded-For.+ # CLIENT_IP_HEADER: ""+ # TRUSTED_PROXIES: ""# Public origin for incoming Git webhooks when API_URL is private; unset uses API_URL.# GIT_WEBHOOK_BASE_URL: ""S3_BUCKET: ""diff --git a/docker-compose.coolify-images.yml b/docker-compose.coolify-images.ymlindex 0850a65c..676c67a4 100644--- a/docker-compose.coolify-images.yml+++ b/docker-compose.coolify-images.yml@@ -135,6 +135,9 @@ services:- APP_URL=${SERVICE_URL_WEB}# To override the cookie parent domain (multi-label TLDs / deep subdomains),# add a COOKIE_DOMAIN env in Coolify — the code reads it if present.+ # Where the client IP for the rate limit is read; empty reads X-Forwarded-For.+ - CLIENT_IP_HEADER=${CLIENT_IP_HEADER:-}+ - TRUSTED_PROXIES=${TRUSTED_PROXIES:-}# Public origin for incoming Git webhooks when API_URL is private; empty uses API_URL.- GIT_WEBHOOK_BASE_URL=${GIT_WEBHOOK_BASE_URL:-}- S3_ENDPOINT=http://minio:9000diff --git a/docker-compose.coolify.yml b/docker-compose.coolify.ymlindex e07a84c1..49f4842e 100644--- a/docker-compose.coolify.yml+++ b/docker-compose.coolify.yml@@ -132,6 +132,9 @@ services:APP_URL: ${SERVICE_URL_WEB:-http://localhost:3001}# To override the cookie parent domain (multi-label TLDs / deep subdomains),# add a COOKIE_DOMAIN env in Coolify — the code reads it if present.+ # Where the client IP for the rate limit is read; empty reads X-Forwarded-For.+ CLIENT_IP_HEADER: ${CLIENT_IP_HEADER:-}+ TRUSTED_PROXIES: ${TRUSTED_PROXIES:-}# Public origin for incoming Git webhooks when API_URL is private; empty uses API_URL.GIT_WEBHOOK_BASE_URL: ${GIT_WEBHOOK_BASE_URL:-}S3_ENDPOINT: http://minio:9000diff --git a/docker-compose.yml b/docker-compose.ymlindex 30017a41..d9f70ccb 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -132,6 +132,9 @@ services:# Set COOKIE_DOMAIN only to override the parent domain derived from# APP_URL (multi-label TLDs, deep subdomains).COOKIE_DOMAIN: ${COOKIE_DOMAIN:-}+ # Where the client IP for the rate limit is read; empty reads X-Forwarded-For.+ CLIENT_IP_HEADER: ${CLIENT_IP_HEADER:-}+ TRUSTED_PROXIES: ${TRUSTED_PROXIES:-}# Public origin for incoming Git webhooks when API_URL is private; empty uses API_URL.GIT_WEBHOOK_BASE_URL: ${GIT_WEBHOOK_BASE_URL:-}S3_ENDPOINT: http://minio:9000diff --git a/docs/railway.md b/docs/railway.mdindex d9546e5b..e1094e60 100644--- a/docs/railway.md+++ b/docs/railway.md@@ -65,5 +65,10 @@ Secrets — auth secret, encryption key, worker token, database password — aredeploy and never stored in the template. Attachments go to the Railway bucket, which is why`S3_FORCE_PATH_STYLE` is `false` here; RustFS in the Compose stack needs the default.+Railway's edge puts the client address in `X-Real-IP`, so the template sets+`CLIENT_IP_HEADER=x-real-ip` on the api service: the sign-in rate limit is then kept per+client. A deploy without that variable reads `X-Forwarded-For`, which Railway does not+document; add it under the api service's **Variables**.+To run the same stack elsewhere, see [self-hosting.md](self-hosting.md) or[coolify.md](coolify.md).diff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex 1c935143..fb6df524 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -94,6 +94,37 @@ webhook is updated in place on the next reconcile: saving the provider token agaregenerating the project's webhook secret. Deliveries are still authenticated by theproject's secret. Auth, email links, the browser and MCP keep using `API_URL`.+### Client IP++The api rate-limits authentication per client address: sign-in, sign-up, password change and+email change to 3 requests per 10 seconds, sign-in links (requesting and opening one) to 5 per+minute, password reset and confirmation emails to 3 per minute. It reads the address from `X-Forwarded-For` and takes it only when the header holds+one address. When it finds no address it can use, every client shares one limit per route,+and the api logs `Rate limiting could not determine a client IP` once.++Caddy, Traefik, and ingress-nginx in its default configuration drop the `X-Forwarded-For` a+client sends and write the address they see, so they need no setting. These setups do:++- **nginx** writes no `X-Forwarded-For` unless told to. Set+ `proxy_set_header X-Forwarded-For $remote_addr;`. The common+ `$proxy_add_x_forwarded_for` appends the address to the one the client sent; with it, list+ the nginx address in `TRUSTED_PROXIES`.+- **Cloudflare** in front of the proxy: set `CLIENT_IP_HEADER=cf-connecting-ip`. Cloudflare+ puts the client address in that header, and the proxy behind it sees only Cloudflare's.+ The proxy must then accept connections from Cloudflare alone: a request sent to it directly+ carries any value in that header.+- **A chain of proxies that append**: list their addresses or CIDR ranges, comma-separated,+ in `TRUSTED_PROXIES`. The chain is read from the right, the listed addresses are skipped,+ and the first other one is the client.++`CLIENT_IP_HEADER` names any header the proxy writes itself, such as `x-real-ip` when nginx+sets `proxy_set_header X-Real-IP $remote_addr;`. A header the proxy passes on unchanged lets a+client pick its own address: Caddy passes `X-Real-IP` on as the client sent it.++A client that reaches the api without the proxy writes any of these headers itself.+`docker-compose.yml` publishes the api on `API_PORT` of the host; keep that port closed to+everything but the proxy.+The instance sends password resets, address confirmations, sign-in links and invites throughdiff --git a/packages/auth/src/index.ts b/packages/auth/src/index.tsindex 4ba38dd7..8c419919 100644--- a/packages/auth/src/index.ts+++ b/packages/auth/src/index.ts@@ -83,6 +83,13 @@ const cookieDomain =// public frontend hostname.const passkeyRpID = process.env.PASSKEY_RP_ID ?? new URL(trustedOrigins[0]).hostname;+// The client IP keys the rate limit. Unset, better-auth reads X-Forwarded-For and takes+// it only when it holds one address; TRUSTED_PROXIES lets it read a chain.+const clientIpHeader = process.env.CLIENT_IP_HEADER;+const trustedProxies = process.env.TRUSTED_PROXIES?.split(',')+ .map((entry) => entry.trim())+ .filter(Boolean);+// Public URL of the backend, with the path a subpath proxy adds; the links in auth// emails and the OAuth redirect URIs are built from it.const apiPublicUrl = (process.env.API_URL ?? '').replace(/\/+$/, '');@@ -982,6 +989,10 @@ function createAuth() {sameSite: 'lax',secure: hostPrefixedCookies || process.env.NODE_ENV === 'production',},+ ipAddress: {+ ipAddressHeaders: clientIpHeader ? [clientIpHeader] : undefined,+ trustedProxies,+ },},});}