Files
lan-clipboard/backend/tests/test_hardening.py
T
dbildhauerandClaude Opus 5.5 66fe8feea4 feat: härtung für den öffentlichen betrieb
- Security-Header (CSP, nosniff, X-Frame-Options, Referrer-/Permissions-Policy)
- HSTS nur bei HTTPS und ohne includeSubDomains
- echtes 404 für dateiartige Pfade wie /.env statt SPA-Fallback
- API-Doku nur mit ENABLE_API_DOCS=true
- API-Requests über 1 MiB werden mit 413 abgelehnt, auch bei Chunked-Upload

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 11:04:33 +01:00

85 lines
2.9 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