Merge branch 'decrypt-heartbeats' into 'main'

Don't fail on decrypting heartbeats

See merge request poezio/slixmpp-omemo!12
This commit is contained in:
Maxime Buquet 2021-12-15 01:03:15 +01:00
commit aa91f43aa0
2 changed files with 29 additions and 12 deletions

View file

@ -150,6 +150,11 @@ class EchoBot(ClientXMPP):
try: try:
encrypted = msg['omemo_encrypted'] encrypted = msg['omemo_encrypted']
body = self['xep_0384'].decrypt_message(encrypted, mfrom, allow_untrusted) body = self['xep_0384'].decrypt_message(encrypted, mfrom, allow_untrusted)
# decrypt_message returns Optional[str]. It is possible to get
# body-less OMEMO message (see KeyTransportMessages), currently
# used for example to send heartbeats to other devices.
if body is None:
return None
decoded = body.decode('utf8') decoded = body.decode('utf8')
if self.is_command(decoded): if self.is_command(decoded):
await self.handle_command(mto, mtype, decoded) await self.handle_command(mto, mtype, decoded)

View file

@ -650,9 +650,10 @@ class XEP_0384(BasePlugin):
allow_untrusted: bool = False, allow_untrusted: bool = False,
) -> Optional[str]: ) -> Optional[str]:
header = encrypted['header'] header = encrypted['header']
if encrypted['payload']['value'] is None:
raise ErroneousPayload('The payload element was empty') payload = None
payload = b64dec(encrypted['payload']['value']) if encrypted['payload']['value'] is not None:
payload = b64dec(encrypted['payload']['value'])
jid = sender.bare jid = sender.bare
sid = int(header['sid']) sid = int(header['sid'])
@ -674,15 +675,26 @@ class XEP_0384(BasePlugin):
# XXX: 'cipher' is part of KeyTransportMessages and is used when no payload # XXX: 'cipher' is part of KeyTransportMessages and is used when no payload
# is passed. We do not implement this yet. # is passed. We do not implement this yet.
try: try:
body = self._omemo.decryptMessage( if payload is None:
jid, self._omemo.decryptRatchetFowardingMessage(
sid, jid,
iv, sid,
message, iv,
isPrekeyMessage, message,
payload, isPrekeyMessage,
allow_untrusted=allow_untrusted, allow_untrusted=allow_untrusted,
) )
body = None
else:
body = self._omemo.decryptMessage(
jid,
sid,
iv,
message,
isPrekeyMessage,
payload,
allow_untrusted=allow_untrusted,
)
except (omemo.exceptions.NoSessionException,): except (omemo.exceptions.NoSessionException,):
# This might happen when the sender is sending using a session # This might happen when the sender is sending using a session
# that we don't know about (deleted session storage, etc.). In # that we don't know about (deleted session storage, etc.). In