⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(auth): resolve the client IP behind multiple proxy hops
Andrej9e8a44835768
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.tsindex 1183a11ad..8fd581e40 100644--- a/apps/api/src/auth.ts+++ b/apps/api/src/auth.ts@@ -167,6 +167,25 @@ function getDeviceAuthClientIds(): Set<string> {return new Set(["kaneo-cli", "kaneo-mcp"]);}+const DEFAULT_TRUSTED_PROXIES = [+ "127.0.0.0/8",+ "::1/128",+ "10.0.0.0/8",+ "172.16.0.0/12",+ "192.168.0.0/16",+];++function trustedProxies(): string[] {+ const raw = process.env.TRUSTED_PROXIES?.trim();+ if (!raw) {+ return DEFAULT_TRUSTED_PROXIES;+ }+ return raw+ .split(",")+ .map((entry) => entry.trim())+ .filter(Boolean);+}+function getDeviceAuthVerificationUri(): string {const base = clientUrl.replace(/\/$/, "");return `${base}/device`;@@ -736,6 +755,10 @@ export const auth = betterAuth({}),},advanced: {+ ipAddress: {+ ipAddressHeaders: ["cf-connecting-ip", "x-forwarded-for"],+ trustedProxies: trustedProxies(),+ },defaultCookieAttributes: {// For cross-subdomain auth with HTTPS, use sameSite: "none" with secure: true// For same-domain or HTTP deployments, use sameSite: "lax" with secure: falsediff --git a/apps/docs/core/installation/environment-variables.mdx b/apps/docs/core/installation/environment-variables.mdxindex cda625bc2..a7dd9d0f6 100644--- a/apps/docs/core/installation/environment-variables.mdx+++ b/apps/docs/core/installation/environment-variables.mdx@@ -98,6 +98,16 @@ Name | Description | Default |Before enabling SSO-only access with `DISABLE_LOGIN_FORM=true` or `CUSTOM_OAUTH_AUTO_LOGIN=true` on an existing installation, make sure existing local accounts have verified email addresses. OAuth/OIDC identities cannot be linked to an unverified local account, and affected users will receive `error=account_not_linked`. Keep the login form available until those users have signed in with an email OTP or magic link. If SSO-only access is already enabled, temporarily set both options to `false`, restart Kaneo, and let affected users complete an email sign-in before restoring the SSO-only settings.</Warning>+### Proxies++Name | Description | Default |+--- | --- | --- |+| `TRUSTED_PROXIES` | Comma-separated IPs or CIDR ranges of the reverse proxies in front of Kaneo. The forwarded header chain is walked right to left, trusted hops are skipped, and the first untrusted address is taken as the client IP. Only set this if your proxies sit outside the default ranges; the default covers loopback and private networks, which is correct for the standard Docker Compose setup. | `127.0.0.0/8, ::1/128, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16` |++<Warning>+ Only list addresses you control. Any host in this range can set `X-Forwarded-For` and have it believed, so do not include a range that clients can reach Kaneo from directly. The client IP is used for rate limiting and session records.+</Warning>+### NotificationsName | Description | Default |diff --git a/tests/api-integration/session-client-ip.test.ts b/tests/api-integration/session-client-ip.test.tsnew file mode 100644index 000000000..cb11cd3a7--- /dev/null+++ b/tests/api-integration/session-client-ip.test.ts@@ -0,0 +1,58 @@+import { randomUUID } from "node:crypto";+import { desc, eq } from "drizzle-orm";+import { beforeEach, describe, expect, it } from "vitest";+import db, { schema } from "../../apps/api/src/database";+import { createApp } from "../../apps/api/src/index";+import { resetTestDatabase } from "./helpers/database";++async function signUpWithForwardedFor(forwardedFor: string) {+ const { app } = createApp();+ const email = `ip-${randomUUID()}@example.com`;++ const response = await app.request("/api/auth/sign-up/email", {+ method: "POST",+ headers: {+ "content-type": "application/json",+ "x-forwarded-for": forwardedFor,+ },+ body: JSON.stringify({+ email,+ password: "TestPassw0rd!23",+ name: "IP Probe",+ }),+ });++ expect(response.status).toBeLessThan(400);++ const [user] = await db+ .select()+ .from(schema.userTable)+ .where(eq(schema.userTable.email, email));++ const [session] = await db+ .select()+ .from(schema.sessionTable)+ .where(eq(schema.sessionTable.userId, user.id))+ .orderBy(desc(schema.sessionTable.createdAt))+ .limit(1);++ return session;+}++describe("session records the real client IP behind the proxy chain", () => {+ beforeEach(async () => {+ await resetTestDatabase();+ });++ it("stores the client, not the in-image nginx or Caddy hop", async () => {+ const session = await signUpWithForwardedFor("203.0.113.9, 172.19.0.5");++ expect(session.ipAddress).toBe("203.0.113.9");+ });++ it("still works with a single proxy in front", async () => {+ const session = await signUpWithForwardedFor("203.0.113.40");++ expect(session.ipAddress).toBe("203.0.113.40");+ });+});