- /api/ws authentifiziert über das Session-Cookie, Origin-Pflicht gegen CSWSH - Hub im Prozess verteilt clip.created/updated/deleted an alle Geräte des Benutzers - Ereignisse erst nach erfolgreichem Commit (BackgroundTasks), keine bei Konflikt - Logout, Session-Widerruf, Passwortänderung und Admin-Aktionen schließen betroffene Verbindungen (4401); Session wird alle 5 Minuten neu geprüft - Heartbeat alle 30 s, höchstens 20 Verbindungen pro Benutzer - CSP erlaubt wss:// zur eigenen Domain, eingehende WS-Nachrichten max. 64 KiB Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.config import get_settings
|
|
|
|
|
|
@pytest.fixture
|
|
def spa(static_dir):
|
|
(static_dir / "index.html").write_text("<html>spa</html>")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path", ["/.env", "/root/.env", "/wp-login.php", "/config.json", "/.git/config"]
|
|
)
|
|
def test_file_like_paths_return_404(spa, client, path):
|
|
response = client.get(path)
|
|
assert response.status_code == 404
|
|
assert "spa" not in response.text
|
|
|
|
|
|
def test_app_routes_still_fall_back_to_index(spa, client):
|
|
assert client.get("/clips/42").status_code == 200
|
|
assert client.get("/admin/users").status_code == 200
|
|
|
|
|
|
def test_security_headers_on_api_and_spa(spa, client):
|
|
for path in ("/api/health", "/", "/does-not-exist.php"):
|
|
headers = client.get(path).headers
|
|
assert "default-src 'self'" in headers["content-security-policy"]
|
|
assert "frame-ancestors 'none'" in headers["content-security-policy"]
|
|
assert headers["x-content-type-options"] == "nosniff"
|
|
assert headers["x-frame-options"] == "DENY"
|
|
assert headers["referrer-policy"] == "same-origin"
|
|
|
|
|
|
def test_hsts_only_over_https(client, app_env):
|
|
assert client.get("/api/health").headers["strict-transport-security"] == "max-age=31536000"
|
|
|
|
with TestClient(client.app, base_url="http://testserver") as plain:
|
|
assert "strict-transport-security" not in plain.get("/api/health").headers
|
|
|
|
|
|
def test_security_headers_on_rejected_requests(client):
|
|
response = client.post("/api/auth/logout", headers={"Origin": "https://evil.example"})
|
|
assert response.status_code == 403
|
|
assert "content-security-policy" in response.headers
|
|
|
|
|
|
def test_api_docs_disabled_by_default(client):
|
|
assert client.get("/api/docs").status_code == 404
|
|
assert client.get("/api/openapi.json").status_code == 404
|
|
|
|
|
|
def test_api_docs_can_be_enabled(app_env, monkeypatch):
|
|
monkeypatch.setenv("ENABLE_API_DOCS", "true")
|
|
get_settings.cache_clear()
|
|
from app.main import create_app
|
|
|
|
with TestClient(create_app(), base_url="https://testserver") as client:
|
|
assert client.get("/api/openapi.json").status_code == 200
|
|
|
|
|
|
def test_oversized_request_is_rejected(client):
|
|
body = b'{"username": "' + b"a" * (1024 * 1024) + b'", "password": "x"}'
|
|
response = client.post(
|
|
"/api/auth/login", content=body, headers={"Content-Type": "application/json"}
|
|
)
|
|
assert response.status_code == 413
|
|
|
|
|
|
def test_oversized_chunked_request_is_rejected(client):
|
|
def chunks():
|
|
for _ in range(20):
|
|
yield b"a" * 100_000
|
|
|
|
response = client.post(
|
|
"/api/auth/login", content=chunks(), headers={"Content-Type": "application/json"}
|
|
)
|
|
assert response.status_code == 413
|
|
|
|
|
|
def test_normal_requests_pass_size_limit(client):
|
|
response = client.post("/api/auth/login", json={"username": "x", "password": "y"})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_csp_allows_websocket_to_own_origin(client):
|
|
csp = client.get("/api/health").headers["content-security-policy"]
|
|
assert "connect-src 'self' wss://testserver" in csp
|