Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ably/pubsub/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
ttypic marked this conversation as resolved.
Resolved

if isinstance(self.data, CipherData):
return

Expand Down
4 changes: 4 additions & 0 deletions ably/pubsub/types/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
ttypic marked this conversation as resolved.
Resolved

if isinstance(self.data, CipherData):
return

Expand Down
25 changes: 25 additions & 0 deletions test/unit/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading