Skip to content

Commit

Permalink
Log decryption errors separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Nov 11, 2022
1 parent 21a8dfa commit 30aa857
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
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 serialization", 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

0 comments on commit 30aa857

Please sign in to comment.