fix(auth): respect HTTPS when setting secure cookies on self-hosted - #5144
fix(auth): respect HTTPS when setting secure cookies on self-hosted#5144hypertonny wants to merge 1 commit into
Conversation
Self-hosted instances sitting behind a TLS-terminating reverse proxy (Traefik, Cloudflare, Nginx) were broken because useSecureCookies and the cookie secure attribute were hardcoded to false for all non-cloud deployments. This caused BetterAuth to emit non-Secure cookies even when the browser reached Dokploy over HTTPS, so the session token was silently dropped on every tRPC request, returning 403 Forbidden. Fix: honour a SECURE_COOKIES=true environment variable so that operators who serve Dokploy over HTTPS can opt-in with a single env var in their Docker Compose file. HTTP-only deployments (direct IP:3000 access) continue to work unchanged because the default remains false. Fixes: Dokploy#5143 Related: Dokploy#4709
There was a problem hiding this comment.
Pull request overview
This PR addresses self-hosted authentication failures when Dokploy is served over HTTPS behind a TLS-terminating reverse proxy by allowing operators to opt into Secure cookies via an environment variable.
Changes:
- Makes BetterAuth cookie security configurable for non-cloud deployments via
SECURE_COOKIES. - Sets
defaultCookieAttributes.securebased onSECURE_COOKIESand tightens thesameSiteliteral typing. - Adds inline guidance in
auth.tsfor self-hosted operators on when to enable Secure cookies.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| // Self-hosted instances behind a TLS-terminating reverse proxy | ||
| // (Traefik, Cloudflare, Nginx) need Secure cookies so that | ||
| // browsers honour the __Secure-* cookie prefix over HTTPS. | ||
| // Set SECURE_COOKIES=true in your environment / docker-compose | ||
| // when serving Dokploy over HTTPS. | ||
| // Without this, BetterAuth emits non-Secure cookies, the browser | ||
| // silently drops them on HTTPS, and every tRPC request returns 403. |
| // silently drops them on HTTPS, and every tRPC request returns 403. | ||
| advanced: { | ||
| useSecureCookies: false, | ||
| useSecureCookies: process.env.SECURE_COOKIES === "true", |
| // when serving Dokploy over HTTPS. | ||
| // Without this, BetterAuth emits non-Secure cookies, the browser | ||
| // silently drops them on HTTPS, and every tRPC request returns 403. | ||
| advanced: { |
There was a problem hiding this comment.
Secure-cookie setting is undiscoverable
SECURE_COOKIES is required to activate the HTTPS fix, but the new setting is absent from checked-in environment examples and deployment configuration. Operators following the existing self-hosted setup therefore retain non-secure cookies until they independently discover and add the variable.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Summary
Fixes a critical authentication bug affecting all self-hosted Dokploy instances served over an HTTPS custom domain behind a TLS-terminating reverse proxy (Traefik, Cloudflare, Nginx).
Closes #5143
Related: #4709
Problem
When a self-hosted Dokploy instance is accessed via an HTTPS custom domain, every tRPC request returns
403 Forbidden, making the dashboard completely unusable over HTTPS.Root Cause
In
packages/server/src/lib/auth.ts,useSecureCookiesandsecureare hardcoded tofalsefor all self-hosted deployments:When a browser connects over HTTPS, it silently drops any cookie without the
Secureattribute. Every tRPC request then has no valid session → 403 Forbidden. Accessing overhttp://<IP>:3000works fine, making this hard to diagnose.Fix
Introduce a
SECURE_COOKIESenvironment variable operators can set when serving Dokploy over HTTPS:Add to
docker-compose.yml:Impact
http://<IP>:3000https://<domain>without env varhttps://<domain>+SECURE_COOKIES=trueNon-breaking change — existing deployments without the env var are unaffected.
Testing
Tested on a live self-hosted instance (Azure VPS, Traefik + Cloudflare, v0.30.2). Without fix: 403 on all tRPC routes over HTTPS. With
SECURE_COOKIES=true: dashboard fully functional.Checklist
canaryGreptile Summary
This PR makes Better Auth’s secure-cookie behavior configurable for self-hosted deployments through
SECURE_COOKIES.Confidence Score: 4/5
The PR appears safe to merge, with a non-blocking configuration-documentation gap that may prevent self-hosted operators from activating the fix.
The cookie configuration consistently applies the new boolean to both relevant Better Auth settings, but no checked-in operator-facing configuration advertises or supplies the required environment variable.
Files Needing Attention: packages/server/src/lib/auth.ts
Reviews (1): Last reviewed commit: "fix(auth): respect HTTPS when setting se..." | Re-trigger Greptile