From 528b0135119fbd208287d92c28ea0beece712289 Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 12 Jun 2026 11:42:45 +0700 Subject: [PATCH] fix: TokenOidcCacheManager recovers from corrupted Hive box in getTokenOidc and persistOneTokenOidc --- .../data/local/token_oidc_cache_manager.dart | 69 ++++++++++++++++--- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/lib/features/login/data/local/token_oidc_cache_manager.dart b/lib/features/login/data/local/token_oidc_cache_manager.dart index ee897a7ba..82d04fe0c 100644 --- a/lib/features/login/data/local/token_oidc_cache_manager.dart +++ b/lib/features/login/data/local/token_oidc_cache_manager.dart @@ -1,3 +1,4 @@ +import 'package:core/domain/exceptions/app_base_exception.dart'; import 'package:core/utils/app_logger.dart'; import 'package:model/oidc/token_oidc.dart'; import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart'; @@ -13,22 +14,62 @@ class TokenOidcCacheManager extends CacheManagerInteraction { Future getTokenOidc(String tokenIdHash) async { log('TokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash'); - final tokenCache = await _tokenOidcCacheClient.getItem(tokenIdHash); - log('TokenOidcCacheManager::getTokenOidc(): tokenCache: $tokenCache'); - if (tokenCache == null) { - throw NotFoundStoredTokenException(); - } else { + try { + final tokenCache = await _tokenOidcCacheClient.getItem(tokenIdHash); + log('TokenOidcCacheManager::getTokenOidc(): tokenCache: $tokenCache'); + if (tokenCache == null) { + throw NotFoundStoredTokenException(); + } return tokenCache.toTokenOidc(); + } on AppBaseException { + rethrow; + } catch (e, stackTrace) { + // AES-CBC decryption failed (e.g. non-block-aligned bytes from a partial + // write or cross-isolate race). Clear the corrupted box so the next read + // starts clean and triggers normal re-authentication instead of looping. + logError( + 'TokenOidcCacheManager::getTokenOidc(): ' + 'token_box_corrupted=true | error_type=${e.runtimeType} | clearing box', + exception: e, + stackTrace: stackTrace, + ); + await _safelyClearBox(); + throw NotFoundStoredTokenException(); } } Future persistOneTokenOidc(TokenOIDC tokenOIDC) async { - // Crash-safe persist: write the new token FIRST, then prune stale entries, so - // the box is never empty if the process is killed mid-write → forced re-login. log('TokenOidcCacheManager::persistOneTokenOidc(): keyHash: ${tokenOIDC.tokenIdHash}'); - await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache()); - await _removeStaleTokens(keepKey: tokenOIDC.tokenIdHash); - log('TokenOidcCacheManager::persistOneTokenOidc(): done'); + try { + // Crash-safe persist: write the new token FIRST, then prune stale entries, + // so the box is never empty if the process is killed mid-write. + await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache()); + await _removeStaleTokens(keepKey: tokenOIDC.tokenIdHash); + log('TokenOidcCacheManager::persistOneTokenOidc(): done'); + } on AppBaseException { + rethrow; + } catch (e, stackTrace) { + // _removeStaleTokens calls getMapItems() → toMap(), which iterates ALL entries + // and decrypts each one. If the box contains a corrupted entry (e.g. from a + // cross-isolate partial write), toMap() throws before prune completes. + // Recover by clearing and re-inserting only the new valid token. + logError( + 'TokenOidcCacheManager::persistOneTokenOidc(): ' + 'box_corrupted=true | error_type=${e.runtimeType} | clearing and re-inserting token', + exception: e, + stackTrace: stackTrace, + ); + await _recoverBoxWithToken(tokenOIDC); + } + } + + Future _recoverBoxWithToken(TokenOIDC tokenOIDC) async { + await _safelyClearBox(); + try { + await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache()); + } catch (e) { + logWarning('TokenOidcCacheManager::_recoverBoxWithToken(): re-insert failed | $e'); + } } /// Removes every token except [keepKey] so the box keeps exactly one entry @@ -45,6 +86,14 @@ class TokenOidcCacheManager extends CacheManagerInteraction { await _tokenOidcCacheClient.clearAllData(); } + Future _safelyClearBox() async { + try { + await _tokenOidcCacheClient.clearAllData(); + } catch (e) { + logWarning('TokenOidcCacheManager::_safelyClearBox(): failed to clear box | $e'); + } + } + @override Future migrateHiveToIsolatedHive() async { try {