Send sync messages to self via UD (only); discard self-sent sync messages.

pull/1/head
Matthew Chen 7 years ago
parent 283cb18288
commit e47b69e0aa

@ -3,6 +3,7 @@
// //
#import "SignalRecipient.h" #import "SignalRecipient.h"
#import "OWSDevice.h"
#import "TSAccountManager.h" #import "TSAccountManager.h"
#import <YapDatabase/YapDatabaseConnection.h> #import <YapDatabase/YapDatabaseConnection.h>
@ -52,7 +53,7 @@ NS_ASSUME_NONNULL_BEGIN
// //
// OWSMessageSender will correct this if it is wrong the next time // OWSMessageSender will correct this if it is wrong the next time
// we send a message to this recipient. // we send a message to this recipient.
_devices = [NSOrderedSet orderedSetWithObject:@(1)]; _devices = [NSOrderedSet orderedSetWithObject:@(OWSDevicePrimaryDeviceId)];
} }
return self; return self;
@ -69,7 +70,8 @@ NS_ASSUME_NONNULL_BEGIN
_devices = [NSOrderedSet new]; _devices = [NSOrderedSet new];
} }
if ([self.uniqueId isEqual:[TSAccountManager localNumber]] && [self.devices containsObject:@(1)]) { if ([self.uniqueId isEqual:[TSAccountManager localNumber]] &&
[self.devices containsObject:@(OWSDevicePrimaryDeviceId)]) {
OWSFailDebug(@"self as recipient device"); OWSFailDebug(@"self as recipient device");
} }
@ -90,7 +92,8 @@ NS_ASSUME_NONNULL_BEGIN
{ {
OWSAssertDebug(devices.count > 0); OWSAssertDebug(devices.count > 0);
if ([self.uniqueId isEqual:[TSAccountManager localNumber]] && [devices containsObject:@(1)]) { if ([self.uniqueId isEqual:[TSAccountManager localNumber]] &&
[devices containsObject:@(OWSDevicePrimaryDeviceId)]) {
OWSFailDebug(@"adding self as recipient device"); OWSFailDebug(@"adding self as recipient device");
return; return;
} }

@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, nullable) NSData *plaintextData; @property (nonatomic, readonly, nullable) NSData *plaintextData;
@property (nonatomic, readonly) NSString *source; @property (nonatomic, readonly) NSString *source;
@property (nonatomic, readonly) UInt32 sourceDevice; @property (nonatomic, readonly) UInt32 sourceDevice;
@property (nonatomic, readonly) BOOL isUDMessage;
@end @end

@ -8,6 +8,7 @@
#import "NotificationsProtocol.h" #import "NotificationsProtocol.h"
#import "OWSAnalytics.h" #import "OWSAnalytics.h"
#import "OWSBlockingManager.h" #import "OWSBlockingManager.h"
#import "OWSDevice.h"
#import "OWSError.h" #import "OWSError.h"
#import "OWSIdentityManager.h" #import "OWSIdentityManager.h"
#import "OWSPrimaryStorage+PreKeyStore.h" #import "OWSPrimaryStorage+PreKeyStore.h"
@ -46,6 +47,7 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
@property (nonatomic, nullable) NSData *plaintextData; @property (nonatomic, nullable) NSData *plaintextData;
@property (nonatomic) NSString *source; @property (nonatomic) NSString *source;
@property (nonatomic) UInt32 sourceDevice; @property (nonatomic) UInt32 sourceDevice;
@property (nonatomic) BOOL isUDMessage;
@end @end
@ -57,6 +59,7 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
plaintextData:(nullable NSData *)plaintextData plaintextData:(nullable NSData *)plaintextData
source:(NSString *)source source:(NSString *)source
sourceDevice:(UInt32)sourceDevice sourceDevice:(UInt32)sourceDevice
isUDMessage:(BOOL)isUDMessage
{ {
OWSAssertDebug(envelopeData); OWSAssertDebug(envelopeData);
OWSAssertDebug(source.length > 0); OWSAssertDebug(source.length > 0);
@ -67,6 +70,7 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
result.plaintextData = plaintextData; result.plaintextData = plaintextData;
result.source = source; result.source = source;
result.sourceDevice = sourceDevice; result.sourceDevice = sourceDevice;
result.isUDMessage = isUDMessage;
return result; return result;
} }
@ -161,7 +165,15 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
OWSMessageDecryptResult *result, YapDatabaseReadWriteTransaction *transaction) { OWSMessageDecryptResult *result, YapDatabaseReadWriteTransaction *transaction) {
// Ensure all blocked messages are discarded. // Ensure all blocked messages are discarded.
if ([self isEnvelopeSenderBlocked:envelope]) { if ([self isEnvelopeSenderBlocked:envelope]) {
OWSLogInfo(@"ignoring blocked envelope: %@", envelope.source); OWSLogInfo(@"Ignoring blocked envelope: %@", envelope.source);
return failureBlock();
}
if ([result.source isEqualToString:TSAccountManager.sharedInstance.localNumber]
&& result.sourceDevice == OWSDevicePrimaryDeviceId) {
OWSAssertDebug(result.isUDMessage);
OWSLogInfo(@"Ignoring self-sent sync message.");
return failureBlock(); return failureBlock();
} }
@ -237,7 +249,8 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
[OWSMessageDecryptResult resultWithEnvelopeData:envelopeData [OWSMessageDecryptResult resultWithEnvelopeData:envelopeData
plaintextData:nil plaintextData:nil
source:envelope.source source:envelope.source
sourceDevice:envelope.sourceDevice]; sourceDevice:envelope.sourceDevice
isUDMessage:NO];
successBlock(result, transaction); successBlock(result, transaction);
}]; }];
// Return to avoid double-acknowledging. // Return to avoid double-acknowledging.
@ -361,11 +374,11 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
// plaintextData may be nil for some envelope types. // plaintextData may be nil for some envelope types.
NSData *_Nullable plaintextData = NSData *_Nullable plaintextData =
[[cipher decrypt:cipherMessage protocolContext:transaction] removePadding]; [[cipher decrypt:cipherMessage protocolContext:transaction] removePadding];
OWSMessageDecryptResult *result = OWSMessageDecryptResult *result = [OWSMessageDecryptResult resultWithEnvelopeData:envelopeData
[OWSMessageDecryptResult resultWithEnvelopeData:envelopeData
plaintextData:plaintextData plaintextData:plaintextData
source:envelope.source source:envelope.source
sourceDevice:envelope.sourceDevice]; sourceDevice:envelope.sourceDevice
isUDMessage:NO];
successBlock(result, transaction); successBlock(result, transaction);
} @catch (NSException *exception) { } @catch (NSException *exception) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@ -471,7 +484,8 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes
OWSMessageDecryptResult *result = [OWSMessageDecryptResult resultWithEnvelopeData:newEnvelopeData OWSMessageDecryptResult *result = [OWSMessageDecryptResult resultWithEnvelopeData:newEnvelopeData
plaintextData:plaintextData plaintextData:plaintextData
source:source source:source
sourceDevice:(uint32_t)sourceDeviceId]; sourceDevice:(uint32_t)sourceDeviceId
isUDMessage:YES];
successBlock(result, transaction); successBlock(result, transaction);
} @catch (NSException *exception) { } @catch (NSException *exception) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

@ -1324,7 +1324,20 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
OWSLogDebug( OWSLogDebug(
@"built message: %@ plainTextData.length: %lu", [messageSend.message class], (unsigned long)plainText.length); @"built message: %@ plainTextData.length: %lu", [messageSend.message class], (unsigned long)plainText.length);
for (NSNumber *deviceNumber in messageSend.recipient.devices) { OWSLogDebug(@"recipient.devices: %@", recipient.devices);
[DDLog flushLog];
NSMutableArray<NSNumber *> *deviceIds = [recipient.devices mutableCopy];
OWSAssertDebug(deviceIds);
if (messageSend.isUDSend && messageSend.isLocalNumber) {
const NSUInteger kLocalDeviceId = 1;
OWSAssertDebug(![deviceIds containsObject:@(OWSDevicePrimaryDeviceId)]);
[deviceIds addObject:@(OWSDevicePrimaryDeviceId)];
}
for (NSNumber *deviceId in deviceIds) {
@try { @try {
__block NSDictionary *messageDict; __block NSDictionary *messageDict;
__block NSException *encryptionException; __block NSException *encryptionException;
@ -1332,7 +1345,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@try { @try {
messageDict = [self encryptedMessageForMessageSend:messageSend messageDict = [self encryptedMessageForMessageSend:messageSend
deviceId:deviceNumber deviceId:deviceId
plainText:plainText plainText:plainText
transaction:transaction]; transaction:transaction];
} @catch (NSException *exception) { } @catch (NSException *exception) {
@ -1353,7 +1366,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} @catch (NSException *exception) { } @catch (NSException *exception) {
if ([exception.name isEqualToString:OWSMessageSenderInvalidDeviceException]) { if ([exception.name isEqualToString:OWSMessageSenderInvalidDeviceException]) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[recipient removeDevicesFromRecipient:[NSSet setWithObject:deviceNumber] transaction:transaction]; [recipient removeDevicesFromRecipient:[NSSet setWithObject:deviceId] transaction:transaction];
}]; }];
} else { } else {
@throw exception; @throw exception;

@ -94,10 +94,17 @@ public class OWSUDManagerImpl: NSObject, OWSUDManager {
return SSKEnvironment.shared.profileManager return SSKEnvironment.shared.profileManager
} }
private var tsAccountManager: TSAccountManager {
return TSAccountManager.sharedInstance()
}
// MARK: - Recipient state // MARK: - Recipient state
@objc @objc
public func supportsUnidentifiedDelivery(recipientId: String) -> Bool { public func supportsUnidentifiedDelivery(recipientId: String) -> Bool {
if tsAccountManager.localNumber() == recipientId {
return true
}
return dbConnection.bool(forKey: recipientId, inCollection: kUDRecipientModeCollection, defaultValue: false) return dbConnection.bool(forKey: recipientId, inCollection: kUDRecipientModeCollection, defaultValue: false)
} }

Loading…
Cancel
Save