⚡ 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(api): stop reflecting arbitrary origins in production
Andrej99571c9a4e14
diff --git a/.env.sample b/.env.sampleindex edaf46a40..fddd65b85 100644--- a/.env.sample+++ b/.env.sample@@ -1,9 +1,10 @@# General URLs-# KANEO_CLIENT_URL=http://localhost:5173-# KANEO_API_URL=http://localhost:1337 # optional — defaults to KANEO_CLIENT_URL/api+KANEO_CLIENT_URL=http://localhost:5173+# KANEO_API_URL=http://localhost:1337 # optional - defaults to KANEO_CLIENT_URL/api+# CORS_ORIGINS= # optional - comma-separated; defaults to KANEO_CLIENT_URL# Database-# DATABASE_URL= # optional — use this for host-native or external DB access+# DATABASE_URL= # optional: use this for host-native or external DB accessPOSTGRES_DB=kaneoPOSTGRES_USER=kaneoPOSTGRES_PASSWORD=@@ -35,14 +36,14 @@ POSTGRES_PASSWORD=# If unset, a random secret is generated at startup (sessions won't survive restarts)AUTH_SECRET=-# GitHub SSO (Sign in with GitHub — OAuth App)+# GitHub SSO (Sign in with GitHub, OAuth App)GITHUB_OAUTH_CLIENT_ID=GITHUB_OAUTH_CLIENT_SECRET=# Legacy fallback for SSO (used only if GITHUB_OAUTH_* are unset)GITHUB_CLIENT_ID=GITHUB_CLIENT_SECRET=-# GitHub integration (GitHub App — repository sync, webhooks)+# GitHub integration (GitHub App: repository sync, webhooks)GITHUB_APP_ID=GITHUB_WEBHOOK_SECRET=GITHUB_PRIVATE_KEY=@@ -74,11 +75,11 @@ SMTP_REQUIRE_TLS=# env var. For local dev (vite), set VITE_TURNSTILE_SITE_KEY in apps/web/.env.#KANEO_TURNSTILE_SITE_KEY=-# Sentry error tracking (Optional) — errors are only reported when a DSN is set+# Sentry error tracking (Optional): errors are only reported when a DSN is set# SENTRY_DSN=# SENTRY_ENVIRONMENT=production-# Kaneo Cloud billing via Creem (Optional) — only active when KANEO_CLOUD=true+# Kaneo Cloud billing via Creem (Optional): only active when KANEO_CLOUD=true# and both keys are set. Self-hosted installs leave these empty; billing stays off.# CREEM_API_KEY=# CREEM_WEBHOOK_SECRET=diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex dadbc6736..308df6a85 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -174,13 +174,23 @@ export function createApp() {.map((origin) => origin.trim()).filter(Boolean);+ const reflectUnconfiguredOrigins = process.env.NODE_ENV !== "production";++ if (!corsOrigins && !reflectUnconfiguredOrigins) {+ console.warn(+ "[cors] Neither CORS_ORIGINS nor KANEO_CLIENT_URL is set, so cross-origin requests are refused. Same-origin deployments (the bundled image) are unaffected; set KANEO_CLIENT_URL if the web app is served from another origin.",+ );+ }+app.use("*",cors({credentials: true,origin: (origin) => {+ // Reflecting an arbitrary origin alongside credentials lets any site+ // read authenticated responses, so it stays a development convenience.if (!corsOrigins) {- return origin || "*";+ return reflectUnconfiguredOrigins ? origin || "*" : null;}if (!origin) {diff --git a/tests/api-integration/cors.test.ts b/tests/api-integration/cors.test.tsnew file mode 100644index 000000000..2b0d67cb0--- /dev/null+++ b/tests/api-integration/cors.test.ts@@ -0,0 +1,70 @@+import { afterEach, describe, expect, it } from "vitest";+import { createApp } from "../../apps/api/src/index";++const originalNodeEnv = process.env.NODE_ENV;+const originalClientUrl = process.env.KANEO_CLIENT_URL;+const originalCorsOrigins = process.env.CORS_ORIGINS;++function restore(key: string, value: string | undefined) {+ if (value === undefined) {+ delete process.env[key];+ } else {+ process.env[key] = value;+ }+}++afterEach(() => {+ restore("NODE_ENV", originalNodeEnv);+ restore("KANEO_CLIENT_URL", originalClientUrl);+ restore("CORS_ORIGINS", originalCorsOrigins);+});++async function originHeaderFor(requestOrigin: string) {+ const { app } = createApp();+ const response = await app.request("/api/health", {+ headers: { origin: requestOrigin },+ });+ return response.headers.get("access-control-allow-origin");+}++describe("API integration: CORS origin policy", () => {+ it("refuses unconfigured cross-origin requests in production", async () => {+ process.env.NODE_ENV = "production";+ delete process.env.KANEO_CLIENT_URL;+ delete process.env.CORS_ORIGINS;++ expect(await originHeaderFor("https://attacker.example")).toBeNull();+ });++ it("still reflects the origin in development", async () => {+ process.env.NODE_ENV = "development";+ delete process.env.KANEO_CLIENT_URL;+ delete process.env.CORS_ORIGINS;++ expect(await originHeaderFor("http://localhost:5173")).toBe(+ "http://localhost:5173",+ );+ });++ it("allows the configured client URL and refuses everything else", async () => {+ process.env.NODE_ENV = "production";+ process.env.KANEO_CLIENT_URL = "https://kaneo.example";+ delete process.env.CORS_ORIGINS;++ expect(await originHeaderFor("https://kaneo.example")).toBe(+ "https://kaneo.example",+ );+ expect(await originHeaderFor("https://attacker.example")).toBeNull();+ });++ it("honours a comma-separated CORS_ORIGINS allowlist", async () => {+ process.env.NODE_ENV = "production";+ delete process.env.KANEO_CLIENT_URL;+ process.env.CORS_ORIGINS = "https://a.example, https://b.example";++ expect(await originHeaderFor("https://b.example")).toBe(+ "https://b.example",+ );+ expect(await originHeaderFor("https://c.example")).toBeNull();+ });+});