From fe39fcdb12c12df0da07d7b00bda88eeeb68a37e Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Thu, 1 Oct 2026 14:39:51 -0400 Subject: [PATCH] gRPC: require group send token for unidentified individual messages to PNIs --- .../grpc/MessagesAnonymousGrpcService.java | 15 ++--- .../main/proto/org/signal/chat/messages.proto | 1 + .../MessagesAnonymousGrpcServiceTest.java | 59 +++++++++++++++++-- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcService.java index 69d6b33ab..bf5c44dfe 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcService.java @@ -86,14 +86,15 @@ public class MessagesAnonymousGrpcService extends SimpleMessagesAnonymousGrpc.Me final ServiceIdentifier destinationServiceIdentifier = GrpcServiceIdentifierUtil.fromGrpcServiceIdentifier(request.getDestination()); + if (destinationServiceIdentifier.identityType() == IdentityType.PNI + && request.getAuthorizationCase() != SendSealedSenderMessageRequest.AuthorizationCase.GROUP_SEND_TOKEN) { + throw GrpcExceptions.fieldViolation("authorization", "group send token is required for PNI destinations"); + } + final Optional maybeDestination = accountsManager.getByServiceIdentifier(destinationServiceIdentifier); final boolean authorized = switch (request.getAuthorizationCase()) { case UNIDENTIFIED_ACCESS_KEY -> { - if (destinationServiceIdentifier.identityType() == IdentityType.PNI) { - throw GrpcExceptions.fieldViolation("authorization", - "message for PNI cannot be authenticated with an unidentified access token"); - } final byte[] uak = request.getUnidentifiedAccessKey().toByteArray(); yield maybeDestination .map(account -> UnidentifiedAccessUtil.checkUnidentifiedAccess(account, uak)) @@ -104,7 +105,7 @@ public class MessagesAnonymousGrpcService extends SimpleMessagesAnonymousGrpc.Me case GROUP_SEND_TOKEN -> groupSendTokenUtil.checkGroupSendToken(request.getGroupSendToken(), destinationServiceIdentifier); case UNRESTRICTED_ACCESS -> - maybeDestination.map(account -> account.isUnrestrictedUnidentifiedAccess()).orElse(false); + maybeDestination.map(Account::isUnrestrictedUnidentifiedAccess).orElse(false); case AUTHORIZATION_NOT_SET -> throw GrpcExceptions.fieldViolation("authorization", "expected authorization token not provided"); }; @@ -118,7 +119,7 @@ public class MessagesAnonymousGrpcService extends SimpleMessagesAnonymousGrpc.Me if (maybeDestination.isEmpty()) { return SendMessageResponse.newBuilder().setDestinationNotFound(NotFound.getDefaultInstance()).build(); } - + return sendIndividualMessage(maybeDestination.get(), destinationServiceIdentifier, request.getMessages(), @@ -168,7 +169,7 @@ public class MessagesAnonymousGrpcService extends SimpleMessagesAnonymousGrpc.Me destinationServiceIdentifier); spamCheckResult.response().ifPresent(grpcResponse -> - grpcResponse.throwStatusOr(_ -> GrpcExceptions.rateLimitExceeded(null))); + grpcResponse.throwStatusOr(_ -> GrpcExceptions.rateLimitExceeded(null))); try { final int totalPayloadLength = messages.getMessagesMap().values().stream() diff --git a/service/src/main/proto/org/signal/chat/messages.proto b/service/src/main/proto/org/signal/chat/messages.proto index 1d580e6fc..3af7f3364 100644 --- a/service/src/main/proto/org/signal/chat/messages.proto +++ b/service/src/main/proto/org/signal/chat/messages.proto @@ -245,6 +245,7 @@ message SendSyncMessageRequest { message SendSealedSenderMessageRequest { // The service identifier of the account to which to deliver the message. + // If `destination` is a PNI, `authorization` must be a group send token. common.ServiceIdentifier destination = 1; // If true, this message will only be delivered to destination devices that diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcServiceTest.java index 126166752..e8be66619 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessagesAnonymousGrpcServiceTest.java @@ -41,13 +41,13 @@ import org.mockito.Mock; import org.signal.chat.errors.FailedUnidentifiedAuthorization; import org.signal.chat.messages.ChallengeRequired; import org.signal.chat.messages.IndividualRecipientMessageBundle; -import org.signal.chat.messages.SendMessageType; import org.signal.chat.messages.MessagesAnonymousGrpc; import org.signal.chat.messages.MismatchedDevices; import org.signal.chat.messages.MultiRecipientMessage; import org.signal.chat.messages.MultiRecipientMismatchedDevices; import org.signal.chat.messages.MultiRecipientSuccess; import org.signal.chat.messages.SendMessageResponse; +import org.signal.chat.messages.SendMessageType; import org.signal.chat.messages.SendMultiRecipientMessageRequest; import org.signal.chat.messages.SendMultiRecipientMessageResponse; import org.signal.chat.messages.SendMultiRecipientStoryRequest; @@ -60,7 +60,6 @@ import org.whispersystems.textsecuregcm.controllers.MultiRecipientMismatchedDevi import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.entities.MessageProtos; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.limits.CardinalityEstimator; @@ -251,7 +250,7 @@ class MessagesAnonymousGrpcServiceTest extends generateRequest(serviceIdentifier, false, true, messages, UNIDENTIFIED_ACCESS_KEY, null))); verifyNoInteractions(messageSender); } - + @CartesianTest void sendUnrestrictedAccessMessage( @CartesianTest.Values(booleans = {true, false}) final boolean useUak, @@ -390,8 +389,9 @@ class MessagesAnonymousGrpcServiceTest extends verify(messageSender, never()).sendMessages(any(), any(), any(), any(), any(), any()); } - @Test - void pniIdentifierWithUak() { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void pniIdentifierWithoutGroupSendToken(final boolean useUak) { final byte deviceId = Device.PRIMARY_ID; final int registrationId = 7; final Device destinationDevice = DevicesHelper.createDevice(deviceId, CLOCK.millis(), registrationId); @@ -399,6 +399,7 @@ class MessagesAnonymousGrpcServiceTest extends final Account destinationAccount = mock(Account.class); when(destinationAccount.getDevices()).thenReturn(List.of(destinationDevice)); when(destinationAccount.getDevice(deviceId)).thenReturn(Optional.of(destinationDevice)); + when(destinationAccount.isUnrestrictedUnidentifiedAccess()).thenReturn(true); when(destinationAccount.getUnidentifiedAccessKey()).thenReturn(Optional.of(UNIDENTIFIED_ACCESS_KEY)); final PniServiceIdentifier pniIdentifier = new PniServiceIdentifier(UUID.randomUUID()); @@ -411,11 +412,57 @@ class MessagesAnonymousGrpcServiceTest extends .build()); final SendSealedSenderMessageRequest request = - generateRequest(pniIdentifier, false, true, messages, UNIDENTIFIED_ACCESS_KEY, null); + generateRequest(pniIdentifier, false, true, messages, useUak ? UNIDENTIFIED_ACCESS_KEY : null, null); GrpcTestUtils.assertStatusException( Status.INVALID_ARGUMENT, () -> unauthenticatedServiceStub().sendSingleRecipientMessage(request)); + verifyNoInteractions(messageSender); + } + + @Test + void pniIdentifierWithGroupSendToken() + throws MessageTooLargeException, MismatchedDevicesException, MessageDeliveryNotAllowedException { + final byte deviceId = Device.PRIMARY_ID; + final int registrationId = 7; + final Device destinationDevice = DevicesHelper.createDevice(deviceId, CLOCK.millis(), registrationId); + + final Account destinationAccount = mock(Account.class); + when(destinationAccount.getDevices()).thenReturn(List.of(destinationDevice)); + when(destinationAccount.getDevice(deviceId)).thenReturn(Optional.of(destinationDevice)); + + final PniServiceIdentifier pniIdentifier = new PniServiceIdentifier(UUID.randomUUID()); + when(accountsManager.getByServiceIdentifier(pniIdentifier)).thenReturn(Optional.of(destinationAccount)); + + final byte[] payload = TestRandomUtil.nextBytes(128); + final Map messages = + Map.of(deviceId, IndividualRecipientMessageBundle.Message.newBuilder() + .setRegistrationId(registrationId) + .setPayload(ByteString.copyFrom(payload)) + .setType(SendMessageType.UNIDENTIFIED_SENDER) + .build()); + + final SendMessageResponse response = unauthenticatedServiceStub().sendSingleRecipientMessage( + generateRequest(pniIdentifier, false, true, messages, null, GROUP_SEND_TOKEN)); + + assertEquals(SendMessageResponse.newBuilder().setSuccess(Empty.getDefaultInstance()).build(), response); + + final MessageProtos.Envelope expectedEnvelope = MessageProtos.Envelope.newBuilder() + .setType(MessageProtos.Envelope.Type.UNIDENTIFIED_SENDER) + .setDestinationServiceId(pniIdentifier.toCompactByteString()) + .setClientTimestamp(CLOCK.millis()) + .setServerTimestamp(CLOCK.millis()) + .setEphemeral(false) + .setUrgent(true) + .setContent(ByteString.copyFrom(payload)) + .build(); + + verify(messageSender).sendMessages(destinationAccount, + pniIdentifier, + Map.of(deviceId, expectedEnvelope), + Map.of(deviceId, registrationId), + Optional.empty(), + null); } @Test