From db90ca882aa2583f666efbf05ac7249bea871571 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 12:34:20 -0400 Subject: [PATCH] fix: skip encryption for messages without data Message.encrypt() and PresenceMessage.encrypt() passed data=None to TypedBuffer.from_obj(), which raises TypeError, so publishing a message that has only a name on an encrypted channel failed. Return early when there is no data, as Annotation.encrypt() already does. --- ably/pubsub/types/message.py | 4 ++++ ably/pubsub/types/presence.py | 4 ++++ test/unit/message_test.py | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/ably/pubsub/types/message.py b/ably/pubsub/types/message.py index aaf1b4d6..9fc52de6 100644 --- a/ably/pubsub/types/message.py +++ b/ably/pubsub/types/message.py @@ -236,6 +236,10 @@ def annotations(self): return self.__annotations def encrypt(self, channel_cipher): + # A message may carry no data (e.g. only a name); there is nothing to encrypt. + if self.data is None: + return + if isinstance(self.data, CipherData): return diff --git a/ably/pubsub/types/presence.py b/ably/pubsub/types/presence.py index 3d771c47..9e4a49d4 100644 --- a/ably/pubsub/types/presence.py +++ b/ably/pubsub/types/presence.py @@ -131,6 +131,10 @@ def encrypt(self, channel_cipher): Encrypt the presence message data using the provided cipher. Similar to Message.encrypt(). """ + # Presence messages may carry no data, so there is nothing to encrypt. + if self.data is None: + return + if isinstance(self.data, CipherData): return diff --git a/test/unit/message_test.py b/test/unit/message_test.py index 22e0b53a..6f4f7e27 100644 --- a/test/unit/message_test.py +++ b/test/unit/message_test.py @@ -95,3 +95,28 @@ def test_validate_message_size_raises_40009_above_the_limit(): assert excinfo.value.code == 40009 assert excinfo.value.status_code == 400 + + +# RSL5: a message without data on an encrypted channel has nothing to encrypt +def test_encrypt_message_without_data_is_a_noop(): + from ably.pubsub.server import generate_random_key, get_default_params + + cipher = get_default_params({'key': generate_random_key()}) + message = Message(name='ping') + + message.encrypt(cipher) + + assert message.data is None + assert message.as_dict() == {'name': 'ping'} + + +def test_encrypt_presence_message_without_data_is_a_noop(): + from ably.pubsub.server import generate_random_key, get_default_params + from ably.pubsub.types.presence import PresenceMessage + + cipher = get_default_params({'key': generate_random_key()}) + message = PresenceMessage(action=1, client_id='c') + + message.encrypt(cipher) + + assert message.data is None