fix: address attachments verify findings

- keep one upload body in memory: BytesIO + getvalue() without copy,
  fixed glibc mmap threshold in the image (no realloc copy on growth)
- stop PNG inflate at expected size + 1 byte
- reject filename that is not valid UTF-8 after percent-decoding (400)
- failure stand: env limit variables do not change attachment limits;
  history with attachment_ids while S3 is stopped
- drop dead constants; restate "old image on new schema" scenario

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 02:16:49 +03:00
co-authored by Claude Opus 5.5
parent 8dc925e8ca
commit dba40bc325
23 changed files with 398 additions and 47 deletions
+11 -3
View File
@@ -15,7 +15,8 @@
суммы.
Память не зависит от числа пикселей: IDAT распаковывается блоками не
больше `_INFLATE_OUT_BYTES`, а вход подаётся кусками `_INFLATE_IN_BYTES`.
больше `_INFLATE_OUT_BYTES` и не дальше ожидаемого объёма плюс байт, а
вход подаётся кусками `_INFLATE_IN_BYTES`.
Строки проверяются срезом с шагом длины строки, конец данных скана JPEG
ищет регулярное выражение — оба в C, без цикла Python по строкам и
байтам. Только stdlib, без логов: бросает свои исключения, сервис
@@ -181,8 +182,15 @@ class _IdatStream:
def _inflate(self, piece: memoryview | bytes) -> None:
while True:
# не дальше ожидаемого объёма: «бомбу» выдаёт первый лишний
# байт, а не блок; больше ожидаемого `_check_rows` не пустит,
# поэтому предел не меньше 1
max_length = min(
_INFLATE_OUT_BYTES,
self._expected - self._total + 1,
)
try:
out = self._inflater.decompress(piece, _INFLATE_OUT_BYTES)
out = self._inflater.decompress(piece, max_length)
except zlib.error as e:
raise DamagedImage("broken image data stream") from e
self._check_rows(out)
@@ -190,7 +198,7 @@ class _IdatStream:
return
piece = self._inflater.unconsumed_tail
# вход кончился, а выход не упёрся в предел — буфер пуст
if not piece and len(out) < _INFLATE_OUT_BYTES:
if not piece and len(out) < max_length:
return
def _check_rows(self, out: bytes) -> None: