Skip to content

Commit f5c21db

Browse files
committed
fix: mark session cookie as Secure when served over HTTPS
Self-hosted better-auth is configured with secure cookies off so plain HTTP access (http://<ip>:3000) keeps working, but that also meant the session cookie never got the Secure attribute even behind HTTPS. A cookie without Secure can be replayed over plain HTTP. Add Secure to the auth response cookies per request when X-Forwarded-Proto is https (what Traefik sends on TLS), leaving plain-HTTP requests untouched. Fixes Dokploy#4709
1 parent ef0272a commit f5c21db

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isHttpsRequest, secureSetCookie } from "@/lib/secure-cookies";
3+
4+
describe("isHttpsRequest", () => {
5+
it("returns true when X-Forwarded-Proto is https", () => {
6+
expect(isHttpsRequest("https")).toBe(true);
7+
});
8+
9+
it("returns false for http or a missing header", () => {
10+
expect(isHttpsRequest("http")).toBe(false);
11+
expect(isHttpsRequest(undefined)).toBe(false);
12+
});
13+
14+
it("uses the left-most value of a proxy chain", () => {
15+
expect(isHttpsRequest("https, http")).toBe(true);
16+
expect(isHttpsRequest("http, https")).toBe(false);
17+
});
18+
19+
it("handles an array header value and is case-insensitive", () => {
20+
expect(isHttpsRequest(["HTTPS", "http"])).toBe(true);
21+
});
22+
});
23+
24+
describe("secureSetCookie", () => {
25+
it("adds Secure to a cookie that lacks it", () => {
26+
expect(
27+
secureSetCookie("better-auth.session_token=abc; Path=/; HttpOnly"),
28+
).toBe("better-auth.session_token=abc; Path=/; HttpOnly; Secure");
29+
});
30+
31+
it("does not duplicate Secure when already present", () => {
32+
const cookie = "better-auth.session_token=abc; Path=/; Secure; HttpOnly";
33+
expect(secureSetCookie(cookie)).toBe(cookie);
34+
});
35+
36+
it("applies to every cookie in an array", () => {
37+
expect(secureSetCookie(["a=1; Path=/", "b=2; Path=/; Secure"])).toEqual([
38+
"a=1; Path=/; Secure",
39+
"b=2; Path=/; Secure",
40+
]);
41+
});
42+
43+
it("passes a numeric header value through untouched", () => {
44+
expect(secureSetCookie(123)).toBe(123);
45+
});
46+
});

‎apps/dokploy/lib/secure-cookies.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const isHttpsRequest = (
2+
forwardedProto: string | string[] | undefined,
3+
): boolean => {
4+
if (!forwardedProto) return false;
5+
const value = Array.isArray(forwardedProto)
6+
? forwardedProto[0]
7+
: forwardedProto;
8+
// take the first value in case a proxy chain sent a list
9+
return value?.split(",")[0]?.trim().toLowerCase() === "https";
10+
};
11+
12+
const hasSecureAttribute = (cookie: string): boolean =>
13+
/;\s*secure(?:\s*;|\s*$)/i.test(cookie);
14+
15+
const withSecure = (cookie: string): string =>
16+
hasSecureAttribute(cookie) ? cookie : `${cookie}; Secure`;
17+
18+
// Add Secure to a Set-Cookie header value (string or string[]).
19+
export const secureSetCookie = (
20+
value: string | number | readonly string[],
21+
): string | number | string[] => {
22+
if (typeof value === "number") return value;
23+
if (Array.isArray(value)) return value.map(withSecure);
24+
return withSecure(value as string);
25+
};
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
import type { IncomingMessage, ServerResponse } from "node:http";
12
import { auth } from "@dokploy/server/index";
23
import { toNodeHandler } from "better-auth/node";
4+
import { isHttpsRequest, secureSetCookie } from "@/lib/secure-cookies";
35

46
// Disallow body parsing, we will parse it manually
57
export const config = { api: { bodyParser: false } };
68

7-
export default toNodeHandler(auth.handler);
9+
const handler = toNodeHandler(auth.handler);
10+
11+
// Mark the session cookie as Secure when the request comes over HTTPS
12+
export default function authHandler(req: IncomingMessage, res: ServerResponse) {
13+
if (isHttpsRequest(req.headers["x-forwarded-proto"])) {
14+
const setHeader = res.setHeader.bind(res);
15+
res.setHeader = (name, value) =>
16+
String(name).toLowerCase() === "set-cookie"
17+
? setHeader(name, secureSetCookie(value))
18+
: setHeader(name, value);
19+
20+
const appendHeader = res.appendHeader?.bind(res);
21+
if (appendHeader) {
22+
res.appendHeader = (name, value) =>
23+
String(name).toLowerCase() === "set-cookie"
24+
? appendHeader(name, secureSetCookie(value) as string | string[])
25+
: appendHeader(name, value);
26+
}
27+
}
28+
29+
return handler(req, res);
30+
}

0 commit comments

Comments
 (0)