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>
This commit is contained in:
1 parent
dc4b804ccf
commit
66fe8feea4
4 files changed
+201
-4
No files matched your search
+14
-3
@@ -12,7 +12,11 @@ from app import __version__
|
||||
from app.api import admin, auth, health
|
||||
from app.config import get_settings
|
||||
from app.db import _session_factory
|
||||
from app.middleware import OriginCheckMiddleware
|
||||
from app.middleware import (
|
||||
BodySizeLimitMiddleware,
|
||||
OriginCheckMiddleware,
|
||||
SecurityHeadersMiddleware,
|
||||
)
|
||||
from app.services.auth import delete_old_login_attempts
|
||||
from app.services.sessions import delete_expired_sessions
|
||||
from app.services.users import UserError, bootstrap_initial_admin
|
||||
@@ -82,6 +86,10 @@ def _register_frontend(app: FastAPI, static_dir: Path) -> None:
|
||||
candidate = (root / path).resolve()
|
||||
if path and candidate.is_relative_to(root) and candidate.is_file():
|
||||
return FileResponse(candidate)
|
||||
# Paths that look like files (/.env, /wp-login.php, /.git/config) are not app routes
|
||||
segments = path.split("/")
|
||||
if "." in segments[-1] or any(seg.startswith(".") for seg in segments):
|
||||
raise HTTPException(status_code=404)
|
||||
return FileResponse(index_file, headers={"Cache-Control": "no-cache"})
|
||||
|
||||
|
||||
@@ -90,12 +98,15 @@ def create_app() -> FastAPI:
|
||||
app = FastAPI(
|
||||
title="LAN-Clipboard",
|
||||
version=__version__,
|
||||
docs_url="/api/docs",
|
||||
openapi_url="/api/openapi.json",
|
||||
docs_url="/api/docs" if settings.enable_api_docs else None,
|
||||
openapi_url="/api/openapi.json" if settings.enable_api_docs else None,
|
||||
redoc_url=None,
|
||||
lifespan=lifespan,
|
||||
)
|
||||
# Last added runs first: security headers wrap every response, including rejections
|
||||
app.add_middleware(OriginCheckMiddleware, allowed_origins=settings.allowed_origin_list)
|
||||
app.add_middleware(BodySizeLimitMiddleware, max_bytes=settings.max_request_bytes)
|
||||
app.add_middleware(SecurityHeadersMiddleware)
|
||||
app.include_router(health.router, prefix="/api")
|
||||
app.include_router(auth.router, prefix="/api")
|
||||
app.include_router(admin.router, prefix="/api")
|
||||
|
||||
Reference in new issue
Block a user