From d4d87bb4e523d7a9f3691689478b6e9adbf763b8 Mon Sep 17 00:00:00 2001 From: Alexander Minges Date: Wed, 11 Feb 2026 10:57:00 +0100 Subject: [PATCH] fix(matrix): block outbound media when maxMediaBytes is zero --- nanobot/channels/matrix.py | 10 +++++++++- tests/test_matrix_channel.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/nanobot/channels/matrix.py b/nanobot/channels/matrix.py index 720aef7..eb921da 100644 --- a/nanobot/channels/matrix.py +++ b/nanobot/channels/matrix.py @@ -496,7 +496,15 @@ class MatrixChannel(BaseChannel): ) return MATRIX_ATTACHMENT_UPLOAD_FAILED_TEMPLATE.format(filename) - if limit_bytes and size_bytes > limit_bytes: + if limit_bytes <= 0: + logger.warning( + "Matrix outbound attachment skipped: media limit {} blocks all uploads for {}", + limit_bytes, + resolved, + ) + return MATRIX_ATTACHMENT_TOO_LARGE_TEMPLATE.format(filename) + + if size_bytes > limit_bytes: logger.warning( "Matrix outbound attachment skipped: {} bytes exceeds limit {} for {}", size_bytes, diff --git a/tests/test_matrix_channel.py b/tests/test_matrix_channel.py index 533d615..222f14e 100644 --- a/tests/test_matrix_channel.py +++ b/tests/test_matrix_channel.py @@ -857,6 +857,29 @@ async def test_send_uses_server_upload_limit_when_smaller_than_local_limit(tmp_p assert client.room_send_calls[0]["content"]["body"] == "[attachment: tiny.txt - too large]" +@pytest.mark.asyncio +async def test_send_blocks_all_outbound_media_when_limit_is_zero(tmp_path) -> None: + channel = MatrixChannel(_make_config(max_media_bytes=0), MessageBus()) + client = _FakeAsyncClient("", "", "", None) + channel.client = client + + file_path = tmp_path / "empty.txt" + file_path.write_bytes(b"") + + await channel.send( + OutboundMessage( + channel="matrix", + chat_id="!room:matrix.org", + content="", + media=[str(file_path)], + ) + ) + + assert client.upload_calls == [] + assert len(client.room_send_calls) == 1 + assert client.room_send_calls[0]["content"]["body"] == "[attachment: empty.txt - too large]" + + @pytest.mark.asyncio async def test_send_omits_ignore_unverified_devices_when_e2ee_disabled() -> None: channel = MatrixChannel(_make_config(e2ee_enabled=False), MessageBus())