feat: auth fail-closed, regles par defaut idempotentes et conteneur non root (tt-2026-11-061)
This commit is contained in:
1 parent
b51a8bba69
commit
6aef9e83e7
11 files changed
+424
-95
No files matched your search
+127
-4
@@ -1,8 +1,11 @@
|
||||
import pytest
|
||||
import jwt
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
from tests.conftest import TEST_SECRET
|
||||
|
||||
def test_health_endpoint_no_auth_required():
|
||||
"""/health reste public et répond 200 avec l'horodatage de Tunis."""
|
||||
client = TestClient(app)
|
||||
resp = client.get("/health")
|
||||
assert resp.status_code == 200
|
||||
@@ -11,22 +14,142 @@ def test_health_endpoint_no_auth_required():
|
||||
assert data["service"] == "courrier"
|
||||
assert "+01:00" in data["horodatage"]
|
||||
|
||||
def test_missing_cf_access_jwt_returns_401():
|
||||
def test_missing_cf_access_jwt_returns_401(monkeypatch):
|
||||
"""Une requête API sans en-tête JWT est rejetée en 401."""
|
||||
monkeypatch.setattr("app.auth.ENV", "test")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "courrier-test-aud")
|
||||
client = TestClient(app)
|
||||
resp = client.get("/api/v1/categories")
|
||||
assert resp.status_code == 401
|
||||
assert "Cf-Access-Jwt-Assertion manquant" in resp.json()["detail"]
|
||||
assert "manquant" in resp.json()["detail"]
|
||||
|
||||
def test_invalid_cf_access_jwt_returns_401():
|
||||
def test_invalid_cf_access_jwt_returns_401(monkeypatch):
|
||||
"""Un jeton malformé est rejeté en 401."""
|
||||
monkeypatch.setattr("app.auth.ENV", "test")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "courrier-test-aud")
|
||||
client = TestClient(app)
|
||||
# Token malformé
|
||||
resp = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": "ceci.n-est-pas.un-jwt-valide"}
|
||||
)
|
||||
assert resp.status_code == 401
|
||||
|
||||
def test_forged_alg_none_jwt_returns_401(monkeypatch):
|
||||
"""
|
||||
Preuve T1 : Un jeton forgé avec l'algorithme 'none' est formellement
|
||||
rejeté en 401 (aucun décodage non vérifié permis).
|
||||
"""
|
||||
monkeypatch.setattr("app.auth.ENV", "test")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "courrier-test-aud")
|
||||
|
||||
payload = {
|
||||
"sub": "hacker@evil.com",
|
||||
"email": "hacker@evil.com",
|
||||
"aud": "courrier-test-aud",
|
||||
"exp": 9999999999
|
||||
}
|
||||
# Jeton non signé (alg: none)
|
||||
forged_token = jwt.encode(payload, key="", algorithm="none")
|
||||
|
||||
client = TestClient(app)
|
||||
resp = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": forged_token}
|
||||
)
|
||||
assert resp.status_code == 401
|
||||
assert "signature non vérifiée" in resp.json()["detail"].lower() or "invalide" in resp.json()["detail"].lower()
|
||||
|
||||
def test_forged_bad_signature_jwt_returns_401(monkeypatch):
|
||||
"""
|
||||
Preuve T1 : Un jeton signé avec une fausse clé secrète est formellement
|
||||
rejeté en 401.
|
||||
"""
|
||||
monkeypatch.setattr("app.auth.ENV", "test")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "courrier-test-aud")
|
||||
|
||||
payload = {
|
||||
"sub": "hacker@evil.com",
|
||||
"email": "hacker@evil.com",
|
||||
"aud": "courrier-test-aud",
|
||||
"exp": 9999999999
|
||||
}
|
||||
# Signé avec une clé pirate différente
|
||||
forged_token = jwt.encode(payload, "mauvaise-cle-pirate-123-longueur-securisee-32-bytes", algorithm="HS256")
|
||||
|
||||
client = TestClient(app)
|
||||
resp = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": forged_token}
|
||||
)
|
||||
assert resp.status_code == 401
|
||||
|
||||
def test_bad_audience_jwt_returns_401(monkeypatch):
|
||||
"""Un jeton portant un audience différent de CF_ACCESS_AUD est rejeté en 401."""
|
||||
monkeypatch.setattr("app.auth.ENV", "test")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "courrier-test-aud")
|
||||
|
||||
payload = {
|
||||
"sub": "user@example.com",
|
||||
"aud": "autre-audience-invalide",
|
||||
"exp": 9999999999
|
||||
}
|
||||
token = jwt.encode(payload, TEST_SECRET, algorithm="HS256")
|
||||
|
||||
client = TestClient(app)
|
||||
resp = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": token}
|
||||
)
|
||||
assert resp.status_code == 401
|
||||
|
||||
def test_valid_cf_access_jwt_returns_200(client):
|
||||
"""Un jeton correctement signé en environnement de test donne accès en 200."""
|
||||
resp = client.get("/api/v1/categories")
|
||||
assert resp.status_code == 200
|
||||
assert "categories" in resp.json()
|
||||
|
||||
def test_unconfigured_auth_returns_503_on_api_and_200_on_health(monkeypatch):
|
||||
"""
|
||||
Preuve T1 : En production (ENV!=test) sans CF_ACCESS_CERTS_URL et CF_ACCESS_AUD,
|
||||
toutes les routes /api répondent 503 tandis que /health reste 200.
|
||||
"""
|
||||
monkeypatch.setattr("app.auth.ENV", "production")
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_CERTS_URL", "")
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "")
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
# 1. /health reste fonctionnel en 200
|
||||
r_health = client.get("/health")
|
||||
assert r_health.status_code == 200
|
||||
|
||||
# 2. Les routes /api renvoient 503 Fail-Closed
|
||||
r_api = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": "dummy.token"}
|
||||
)
|
||||
assert r_api.status_code == 503
|
||||
assert "obligatoires" in r_api.json()["detail"]
|
||||
|
||||
def test_cf_test_secret_ignored_when_not_in_test_env(monkeypatch):
|
||||
"""
|
||||
Preuve T1 : Le mode CF_TEST_SECRET n'existe que si ENV=test.
|
||||
Si ENV=production, CF_TEST_SECRET est strictement ignoré et le service
|
||||
répond 503 si les clés JWKS de production ne sont pas configurées.
|
||||
"""
|
||||
monkeypatch.setattr("app.auth.ENV", "production")
|
||||
monkeypatch.setattr("app.auth.CF_TEST_SECRET", TEST_SECRET)
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_CERTS_URL", "")
|
||||
monkeypatch.setattr("app.auth.CF_ACCESS_AUD", "")
|
||||
|
||||
client = TestClient(app)
|
||||
r = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": "token"}
|
||||
)
|
||||
assert r.status_code == 503
|
||||
Reference in new issue
Block a user