When building secure software, developers often spend weeks hardening authentication:
- Implementing 50,000-iteration PBKDF2 key derivation.
- Integrating Android BiometricPrompt with cryptographic cipher objects.
- Setting up hardware Keystore keys.
And then, once the user successfully authenticates, they make a fatal operational mistake:
They leave the session unlocked indefinitely.
Consider what happens in real life:
- You authenticate and open your secret emergency fund. You check your balance.
- Your friend calls your name, or your phone rings.
- You set your phone down on a table or slip it into your pocket without explicitly tapping "Lock".
- Three minutes later, a curious roommate or coworker picks up your phone to look at a photo.
- They tap the open finance app. Because your session never expired, your secret vault is wide open.
In security engineering, authentication is only half the battle; session lifecycle management is the other half.
Here is how FinLedger architected an Ephemeral, In-Memory Vault Session Engine (VaultSecurityManager.kt, VaultSecurityState.kt) that prevents private financial records from lingering in memory.
The Threat Model of Lingering Sessions
flowchart TD subgraph LingeringRisk["The Persistent Session Threat Model"] Auth["User Unlocks Vault via Biometrics"] --> Open["Vault Session Marked 'Unlocked'"] Open --> Distraction["User Distracted / Sets Phone Down on Desk"] Distraction --> Risk1["Threat 1: Shoulder Surfing & Physical Access Colleague picks up phone. Vault remains 100% unlocked."] Distraction --> Risk2["Threat 2: Process Snapshot / RAM Dumps Session state saved to disk/cache. Malicious tools inspect lingering memory."] Risk1 --> Breach["Complete Compromise of Confidential Reserves"] Risk2 --> Breach end To defend against physical access and process snooping:
- An unlocked vault state must never be written to persistent flash storage (no SQLite flags, no SharedPreferences).
- The session must be strictly bounded in time.
- Inactivity must trigger deterministic auto-locking.
The Ephemeral In-Memory Architecture (VaultSecurityManager.kt)
In FinLedger, the vault's security state is managed by a dedicated singleton holding an ephemeral Kotlin StateFlow<VaultSecurityState>:
flowchart TD subgraph EphemeralPipeline["FinLedger Ephemeral Session Lifecycle"] direction TB LockedState["1. Initial State: VaultSecurityState.Locked Vault tables closed. Compose screens unmounted."] LockedState --> AuthEvent["User Authenticates via Passcode / Biometrics"] AuthEvent --> UnlockedState["2. VaultSecurityState.Unlocked(expiresAt) - Token held STRICTLY in volatile RAM - ZERO disk persistence - 5-Minute Sliding Inactivity Timer Started"] UnlockedState --> Touch["User Interacts with Screen: Touch event detected in Jetpack Compose"] Touch --> ResetTimer["Reset Expiry Timestamp: expiresAt = System.currentTimeMillis() + 5_MINUTES"] ResetTimer --> UnlockedState UnlockedState --> AutoLockCheck{"Inactivity Check: Has 5 minutes elapsed without touch?"} AutoLockCheck -->|YES: Idle Timeout| TriggerLock["3. Execute Deterministic Auto-Lock"] UnlockedState --> BackgroundCheck{"App Minimized / onStop()?"} BackgroundCheck -->|YES: App Backgrounded| TriggerLock TriggerLock --> ScrubRAM["4. Memory Scrubbing: Overwrite volatile credentials with zeros. Emit VaultSecurityState.Locked. Screens unmounted immediately."] end Let's examine how each layer operates:
1. Zero Disk Persistence
The unlocked session token exists strictly in volatile device RAM.
If Android terminates the app process, or if the user swipes the app away from the App Switcher, the memory is instantly reclaimed by the OS kernel. There is zero persisted boolean flag in SQLite or preferences that a rogue process could flip to bypass authentication on the next boot.
2. The 5-Minute Sliding Inactivity Window
When unlocked, VaultSecurityState.Unlocked records an expiresAt millisecond timestamp initialized to System.currentTimeMillis() + 300_000L (5 minutes).
In Jetpack Compose, the root vault scaffold wraps its children in a gesture detector:
- Every tap, scroll, or transaction entry resets the timer, extending the window by another five minutes.
- If no touch events occur for 300 seconds, an internal coroutine timer fires, transitioning the state back to
VaultSecurityState.Locked.
3. Immediate Background Lockout (onStop)
Leaving an unlocked vault in your pocket is a massive security hazard.
The moment the user presses the Android Home button or switches to another app:
- The Activity lifecycle transitions to
onStop(). -
VaultSecurityManagerintercepts the lifecycle event and invalidates the session token immediately. - When the user reopens FinLedger, the vault is locked tight, requiring fresh biometric authentication.
4. Explicit Memory Scrubbing
In high-level languages like Java and Kotlin, strings are immutable and managed by the garbage collector. If you store a password in a standard String, it may linger in heap memory for minutes until the next GC sweep.
FinLedger handles authentication passcodes exclusively as CharArray buffers:
- The moment authentication completes or the vault locks, the array is explicitly scrubbed:
Arrays.fill(passcodeChars, '0') - The sensitive plaintext is overwritten in place, eliminating credentials from mobile memory dumps.
Key Takeaway
A secure vault that stays unlocked is no vault at all.
By engineering ephemeral, in-memory session lifecycles with sliding 5-minute inactivity timeouts, automatic background lockout, and explicit memory scrubbing, FinLedger ensures that your secret emergency funds stay accessible when you need them, and completely protected the moment you look away.
FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.
Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app
