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

Log decryption errors separately with Crypto V2 #1632

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
100 changes: 73 additions & 27 deletions MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventDecryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ protocol MXRoomEventDecrypting: Actor {
actor MXRoomEventDecryption: MXRoomEventDecrypting {
typealias SessionId = String
typealias EventId = String

// `Megolm` error does not currently expose the type of "missing keys" error, so have to match against
// hardcoded non-localized error message. Will be changed in future PR
private static let MissingKeysMessage = "decryption failed because the room key is missing"

private let handler: MXCryptoRoomEventDecrypting
private var undecryptedEvents: [SessionId: [EventId: MXEvent]]
Expand Down Expand Up @@ -117,34 +121,10 @@ actor MXRoomEventDecryption: MXRoomEventDecrypting {
log.debug("Successfully decrypted event `\(result.clearEvent["type"] ?? "unknown")`")
return result

// `Megolm` error does not currently expose the type of "missing keys" error, so have to match against
// hardcoded non-localized error message. Will be changed in future PR
} catch DecryptionError.Megolm(message: "decryption failed because the room key is missing") {
if undecryptedEvents[sessionId] == nil {
log.error("Failed to decrypt one or more events due to missing room keys", context: [
"session_id": sessionId,
"details": "further errors for the same key will be supressed"
])
}

addUndecryptedEvent(event)

let result = MXEventDecryptionResult()
result.error = NSError(
domain: MXDecryptingErrorDomain,
code: Int(MXDecryptingErrorUnknownInboundSessionIdCode.rawValue),
userInfo: [
NSLocalizedDescriptionKey: MXDecryptingErrorUnknownInboundSessionIdReason
]
)
return result
} catch let error as DecryptionError {
return handleDecryptionError(for: event, sessionId: sessionId, error: error)
} catch {
log.error("Failed to decrypt event", context: error)
addUndecryptedEvent(event)

let result = MXEventDecryptionResult()
result.error = error
return result
return handleGenericError(for: event, sessionId: sessionId, error: error)
}
}

Expand Down Expand Up @@ -220,6 +200,72 @@ actor MXRoomEventDecryption: MXRoomEventDecrypting {
}
return sessionId
}

// MARK: - Error handling

private func handleDecryptionError(for event: MXEvent, sessionId: String, error: DecryptionError) -> MXEventDecryptionResult {
switch error {
case .Identifier(let message):
log.error("Failed to decrypt event due to identifier", context: [
"session_id": sessionId,
"message": message,
"error": error
])
return trackedDecryptionResult(for: event, error: error)

case .Serialization(let message):
log.error("Failed to decrypt event due to serialization", context: [
"session_id": sessionId,
"message": message,
"error": error
])
return trackedDecryptionResult(for: event, error: error)

case .Megolm(let message):
if message == Self.MissingKeysMessage {
if undecryptedEvents[sessionId] == nil {
log.error("Failed to decrypt event(s) due to missing room keys", context: [
"session_id": sessionId,
"message": message,
"error": error,
"details": "further errors for the same key will be supressed",
])
}

let keysError = NSError(
domain: MXDecryptingErrorDomain,
code: Int(MXDecryptingErrorUnknownInboundSessionIdCode.rawValue),
userInfo: [
NSLocalizedDescriptionKey: MXDecryptingErrorUnknownInboundSessionIdReason
]
)
return trackedDecryptionResult(for: event, error: keysError)
} else {
log.error("Failed to decrypt event due to megolm error", context: [
"session_id": sessionId,
"message": message,
"error": error
])
return trackedDecryptionResult(for: event, error: error)
}
}
}

private func handleGenericError(for event: MXEvent, sessionId: String, error: Error) -> MXEventDecryptionResult {
log.error("Failed to decrypt event", context: [
"session_id": sessionId,
"error": error
])
return trackedDecryptionResult(for: event, error: error)
}

private func trackedDecryptionResult(for event: MXEvent, error: Error) -> MXEventDecryptionResult {
addUndecryptedEvent(event)

let result = MXEventDecryptionResult()
result.error = error
return result
}
}

#endif
2 changes: 1 addition & 1 deletion MatrixSDK/Utils/Logs/MXAnalyticsDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MXAnalyticsDestination: BaseDestination {
return dictionary
} else if let error = context as? Error {
return [
"error": error.localizedDescription
"error": error
]
} else {
return [
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1632.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CryptoV2: Log decryption errors separately