From e003df79d8928c156f85190eec6e0b29781dbbb4 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 3 Sep 2020 15:20:16 +1000 Subject: [PATCH 1/4] Fix session reset bug --- .../securesms/loki/protocol/SessionManagementProtocol.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt index 4e986fcf59..073af39aff 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt @@ -28,7 +28,6 @@ object SessionManagementProtocol { val recipient = recipient(context, publicKey) if (recipient.isGroupRecipient) { return } val lokiThreadDB = DatabaseFactory.getLokiThreadDatabase(context) - if (lokiThreadDB.getSessionResetStatus(publicKey) != SessionResetStatus.NONE) { return } val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient) val devices = lokiThreadDB.getSessionRestoreDevices(threadID) for (device in devices) { From a0c903a3d3385df4913d2e4cf8bbe68adffbfc8a Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 4 Sep 2020 09:06:18 +1000 Subject: [PATCH 2/4] Clean up error handling --- .../securesms/jobs/PushDecryptJob.java | 28 ++++++++++++------- .../loki/protocol/SessionMetaProtocol.kt | 4 +-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 69ce6a08a6..607a6597d6 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1085,7 +1085,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { @NonNull Optional smsMessageId, @NonNull Throwable e) { SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context); - if (SessionMetaProtocol.shouldErrorMessageShow(context, timestamp)) { + if (!SessionMetaProtocol.shouldIgnoreDecryptionException(context, timestamp)) { if (!smsMessageId.isPresent()) { Optional insertResult = insertPlaceholder(sender, senderDevice, timestamp); @@ -1098,29 +1098,35 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } } - // FIXME: This is a temporary patch for bad mac issues. At least with this people will be able to message again. We have to figure out the root cause of the issue though. + if (canRecoverAutomatically(e)) { + SessionManagementProtocol.startSessionReset(context, sender); + } else { + SessionManagementProtocol.triggerSessionRestorationUI(context, sender, timestamp); + } + } + + private boolean canRecoverAutomatically(Throwable e) { + // Corrupt message exception if (e.getCause() != null) { Throwable e2 = e.getCause(); if (e2.getCause() != null) { Throwable e3 = e2.getCause(); if (e3 instanceof InvalidMessageException) { String message = e3.getMessage(); - if (message != null && message.startsWith("Bad Mac!")) { - SessionManagementProtocol.startSessionReset(context, sender); - return; // Don't trigger the session restoration UI - } + return (message != null && message.startsWith("Bad Mac!")); } } } - - SessionManagementProtocol.triggerSessionRestorationUI(context, sender, timestamp); + // Invalid metadata exception + // TODO + return false; } private void handleNoSessionMessage(@NonNull String sender, int senderDevice, long timestamp, @NonNull Optional smsMessageId) { SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context); - if (SessionMetaProtocol.shouldErrorMessageShow(context, timestamp)) { + if (!SessionMetaProtocol.shouldIgnoreDecryptionException(context, timestamp)) { if (!smsMessageId.isPresent()) { Optional insertResult = insertPlaceholder(sender, senderDevice, timestamp); @@ -1132,7 +1138,9 @@ public class PushDecryptJob extends BaseJob implements InjectableType { smsDatabase.markAsNoSession(smsMessageId.get()); } } - SessionManagementProtocol.triggerSessionRestorationUI(context, sender, timestamp); + + // Attempt to recover automatically + ApplicationContext.getInstance(context).sendSessionRequestIfNeeded(sender); } private void handleLegacyMessage(@NonNull String sender, int senderDevice, long timestamp, diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt index c3322f9a08..a7acaa25fe 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt @@ -30,9 +30,9 @@ object SessionMetaProtocol { } @JvmStatic - fun shouldErrorMessageShow(context: Context, timestamp: Long): Boolean { + fun shouldIgnoreDecryptionException(context: Context, timestamp: Long): Boolean { val restorationTimestamp = TextSecurePreferences.getRestorationTime(context) - return timestamp > restorationTimestamp + return timestamp <= restorationTimestamp } @JvmStatic From ff9c88a192352d7877fbb484e0b0b16b140539ba Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 4 Sep 2020 09:09:37 +1000 Subject: [PATCH 3/4] Remove TODO --- src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 607a6597d6..15532460f3 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1117,8 +1117,6 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } } } - // Invalid metadata exception - // TODO return false; } From 5e86b253a831417c7ee67894a37bba9857f1d96d Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 4 Sep 2020 09:42:09 +1000 Subject: [PATCH 4/4] Debug --- src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 15532460f3..5a89e24b54 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -66,6 +66,7 @@ import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.loki.activities.HomeActivity; import org.thoughtcrime.securesms.loki.database.LokiMessageDatabase; +import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase; import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionManagementProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol; @@ -1099,6 +1100,10 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } if (canRecoverAutomatically(e)) { + Recipient recipient = Recipient.from(context, Address.fromSerialized(sender), false); + LokiThreadDatabase threadDB = DatabaseFactory.getLokiThreadDatabase(context); + long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient); + threadDB.addSessionRestoreDevice(threadID, sender); SessionManagementProtocol.startSessionReset(context, sender); } else { SessionManagementProtocol.triggerSessionRestorationUI(context, sender, timestamp);