mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-14 06:43:53 +00:00
fix(matrix): reject boolean media sizes
This commit is contained in:
parent
4dd89f4c46
commit
1d4000560d
@ -749,7 +749,7 @@ class MatrixChannel(BaseChannel):
|
|||||||
def _event_declared_size_bytes(self, event: MatrixMediaEvent) -> int | None:
|
def _event_declared_size_bytes(self, event: MatrixMediaEvent) -> int | None:
|
||||||
info = self._event_source_content(event).get("info")
|
info = self._event_source_content(event).get("info")
|
||||||
size = info.get("size") if isinstance(info, dict) else None
|
size = info.get("size") if isinstance(info, dict) else None
|
||||||
return size if isinstance(size, int) and size >= 0 else None
|
return size if type(size) is int and size >= 0 else None
|
||||||
|
|
||||||
def _event_mime(self, event: MatrixMediaEvent) -> str | None:
|
def _event_mime(self, event: MatrixMediaEvent) -> str | None:
|
||||||
info = self._event_source_content(event).get("info")
|
info = self._event_source_content(event).get("info")
|
||||||
|
|||||||
@ -1828,6 +1828,34 @@ async def test_fetch_media_rejects_missing_declared_size(monkeypatch, tmp_path)
|
|||||||
assert marker == "[attachment: payload.bin - too large]"
|
assert marker == "[attachment: payload.bin - too large]"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_fetch_media_rejects_bool_declared_size(monkeypatch, tmp_path) -> None:
|
||||||
|
channel = MatrixChannel(_make_config(max_media_bytes=8), MessageBus())
|
||||||
|
client = _FakeAsyncClient("https://matrix.org", "", "", None)
|
||||||
|
channel.client = client
|
||||||
|
monkeypatch.setattr("nanobot.channels.matrix.get_media_dir", lambda _name: tmp_path)
|
||||||
|
|
||||||
|
async def _download_should_not_run(*_args, **_kwargs):
|
||||||
|
raise AssertionError("bool size should be rejected before fetching bytes")
|
||||||
|
|
||||||
|
monkeypatch.setattr(channel, "_download_media_bytes", _download_should_not_run)
|
||||||
|
event = SimpleNamespace(
|
||||||
|
sender="@alice:matrix.org",
|
||||||
|
event_id="$event1",
|
||||||
|
body="payload.bin",
|
||||||
|
url="mxc://example.org/media",
|
||||||
|
source={"content": {"msgtype": "m.file", "info": {"size": True}}},
|
||||||
|
)
|
||||||
|
|
||||||
|
attachment, marker = await channel._fetch_media_attachment(
|
||||||
|
SimpleNamespace(room_id="!room:matrix.org"),
|
||||||
|
event,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert attachment is None
|
||||||
|
assert marker == "[attachment: payload.bin - too large]"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_fetch_media_rejects_declared_oversized_before_download(monkeypatch, tmp_path) -> None:
|
async def test_fetch_media_rejects_declared_oversized_before_download(monkeypatch, tmp_path) -> None:
|
||||||
channel = MatrixChannel(_make_config(max_media_bytes=8), MessageBus())
|
channel = MatrixChannel(_make_config(max_media_bytes=8), MessageBus())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user