Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

docs(self-hosting): require COOKIE_DOMAIN for split-domain deployments (MUL-5413) (#6055)

Bohan Jiang0abee49005dd
diff --git a/.env.example b/.env.example
index 418e191277..515226210b 100644
--- a/.env.example
+++ b/.env.example
@@ -190,10 +190,21 @@ CLOUDFRONT_DOMAIN=
# COOKIE_DOMAIN — optional Domain attribute on session + CloudFront cookies.
# Leave empty for single-host deployments (localhost, LAN IP, or a single
# hostname) — session cookies become host-only, which is what the browser
-# wants. Only set it when the frontend and backend sit on different
-# subdomains of one registered domain (e.g. ".example.com"). Do NOT set it
-# to an IP address: RFC 6265 forbids IP literals in the cookie Domain
-# attribute and browsers silently drop such cookies.
+# wants. Set it when the frontend and backend sit on different subdomains of
+# one registered domain (e.g. ".example.com") — there it is REQUIRED, not
+# optional: without it the frontend cannot read the multica_csrf cookie
+# issued by the API host, so every write fails with 403 "CSRF validation
+# failed" while reads keep working.
+# When you do set it, use the NARROWEST parent domain that still covers both
+# hosts (for agent.example.com + api.agent.example.com that is
+# ".agent.example.com", not ".example.com"). This value also scopes the
+# multica_auth session cookie, and the browser sends it to every host under
+# that domain — HttpOnly stops page scripts from reading it, not sibling
+# subdomains from receiving it. Only use this layout when every host under
+# the chosen domain is operated by the same trusted party; otherwise serve
+# the API from the frontend's own origin and leave this empty.
+# Do NOT set it to an IP address: RFC 6265 forbids IP literals in the cookie
+# Domain attribute and browsers silently drop such cookies.
COOKIE_DOMAIN=
# AUTH_TOKEN_TTL — auth token lifetime. Accepts Go duration strings (e.g.
diff --git a/SELF_HOSTING_ADVANCED.md b/SELF_HOSTING_ADVANCED.md
index 9fcaa4619a..8792dfdde8 100644
--- a/SELF_HOSTING_ADVANCED.md
+++ b/SELF_HOSTING_ADVANCED.md
@@ -109,6 +109,8 @@ For file uploads and attachments, configure S3 and (optionally) CloudFront:
The `Secure` flag on session cookies is derived automatically from the scheme of `FRONTEND_ORIGIN`: HTTPS origins get `Secure` cookies; plain-HTTP origins (LAN / private-network self-host) get non-secure cookies so the browser can actually store them.
+If the frontend and backend are served from different hostnames, `COOKIE_DOMAIN` is **required**, not optional: the browser must be able to read the `multica_csrf` cookie from the page's own origin to send the `X-CSRF-Token` header, and without it every write request fails with `403 {"error":"CSRF validation failed"}`. Scope it to the narrowest parent domain that covers both hosts — it also shares the `multica_auth` session cookie with every host under that domain. See [Reverse Proxy](#reverse-proxy) for the full split-domain configuration and its trust requirements.
+
### Server
| Variable | Default | Description |
@@ -374,6 +376,7 @@ When using separate domains for frontend and backend, set these environment vari
# Backend
FRONTEND_ORIGIN=https://app.example.com
CORS_ALLOWED_ORIGINS=https://app.example.com
+COOKIE_DOMAIN=.example.com # narrowest parent covering both hosts — read the scope warning below
# Frontend (only if you are building the web image from source via docker-compose.selfhost.build.yml)
REMOTE_API_URL=https://api.example.com
@@ -381,6 +384,53 @@ NEXT_PUBLIC_API_URL=https://api.example.com
NEXT_PUBLIC_WS_URL=wss://api.example.com/ws
```
+> **`COOKIE_DOMAIN` is required in this setup — omitting it breaks every write.** The web app authenticates with an HttpOnly `multica_auth` cookie plus a JS-readable `multica_csrf` cookie, and sends the CSRF value as an `X-CSRF-Token` header on every non-GET request. Both cookies are host-only unless `COOKIE_DOMAIN` is set, so a frontend on `app.example.com` cannot read a cookie issued by `api.example.com`. The header is then never sent and the backend rejects the request with `403 {"error":"CSRF validation failed"}` — while GET requests keep working, so the app renders but nothing can be created or edited.
+>
+> After changing `COOKIE_DOMAIN`, delete the existing `multica_auth` / `multica_csrf` cookies on **both** hosts and log in again. Stale host-only cookies otherwise sit alongside the new domain-scoped ones and the browser sends both.
+
+> **Scope `COOKIE_DOMAIN` as narrowly as possible.** The same value also scopes `multica_auth`, the session JWT, and the browser then sends it to **every** host under that domain. `HttpOnly` only stops page scripts from reading the cookie — it does not stop the server behind a sibling subdomain from receiving it on every request. Use the narrowest parent that still covers both hosts: for `agent.example.com` + `api.agent.example.com` that is `.agent.example.com`, **not** `.example.com`, which would also hand your users' session cookie to unrelated hosts such as a separately deployed `docs.example.com`.
+
+Both of these must hold, or this layout is not safe to use:
+
+- The frontend and backend share a common parent domain. Unrelated domains (`app.com` and `api.io`) cannot be covered by any `COOKIE_DOMAIN` value.
+- Every host under that parent domain is operated by the same trusted party. If anything in scope is third-party hosted, or other teams can create records under it, a compromise or a rogue service there receives valid sessions for your deployment.
+
+If either condition fails, use the same-origin layout below, which keeps the session cookie host-only.
+
+### Same Origin (Recommended)
+
+A separate API domain is not required. Leave `NEXT_PUBLIC_API_URL` and `NEXT_PUBLIC_WS_URL` at their defaults and the browser calls `/api` and `/ws` on the page's own origin, so no cross-host cookie problem can arise in the first place:
+
+```bash
+# Backend
+FRONTEND_ORIGIN=https://app.example.com
+CORS_ALLOWED_ORIGINS=https://app.example.com
+COOKIE_DOMAIN= # empty: cookies are host-only on app.example.com, which is correct here
+
+# Frontend
+NEXT_PUBLIC_API_URL= # empty: the client uses relative /api paths on the page origin
+NEXT_PUBLIC_WS_URL= # empty
+REMOTE_API_URL=http://backend:8080 # target the Next.js rewrites proxy /api, /auth and /uploads to
+```
+
+Serve everything from the single `app.example.com` vhost. HTTP works out of the box, because Next.js rewrites forward `/api`, `/auth` and `/uploads` to `REMOTE_API_URL`. WebSockets do **not** go through those rewrites, so add a `/ws` block to the frontend vhost that reaches the backend directly:
+
+```nginx
+# Add to the app.example.com server block above
+location /ws {
+ proxy_pass http://localhost:8080;
+ proxy_http_version 1.1;
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection "upgrade";
+ proxy_set_header Host $host;
+ proxy_read_timeout 86400;
+}
+```
+
+See [WebSocket for LAN / Non-localhost Access](#websocket-for-lan--non-localhost-access) for why the rewrites cannot carry the `Upgrade` handshake.
+
+This keeps cookies, CORS, and the WebSocket origin check on a single origin. It is both the configuration least likely to break and the safer one: the session cookie stays host-only, so no sibling subdomain can ever receive it. An `api.example.com` vhost can still be kept for CLI and daemon use: those clients authenticate with a `mul_` personal access token over `Authorization: Bearer`, which never goes through the cookie or CSRF path.
+
## LAN / Non-localhost Access
By default, Multica works on `localhost`. If you access it from another machine on the LAN (e.g. `http://192.168.1.100:3000`), you need to tell the backend to accept that origin:
diff --git a/apps/docs/content/docs/environment-variables.ja.mdx b/apps/docs/content/docs/environment-variables.ja.mdx
index c31e26e818..739d7bdcf7 100644
--- a/apps/docs/content/docs/environment-variables.ja.mdx
+++ b/apps/docs/content/docs/environment-variables.ja.mdx
@@ -122,6 +122,8 @@ S3 の前段に CloudFront を置く場合、3 つの変数が適用されます
- **空**: cookie は訪問した正確なホストでのみ有効です(単一ホストのデプロイに適切)
- **`.example.com` に設定**: cookie がサブドメイン間で共有されます(そのため `app.example.com` と `api.example.com` がサインインセッションを共有します)
+- **フロントエンドとバックエンドが別のホストにある場合は任意ではなく必須です**: ブラウザがページ自身の origin から `multica_csrf` cookie を読める必要があり、読み取りは動作したまま書き込みリクエストがすべて `403 CSRF validation failed` で失敗します
+- 両方のホストを含む**最も狭い**親ドメインを使ってください(`.example.com` ではなく `.agent.example.com`)。この値は `multica_auth` セッション cookie もそのドメイン配下のすべてのホストに広げるため、それらがすべて同一の信頼できる主体によって運用されている場合にのみこの構成を使ってください — 詳細な設定は [Self-Hosting](/getting-started/self-hosting) を参照してください
- 警告: IP アドレスにはできません(ブラウザは無視します)
## 誰がサインアップできるかを制限する
diff --git a/apps/docs/content/docs/environment-variables.ko.mdx b/apps/docs/content/docs/environment-variables.ko.mdx
index 1715a218ac..3a652d451f 100644
--- a/apps/docs/content/docs/environment-variables.ko.mdx
+++ b/apps/docs/content/docs/environment-variables.ko.mdx
@@ -122,6 +122,8 @@ S3 앞에 CloudFront를 두는 경우 세 가지 변수가 적용됩니다: `CLO
- **비어 있음**: cookie는 방문한 정확한 호스트에서만 유효합니다 (단일 호스트 배포에 적합)
- **`.example.com`으로 설정**: cookie가 하위 도메인 간에 공유됩니다 (그래서 `app.example.com`과 `api.example.com`이 로그인 세션을 공유함)
+- **프런트엔드와 백엔드가 서로 다른 호스트에 있으면 선택이 아니라 필수입니다**: 브라우저가 페이지 자체 origin에서 `multica_csrf` cookie를 읽을 수 있어야 하며, 그렇지 않으면 읽기는 계속 동작하는 반면 모든 쓰기 요청이 `403 CSRF validation failed`로 실패합니다
+- 두 호스트를 모두 포함하는 **가장 좁은** 상위 도메인을 사용하세요 (`.example.com`이 아니라 `.agent.example.com`). 이 값은 `multica_auth` 세션 cookie도 해당 도메인 아래의 모든 호스트로 확장하므로, 그 호스트들이 모두 동일한 신뢰 주체가 운영하는 경우에만 이 구성을 사용하세요 — 전체 설정은 [Self-Hosting](/getting-started/self-hosting)을 참고하세요
- 경고: IP 주소일 수 없습니다 (브라우저가 무시함)
## 누가 가입할 수 있는지 제한하기
diff --git a/apps/docs/content/docs/environment-variables.mdx b/apps/docs/content/docs/environment-variables.mdx
index 2dc709a894..a60060b191 100644
--- a/apps/docs/content/docs/environment-variables.mdx
+++ b/apps/docs/content/docs/environment-variables.mdx
@@ -122,6 +122,8 @@ If you front S3 with CloudFront, three variables apply: `CLOUDFRONT_DOMAIN`, `CL
- **Empty**: the cookie is valid only on the exact host visited (correct for single-host deployments)
- **Set to `.example.com`**: the cookie is shared across subdomains (so `app.example.com` and `api.example.com` share a sign-in session)
+- **Required, not optional, when the frontend and backend are on different hostnames**: the browser has to read the `multica_csrf` cookie from the page's own origin, and without it every write request fails with `403 CSRF validation failed` while reads keep working
+- Use the **narrowest** parent domain that still covers both hosts (`.agent.example.com`, not `.example.com`). It also scopes the `multica_auth` session cookie to every host under that domain, so only use this layout when they are all operated by the same trusted party — see [Self-Hosting](/getting-started/self-hosting) for the full configuration
- Warning: it cannot be an IP address (browsers ignore it)
## Restricting who can sign up
diff --git a/apps/docs/content/docs/environment-variables.zh.mdx b/apps/docs/content/docs/environment-variables.zh.mdx
index 0151c0daf0..68e75636b0 100644
--- a/apps/docs/content/docs/environment-variables.zh.mdx
+++ b/apps/docs/content/docs/environment-variables.zh.mdx
@@ -122,6 +122,8 @@ API 返回的 `download_url` 在未配置 CloudFront 签名时会指向 `GET /ap
- **空**:cookie 只对访问的那个 host 生效(单主机部署正确)
- **设成 `.example.com`**:cookie 在所有子域共享(让 `app.example.com` 和 `api.example.com` 共用登录态)
+- **前后端在不同 host 上时,这是必需项而非可选项**:浏览器必须能从页面自身的 origin 读到 `multica_csrf` cookie,否则所有写请求都会 `403 CSRF validation failed`,而读请求照常
+- 用能覆盖前后端的**最窄**父域(`.agent.example.com`,而不是 `.example.com`)。这个值同时会把 `multica_auth` 会话 cookie 扩散到该域下的每个 host,所以只有当这些 host 都由同一可信主体运维时才用这种部署方式——完整配置见 [Self-Hosting](/getting-started/self-hosting)
- ⚠️ 不能是 IP 地址(浏览器会忽略)
## 怎么限制谁能注册
diff --git a/apps/docs/content/docs/getting-started/self-hosting.zh.mdx b/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
index f8e2c8c301..9726433866 100644
--- a/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
+++ b/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
@@ -290,6 +290,8 @@ For file uploads and attachments, configure S3 and (optionally) CloudFront:
The `Secure` flag on session cookies is derived automatically from the scheme of `FRONTEND_ORIGIN`: HTTPS origins get `Secure` cookies; plain-HTTP origins (LAN / private-network self-host) get non-secure cookies so the browser can actually store them.
+If the frontend and backend are served from different hostnames, `COOKIE_DOMAIN` is **required**, not optional: the browser must be able to read the `multica_csrf` cookie from the page's own origin to send the `X-CSRF-Token` header, and without it every write request fails with `403 {"error":"CSRF validation failed"}`. Scope it to the narrowest parent domain that covers both hosts — it also shares the `multica_auth` session cookie with every host under that domain. See [Reverse Proxy](#reverse-proxy) for the full split-domain configuration and its trust requirements.
+
### Server
| Variable | Default | Description |
@@ -496,6 +498,7 @@ When using separate domains for frontend and backend, set these environment vari
# Backend
FRONTEND_ORIGIN=https://app.example.com
CORS_ALLOWED_ORIGINS=https://app.example.com
+COOKIE_DOMAIN=.example.com # narrowest parent covering both hosts — read the scope warning below
# Frontend (runtime env; no rebuild required)
REMOTE_API_URL=https://api.example.com
@@ -505,6 +508,55 @@ NEXT_PUBLIC_WS_URL=wss://api.example.com/ws
DOCS_URL=https://docs.example.com
```
+<Callout type="warning">
+`COOKIE_DOMAIN` is required in this setup — omitting it breaks every write. The web app authenticates with an HttpOnly `multica_auth` cookie plus a JS-readable `multica_csrf` cookie, and sends the CSRF value as an `X-CSRF-Token` header on every non-GET request. Both cookies are host-only unless `COOKIE_DOMAIN` is set, so a frontend on `app.example.com` cannot read a cookie issued by `api.example.com`. The header is then never sent and the backend rejects the request with `403 {"error":"CSRF validation failed"}` — while GET requests keep working, so the app renders but nothing can be created or edited.
+
+After changing `COOKIE_DOMAIN`, delete the existing `multica_auth` / `multica_csrf` cookies on **both** hosts and log in again. Stale host-only cookies otherwise sit alongside the new domain-scoped ones and the browser sends both.
+</Callout>
+
+<Callout type="warning">
+**Scope `COOKIE_DOMAIN` as narrowly as possible.** The same value also scopes `multica_auth`, the session JWT, and the browser then sends it to **every** host under that domain. `HttpOnly` only stops page scripts from reading the cookie — it does not stop the server behind a sibling subdomain from receiving it on every request. Use the narrowest parent that still covers both hosts: for `agent.example.com` + `api.agent.example.com` that is `.agent.example.com`, **not** `.example.com`, which would also hand your users' session cookie to unrelated hosts such as the separately deployed `docs.example.com` above.
+</Callout>
+
+Both of these must hold, or this layout is not safe to use:
+
+- The frontend and backend share a common parent domain. Unrelated domains (`app.com` and `api.io`) cannot be covered by any `COOKIE_DOMAIN` value.
+- Every host under that parent domain is operated by the same trusted party. If anything in scope is third-party hosted, or other teams can create records under it, a compromise or a rogue service there receives valid sessions for your deployment.
+
+If either condition fails, use the same-origin layout below, which keeps the session cookie host-only.
+
+### Same Origin (Recommended)
+
+A separate API domain is not required. Leave `NEXT_PUBLIC_API_URL` and `NEXT_PUBLIC_WS_URL` at their defaults and the browser calls `/api` and `/ws` on the page's own origin, so no cross-host cookie problem can arise in the first place:
+
+```bash
+# Backend
+FRONTEND_ORIGIN=https://app.example.com
+CORS_ALLOWED_ORIGINS=https://app.example.com
+COOKIE_DOMAIN= # empty: cookies are host-only on app.example.com, which is correct here
+
+# Frontend
+NEXT_PUBLIC_API_URL= # empty: the client uses relative /api paths on the page origin
+NEXT_PUBLIC_WS_URL= # empty
+REMOTE_API_URL=http://backend:8080 # target the Next.js rewrites proxy /api, /auth and /uploads to
+```
+
+Serve everything from the single `app.example.com` vhost. HTTP works out of the box, because Next.js rewrites forward `/api`, `/auth` and `/uploads` to `REMOTE_API_URL`. WebSockets do **not** go through those rewrites, so add a `/ws` block to the frontend vhost that reaches the backend directly:
+
+```nginx
+# Add to the app.example.com server block above
+location /ws {
+ proxy_pass http://localhost:8080;
+ proxy_http_version 1.1;
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection "upgrade";
+ proxy_set_header Host $host;
+ proxy_read_timeout 86400;
+}
+```
+
+This keeps cookies, CORS, and the WebSocket origin check on a single origin. It is both the configuration least likely to break and the safer one: the session cookie stays host-only, so no sibling subdomain can ever receive it. An `api.example.com` vhost can still be kept for CLI and daemon use: those clients authenticate with a `mul_` personal access token over `Authorization: Bearer`, which never goes through the cookie or CSRF path.
+
## Health Check
The backend exposes public health endpoints: