Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto: Duplicate message index after using the share extension #1044

Merged
merged 3 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Changes to be released in next version
*

🐛 Bugfix
*
* Crypto: Duplicate message index after using the share extension (vector-im/element-ios#4104)

⚠️ API Changes
*
Expand Down
35 changes: 17 additions & 18 deletions MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmEncryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ @interface MXMegolmEncryption ()

NSString *deviceId;

// OutboundSessionInfo. Null if we haven't yet started setting one up. Note
// that even if this is non-null, it may not be ready for use (in which
// case outboundSession.shareOperation will be non-nill.)
MXOutboundSessionInfo *outboundSession;

NSMutableArray<MXQueuedEncryption*> *pendingEncryptions;

// Session rotation periods
Expand Down Expand Up @@ -79,15 +74,6 @@ - (instancetype)initWithCrypto:(MXCrypto *)theCrypto andRoom:(NSString *)theRoom
// TODO: Make it configurable via parameters
sessionRotationPeriodMsgs = 100;
sessionRotationPeriodMs = 7 * 24 * 3600 * 1000;

// restore last saved outbound session for this room
MXOlmOutboundGroupSession *restoredOutboundGroupSession = [crypto.olmDevice outboundGroupSessionForRoomWithRoomId:roomId];

if (restoredOutboundGroupSession)
{
outboundSession = [[MXOutboundSessionInfo alloc] initWithSession:restoredOutboundGroupSession];
outboundSession.sharedWithDevices = [crypto.store sharedDevicesForOutboundGroupSessionInRoomWithId:roomId sessionId:outboundSession.sessionId];
}
}
return self;
}
Expand Down Expand Up @@ -146,6 +132,21 @@ - (MXHTTPOperation*)ensureSessionForUsers:(NSArray<NSString*>*)users

#pragma mark - Private methods

- (MXOutboundSessionInfo *)outboundSession
{
// restore last saved outbound session for this room
MXOlmOutboundGroupSession *restoredOutboundGroupSession = [crypto.olmDevice outboundGroupSessionForRoomWithRoomId:roomId];

MXOutboundSessionInfo *outboundSession;
if (restoredOutboundGroupSession)
{
outboundSession = [[MXOutboundSessionInfo alloc] initWithSession:restoredOutboundGroupSession];
outboundSession.sharedWithDevices = [crypto.store sharedDevicesForOutboundGroupSessionInRoomWithId:roomId sessionId:outboundSession.sessionId];
}

return outboundSession;
}

/*
Get the list of devices which can encrypt data to.

Expand Down Expand Up @@ -238,27 +239,25 @@ - (MXHTTPOperation *)ensureOutboundSession:(MXUsersDevicesMap<MXDeviceInfo *> *)
success:(void (^)(MXOutboundSessionInfo *session))success
failure:(void (^)(NSError *))failure
{
__block MXOutboundSessionInfo *session = outboundSession;
__block MXOutboundSessionInfo *session = self.outboundSession;

// Need to make a brand new session?
if (session && [session needsRotation:sessionRotationPeriodMsgs rotationPeriodMs:sessionRotationPeriodMs])
{
[crypto.olmDevice discardOutboundGroupSessionForRoomWithRoomId:roomId];
outboundSession = nil;
session = nil;
}

// Determine if we have shared with anyone we shouldn't have
if (session && [session sharedWithTooManyDevices:devicesInRoom])
{
[crypto.olmDevice discardOutboundGroupSessionForRoomWithRoomId:roomId];
outboundSession = nil;
session = nil;
}

if (!session)
{
outboundSession = session = [self prepareNewSession];
session = [self prepareNewSession];
}

if (session.shareOperation)
Expand Down