From f70e41e7cd4a24abf9fa1d990c31fcd715e79d93 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 1 Jun 2020 12:55:30 -0400 Subject: [PATCH] Don't allow account record updates to delete our profile key. --- .../securesms/database/RecipientDatabase.java | 11 ++++++++--- .../securesms/storage/StorageSyncHelper.java | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java index 62cae4deb5..3ed46ffd74 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -648,13 +648,18 @@ public class RecipientDatabase extends Database { public void applyStorageSyncUpdates(@NonNull StorageId storageId, SignalAccountRecord update) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); - ContentValues values = new ContentValues(); - ProfileName profileName = ProfileName.fromParts(update.getGivenName().orNull(), update.getFamilyName().orNull()); + ContentValues values = new ContentValues(); + ProfileName profileName = ProfileName.fromParts(update.getGivenName().orNull(), update.getFamilyName().orNull()); + String profileKey = update.getProfileKey().or(Optional.fromNullable(Recipient.self().getProfileKey())).transform(Base64::encodeBytes).orNull(); + + if (!update.getProfileKey().isPresent()) { + Log.w(TAG, "Got an empty profile key while applying an account record update!"); + } values.put(PROFILE_GIVEN_NAME, profileName.getGivenName()); values.put(PROFILE_FAMILY_NAME, profileName.getFamilyName()); values.put(PROFILE_JOINED_NAME, profileName.toString()); - values.put(PROFILE_KEY, update.getProfileKey().transform(Base64::encodeBytes).orNull()); + values.put(PROFILE_KEY, profileKey); values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(update.getId().getRaw())); values.put(DIRTY, DirtyState.CLEAN.getId()); diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java index c66b218d27..3196224a28 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java @@ -385,7 +385,6 @@ public final class StorageSyncHelper { } public static SignalStorageRecord buildAccountRecord(@NonNull Context context, @NonNull Recipient self) { - SignalAccountRecord account = new SignalAccountRecord.Builder(self.getStorageServiceId()) .setProfileKey(self.getProfileKey()) .setGivenName(self.getProfileName().getGivenName())