Skip to content

Commit

Permalink
fix: Sending of message in empty MLS group
Browse files Browse the repository at this point in the history
  • Loading branch information
borichellow committed Dec 13, 2024
1 parent c9f091b commit 40e523e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ internal class MLSConversationDataSource(
val keyPackages = result.successfullyFetchedKeyPackages
val clientKeyPackageList = keyPackages.map { it.keyPackage.decodeBase64Bytes() }
wrapMLSRequest {
if (userIdList.isEmpty()) {
if (clientKeyPackageList.isEmpty()) {
// We are creating a group with only our self client which technically
// doesn't need be added with a commit, but our backend API requires one,
// so we create a commit by updating our key material.
Expand Down Expand Up @@ -566,6 +566,7 @@ internal class MLSConversationDataSource(

keys.flatMap { externalSenders ->
establishMLSGroup(
mlsClient = mlsClient,
groupID = groupID,
members = members,
externalSenders = externalSenders,
Expand All @@ -583,6 +584,7 @@ internal class MLSConversationDataSource(
conversationDAO.getMLSGroupIdByConversationId(parentId.toDao())?.let { parentGroupId ->
val externalSenderKey = mlsClient.getExternalSenders(GroupID(parentGroupId).toCrypto())
establishMLSGroup(
mlsClient = mlsClient,
groupID = groupID,
members = emptyList(),
externalSenders = externalSenderKey.value,
Expand All @@ -593,45 +595,44 @@ internal class MLSConversationDataSource(
}

private suspend fun establishMLSGroup(
mlsClient: MLSClient,
groupID: GroupID,
members: List<UserId>,
externalSenders: ByteArray,
allowPartialMemberList: Boolean = false,
): Either<CoreFailure, MLSAdditionResult> = withContext(serialDispatcher) {
kaliumLogger.d("establish MLS group: $groupID")
mlsClientProvider.getMLSClient().flatMap { mlsClient ->
wrapMLSRequest {
mlsClient.createConversation(
idMapper.toCryptoModel(groupID),
externalSenders
)
}.flatMapLeft {
if (it is MLSFailure.ConversationAlreadyExists) {
Either.Right(Unit)
} else {
Either.Left(it)
}
}.flatMap {
internalAddMemberToMLSGroup(
groupID = groupID,
userIdList = members,
retryOnStaleMessage = false,
allowPartialMemberList = allowPartialMemberList,
cipherSuite = CipherSuite.fromTag(mlsClient.getDefaultCipherSuite())
).onFailure {
wrapMLSRequest {
mlsClient.wipeConversation(groupID.toCrypto())
}
wrapMLSRequest {
mlsClient.createConversation(
idMapper.toCryptoModel(groupID),
externalSenders
)
}.flatMapLeft {
if (it is MLSFailure.ConversationAlreadyExists) {
Either.Right(Unit)
} else {
Either.Left(it)
}
}.flatMap {
internalAddMemberToMLSGroup(
groupID = groupID,
userIdList = members,
retryOnStaleMessage = false,
allowPartialMemberList = allowPartialMemberList,
cipherSuite = CipherSuite.fromTag(mlsClient.getDefaultCipherSuite())
).onFailure {
wrapMLSRequest {
mlsClient.wipeConversation(groupID.toCrypto())
}
}.flatMap { additionResult ->
wrapStorageRequest {
conversationDAO.updateMlsGroupStateAndCipherSuite(
ConversationEntity.GroupState.ESTABLISHED,
ConversationEntity.CipherSuite.fromTag(mlsClient.getDefaultCipherSuite().toInt()),
idMapper.toGroupIDEntity(groupID)
)
}.map { additionResult }
}
}.flatMap { additionResult ->
wrapStorageRequest {
conversationDAO.updateMlsGroupStateAndCipherSuite(
ConversationEntity.GroupState.ESTABLISHED,
ConversationEntity.CipherSuite.fromTag(mlsClient.getDefaultCipherSuite().toInt()),
idMapper.toGroupIDEntity(groupID)
)
}.map { additionResult }
}
}

Expand All @@ -655,9 +656,7 @@ internal class MLSConversationDataSource(
kaliumLogger.w("enrollment for existing client: upload new keypackages and drop old ones")
keyPackageRepository
.replaceKeyPackages(clientId, rotateBundle.newKeyPackages, CipherSuite.fromTag(mlsClient.getDefaultCipherSuite()))
.flatMapLeft {
return E2EIFailure.RotationAndMigration(it).left()
}
.flatMapLeft { E2EIFailure.RotationAndMigration(it).left() }
}
kaliumLogger.w("send migration commits after key rotations")
kaliumLogger.w("rotate bundles: ${rotateBundle.commits.size}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ class MLSConversationRepositoryTest {

val (arrangement, mlsConversationRepository) = Arrangement(testKaliumDispatcher)
.withCommitPendingProposalsReturningNothing()
.withClaimKeyPackagesSuccessful()
.withClaimKeyPackagesSuccessful(emptyList()) // empty cause members is empty in case of establishMLSSubConversationGroup
.withGetMLSClientSuccessful()
.withGetMLSGroupIdByConversationIdReturns(Arrangement.GROUP_ID.value)
.withGetExternalSenderKeySuccessful()
Expand Down

0 comments on commit 40e523e

Please sign in to comment.