From 6a5019687bfa28b9058be24b42204116eeaaf0cd Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 12 Nov 2024 14:13:28 +0200 Subject: [PATCH] Fix #1947 - Check expected files are still present before restoring a session. --- .../Sources/Services/UserSession/SessionDirectories.swift | 8 ++++++++ .../Sources/Services/UserSession/UserSessionStore.swift | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/ElementX/Sources/Services/UserSession/SessionDirectories.swift b/ElementX/Sources/Services/UserSession/SessionDirectories.swift index 8a93aed12..29b1b076b 100644 --- a/ElementX/Sources/Services/UserSession/SessionDirectories.swift +++ b/ElementX/Sources/Services/UserSession/SessionDirectories.swift @@ -51,6 +51,14 @@ struct SessionDirectories: Hashable, Codable { } } + /// Check that mission critical files (the crypto db) are still in the right place when restoring a session + /// iOS might decide to move the app with its user defaults and keychain but without + /// some of the files stored in the shared container e.g. after a device transfer, offloading etc. + /// If that happens we should fail the session restoration. + func isNonTransientUserDataValid() -> Bool { + FileManager.default.fileExists(atPath: dataPath.appending("/matrix-sdk-crypto.sqlite3")) + } + private func deleteFiles(at url: URL, with prefix: String) throws { let sessionDirectoryContents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) for url in sessionDirectoryContents where url.lastPathComponent.hasPrefix(prefix) { diff --git a/ElementX/Sources/Services/UserSession/UserSessionStore.swift b/ElementX/Sources/Services/UserSession/UserSessionStore.swift index 310475032..1f4ca5e5b 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionStore.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionStore.swift @@ -118,6 +118,11 @@ class UserSessionStore: UserSessionStoreProtocol { MXLog.info("Restoring client with encrypted store.") } + guard credentials.restorationToken.sessionDirectories.isNonTransientUserDataValid() else { + MXLog.error("Failed restoring login, missing non-transient user data") + return .failure(.failedRestoringLogin) + } + let homeserverURL = credentials.restorationToken.session.homeserverUrl let builder = ClientBuilder