feat(auth): ADMIN_COOKIE_SECURE forces the session cookie Secure
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.
This commit is contained in:
parent
d8113b37b8
commit
72757e7c51
4 changed files with 67 additions and 4 deletions
|
|
@ -318,7 +318,8 @@ def test_login_missing_fields_is_401_not_500():
|
|||
assert resp.status_code == 401
|
||||
|
||||
|
||||
def test_login_success_sets_httponly_cookie():
|
||||
def test_login_success_sets_httponly_cookie(monkeypatch):
|
||||
monkeypatch.delenv("ADMIN_COOKIE_SECURE", raising=False)
|
||||
resp = _login()
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == {"authenticated": True, "admin": True}
|
||||
|
|
@ -337,12 +338,48 @@ def test_login_accepts_json_body():
|
|||
assert resp.json()["authenticated"] is True
|
||||
|
||||
|
||||
def test_login_cookie_marks_secure_behind_https_proxy():
|
||||
def test_login_cookie_marks_secure_behind_https_proxy(monkeypatch):
|
||||
monkeypatch.delenv("ADMIN_COOKIE_SECURE", raising=False)
|
||||
resp = _login(headers={"X-Forwarded-Proto": "https"})
|
||||
assert resp.status_code == 200
|
||||
assert "Secure" in resp.headers.get("set-cookie", "")
|
||||
|
||||
|
||||
def test_login_cookie_secure_via_env_flag_over_http_origin(monkeypatch):
|
||||
"""ADMIN_COOKIE_SECURE is what makes the cookie Secure in the cluster:
|
||||
the header arrives as ``http`` from the Cilium Gateway."""
|
||||
monkeypatch.setenv("ADMIN_COOKIE_SECURE", "1")
|
||||
resp = _login(headers={"X-Forwarded-Proto": "http"})
|
||||
assert resp.status_code == 200
|
||||
raw = resp.headers.get("set-cookie", "")
|
||||
assert "Secure" in raw
|
||||
assert "SameSite=lax" in raw.replace("samesite=lax", "SameSite=lax")
|
||||
assert "HttpOnly" in raw
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["true", "TRUE", " Yes ", "yes"])
|
||||
def test_login_cookie_env_flag_accepts_truthy_spellings(monkeypatch, value):
|
||||
monkeypatch.setenv("ADMIN_COOKIE_SECURE", value)
|
||||
resp = _login()
|
||||
assert resp.status_code == 200
|
||||
assert "Secure" in resp.headers.get("set-cookie", "")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["", "0", "false", "no", "off"])
|
||||
def test_login_cookie_env_flag_ignores_falsey_spellings(monkeypatch, value):
|
||||
monkeypatch.setenv("ADMIN_COOKIE_SECURE", value)
|
||||
resp = _login(headers={"X-Forwarded-Proto": "http"})
|
||||
assert resp.status_code == 200
|
||||
assert "Secure" not in resp.headers.get("set-cookie", "")
|
||||
|
||||
|
||||
def test_login_cookie_without_header_or_flag_is_not_secure_locally(monkeypatch):
|
||||
monkeypatch.delenv("ADMIN_COOKIE_SECURE", raising=False)
|
||||
resp = _login()
|
||||
assert resp.status_code == 200
|
||||
assert "Secure" not in resp.headers.get("set-cookie", "")
|
||||
|
||||
|
||||
def test_session_cookie_unlocks_admin_routes(no_db):
|
||||
login = _login()
|
||||
cookie = {auth.COOKIE_NAME: login.cookies.get(auth.COOKIE_NAME)}
|
||||
|
|
|
|||
Loading…
Reference in a new issue