Files
mtusi2026lab7/app/middleware.py
dawudukandCursor fb566d874c Перевести пароли на Argon2id и читать секреты через AppRole.
Лимит входа общий для реплик, размер тела ограничен, исходящие клиенты проверяют TLS, если задан CA.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-26 19:40:28 +03:00

125 lines
4.9 KiB
Python

import logging
import time
import uuid
from contextlib import nullcontext
from starlette.datastructures import MutableHeaders
from starlette.responses import Response
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from app import config, telemetry
from app.config import INSTANCE_ID
from app.errors import AppError, error_response, from_app_error
logger = logging.getLogger("messenger.access")
def _body_limit(path: str, method: str) -> int:
if method in ("GET", "HEAD", "OPTIONS"):
return 1024
if "/attachments" in path or "/uploads" in path:
return config.MAX_UPLOAD_BYTES
return config.MAX_JSON_BYTES
class RequestContextMiddleware:
"""Pure ASGI middleware so upload handlers can read the body stream themselves."""
def __init__(self, app: ASGIApp) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
headers = {key.decode("latin1").lower(): value.decode("latin1") for key, value in scope.get("headers", [])}
request_id = headers.get("x-request-id") or str(uuid.uuid4())
raw_state = scope.get("state")
if not isinstance(raw_state, dict):
raw_state = {}
scope["state"] = raw_state
raw_state["request_id"] = request_id
path = scope.get("path", "")
method = scope.get("method", "")
limit = _body_limit(path, method)
content_length = headers.get("content-length")
announced = None
if content_length:
try:
announced = int(content_length)
except ValueError:
announced = limit + 1
received = 0
async def limited_receive() -> Message:
nonlocal received
message = await receive()
if message["type"] == "http.request":
received += len(message.get("body", b""))
if received > limit:
raise AppError(413, "payload too large")
return message
started = time.perf_counter()
status_code = 500
record = telemetry.observe_http(path)
route = telemetry.route_template(path)
async def send_wrapper(message: Message) -> None:
nonlocal status_code
if message["type"] == "http.response.start":
status_code = int(message["status"])
response_headers = MutableHeaders(scope=message)
response_headers["X-Request-Id"] = request_id
response_headers["X-Instance-Id"] = INSTANCE_ID
await send(message)
parent = telemetry.remote_context(headers.get("traceparent"), headers.get("tracestate"))
span_cm = (
telemetry.span(
"%s %s" % (method, route),
context=parent,
kind="server",
attributes={
"http.request.method": method,
"http.route": route,
"http.request_id": request_id,
},
)
if record
else nullcontext(None)
)
with span_cm as current:
app = scope.get("app")
shutting_down = bool(app and getattr(app.state, "shutting_down", False))
try:
if announced is not None and announced > limit:
status_code = 413
response: Response = error_response(413, "payload too large", request_id)
await response(scope, receive, send_wrapper)
elif shutting_down and path not in ("/health/live", "/health/ready"):
response: Response = error_response(503, "service is shutting down", request_id)
await response(scope, receive, send_wrapper)
else:
await self.app(scope, limited_receive, send_wrapper)
except AppError as exc:
status_code = exc.status
await from_app_error(exc, request_id)(scope, receive, send_wrapper)
except Exception:
status_code = 500
logger.exception("request failed request_id=%s route=%s", request_id, route)
await error_response(500, "internal error", request_id)(scope, receive, send_wrapper)
finally:
if record:
telemetry.finish_http_span(current, status_code)
telemetry.record_http(method, route, status_code, time.perf_counter() - started)
if record:
logger.info(
"request completed request_id=%s method=%s route=%s status=%s duration_ms=%.2f",
request_id,
method,
route,
status_code,
(time.perf_counter() - started) * 1000,
)