fix: honor Require PIN after timeout on resume (CW-1707) (#3726)
What changed, and why it matters
This commit fixes a bug in Cake Wallet where the app could resume from the background without asking for the PIN, even when the user had turned on 'Require PIN after timeout.' In some cases, especially on Android, a secure-storage read error could silently prevent the lock screen from appearing. The patch makes the app ask for the PIN whenever the last-auth time cannot be read, and it also tightens the lifecycle handling so the PIN prompt is honored when returning from the background after a wallet is created or restored.
Users should update to the fixed build and verify that 'Require PIN after timeout' is enabled. Developers should review other lifecycle/resume paths for similar authentication-state races and ensure secure-storage failures fail-closed (require authentication).
Security signals we found
Authentication bypass possible when secure storage read fails
Lifecycle state race between resume and requireAuth() result
allowedCreate state could suppress PIN re-prompt on resume
Missing last-auth timestamp after PIN setup could cause immediate re-prompt or timing inconsistency
Evidence from the diff
The change modifies three files. auth_service.dart now wraps the last-auth-time read in a try/catch and returns true (PIN required) on any exception, preventing a PlatformException from bypassing authentication. root.dart removes a special case that suppressed PIN re-prompt after wallet creation/restoration (allowedCreate), resets the local _requestAuth flag when the app goes inactive, and defers clearing the inactive overlay until requireAuth() resolves and confirms no PIN is needed. setup_pin_code_view_model.dart now calls saveLastAuthTime() after setting a new PIN so the timeout baseline is established immediately.
Changed components
lib/core/auth_service.dartlib/src/screens/root/root.dartlib/view_model/setup_pin_code_view_model.dartInspect captured patch +22 / −15
### lib/core/auth_service.dart
@@ -178,12 +178,18 @@ class AuthService with Store {
}
Future<bool> requireAuth() async {
- final timestamp =
- int.tryParse(await secureStorage.read(key: SecureKey.lastAuthTimeMilliseconds) ?? '0');
- final duration = _durationToRequireAuth(timestamp ?? 0);
- final requiredPinInterval = settingsStore.pinTimeOutDuration;
+ try {
+ final timestamp =
+ int.tryParse(await secureStorage.read(key: SecureKey.lastAuthTimeMilliseconds) ?? '0');
+ final duration = _durationToRequireAuth(timestamp ?? 0);
+ final requiredPinInterval = settingsStore.pinTimeOutDuration;
- return duration >= requiredPinInterval.value;
+ return duration >= requiredPinInterval.value;
+ } catch (e) {
+ // Without the last auth time we can't tell if the timeout passed, so ask for the PIN.
+ printV(e);
+ return true;
+ }
}
int _durationToRequireAuth(int timestamp) {
### lib/src/screens/root/root.dart
@@ -135,10 +135,6 @@ class RootState extends State<Root> with WidgetsBindingObserver {
bool requireAuth = await widget.authService.requireAuth();
- if (widget.authenticationStore.state == AuthenticationState.allowedCreate) {
- requireAuth = false;
- }
-
if (!requireAuth &&
(widget.authenticationStore.state == AuthenticationState.allowed ||
widget.authenticationStore.state == AuthenticationState.allowedCreate)) {
@@ -170,8 +166,13 @@ class RootState extends State<Root> with WidgetsBindingObserver {
return;
}
- if (!_isInactive && widget.authenticationStore.state == AuthenticationState.allowed) {
- setState(() => _setInactive(true));
+ if (!_isInactive &&
+ (widget.authenticationStore.state == AuthenticationState.allowed ||
+ widget.authenticationStore.state == AuthenticationState.allowedCreate)) {
+ setState(() {
+ _requestAuth = false;
+ _setInactive(true);
+ });
}
if (widget.appStore.wallet?.type == WalletType.bitcoin) {
@@ -182,14 +183,13 @@ class RootState extends State<Root> with WidgetsBindingObserver {
break;
case AppLifecycleState.resumed:
-
- // Reset inactive state when app resumes
- if (_isInactive) setState(() => _setInactive(false));
-
widget.authService.requireAuth().then((value) {
if (mounted) {
setState(() {
_requestAuth = value;
+ if (!value && WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed) {
+ _setInactive(false);
+ }
});
}
});
### lib/view_model/setup_pin_code_view_model.dart
@@ -83,6 +83,7 @@ class SetupPinCodeViewModel {
}
await _authService.setPassword(repeatedPinCode);
+ _authService.saveLastAuthTime();
_settingsStore.pinCodeLength = pinCodeLength;
}
}Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.