Перевести пароли на Argon2id и читать секреты через AppRole.
Лимит входа общий для реплик, размер тела ограничен, исходящие клиенты проверяют TLS, если задан CA. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+34
-3
@@ -7,13 +7,21 @@ from starlette.datastructures import MutableHeaders
|
||||
from starlette.responses import Response
|
||||
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
||||
|
||||
from app import telemetry
|
||||
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."""
|
||||
|
||||
@@ -33,6 +41,25 @@ class RequestContextMiddleware:
|
||||
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)
|
||||
@@ -66,11 +93,15 @@ class RequestContextMiddleware:
|
||||
app = scope.get("app")
|
||||
shutting_down = bool(app and getattr(app.state, "shutting_down", False))
|
||||
try:
|
||||
if shutting_down and path not in ("/health/live", "/health/ready"):
|
||||
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, receive, send_wrapper)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user