Harden auth, redaction, upload size checks, and compose token requirements
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""FastAPI entrypoint for the DMS backend service."""
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.api.router import api_router
|
||||
from app.core.config import get_settings
|
||||
@@ -28,6 +29,35 @@ def create_app() -> FastAPI:
|
||||
)
|
||||
app.include_router(api_router, prefix="/api/v1")
|
||||
|
||||
@app.middleware("http")
|
||||
async def enforce_upload_request_size(request: Request, call_next):
|
||||
"""Rejects upload requests without deterministic length or exceeding configured limits."""
|
||||
|
||||
if request.url.path.endswith("/api/v1/documents/upload"):
|
||||
content_length = request.headers.get("content-length", "").strip()
|
||||
if not content_length:
|
||||
return JSONResponse(
|
||||
status_code=411,
|
||||
content={"detail": "Content-Length header is required for document uploads"},
|
||||
)
|
||||
try:
|
||||
content_length_value = int(content_length)
|
||||
except ValueError:
|
||||
return JSONResponse(status_code=400, content={"detail": "Invalid Content-Length header"})
|
||||
if content_length_value <= 0:
|
||||
return JSONResponse(status_code=400, content={"detail": "Content-Length must be a positive integer"})
|
||||
if content_length_value > settings.max_upload_request_size_bytes:
|
||||
return JSONResponse(
|
||||
status_code=413,
|
||||
content={
|
||||
"detail": (
|
||||
"Upload request exceeds total size limit "
|
||||
f"({content_length_value} > {settings.max_upload_request_size_bytes} bytes)"
|
||||
)
|
||||
},
|
||||
)
|
||||
return await call_next(request)
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup_event() -> None:
|
||||
"""Initializes storage directories and database schema on service startup."""
|
||||
|
||||
Reference in New Issue
Block a user