fix(napcat): keep a message whose image declares a non-numeric size
Napcat reports an image segment's file_size as a string that is not always numeric, so int() raises ValueError. The upfront size guard only caught TypeError and KeyError, the ValueError escaped _download_image into _on_message, and the whole inbound message was dropped — text included — with only a background-handler warning left behind. Handle ValueError as the guard already handles a missing size: skip the upfront check and let the streamed download enforce max_image_bytes. Log it, since a declared size we cannot read is worth knowing about.
This commit is contained in:
1 parent
90fcb7f903
commit
17684ca5c4
2 files changed
+64
No files matched your search
@@ -549,6 +549,16 @@ class NapcatChannel(BaseChannel):
|
|||||||
return None
|
return None
|
||||||
except (TypeError, KeyError):
|
except (TypeError, KeyError):
|
||||||
pass
|
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:
|
try:
|
||||||
async with self._http.get(url, allow_redirects=False) as resp:
|
async with self._http.get(url, allow_redirects=False) as resp:
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -163,6 +164,59 @@ async def test_download_image_rejects_redirects(tmp_path, monkeypatch) -> None:
|
|||||||
assert list(tmp_path.iterdir()) == []
|
assert list(tmp_path.iterdir()) == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_download_image_ignores_unparsable_declared_size(tmp_path, monkeypatch) -> None:
|
||||||
|
channel = _channel()
|
||||||
|
channel._media_root = tmp_path
|
||||||
|
channel._http = _FakeHttp(_FakeResponse(status=200, chunks=[b"png-bytes"]))
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"nanobot.channels.napcat.runtime.validate_url_target",
|
||||||
|
lambda _url: (True, ""),
|
||||||
|
)
|
||||||
|
|
||||||
|
for declared in ("", "unknown", "12KB"):
|
||||||
|
result = await channel._download_image(
|
||||||
|
{"url": "https://example.com/a.png", "file": "a.png", "file_size": declared}
|
||||||
|
)
|
||||||
|
assert result is not None, declared
|
||||||
|
assert Path(result).read_bytes() == b"png-bytes"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_message_with_unparsable_image_size_still_routes(tmp_path, monkeypatch) -> None:
|
||||||
|
channel = _channel()
|
||||||
|
channel._media_root = tmp_path
|
||||||
|
channel._http = _FakeHttp(_FakeResponse(status=200, chunks=[b"png-bytes"]))
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"nanobot.channels.napcat.runtime.validate_url_target",
|
||||||
|
lambda _url: (True, ""),
|
||||||
|
)
|
||||||
|
|
||||||
|
await channel._on_message(
|
||||||
|
{
|
||||||
|
"message_id": 7,
|
||||||
|
"message_type": "private",
|
||||||
|
"user_id": "user1",
|
||||||
|
"sender": {"nickname": "Alice"},
|
||||||
|
"message": [
|
||||||
|
{"type": "text", "data": {"text": "look at this"}},
|
||||||
|
{
|
||||||
|
"type": "image",
|
||||||
|
"data": {
|
||||||
|
"url": "https://example.com/a.png",
|
||||||
|
"file": "a.png",
|
||||||
|
"file_size": "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
msg = await channel.bus.consume_inbound()
|
||||||
|
assert msg.content == "look at this"
|
||||||
|
assert msg.media == [str(tmp_path / "a.png")]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_tracks_and_discards_background_tasks() -> None:
|
async def test_dispatch_tracks_and_discards_background_tasks() -> None:
|
||||||
channel = _channel()
|
channel = _channel()
|
||||||
|
|||||||
Reference in new issue
Block a user