Skip to content

fix(auth): respect HTTPS when setting secure cookies on self-hosted - #5144

Open
hypertonny wants to merge 1 commit into
Dokploy:canaryfrom
hypertonny:fix/self-hosted-https-secure-cookies
Open

fix(auth): respect HTTPS when setting secure cookies on self-hosted#5144
hypertonny wants to merge 1 commit into
Dokploy:canaryfrom
hypertonny:fix/self-hosted-https-secure-cookies

Conversation

@hypertonny

@hypertonny hypertonny commented Aug 20, 2026

Copy link
Copy Markdown

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, useSecureCookies and secure are hardcoded to false for all self-hosted deployments:

advanced: {
  useSecureCookies: false,   // ← hardcoded regardless of protocol
  defaultCookieAttributes: {
    secure: false,           // ← hardcoded regardless of protocol
  },
},

When a browser connects over HTTPS, it silently drops any cookie without the Secure attribute. Every tRPC request then has no valid session → 403 Forbidden. Accessing over http://<IP>:3000 works fine, making this hard to diagnose.


Fix

Introduce a SECURE_COOKIES environment variable operators can set when serving Dokploy over HTTPS:

advanced: {
  useSecureCookies: process.env.SECURE_COOKIES === "true",
  defaultCookieAttributes: {
    sameSite: "lax" as const,
    secure: process.env.SECURE_COOKIES === "true",
    httpOnly: true,
    path: "/",
  },
},

Add to docker-compose.yml:

services:
  dokploy:
    environment:
      SECURE_COOKIES: "true"

Impact

Scenario Before After
http://<IP>:3000 ✅ Works ✅ Works
https://<domain> without env var ❌ 403 ❌ Same (opt-in required)
https://<domain> + SECURE_COOKIES=true ❌ 403 ✅ Fixed
Cloud deployments ✅ Works ✅ Works

Non-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

  • Branch based on canary
  • Read CONTRIBUTING.md
  • Tested on a live self-hosted instance
  • Non-breaking change

Greptile Summary

This PR makes Better Auth’s secure-cookie behavior configurable for self-hosted deployments through SECURE_COOKIES.

  • Applies the setting to both Better Auth’s secure-cookie mode and default cookie attributes.
  • Preserves the existing non-secure default for self-hosted HTTP deployments.
  • Does not add the new setting to checked-in operator-facing configuration or environment examples.

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

Greptile also left 1 inline comment on this PR.

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
Copilot AI lite review requested due to automatic review settings August 20, 2026 22:55
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Aug 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.secure based on SECURE_COOKIES and tightens the sameSite literal typing.
  • Adds inline guidance in auth.ts for 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.

Comment on lines +85 to +91
// 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: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Initial Admin Role, HTTPS Session 403, and Git Provider Access on Self-Hosted

2 participants