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

Capture decryption errors (PSG-1023) #1673

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
13 changes: 13 additions & 0 deletions MatrixSDK/MXSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ FOUNDATION_EXPORT NSString *const kMXSessionDidUpdateGroupUsersNotification;
*/
FOUNDATION_EXPORT NSString *const kMXSessionDidUpdatePublicisedGroupsForUsersNotification;

/**
Posted when MXSession fails to decrypt a list of events.

The passed userInfo dictionary contains:
- `kMXSessionNotificationEventsArrayKey` the list of the events the session fails to decrypt.
*/
FOUNDATION_EXPORT NSString *const kMXSessionDidFailToDecryptEventsNotification;

#pragma mark - Notifications keys
/**
The key in notification userInfo dictionary representating the roomId.
Expand Down Expand Up @@ -339,6 +347,11 @@ FOUNDATION_EXPORT NSString *const kMXSessionNotificationErrorKey;
*/
FOUNDATION_EXPORT NSString *const kMXSessionNotificationUserIdsArrayKey;

/**
The key in notification userInfo dictionary representating a list of events.
*/
FOUNDATION_EXPORT NSString *const kMXSessionNotificationEventsArrayKey;


#pragma mark - Other constants
/**
Expand Down
8 changes: 8 additions & 0 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
NSString *const kMXSessionDidUpdateGroupRoomsNotification = @"kMXSessionDidUpdateGroupRoomsNotification";
NSString *const kMXSessionDidUpdateGroupUsersNotification = @"kMXSessionDidUpdateGroupUsersNotification";
NSString *const kMXSessionDidUpdatePublicisedGroupsForUsersNotification = @"kMXSessionDidUpdatePublicisedGroupsForUsersNotification";
NSString *const kMXSessionDidFailToDecryptEventsNotification = @"kMXSessionDidFailToDecryptEventsNotification";

NSString *const kMXSessionNotificationRoomIdKey = @"roomId";
NSString *const kMXSessionNotificationGroupKey = @"group";
Expand All @@ -79,6 +80,7 @@
NSString *const kMXSessionNotificationSyncResponseKey = @"syncResponse";
NSString *const kMXSessionNotificationErrorKey = @"error";
NSString *const kMXSessionNotificationUserIdsArrayKey = @"userIds";
NSString *const kMXSessionNotificationEventsArrayKey = @"events";

NSString *const kMXSessionNoRoomTag = @"m.recent"; // Use the same value as matrix-react-sdk

Expand Down Expand Up @@ -5000,6 +5002,12 @@ - (void)decryptEvents:(NSArray<MXEvent*> *)events
}
}

if (failedEvents.count > 0) {
[NSNotificationCenter.defaultCenter postNotificationName:kMXSessionDidFailToDecryptEventsNotification
alfogrillo marked this conversation as resolved.
Show resolved Hide resolved
object:self
userInfo:@{ kMXSessionNotificationEventsArrayKey: failedEvents }];
}

onComplete(failedEvents);
}];
}
Expand Down
41 changes: 35 additions & 6 deletions MatrixSDK/Room/Polls/PollAggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class PollAggregator {

private var events: [MXEvent] = []
private var hasBeenEdited = false
private var relatedUndecryptableEvents: Set<String> = []

public private(set) var poll: PollProtocol! {
didSet {
Expand All @@ -77,8 +78,31 @@ public class PollAggregator {
self.pollStartEventId = pollStartEventId
self.pollBuilder = PollBuilder()

NotificationCenter.default.addObserver(self, selector: #selector(handleRoomDataFlush), name: NSNotification.Name.mxRoomDidFlushData, object: self.room)
NotificationCenter.default.addObserver(self, selector: #selector(handleRoomDataFlush), name: .mxRoomDidFlushData, object: self.room)
setupEditListener()
try buildPollStartContent()
}

public func handleErroredRelatedEventsIds(_ ids: Set<String>?, to parentEvent: String) {
guard parentEvent == pollStartEventId else {
return
}

let newUndecryptableEvents = ids ?? []
guard relatedUndecryptableEvents != newUndecryptableEvents else {
return
}

relatedUndecryptableEvents = newUndecryptableEvents

self.poll = pollBuilder.build(pollStartEventContent: pollStartEventContent,
events: events,
currentUserIdentifier: session.myUserId,
hasBeenEdited: hasBeenEdited,
hasDecryptionError: hasDecryptionError)
}

private func setupEditListener() {
editEventsListener = session.aggregations.listenToEditsUpdate(inRoom: self.room.roomId) { [weak self] event in
guard let self = self,
self.pollStartEventId == event.relatesTo.eventId
Expand All @@ -92,8 +116,6 @@ public class PollAggregator {
self.delegate?.pollAggregator(self, didFailWithError: PollAggregatorError.invalidPollStartEvent)
}
}

try buildPollStartContent()
}

private func buildPollStartContent() throws {
Expand All @@ -111,7 +133,8 @@ public class PollAggregator {
poll = pollBuilder.build(pollStartEventContent: eventContent,
events: events,
currentUserIdentifier: session.myUserId,
hasBeenEdited: hasBeenEdited)
hasBeenEdited: hasBeenEdited,
hasDecryptionError: hasDecryptionError)

reloadPollData()
}
Expand Down Expand Up @@ -149,13 +172,15 @@ public class PollAggregator {
self.poll = self.pollBuilder.build(pollStartEventContent: self.pollStartEventContent,
events: self.events,
currentUserIdentifier: self.session.myUserId,
hasBeenEdited: self.hasBeenEdited)
hasBeenEdited: self.hasBeenEdited,
hasDecryptionError: self.hasDecryptionError)
} as Any

self.poll = self.pollBuilder.build(pollStartEventContent: self.pollStartEventContent,
events: self.events,
currentUserIdentifier: self.session.myUserId,
hasBeenEdited: self.hasBeenEdited)
hasBeenEdited: self.hasBeenEdited,
hasDecryptionError: self.hasDecryptionError)

self.delegate?.pollAggregatorDidEndLoading(self)

Expand All @@ -167,4 +192,8 @@ public class PollAggregator {
self.delegate?.pollAggregator(self, didFailWithError: error)
}
}

private var hasDecryptionError: Bool {
relatedUndecryptableEvents.isEmpty == false
}
}
3 changes: 2 additions & 1 deletion MatrixSDK/Room/Polls/PollBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ struct PollBuilder {
static let maxAnswerOptionCount = 20
}

func build(pollStartEventContent: MXEventContentPollStart, events: [MXEvent], currentUserIdentifier: String, hasBeenEdited: Bool = false) -> PollProtocol {
func build(pollStartEventContent: MXEventContentPollStart, events: [MXEvent], currentUserIdentifier: String, hasBeenEdited: Bool = false, hasDecryptionError: Bool = false) -> PollProtocol {

let poll = Poll()
poll.hasBeenEdited = hasBeenEdited
poll.hasDecryptionError = hasDecryptionError

poll.text = pollStartEventContent.question
poll.maxAllowedSelections = max(1, pollStartEventContent.maxSelections.uintValue)
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Room/Polls/PollModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public protocol PollProtocol {
var isClosed: Bool { get }
var totalAnswerCount: UInt { get }
var hasBeenEdited: Bool { get }
var hasDecryptionError: Bool { get }
}

class PollAnswerOption: PollAnswerOptionProtocol {
Expand All @@ -56,6 +57,7 @@ class Poll: PollProtocol {
var maxAllowedSelections: UInt = 1
var isClosed: Bool = false
var hasBeenEdited: Bool = false
var hasDecryptionError: Bool = false

var totalAnswerCount: UInt {
return self.answerOptions.reduce(0) { $0 + $1.count}
Expand Down
32 changes: 32 additions & 0 deletions MatrixSDKTests/MXPollAggregatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,38 @@ class MXPollAggregatorTest: XCTestCase {
}
}

func testErrorsAreIgnoredForOtherPolls() {
createScenarioForBobAndAlice { bobSession, aliceSession, bobRoom, aliceRoom, pollStartEvent, expectation in
self.pollAggregator = try! PollAggregator(session: bobSession, room: bobRoom, pollStartEventId: pollStartEvent.eventId)
self.pollAggregator.handleErroredRelatedEventsIds(["A", "B"], to: "someId")
XCTAssertFalse(self.pollAggregator.poll.hasDecryptionError)
expectation.fulfill()
}
}

func testNoErrorsAreIgnored() {
createScenarioForBobAndAlice { bobSession, aliceSession, bobRoom, aliceRoom, pollStartEvent, expectation in
self.pollAggregator = try! PollAggregator(session: bobSession, room: bobRoom, pollStartEventId: pollStartEvent.eventId)
self.pollAggregator.handleErroredRelatedEventsIds([], to: pollStartEvent.eventId)
XCTAssertFalse(self.pollAggregator.poll.hasDecryptionError)
expectation.fulfill()
}
}

func testErrorsAreConsideredWhenRelatedToThisPoll() {
createScenarioForBobAndAlice { bobSession, aliceSession, bobRoom, aliceRoom, pollStartEvent, expectation in
self.pollAggregator = try! PollAggregator(session: bobSession, room: bobRoom, pollStartEventId: pollStartEvent.eventId)

self.pollAggregator.delegate = PollAggregatorBlockWrapper(dataUpdateCallback: {
XCTAssertTrue(self.pollAggregator.poll.hasDecryptionError)
expectation.fulfill()
self.pollAggregator.delegate = nil
})

self.pollAggregator.handleErroredRelatedEventsIds(["A", "B"], to: pollStartEvent.eventId)
}
}

// MARK: - Private

func createScenarioForBobAndAlice(_ readyToTest: @escaping (MXSession, MXSession, MXRoom, MXRoom, MXEvent, XCTestExpectation) -> Void) {
Expand Down
25 changes: 25 additions & 0 deletions MatrixSDKTests/MXSessionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,31 @@ - (void)testMXSDKOptionsWellknownDomainUrl
}];
}

#pragma mark Failing decryption

-(void)testNotificationIsCorrectAfterDecryptionFailure
{
MXEvent* fakeEncryptedEvent = [MXEvent modelFromJSON:@{
@"event_id": @"abc",
@"type": kMXEventTypeStringRoomEncrypted,
@"content": @{ @"garbage": @"0000" }
}];

[matrixSDKTestsData doMXSessionTestWithBob:self readyToTest:^(MXSession *mxSession, XCTestExpectation *expectation) {
[NSNotificationCenter.defaultCenter addObserverForName:kMXSessionDidFailToDecryptEventsNotification object:mxSession queue:nil usingBlock:^(NSNotification * _Nonnull notification) {
XCTAssertTrue(mxSession == notification.object);
NSArray* events = notification.userInfo[kMXSessionNotificationEventsArrayKey];
XCTAssertTrue(fakeEncryptedEvent == events.firstObject);
[expectation fulfill];
}];

[mxSession enableCrypto:YES
success:^{ [mxSession decryptEvents:@[fakeEncryptedEvent] inTimeline:nil onComplete:^(NSArray<MXEvent *> *failedEvents) {}]; }
failure:^(NSError *error) { XCTFail();}
];
}];
}

#pragma mark Account Data tests

-(void)testAccountDataIsDeletedLocally
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1673.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Polls: handle decryption errors.