Skip to content

Commit 5bede00

Browse files
committed
fix: CSRF cookie must be readable by JS and work behind proxy
- Set cookie_httponly=False for CSRF cookie (JS needs to read it) - Remove secure flag from cookies (breaks when proxy terminates SSL) - Security still enforced at proxy level (Coolify/nginx) Fixes CSRF verification failure on API key regeneration.
1 parent a32b008 commit 5bede00

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/polar_flow_server/app.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ def create_app() -> Litestar:
9999
session_store = MemoryStore()
100100

101101
# Session middleware config with explicit security settings
102-
is_debug = settings.log_level == "DEBUG"
102+
# Note: We don't set secure=True because Coolify/nginx terminates SSL
103+
# and forwards HTTP internally. The cookies would be rejected over HTTP.
104+
# Security is still enforced at the proxy level.
103105
session_config = ServerSideSessionConfig(
104106
key=settings.get_session_secret(),
105107
store="session_store",
106108
max_age=86400, # 24 hours
107-
secure=not is_debug, # HTTPS only in production
108-
httponly=True, # Prevent JS access
109+
httponly=True, # Prevent JS access to session cookie
109110
samesite="lax", # CSRF protection
110111
)
111112

@@ -116,6 +117,7 @@ def create_app() -> Litestar:
116117
secret=settings.get_session_secret(),
117118
cookie_name="csrf_token",
118119
header_name="X-CSRF-Token",
120+
cookie_httponly=False, # JS needs to read this cookie to send in header
119121
exclude=[
120122
# Entry points (no session yet)
121123
"/admin/login",
@@ -156,7 +158,7 @@ def create_app() -> Litestar:
156158
middleware=[session_config.middleware, RateLimitHeadersMiddleware],
157159
csrf_config=csrf_config,
158160
stores={"session_store": session_store},
159-
debug=is_debug,
161+
debug=settings.log_level == "DEBUG",
160162
)
161163

162164

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)