Improve source serviceId handling

This commit is contained in:
AsamK 2023-11-21 19:59:06 +01:00
parent 22bab461b5
commit fe2f6bd48f
5 changed files with 33 additions and 33 deletions

View file

@ -32,6 +32,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptM
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage; import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.messages.multidevice.ViewOnceOpenMessage; import org.whispersystems.signalservice.api.messages.multidevice.ViewOnceOpenMessage;
import org.whispersystems.signalservice.api.messages.multidevice.ViewedMessage; import org.whispersystems.signalservice.api.messages.multidevice.ViewedMessage;
import org.whispersystems.signalservice.api.push.ServiceId;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -904,8 +905,9 @@ public record MessageEnvelope(
final AttachmentFileProvider fileProvider, final AttachmentFileProvider fileProvider,
Exception exception Exception exception
) { ) {
final var source = !envelope.isUnidentifiedSender() && envelope.hasSourceServiceId() final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
? recipientResolver.resolveRecipient(envelope.getSourceAddress()) final var source = !envelope.isUnidentifiedSender() && serviceId != null
? recipientResolver.resolveRecipient(serviceId)
: envelope.isUnidentifiedSender() && content != null : envelope.isUnidentifiedSender() && content != null
? recipientResolver.resolveRecipient(content.getSender()) ? recipientResolver.resolveRecipient(content.getSender())
: exception instanceof ProtocolException e : exception instanceof ProtocolException e

View file

@ -131,15 +131,10 @@ public final class IncomingMessageHandler {
final var actions = new ArrayList<HandleAction>(); final var actions = new ArrayList<HandleAction>();
SignalServiceContent content = null; SignalServiceContent content = null;
Exception exception = null; Exception exception = null;
try { envelope.getSourceServiceId().map(ServiceId::parseOrNull)
if (envelope.hasSourceServiceId()) {
// Store uuid if we don't have it already // Store uuid if we don't have it already
// uuid in envelope is sent by server // uuid in envelope is sent by server
account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress()); .ifPresent(serviceId -> account.getRecipientResolver().resolveRecipient(serviceId));
}
} catch (Exception e) {
exception = e;
}
if (!envelope.isReceipt()) { if (!envelope.isReceipt()) {
try { try {
final var cipherResult = dependencies.getCipher() final var cipherResult = dependencies.getCipher()
@ -488,7 +483,7 @@ public final class IncomingMessageHandler {
sender, sender,
destination == null destination == null
? null ? null
: new DeviceAddress(context.getRecipientHelper().resolveRecipient(destination), : new DeviceAddress(account.getRecipientResolver().resolveRecipient(destination),
destination.getServiceId(), destination.getServiceId(),
0), 0),
ignoreAttachments)); ignoreAttachments));
@ -530,7 +525,7 @@ public final class IncomingMessageHandler {
final var blockedListMessage = syncMessage.getBlockedList().get(); final var blockedListMessage = syncMessage.getBlockedList().get();
for (var address : blockedListMessage.getAddresses()) { for (var address : blockedListMessage.getAddresses()) {
context.getContactHelper() context.getContactHelper()
.setContactBlocked(context.getRecipientHelper().resolveRecipient(address), true); .setContactBlocked(account.getRecipientResolver().resolveRecipient(address), true);
} }
for (var groupId : blockedListMessage.getGroupIds() for (var groupId : blockedListMessage.getGroupIds()
.stream() .stream()
@ -654,7 +649,7 @@ public final class IncomingMessageHandler {
if (source == null) { if (source == null) {
return false; return false;
} }
final var recipientId = context.getRecipientHelper().resolveRecipient(source); final var recipientId = account.getRecipientResolver().resolveRecipient(source);
if (context.getContactHelper().isContactBlocked(recipientId)) { if (context.getContactHelper().isContactBlocked(recipientId)) {
return true; return true;
} }
@ -694,7 +689,7 @@ public final class IncomingMessageHandler {
final var message = content.getDataMessage().orElse(null); final var message = content.getDataMessage().orElse(null);
final var recipientId = context.getRecipientHelper().resolveRecipient(source); final var recipientId = account.getRecipientResolver().resolveRecipient(source);
if (!group.isMember(recipientId) && !( if (!group.isMember(recipientId) && !(
group.isPendingMember(recipientId) && message != null && message.isGroupV2Update() group.isPendingMember(recipientId) && message != null && message.isGroupV2Update()
)) { )) {
@ -745,10 +740,11 @@ public final class IncomingMessageHandler {
} }
if (groupInfo.getMembers().isPresent()) { if (groupInfo.getMembers().isPresent()) {
final var recipientResolver = account.getRecipientResolver();
groupV1.addMembers(groupInfo.getMembers() groupV1.addMembers(groupInfo.getMembers()
.get() .get()
.stream() .stream()
.map(context.getRecipientHelper()::resolveRecipient) .map(recipientResolver::resolveRecipient)
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
} }
@ -921,8 +917,9 @@ public final class IncomingMessageHandler {
} }
private SignalServiceAddress getSenderAddress(SignalServiceEnvelope envelope, SignalServiceContent content) { private SignalServiceAddress getSenderAddress(SignalServiceEnvelope envelope, SignalServiceContent content) {
if (!envelope.isUnidentifiedSender() && envelope.hasSourceServiceId()) { final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
return envelope.getSourceAddress(); if (!envelope.isUnidentifiedSender() && serviceId != null) {
return new SignalServiceAddress(serviceId);
} else if (content != null) { } else if (content != null) {
return content.getSender(); return content.getSender();
} else { } else {
@ -931,12 +928,13 @@ public final class IncomingMessageHandler {
} }
private DeviceAddress getSender(SignalServiceEnvelope envelope, SignalServiceContent content) { private DeviceAddress getSender(SignalServiceEnvelope envelope, SignalServiceContent content) {
if (!envelope.isUnidentifiedSender() && envelope.hasSourceServiceId()) { final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
return new DeviceAddress(context.getRecipientHelper().resolveRecipient(envelope.getSourceAddress()), if (!envelope.isUnidentifiedSender() && serviceId != null) {
envelope.getSourceAddress().getServiceId(), return new DeviceAddress(account.getRecipientResolver().resolveRecipient(serviceId),
serviceId,
envelope.getSourceDevice()); envelope.getSourceDevice());
} else { } else {
return new DeviceAddress(context.getRecipientHelper().resolveRecipient(content.getSender()), return new DeviceAddress(account.getRecipientResolver().resolveRecipient(content.getSender()),
content.getSender().getServiceId(), content.getSender().getServiceId(),
content.getSenderDevice()); content.getSenderDevice());
} }
@ -951,7 +949,7 @@ public final class IncomingMessageHandler {
return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId()); return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId());
} }
final var address = addressOptional.get(); final var address = addressOptional.get();
return new DeviceAddress(context.getRecipientHelper().resolveRecipient(address), return new DeviceAddress(account.getRecipientResolver().resolveRecipient(address),
address.getServiceId(), address.getServiceId(),
account.getDeviceId()); account.getDeviceId());
} }

View file

@ -12,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.SignalWebSocket; import org.whispersystems.signalservice.api.SignalWebSocket;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.ServiceId.ACI; import org.whispersystems.signalservice.api.push.ServiceId.ACI;
import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState; import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState;
import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException; import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException;
@ -147,8 +148,10 @@ public class ReceiveHelper {
for (final var it : batch) { for (final var it : batch) {
SignalServiceEnvelope envelope1 = new SignalServiceEnvelope(it.getEnvelope(), SignalServiceEnvelope envelope1 = new SignalServiceEnvelope(it.getEnvelope(),
it.getServerDeliveredTimestamp()); it.getServerDeliveredTimestamp());
final var recipientId = envelope1.hasSourceServiceId() ? account.getRecipientResolver() final var recipientId = envelope1.getSourceServiceId()
.resolveRecipient(envelope1.getSourceAddress()) : null; .map(ServiceId::parseOrNull)
.map(s -> account.getRecipientResolver().resolveRecipient(s))
.orElse(null);
logger.trace("Storing new message from {}", recipientId); logger.trace("Storing new message from {}", recipientId);
// store message on disk, before acknowledging receipt to the server // store message on disk, before acknowledging receipt to the server
cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId); cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId);
@ -230,7 +233,7 @@ public class ReceiveHelper {
if (exception instanceof UntrustedIdentityException) { if (exception instanceof UntrustedIdentityException) {
logger.debug("Keeping message with untrusted identity in message cache"); logger.debug("Keeping message with untrusted identity in message cache");
final var address = ((UntrustedIdentityException) exception).getSender(); final var address = ((UntrustedIdentityException) exception).getSender();
if (!envelope.hasSourceServiceId() && address.uuid().isPresent()) { if (envelope.getSourceServiceId().isEmpty() && address.uuid().isPresent()) {
final var recipientId = account.getRecipientResolver() final var recipientId = account.getRecipientResolver()
.resolveRecipient(ACI.from(address.uuid().get())); .resolveRecipient(ACI.from(address.uuid().get()));
try { try {
@ -282,7 +285,7 @@ public class ReceiveHelper {
cachedMessage.delete(); cachedMessage.delete();
return null; return null;
} }
if (!envelope.hasSourceServiceId()) { if (envelope.getSourceServiceId().isEmpty()) {
final var identifier = ((UntrustedIdentityException) exception).getSender(); final var identifier = ((UntrustedIdentityException) exception).getSender();
final var recipientId = account.getRecipientResolver() final var recipientId = account.getRecipientResolver()
.resolveRecipient(new RecipientAddress(identifier)); .resolveRecipient(new RecipientAddress(identifier));

View file

@ -67,10 +67,6 @@ public class RecipientHelper {
.toSignalServiceAddress(); .toSignalServiceAddress();
} }
public RecipientId resolveRecipient(final SignalServiceAddress address) {
return account.getRecipientResolver().resolveRecipient(address);
}
public Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws UnregisteredRecipientException { public Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws UnregisteredRecipientException {
final var recipientIds = new HashSet<RecipientId>(recipients.size()); final var recipientIds = new HashSet<RecipientId>(recipients.size());
for (var number : recipients) { for (var number : recipients) {

View file

@ -475,9 +475,10 @@ public class SendHelper {
senderKeyTargets = Set.of(); senderKeyTargets = Set.of();
} else { } else {
results.stream().filter(SendMessageResult::isSuccess).forEach(allResults::add); results.stream().filter(SendMessageResult::isSuccess).forEach(allResults::add);
final var recipientResolver = account.getRecipientResolver();
final var failedTargets = results.stream() final var failedTargets = results.stream()
.filter(r -> !r.isSuccess()) .filter(r -> !r.isSuccess())
.map(r -> context.getRecipientHelper().resolveRecipient(r.getAddress())) .map(r -> recipientResolver.resolveRecipient(r.getAddress()))
.toList(); .toList();
if (!failedTargets.isEmpty()) { if (!failedTargets.isEmpty()) {
senderKeyTargets = new HashSet<>(senderKeyTargets); senderKeyTargets = new HashSet<>(senderKeyTargets);
@ -724,7 +725,7 @@ public class SendHelper {
private void handleSendMessageResult(final SendMessageResult r) { private void handleSendMessageResult(final SendMessageResult r) {
if (r.isSuccess() && !r.getSuccess().isUnidentified()) { if (r.isSuccess() && !r.getSuccess().isUnidentified()) {
final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress()); final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
final var profile = account.getProfileStore().getProfile(recipientId); final var profile = account.getProfileStore().getProfile(recipientId);
if (profile != null && ( if (profile != null && (
profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED
@ -738,7 +739,7 @@ public class SendHelper {
} }
} }
if (r.isUnregisteredFailure()) { if (r.isUnregisteredFailure()) {
final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress()); final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
final var profile = account.getProfileStore().getProfile(recipientId); final var profile = account.getProfileStore().getProfile(recipientId);
if (profile != null && ( if (profile != null && (
profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED
@ -752,7 +753,7 @@ public class SendHelper {
} }
} }
if (r.getIdentityFailure() != null) { if (r.getIdentityFailure() != null) {
final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress()); final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
context.getIdentityHelper() context.getIdentityHelper()
.handleIdentityFailure(recipientId, r.getAddress().getServiceId(), r.getIdentityFailure()); .handleIdentityFailure(recipientId, r.getAddress().getServiceId(), r.getIdentityFailure());
} }