diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcService.java index 0d491ca2b..82cc712d0 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcService.java @@ -21,6 +21,7 @@ import java.util.stream.Collectors; import org.signal.chat.common.S3UploadForm; import org.signal.chat.errors.FailedPrecondition; import org.signal.chat.errors.NotFound; +import org.signal.chat.profile.ExpectedVersionConflict; import org.signal.chat.profile.GetAvatarCredentialRequest; import org.signal.chat.profile.GetAvatarCredentialResponse; import org.signal.chat.profile.GetProfileRequest; @@ -115,9 +116,11 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase { final boolean currentVersionMatchesExpected = Arrays.equals(account.getCurrentProfileVersion().orElse(new byte[0]), expectedCurrentVersion); if (!currentVersionMatchesExpected) { - return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(FailedPrecondition.newBuilder() - .setDescription("current and expected profile versions must match") - .build()).build(); + final ExpectedVersionConflict conflict = account.getCurrentProfileVersion() + .map(v -> ExpectedVersionConflict.newBuilder().setCurrentProfileVersion(ByteString.copyFrom(v)).build()) + .orElseGet(ExpectedVersionConflict::getDefaultInstance); + return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(conflict) + .build(); } final byte[] version = request.getVersion().toByteArray(); @@ -190,12 +193,13 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase { a.setBadges(clock, updatedBadges); }); - } catch (final WriteConflictException _) { - return SetProfileResponse.newBuilder() - .setExpectedVersionWriteConflict(FailedPrecondition.newBuilder() - .setDescription("current and expected version mismatch") - .build()) - .build(); + } catch (final WriteConflictException e) { + + final ExpectedVersionConflict conflict = e.getConflictedValue() + .map(v -> ExpectedVersionConflict.newBuilder().setCurrentProfileVersion(ByteString.copyFrom(v)).build()) + .orElseGet(ExpectedVersionConflict::getDefaultInstance); + + return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(conflict).build(); } if (request.getV1Request().getAvatarChange() != AvatarChange.AVATAR_CHANGE_UNCHANGED && avatarData.currentAvatar().isPresent()) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index ff34ecd75..c099e540f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -1345,7 +1345,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen }); if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> Arrays.equals(v, newVersion)).orElse(false)) { - throw new WriteConflictException(); + throw new WriteConflictException(maybeUpdatedAccount.getCurrentProfileVersion().orElse(null)); } return maybeUpdatedAccount; diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java index 922bc3b76..fb77814a4 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java @@ -128,7 +128,7 @@ public class IssuedReceiptsManager { try { dynamoDbClient.updateItem(updateItemRequest); } catch (final ConditionalCheckFailedException _) { - throw new WriteConflictException(); + throw new WriteConflictException(null); } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/ProfilesV2.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/ProfilesV2.java index 88dbe0581..f0d3c5f18 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/ProfilesV2.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/ProfilesV2.java @@ -162,7 +162,7 @@ public class ProfilesV2 { if (cancellationCode.isPresent()) { // any other conditional check failure is an expected data hash mismatch, which is a write conflict Metrics.counter(WRITE_CONFLICTS_COUNTER_NAME, "code", cancellationCode.get()).increment(); - throw new WriteConflictException(); + throw new WriteConflictException(null); } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/WriteConflictException.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/WriteConflictException.java index 1d7b97dea..0f221768d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/WriteConflictException.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/WriteConflictException.java @@ -5,7 +5,21 @@ package org.whispersystems.textsecuregcm.storage; +import javax.annotation.Nullable; import org.whispersystems.textsecuregcm.util.NoStackTraceException; +import java.util.Optional; public class WriteConflictException extends NoStackTraceException { + + @Nullable + private final byte[] conflictedValue; + + public WriteConflictException(@Nullable final byte[] conflictedValue) { + this.conflictedValue = conflictedValue; + } + + /// The conflicted key, if available and relevant + public Optional getConflictedValue() { + return Optional.ofNullable(conflictedValue); + } } diff --git a/service/src/main/proto/org/signal/chat/profile.proto b/service/src/main/proto/org/signal/chat/profile.proto index 7b35a8474..bc5f1a5ed 100644 --- a/service/src/main/proto/org/signal/chat/profile.proto +++ b/service/src/main/proto/org/signal/chat/profile.proto @@ -175,6 +175,13 @@ message SetProfileResult { // next: 1 } +message ExpectedVersionConflict { + // In certain, highly unlikely, cases, a new profile version might not be + // distributed among all devices. These devices wouldn't be able to decrypt the + // profile, but the value can be used to set a new version. + bytes current_profile_version = 2; +} + message SetProfileResponse { oneof response { SetProfileResult result = 1; @@ -187,7 +194,7 @@ message SetProfileResponse { PaymentsForbiddenInRegion payments_forbidden_in_region = 3 [(tag.reason) = "payments_forbidden_in_region"]; // The current version did not match the request's expectation, indicating // another device on the account may have created a new version the caller does not know about. - errors.FailedPrecondition expected_version_write_conflict = 4 [(tag.reason) = "expected_version_write_conflict"]; + ExpectedVersionConflict expected_version_write_conflict = 4 [(tag.reason) = "expected_version_write_conflict"]; // next: 5 } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcServiceTest.java index 430266d69..35cf8dc2a 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcServiceTest.java @@ -6,6 +6,7 @@ package org.whispersystems.textsecuregcm.grpc; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -339,7 +340,9 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));