Discard transient messages if there is no session.

pull/1/head
Matthew Chen 6 years ago
parent 27bc569fbe
commit 78d0685cba

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
@ -57,6 +57,8 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *NoSessionForTransientMessageException = @"NoSessionForTransientMessageException";
const NSUInteger kOversizeTextMessageSizeThreshold = 2 * 1024; const NSUInteger kOversizeTextMessageSizeThreshold = 2 * 1024;
NSError *SSKEnsureError(NSError *_Nullable error, OWSErrorCode fallbackCode, NSString *fallbackErrorDescription) NSError *SSKEnsureError(NSError *_Nullable error, OWSErrorCode fallbackCode, NSString *fallbackErrorDescription)
@ -858,7 +860,18 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
@try { @try {
deviceMessages = [self throws_deviceMessagesForMessageSend:messageSend]; deviceMessages = [self throws_deviceMessagesForMessageSend:messageSend];
} @catch (NSException *exception) { } @catch (NSException *exception) {
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) { if ([exception.name isEqualToString:NoSessionForTransientMessageException]) {
// When users re-register, we don't want transient messages (like typing
// indicators) to cause users to hit the prekey fetch rate limit. So
// we silently discard these message if there is no pre-existing session
// for the recipient.
NSError *error = OWSErrorWithCodeDescription(
OWSErrorCodeNoSessionForTransientMessage, @"No session for transient message.");
[error setIsRetryable:NO];
[error setIsFatal:YES];
*errorHandle = error;
return nil;
} else if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
// This *can* happen under normal usage, but it should happen relatively rarely. // This *can* happen under normal usage, but it should happen relatively rarely.
// We expect it to happen whenever Bob reinstalls, and Alice messages Bob before // We expect it to happen whenever Bob reinstalls, and Alice messages Bob before
// she can pull down his latest identity. // she can pull down his latest identity.
@ -1548,6 +1561,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
if (hasSession) { if (hasSession) {
return; return;
} }
// Discard "typing indicator" messages if there is no existing session with the user.
BOOL canSafelyBeDiscarded = messageSend.message.isOnline;
if (canSafelyBeDiscarded) {
OWSRaiseException(NoSessionForTransientMessageException, @"No session for transient message.");
}
__block dispatch_semaphore_t sema = dispatch_semaphore_create(0); __block dispatch_semaphore_t sema = dispatch_semaphore_create(0);
__block PreKeyBundle *_Nullable bundle; __block PreKeyBundle *_Nullable bundle;

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -51,6 +51,7 @@ typedef NS_ENUM(NSInteger, OWSErrorCode) {
OWSErrorCodeProfileUpdateFailed = 777424, OWSErrorCodeProfileUpdateFailed = 777424,
OWSErrorCodeAvatarWriteFailed = 777425, OWSErrorCodeAvatarWriteFailed = 777425,
OWSErrorCodeAvatarUploadFailed = 777426, OWSErrorCodeAvatarUploadFailed = 777426,
OWSErrorCodeNoSessionForTransientMessage,
}; };
extern NSString *const OWSErrorRecipientIdentifierKey; extern NSString *const OWSErrorRecipientIdentifierKey;

Loading…
Cancel
Save