Skip to content

Your lightweight open-source personal agent.

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

Xubin Ren271bda1d811a
diff --git a/nanobot/channels/napcat/runtime.py b/nanobot/channels/napcat/runtime.py
index 12e4a35be..e1682743d 100644
--- a/nanobot/channels/napcat/runtime.py
+++ b/nanobot/channels/napcat/runtime.py
@@ -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:
diff --git a/nanobot/channels/napcat/tests/test_napcat_channel.py b/nanobot/channels/napcat/tests/test_napcat_channel.py
index 46dc22cc9..2c7b7767f 100644
--- a/nanobot/channels/napcat/tests/test_napcat_channel.py
+++ b/nanobot/channels/napcat/tests/test_napcat_channel.py
@@ -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()
[email protected]("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