Skip to content

Commit 0c6db32

Browse files
owenpearsonclaude
andauthored
fix: deliver messages whose base64 payload cannot be decoded
`base64.b64decode` raises on a payload that is not valid base64, and the exception escaped `Message.from_encoded` rather than being reported. RSL6b asks for the failure to be logged and the message delivered with the encodings that were not applied, which is what the missing-cipher and unsupported-encoding branches alongside it already do. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent cd5fa75 commit 0c6db32

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎ably/types/mixins.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import binascii
23
import json
34
import logging
45

@@ -67,8 +68,14 @@ def decode(data, encoding='', cipher=None, context=None):
6768
continue
6869
data = json.loads(data)
6970
elif encoding == 'base64':
70-
data = bytearray(base64.b64decode(data)) if isinstance(data, bytes) \
71-
else bytearray(base64.b64decode(data.encode('utf-8')))
71+
try:
72+
data = bytearray(base64.b64decode(data)) if isinstance(data, bytes) \
73+
else bytearray(base64.b64decode(data.encode('utf-8')))
74+
except (binascii.Error, UnicodeEncodeError) as e:
75+
log.error('Message cannot be decoded. '
76+
f'Invalid base64 payload: {e}')
77+
encoding_list.append(encoding)
78+
break
7279
if not encoding_list:
7380
last_payload = data
7481
elif encoding == ENC_VCDIFF:

‎test/unit/message_test.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ def test_update_inner_message_fields_for_presence_msg_tm2():
4545
assert presence_msg.get('connectionId') == 'custom_connection_id'
4646
assert presence_msg.get('timestamp') == 23134
4747
msg_index = msg_index + 1
48+
49+
50+
# RSL6b
51+
def test_invalid_base64_data_is_delivered_with_remaining_encoding():
52+
message = ably.types.message.Message.from_encoded({
53+
'name': 'event',
54+
'data': 'encrypted-data-here',
55+
'encoding': 'custom-encryption/base64',
56+
})
57+
58+
assert message.data == 'encrypted-data-here'
59+
assert message.encoding == 'custom-encryption/base64'

0 commit comments

Comments
 (0)