fix: валидировать заголовок после проверки лимита размера
Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
@@ -107,8 +107,6 @@ async def read_upload(stream, declared_mime: str) -> tuple[bytes, str, str]:
|
|||||||
limit = settings.max_upload_bytes
|
limit = settings.max_upload_bytes
|
||||||
buf = bytearray()
|
buf = bytearray()
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
header_checked = False
|
|
||||||
actual = ""
|
|
||||||
async for chunk in stream:
|
async for chunk in stream:
|
||||||
if not chunk:
|
if not chunk:
|
||||||
continue
|
continue
|
||||||
@@ -118,13 +116,12 @@ async def read_upload(stream, declared_mime: str) -> tuple[bytes, str, str]:
|
|||||||
# требует 413 при превышении bytes, даже если заголовок битый.
|
# требует 413 при превышении bytes, даже если заголовок битый.
|
||||||
if len(buf) > limit:
|
if len(buf) > limit:
|
||||||
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
|
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
|
||||||
if not header_checked and len(buf) >= _HEADER_BYTES:
|
|
||||||
actual = _validate_header(bytes(buf[:_HEADER_BYTES]), declared_mime)
|
|
||||||
header_checked = True
|
|
||||||
if not buf:
|
if not buf:
|
||||||
raise ApiError(400, "invalid_request", "Пустое тело запроса")
|
raise ApiError(400, "invalid_request", "Пустое тело запроса")
|
||||||
if not header_checked:
|
# Валидация заголовка — только после того, как убедились, что лимит
|
||||||
actual = _validate_header(bytes(buf), declared_mime)
|
# не превышен. Иначе битый заголовок в oversized-файле дал бы 415
|
||||||
|
# вместо 413, что нарушает приоритет кодов контракта.
|
||||||
|
actual = _validate_header(bytes(buf[:_HEADER_BYTES]), declared_mime)
|
||||||
return bytes(buf), hasher.hexdigest(), actual
|
return bytes(buf), hasher.hexdigest(), actual
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user