From f794458cba94650464abef0f287db8ef6f881c9b Mon Sep 17 00:00:00 2001 From: SessionHero01 <180888785+SessionHero01@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:36:58 +1100 Subject: [PATCH] Kick handling --- .../messenger/libsession_util/util/Sodium.kt | 2 - .../groups/RemoveGroupMemberHandler.kt | 3 +- .../pollers/ClosedGroupPoller.kt | 37 +++++++++++-------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt index f2a6cd02e2..a7f7e6f1c2 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt @@ -1,11 +1,9 @@ package network.loki.messenger.libsession_util.util -import java.util.regex.Pattern object Sodium { const val KICKED_DOMAIN = "SessionGroupKickedMessage" - val KICKED_REGEX: Pattern = Pattern.compile("^(05[a-zA-Z0-9]{64})(\\d+)$") init { System.loadLibrary("session_util") diff --git a/libsession/src/main/java/org/session/libsession/messaging/groups/RemoveGroupMemberHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/groups/RemoveGroupMemberHandler.kt index 74b6b62779..601448a2f8 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/groups/RemoveGroupMemberHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/groups/RemoveGroupMemberHandler.kt @@ -216,7 +216,8 @@ class RemoveGroupMemberHandler @Inject constructor( data = Base64.encodeBytes( Sodium.encryptForMultipleSimple( messages = Array(pendingRemovals.size) { - "${pendingRemovals[it].sessionId}${keys.currentGeneration()}".encodeToByteArray() + AccountId(pendingRemovals[it].sessionId).pubKeyBytes + .plus(keys.currentGeneration().toString().toByteArray()) }, recipients = Array(pendingRemovals.size) { AccountId(pendingRemovals[it].sessionId).pubKeyBytes diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt index c6f181591f..42cd01ba18 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt @@ -29,6 +29,7 @@ import org.session.libsession.utilities.getClosedGroup import org.session.libsignal.database.LokiAPIDatabaseProtocol import org.session.libsignal.exceptions.NonRetryableException import org.session.libsignal.utilities.AccountId +import org.session.libsignal.utilities.IdPrefix import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Namespace import org.session.libsignal.utilities.Snode @@ -299,23 +300,29 @@ class ClosedGroupPoller( ) if (decoded != null) { - val message = decoded.decodeToString() - val matcher = Sodium.KICKED_REGEX.matcher(message) - if (matcher.matches()) { - val sessionId = matcher.group(1) - val messageGeneration = matcher.group(2)!!.toInt() - val currentKeysGeneration = configFactoryProtocol.withGroupConfigs(closedGroupSessionId) { - it.groupKeys.currentGeneration() - } + // The message should be in the format of "", + // where the pub key is 32 bytes, so we need to have at least 33 bytes of data + if (decoded.size < 33) { + Log.w(TAG, "Received an invalid kicked message, expecting at least 33 bytes, got ${decoded.size}") + return@forEach + } - val isForMe = sessionId == storage.getUserPublicKey() - Log.d(TAG, "Received kicked message, for us? ${sessionId == storage.getUserPublicKey()}, message key generation = $messageGeneration, our key generation = $currentKeysGeneration") + val sessionId = AccountId(IdPrefix.STANDARD, decoded.copyOfRange(0, 32)) + val messageGeneration = decoded.copyOfRange(32, decoded.size).decodeToString().toIntOrNull() + if (messageGeneration == null) { + Log.w(TAG, "Received an invalid kicked message: missing message generation") + return@forEach + } - if (isForMe && messageGeneration >= currentKeysGeneration) { - groupManagerV2.handleKicked(closedGroupSessionId) - } - } else { - Log.w(TAG, "Received an invalid kicked message") + val currentKeysGeneration = configFactoryProtocol.withGroupConfigs(closedGroupSessionId) { + it.groupKeys.currentGeneration() + } + + val isForMe = sessionId.hexString == storage.getUserPublicKey() + Log.d(TAG, "Received kicked message, for us? ${isForMe}, message key generation = $messageGeneration, our key generation = $currentKeysGeneration") + + if (isForMe && messageGeneration >= currentKeysGeneration) { + groupManagerV2.handleKicked(closedGroupSessionId) } } }