fix(security): secure session cookies and HSTS over https #30

Manually merged
jfro merged 2 commits from fix/15-secure-cookies into main 2026-11-01 18:32:50 +00:00
Owner

Closes #15.

Session (_lamplighter_key) and remember-me cookies weren't secure, and there was no HSTS.

What changes

  • LamplighterWeb.Endpoint.https?/0: the endpoint's url scheme is https, which runtime.exs sets in production.
  • When it is: the session cookie (now Plug.Session initialised per request from a function plug, so secure can follow runtime config) and the remember-me cookie are secure, and the :browser pipeline sends strict-transport-security: max-age=31536000. No includeSubDomains, since homelab domains are often shared with http-only services.
  • I didn't turn on force_ssl: behind a TLS proxy the app sees plain http, and redirecting relies on the proxy sending X-Forwarded-Proto, which risks redirect loops. HSTS plus secure cookies cover the issue without that.
  • Dev/test (http) are unchanged. When #18 (PHX_SCHEME for http-only LAN installs) sets the scheme to http, both turn off with it.
  • README: reach it over https, since logging in over plain http won't stick.

Tests: secure_cookies_test.exs (sync) switches the endpoint url scheme: over https both cookies are secure and HSTS is sent; over http neither. The https test fails on main. mix precommit: 626 pass.

Closes #15. Session (`_lamplighter_key`) and remember-me cookies weren't `secure`, and there was no HSTS. **What changes** - `LamplighterWeb.Endpoint.https?/0`: the endpoint's url scheme is https, which `runtime.exs` sets in production. - When it is: the session cookie (now `Plug.Session` initialised per request from a function plug, so `secure` can follow runtime config) and the remember-me cookie are `secure`, and the `:browser` pipeline sends `strict-transport-security: max-age=31536000`. No `includeSubDomains`, since homelab domains are often shared with http-only services. - I didn't turn on `force_ssl`: behind a TLS proxy the app sees plain http, and redirecting relies on the proxy sending `X-Forwarded-Proto`, which risks redirect loops. HSTS plus secure cookies cover the issue without that. - Dev/test (http) are unchanged. When #18 (PHX_SCHEME for http-only LAN installs) sets the scheme to http, both turn off with it. - README: reach it over https, since logging in over plain http won't stick. **Tests**: `secure_cookies_test.exs` (sync) switches the endpoint url scheme: over https both cookies are secure and HSTS is sent; over http neither. The https test fails on main. `mix precommit`: 626 pass.
jfro added this to the v1.0 milestone 2026-11-01 17:39:20 +00:00
fix(security): secure session cookies and HSTS over https
All checks were successful
CI / test (pull_request) Successful in 23s
bf4a75485c
Session and remember-me cookies weren't secure and there was no HSTS.
When the endpoint's url scheme is https (production), both cookies are
now secure and browser responses send Strict-Transport-Security
(max-age one year, no includeSubDomains). Dev and test stay on http.

Closes #15
Author
Owner

Review (Nye) at bf4a754: changes requested (F7, medium).
Agree on leaving force_ssl off: the Secure flag and HSTS don't depend on conn.scheme, so they work behind a TLS proxy without redirect loops.
Problem: prod hard-codes url scheme https (config/runtime.exs), and cookies are now Secure whenever the scheme is https. A plain-http LAN install (docker run -p 4000:4000, browse http://nas:4000), which could log in before this PR, now can't: browsers drop Secure cookies set over http, and there's no way to opt out.
Fix: read PHX_SCHEME (default "https") and PHX_URL_PORT (default 443) in runtime.exs for the endpoint url, and document both in the README env table (this is also audit gap #7, the OIDC redirect_uri/links on http installs). Failing test: test/review/http_install_test.exs (sent to you).
Otherwise good: session and remember-me cookies both get secure, HSTS only on the https config, tests cover both schemes. mix precommit: 626 pass. CI green.

Review (Nye) at bf4a754: changes requested (F7, medium). Agree on leaving force_ssl off: the Secure flag and HSTS don't depend on conn.scheme, so they work behind a TLS proxy without redirect loops. Problem: prod hard-codes url scheme https (config/runtime.exs), and cookies are now Secure whenever the scheme is https. A plain-http LAN install (docker run -p 4000:4000, browse http://nas:4000), which could log in before this PR, now can't: browsers drop Secure cookies set over http, and there's no way to opt out. Fix: read PHX_SCHEME (default "https") and PHX_URL_PORT (default 443) in runtime.exs for the endpoint url, and document both in the README env table (this is also audit gap #7, the OIDC redirect_uri/links on http installs). Failing test: test/review/http_install_test.exs (sent to you). Otherwise good: session and remember-me cookies both get secure, HSTS only on the https config, tests cover both schemes. mix precommit: 626 pass. CI green.
jfro force-pushed fix/15-secure-cookies from bf4a75485c
All checks were successful
CI / test (pull_request) Successful in 23s
to 7ba15f4626
All checks were successful
CI / test (pull_request) Successful in 25s
2026-11-01 18:00:59 +00:00
Compare
jfro force-pushed fix/15-secure-cookies from 7ba15f4626
All checks were successful
CI / test (pull_request) Successful in 25s
to 5b8b776945
All checks were successful
CI / test (pull_request) Successful in 23s
2026-11-01 18:29:53 +00:00
Compare
Author
Owner

Review (Nye): Re-reviewed at 7ba15f4: F7 fixed, PHX_SCHEME/PHX_URL_PORT read and validated, cookies secure + HSTS only over https, precommit 647 tests green. Ready.

(Rebased onto main 8f9ff14 as 5b8b776 after #31: kept url_scheme/url_port, dropped the dns_cluster_query line. Precommit 647 green.)

Review (Nye): Re-reviewed at 7ba15f4: F7 fixed, PHX_SCHEME/PHX_URL_PORT read and validated, cookies secure + HSTS only over https, precommit 647 tests green. Ready. (Rebased onto main 8f9ff14 as 5b8b776 after #31: kept url_scheme/url_port, dropped the dns_cluster_query line. Precommit 647 green.)
jfro force-pushed fix/15-secure-cookies from 5b8b776945
All checks were successful
CI / test (pull_request) Successful in 23s
to 9df63c9d05
All checks were successful
CI / test (pull_request) Successful in 25s
2026-11-01 18:32:47 +00:00
Compare
jfro manually merged commit 7e178ddf88 into main 2026-11-01 18:32:50 +00:00
Sign in to join this conversation.
No description provided.