Skip to content

Your lightweight open-source personal agent.

fix(matrix): block outbound media when maxMediaBytes is zero

Alexander Mingesd4d87bb4e523
diff --git a/nanobot/channels/matrix.py b/nanobot/channels/matrix.py
index 720aef752..eb921da7b 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 533d615d1..222f14e05 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]"
+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())