Validate closed group update messages

pull/247/head
nielsandriesse 4 years ago
parent 9ca4c83b70
commit 61e0cdd5fe

@ -206,7 +206,7 @@ public final class ClosedGroupsProtocol : NSObject {
public static func handleSharedSenderKeysUpdateIfNeeded(_ dataMessage: SSKProtoDataMessage, from publicKey: String, using transaction: YapDatabaseReadWriteTransaction) {
// Note that `publicKey` is either the public key of the group or the public key of the
// sender, depending on how the message was sent
guard let closedGroupUpdate = dataMessage.closedGroupUpdate else { return }
guard let closedGroupUpdate = dataMessage.closedGroupUpdate, isValid(closedGroupUpdate) else { return }
switch closedGroupUpdate.type {
case .new: handleNewGroupMessage(closedGroupUpdate, using: transaction)
case .info: handleInfoMessage(closedGroupUpdate, from: publicKey, using: transaction)
@ -215,6 +215,17 @@ public final class ClosedGroupsProtocol : NSObject {
}
}
private static func isValid(_ closedGroupUpdate: SSKProtoDataMessageClosedGroupUpdate) -> Bool {
guard !closedGroupUpdate.groupPublicKey.isEmpty else { return false }
switch closedGroupUpdate.type {
case .new: return !(closedGroupUpdate.name ?? "").isEmpty && !(closedGroupUpdate.groupPrivateKey ?? Data()).isEmpty && !closedGroupUpdate.senderKeys.isEmpty
&& !closedGroupUpdate.members.isEmpty && !closedGroupUpdate.admins.isEmpty
case .info: return !(closedGroupUpdate.name ?? "").isEmpty && !closedGroupUpdate.members.isEmpty && !closedGroupUpdate.admins.isEmpty // senderKeys may be empty
case .senderKey: return true
case .senderKeyRequest: return !closedGroupUpdate.senderKeys.isEmpty
}
}
private static func handleNewGroupMessage(_ closedGroupUpdate: SSKProtoDataMessageClosedGroupUpdate, using transaction: YapDatabaseReadWriteTransaction) {
// Unwrap the message
let groupPublicKey = closedGroupUpdate.groupPublicKey.toHexString()

Loading…
Cancel
Save