fix(napcat): simplify unknown-size handling and check download cap

This commit is contained in:
Xubin Ren committed 2026-10-18 18:56:28 +08:00
1 parent 17684ca5c4
commit 271bda1d81
2 files changed
+7 -14

No files matched your search

+1 -11
View File
@@ -547,18 +547,8 @@ class NapcatChannel(BaseChannel):
url,
)
return None
except (TypeError, KeyError):
except (TypeError, KeyError, ValueError):
pass
except ValueError:
# Napcat reports file_size as a string, and it is not always numeric.
# Skip the upfront check; the streamed download below still caps the
# image at max_bytes, so nothing unbounded reaches disk.
logger.warning(
"napcat: unparsable image file_size={!r} url={}, enforcing the size cap "
"while streaming",
info["file_size"],
url,
)
try:
async with self._http.get(url, allow_redirects=False) as resp:
@@ -183,8 +183,9 @@ async def test_download_image_ignores_unparsable_declared_size(tmp_path, monkeyp
@pytest.mark.asyncio
async def test_message_with_unparsable_image_size_still_routes(tmp_path, monkeypatch) -> None:
channel = _channel()
@pytest.mark.parametrize("max_bytes", [32, 3])
async def test_message_with_unparsable_image_size_still_routes(tmp_path, monkeypatch, max_bytes) -> None:
channel = _channel(NapcatConfig(allow_from=["*"], max_image_bytes=max_bytes))
channel._media_root = tmp_path
channel._http = _FakeHttp(_FakeResponse(status=200, chunks=[b"png-bytes"]))
monkeypatch.setattr(
@@ -214,7 +215,9 @@ async def test_message_with_unparsable_image_size_still_routes(tmp_path, monkeyp
msg = await channel.bus.consume_inbound()
assert msg.content == "look at this"
assert msg.media == [str(tmp_path / "a.png")]
expected = [str(tmp_path / "a.png")] if max_bytes >= len(b"png-bytes") else []
assert msg.media == expected
assert sorted(str(path) for path in tmp_path.iterdir()) == expected
@pytest.mark.asyncio