From ea38376c3aa07b81d3a7d0286c7eaba648b84e2f Mon Sep 17 00:00:00 2001 From: Michelle Tang Date: Fri, 8 Nov 2024 11:02:28 -0500 Subject: [PATCH] Fix extraneous incremental chunk sizes. --- .../securesms/database/AttachmentTable.kt | 27 ++++++++++++++----- .../helpers/SignalDatabaseMigrations.kt | 6 +++-- .../V256_FixIncrementalDigestColumns.kt | 26 ++++++++++++++++++ .../push/http/DigestingRequestBody.kt | 8 +++--- 4 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V256_FixIncrementalDigestColumns.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt index 85cc940c44..4d780c3003 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt @@ -2236,8 +2236,6 @@ class AttachmentTable( put(CDN_NUMBER, attachment.cdn.serialize()) put(REMOTE_LOCATION, attachment.remoteLocation) put(REMOTE_DIGEST, attachment.remoteDigest) - put(REMOTE_INCREMENTAL_DIGEST, attachment.incrementalDigest) - put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, attachment.incrementalMacChunkSize) put(REMOTE_KEY, attachment.remoteKey) put(FILE_NAME, StorageUtil.getCleanFileName(attachment.fileName)) put(DATA_SIZE, attachment.size) @@ -2259,6 +2257,13 @@ class AttachmentTable( put(STICKER_ID, sticker.stickerId) put(STICKER_EMOJI, sticker.emoji) } + + if (attachment.incrementalDigest?.isNotEmpty() == true && attachment.incrementalMacChunkSize != 0) { + put(REMOTE_INCREMENTAL_DIGEST, attachment.incrementalDigest) + put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, attachment.incrementalMacChunkSize) + } else { + putNull(REMOTE_INCREMENTAL_DIGEST) + } } val rowId = db.insert(TABLE_NAME, null, contentValues) @@ -2288,8 +2293,6 @@ class AttachmentTable( put(CDN_NUMBER, attachment.cdn.serialize()) put(REMOTE_LOCATION, attachment.remoteLocation) put(REMOTE_DIGEST, attachment.remoteDigest) - put(REMOTE_INCREMENTAL_DIGEST, attachment.incrementalDigest) - put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, attachment.incrementalMacChunkSize) put(REMOTE_KEY, attachment.remoteKey) put(FILE_NAME, StorageUtil.getCleanFileName(attachment.fileName)) put(DATA_SIZE, attachment.size) @@ -2317,6 +2320,13 @@ class AttachmentTable( put(STICKER_ID, sticker.stickerId) put(STICKER_EMOJI, sticker.emoji) } + + if (attachment.incrementalDigest?.isNotEmpty() == true && attachment.incrementalMacChunkSize != 0) { + put(REMOTE_INCREMENTAL_DIGEST, attachment.incrementalDigest) + put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, attachment.incrementalMacChunkSize) + } else { + putNull(REMOTE_INCREMENTAL_DIGEST) + } } val rowId = db.insert(TABLE_NAME, null, contentValues) @@ -2437,8 +2447,6 @@ class AttachmentTable( contentValues.put(CDN_NUMBER, uploadTemplate?.cdn?.serialize() ?: Cdn.CDN_0.serialize()) contentValues.put(REMOTE_LOCATION, uploadTemplate?.remoteLocation) contentValues.put(REMOTE_DIGEST, uploadTemplate?.remoteDigest) - contentValues.put(REMOTE_INCREMENTAL_DIGEST, uploadTemplate?.incrementalDigest) - contentValues.put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, uploadTemplate?.incrementalMacChunkSize ?: 0) contentValues.put(REMOTE_KEY, uploadTemplate?.remoteKey) contentValues.put(REMOTE_IV, uploadTemplate?.remoteIv) contentValues.put(FILE_NAME, StorageUtil.getCleanFileName(attachment.fileName)) @@ -2454,6 +2462,13 @@ class AttachmentTable( contentValues.put(TRANSFORM_PROPERTIES, transformProperties.serialize()) contentValues.put(ATTACHMENT_UUID, attachment.uuid?.toString()) + if (uploadTemplate?.incrementalDigest?.isNotEmpty() == true && uploadTemplate.incrementalMacChunkSize != 0) { + contentValues.put(REMOTE_INCREMENTAL_DIGEST, uploadTemplate.incrementalDigest) + contentValues.put(REMOTE_INCREMENTAL_DIGEST_CHUNK_SIZE, uploadTemplate.incrementalMacChunkSize) + } else { + contentValues.putNull(REMOTE_INCREMENTAL_DIGEST) + } + if (attachment.transformProperties?.videoTrimStartTimeUs != 0L) { contentValues.putNull(BLUR_HASH) } else { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt index d7e30a8f25..963d34530d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt @@ -112,6 +112,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V252_AttachmentOffl import org.thoughtcrime.securesms.database.helpers.migration.V253_CreateChatFolderTables import org.thoughtcrime.securesms.database.helpers.migration.V254_AddChatFolderConstraint import org.thoughtcrime.securesms.database.helpers.migration.V255_AddCallTableLogIndex +import org.thoughtcrime.securesms.database.helpers.migration.V256_FixIncrementalDigestColumns /** * Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness. @@ -226,10 +227,11 @@ object SignalDatabaseMigrations { 252 to V252_AttachmentOffloadRestoredAtColumn, 253 to V253_CreateChatFolderTables, 254 to V254_AddChatFolderConstraint, - 255 to V255_AddCallTableLogIndex + 255 to V255_AddCallTableLogIndex, + 256 to V256_FixIncrementalDigestColumns ) - const val DATABASE_VERSION = 255 + const val DATABASE_VERSION = 256 @JvmStatic fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V256_FixIncrementalDigestColumns.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V256_FixIncrementalDigestColumns.kt new file mode 100644 index 0000000000..f4e16779c6 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V256_FixIncrementalDigestColumns.kt @@ -0,0 +1,26 @@ +package org.thoughtcrime.securesms.database.helpers.migration + +import android.app.Application +import net.zetetic.database.sqlcipher.SQLiteDatabase + +/** + * Fixes a bug where sometimes incremental chunk size would be set when the attachment was not actually incremental + * Clears out any cases where only one of the incremental_digest / incremental_size fields were previously set + */ +@Suppress("ClassName") +object V256_FixIncrementalDigestColumns : SignalDatabaseMigration { + override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL( + """ + UPDATE attachment + SET + remote_incremental_digest = NULL, + remote_incremental_digest_chunk_size = 0 + WHERE + remote_incremental_digest IS NULL OR + LENGTH(remote_incremental_digest) = 0 OR + remote_incremental_digest_chunk_size = 0 + """ + ) + } +} diff --git a/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/push/http/DigestingRequestBody.kt b/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/push/http/DigestingRequestBody.kt index 85cf552d30..99c957e85c 100644 --- a/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/push/http/DigestingRequestBody.kt +++ b/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/push/http/DigestingRequestBody.kt @@ -63,7 +63,7 @@ class DigestingRequestBody( outputStream.flush() - val incrementalDigest: ByteArray = if (isIncremental) { + val incrementalDigest: ByteArray? = if (isIncremental) { if (contentLength != outputStream.totalBytesWritten) { Log.w(TAG, "Content uploaded ${logMessage(outputStream.totalBytesWritten, contentLength)} bytes compared to expected!") } else { @@ -73,10 +73,12 @@ class DigestingRequestBody( digestStream.close() digestStream.toByteArray() } else { - ByteArray(0) + null } - attachmentDigest = AttachmentDigest(outputStream.transmittedDigest, incrementalDigest, sizeChoice.sizeInBytes) + val incrementalDigestChunkSize: Int = if (incrementalDigest?.isNotEmpty() == true) sizeChoice.sizeInBytes else 0 + + attachmentDigest = AttachmentDigest(outputStream.transmittedDigest, incrementalDigest, incrementalDigestChunkSize) } override fun contentLength(): Long {