fix: bound full image validation and cover progressive JPEG and APNG

This commit is contained in:
2026-11-06 19:08:18 +03:00
parent 8de05d3899
commit de36c967cc
8 changed files with 617 additions and 62 deletions
+19
View File
@@ -67,6 +67,23 @@ def error(request, status, message, headers=None):
)
async def invalid_json_encoding(request: Request):
path = request.url.path
if request.method != "POST" or not (
path in {"/api/v1/users", "/api/v1/chats"}
or re.fullmatch(r"/api/v1/chats/[^/]+/messages", path)
):
return False
raw = await request.body()
try:
raw.decode("utf-8")
except UnicodeDecodeError:
return True
# Literal NUL is invalid JSON; it also prevents json.loads(bytes) from
# autodetecting BOM-less UTF-16/32. Escaped JSON \u0000 remains valid.
return b"\x00" in raw
@app.middleware("http")
async def request_log(request: Request, call_next):
request.state.request_id = str(uuid4())
@@ -78,6 +95,8 @@ async def request_log(request: Request, call_next):
and await request.body()
):
response = error(request, 400, "Вход не принимает тело запроса")
elif await invalid_json_encoding(request):
response = error(request, 400, "JSON должен быть закодирован в UTF-8")
else:
response = await call_next(request)
except Exception: