forked from mtusi2026/mtusi2026lab4
feat: ЛР4 - S3 хранилище MinIO, загрузка вложений и защита от атак
This commit is contained in:
+20
-4
@@ -32,19 +32,35 @@ class RequestIdAndLoggingMiddleware(BaseHTTPMiddleware):
|
||||
start_time = time.monotonic()
|
||||
|
||||
if request.method in ("POST", "PUT", "PATCH") and request.url.path != "/api/v1/auth/sessions":
|
||||
is_attachment_upload = (
|
||||
request.method == "POST"
|
||||
and "/attachments" in request.url.path
|
||||
and request.url.path.startswith("/api/v1/chats/")
|
||||
)
|
||||
ct = request.headers.get("content-type")
|
||||
if ct:
|
||||
ct_clean = ct.split(";")[0].strip().lower()
|
||||
if ct_clean != "application/json":
|
||||
ct_clean = ct.split(";")[0].strip().lower() if ct else ""
|
||||
|
||||
if is_attachment_upload:
|
||||
if not ct or ct_clean not in ("image/png", "image/jpeg"):
|
||||
from fastapi import status
|
||||
from app.errors import make_error_response
|
||||
from app.schemas import ErrorCode
|
||||
return make_error_response(
|
||||
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||
f"Неподдерживаемый тип содержимого '{ct}'. Ожидается application/json",
|
||||
f"Неподдерживаемый тип содержимого '{ct}'. Ожидается image/png или image/jpeg",
|
||||
request_id,
|
||||
)
|
||||
elif ct and ct_clean != "application/json":
|
||||
from fastapi import status
|
||||
from app.errors import make_error_response
|
||||
from app.schemas import ErrorCode
|
||||
return make_error_response(
|
||||
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||
f"Неподдерживаемый тип содержимого '{ct}'. Ожидается application/json",
|
||||
request_id,
|
||||
)
|
||||
|
||||
try:
|
||||
response = await call_next(request)
|
||||
|
||||
Reference in New Issue
Block a user