feat(auth): ADMIN_COOKIE_SECURE forces the session cookie Secure #51

Merged
sirius merged 1 commit from wt/t_e5a0c384 into master 2026-11-14 02:16:58 +00:00
Owner

Why

Live at https://osint.siriusdevops.com the session cookie is issued without Secure:

set-cookie: osint_session=<token>; HttpOnly; Max-Age=604800; Path=/; SameSite=lax

_is_https() trusts X-Forwarded-Proto, but that header never arrives as https: the Cloudflare tunnel originates at the Cilium Gateway on plain HTTP and Envoy normalizes the header to http (an explicit client X-Forwarded-Proto: https sent straight at 10.10.0.2:8080 is discarded too). A Secure-less cookie on an https-only site is sendable over a plaintext downgrade.

What changed

  • app/auth.py — set_session_cookie() now sets secure=True when _is_https(request) or ADMIN_COOKIE_SECURE is truthy (1/true/yes, case-insensitive, whitespace-stripped). Header inference kept as the fallback; cookie_secure_forced() reads the env per call.
  • clear_session_cookie() untouched (Secure on a delete would break clearing a cookie set over http in local dev). SameSite=Lax unchanged.
  • docs/auth.md — paragraph in “Session cookie” explaining why the header inference is useless behind an HTTP origin listener and that the flag is what makes the cluster cookie Secure.
  • .env.example — documents ADMIN_COOKIE_SECURE next to the other three admin vars.
  • tests/test_auth.py — flag + X-Forwarded-Proto: http ⇒ Secure; unset + X-Forwarded-Proto: https ⇒ Secure; unset + no header ⇒ no Secure; truthy/falsey spellings parametrized.

Verification

pytest -q -rs: 344 passed, 17 skipped — all 17 skips are integration (test DB unreachable: DB_HOST/DB_PORT unset, tests/test_api_*.py, test_fire_ingest.py).

tests/test_auth.py alone: 54 passed.

Manual, uvicorn main:app --port 8099 --lifespan off (--lifespan off only because the startup lifespan requires Postgres; the login route does not):

# ADMIN_COOKIE_SECURE=1
set-cookie: osint_session=eyJleH...7tlY; HttpOnly; Max-Age=604800; Path=/; SameSite=lax; Secure

# flag unset (local-dev path)
set-cookie: osint_session=eyJleH...kvic; HttpOnly; Max-Age=604800; Path=/; SameSite=lax

# flag unset + X-Forwarded-Proto: https (existing inference preserved)
set-cookie: osint_session=eyJleH...kvic; HttpOnly; Max-Age=604800; Path=/; SameSite=lax; Secure

Operator follow-up (not in this PR)

Set ADMIN_COOKIE_SECURE=1 in the cluster ConfigMap (apps/base/osint/ in sirius/homek8s), rebuild the amd64 image and bump the pin.

Premise note: no HTTPS listener was found in this deployment path — the app is reached at plain HTTP by both Cloudflare and Cilium Gateway, so the env flag is required, not merely additive.

## Why Live at `https://osint.siriusdevops.com` the session cookie is issued **without `Secure`**: ``` set-cookie: osint_session=<token>; HttpOnly; Max-Age=604800; Path=/; SameSite=lax ``` `_is_https()` trusts `X-Forwarded-Proto`, but that header never arrives as `https`: the Cloudflare tunnel originates at the Cilium Gateway on plain HTTP and Envoy normalizes the header to `http` (an explicit client `X-Forwarded-Proto: https` sent straight at `10.10.0.2:8080` is discarded too). A `Secure`-less cookie on an https-only site is sendable over a plaintext downgrade. ## What changed * `app/auth.py` — `set_session_cookie()` now sets `secure=True` when `_is_https(request)` **or** `ADMIN_COOKIE_SECURE` is truthy (`1`/`true`/`yes`, case-insensitive, whitespace-stripped). Header inference kept as the fallback; `cookie_secure_forced()` reads the env per call. * `clear_session_cookie()` untouched (`Secure` on a delete would break clearing a cookie set over http in local dev). `SameSite=Lax` unchanged. * `docs/auth.md` — paragraph in “Session cookie” explaining why the header inference is useless behind an HTTP origin listener and that the flag is what makes the cluster cookie `Secure`. * `.env.example` — documents `ADMIN_COOKIE_SECURE` next to the other three admin vars. * `tests/test_auth.py` — flag + `X-Forwarded-Proto: http` ⇒ `Secure`; unset + `X-Forwarded-Proto: https` ⇒ `Secure`; unset + no header ⇒ no `Secure`; truthy/falsey spellings parametrized. ## Verification `pytest -q -rs`: **344 passed, 17 skipped** — all 17 skips are `integration` (test DB unreachable: `DB_HOST/DB_PORT` unset, `tests/test_api_*.py`, `test_fire_ingest.py`). `tests/test_auth.py` alone: **54 passed**. Manual, `uvicorn main:app --port 8099 --lifespan off` (`--lifespan off` only because the startup lifespan requires Postgres; the login route does not): ``` # ADMIN_COOKIE_SECURE=1 set-cookie: osint_session=eyJleH...7tlY; HttpOnly; Max-Age=604800; Path=/; SameSite=lax; Secure # flag unset (local-dev path) set-cookie: osint_session=eyJleH...kvic; HttpOnly; Max-Age=604800; Path=/; SameSite=lax # flag unset + X-Forwarded-Proto: https (existing inference preserved) set-cookie: osint_session=eyJleH...kvic; HttpOnly; Max-Age=604800; Path=/; SameSite=lax; Secure ``` ## Operator follow-up (not in this PR) Set `ADMIN_COOKIE_SECURE=1` in the cluster ConfigMap (`apps/base/osint/` in `sirius/homek8s`), rebuild the amd64 image and bump the pin. > Premise note: no HTTPS listener was found in this deployment path — the app is reached at plain HTTP by both Cloudflare and Cilium Gateway, so the env flag is required, not merely additive.
Behind the cluster's HTTP origin listener Envoy normalizes
X-Forwarded-Proto to http, so _is_https() never fires and the session
cookie was issued without Secure. Add an explicit deploy-time flag:
set_session_cookie() now marks the cookie Secure when the env var is
1/true/yes (case-insensitive, stripped) or the header inference says
https. clear_session_cookie() is unchanged, SameSite stays Lax.

Tests cover flag-over-http, header-without-flag, and the local-dev path;
docs/auth.md and .env.example document the variable.
sirius changed title from probe to feat(auth): ADMIN_COOKIE_SECURE forces the session cookie Secure 2026-11-14 02:15:36 +00:00
sirius merged commit 84ea0a17b1 into master 2026-11-14 02:16:58 +00:00
sirius deleted branch wt/t_e5a0c384 2026-11-14 02:16:58 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/osint-dashboard!51
No description provided.