⚡ 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): secure cookies on HTTPS deployments (#1560)
N1arko6d37312945e1
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.tsindex 569b9fd9b..c4a1091f9 100644--- a/apps/api/src/auth.ts+++ b/apps/api/src/auth.ts@@ -46,6 +46,7 @@ import { checkRegistrationAllowed } from "./utils/check-registration-allowed";import { checkWorkspaceName } from "./utils/check-workspace-name";import { mapCustomOAuthProfileToUser } from "./utils/custom-oauth-profile";import { generateDemoName } from "./utils/generate-demo-name";+import { getDefaultCookieAttributes } from "./utils/get-default-cookie-attributes";import { getInvitationEmailSubject } from "./utils/get-invitation-email-subject";import { getWorkspaceInvitationEmailCopy } from "./utils/get-workspace-invitation-email-copy";import { getGithubSsoOAuthCredentials } from "./utils/github-sso-env";@@ -79,20 +80,6 @@ function isOAuthCallbackPath(path: unknown): boolean {const apiUrl = process.env.KANEO_API_URL || "http://localhost:1337";const clientUrl = process.env.KANEO_CLIENT_URL || "http://localhost:5173";-const isHttps = apiUrl.startsWith("https://");-const isCrossSubdomain = (() => {- try {- const apiHost = new URL(apiUrl).hostname;- const clientHost = new URL(clientUrl).hostname;- return (- apiHost !== clientHost &&- apiHost !== "localhost" &&- clientHost !== "localhost"- );- } catch {- return false;- }-})();const trustedOrigins = [clientUrl];try {@@ -780,13 +767,10 @@ export const auth = betterAuth({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: false- sameSite: isCrossSubdomain && isHttps ? "none" : "lax",- secure: isCrossSubdomain && isHttps, // must be true when sameSite is "none"- partitioned: isCrossSubdomain && isHttps,- domain: process.env.COOKIE_DOMAIN || undefined, // Optional: e.g., ".andrej.com" for explicit cross-subdomain cookies- },+ defaultCookieAttributes: getDefaultCookieAttributes({+ apiUrl,+ clientUrl,+ cookieDomain: process.env.COOKIE_DOMAIN,+ }),},});diff --git a/apps/api/src/utils/get-default-cookie-attributes.ts b/apps/api/src/utils/get-default-cookie-attributes.tsnew file mode 100644index 000000000..7a42291a7--- /dev/null+++ b/apps/api/src/utils/get-default-cookie-attributes.ts@@ -0,0 +1,42 @@+type CookieAttributeUrls = {+ apiUrl: string;+ clientUrl: string;+ cookieDomain?: string;+};++export function getDefaultCookieAttributes({+ apiUrl,+ clientUrl,+ cookieDomain,+}: CookieAttributeUrls) {+ const parsedApiUrl = (() => {+ try {+ return new URL(apiUrl);+ } catch {+ return undefined;+ }+ })();+ const isHttps = parsedApiUrl?.protocol === "https:";+ const isCrossSubdomain = (() => {+ try {+ const apiHost = parsedApiUrl?.hostname;+ const clientHost = new URL(clientUrl).hostname;+ return (+ apiHost !== undefined &&+ apiHost !== clientHost &&+ apiHost !== "localhost" &&+ clientHost !== "localhost"+ );+ } catch {+ return false;+ }+ })();++ return {+ sameSite:+ isCrossSubdomain && isHttps ? ("none" as const) : ("lax" as const),+ secure: isHttps,+ partitioned: isCrossSubdomain && isHttps,+ domain: cookieDomain || undefined,+ };+}diff --git a/tests/api/utils/get-default-cookie-attributes.test.ts b/tests/api/utils/get-default-cookie-attributes.test.tsnew file mode 100644index 000000000..f8f0cf8a4--- /dev/null+++ b/tests/api/utils/get-default-cookie-attributes.test.ts@@ -0,0 +1,61 @@+import { describe, expect, it } from "vitest";+import { getDefaultCookieAttributes } from "../../../apps/api/src/utils/get-default-cookie-attributes";++describe("getDefaultCookieAttributes", () => {+ it("marks same-domain HTTPS cookies as secure", () => {+ const attributes = getDefaultCookieAttributes({+ apiUrl: "https://kaneo.example.com/api",+ clientUrl: "https://kaneo.example.com",+ });++ expect(attributes).toEqual({+ sameSite: "lax",+ secure: true,+ partitioned: false,+ domain: undefined,+ });+ });++ it("uses cross-subdomain attributes for HTTPS deployments", () => {+ const attributes = getDefaultCookieAttributes({+ apiUrl: "https://api.example.com",+ clientUrl: "https://app.example.com",+ cookieDomain: ".example.com",+ });++ expect(attributes).toEqual({+ sameSite: "none",+ secure: true,+ partitioned: true,+ domain: ".example.com",+ });+ });++ it("keeps local HTTP cookies compatible with development", () => {+ const attributes = getDefaultCookieAttributes({+ apiUrl: "http://localhost:1337",+ clientUrl: "http://localhost:5173",+ });++ expect(attributes).toEqual({+ sameSite: "lax",+ secure: false,+ partitioned: false,+ domain: undefined,+ });+ });++ it("handles uppercase HTTPS schemes", () => {+ const attributes = getDefaultCookieAttributes({+ apiUrl: "HTTPS://api.example.com",+ clientUrl: "HTTPS://app.example.com",+ });++ expect(attributes).toEqual({+ sameSite: "none",+ secure: true,+ partitioned: true,+ domain: undefined,+ });+ });+});