Sub topic 13
Limits of On-Device Secure Storage
This chapter assumes that you know basic Dart, async and await, Flutter package management, and the purpose of authentication tokens. The goal is to decide when flutter_secure_storage is suitable and when another design is needed.
What secure storage can protect
flutter_secure_storage stores small values through platform security features. On iOS, it uses the Keychain. On Android, it uses storage protected by the Android Keystore and related platform mechanisms.
This protection is useful when an attacker obtains a copy of the app's ordinary files. For example, a refresh token in a normal preferences file is usually easy to extract from an app backup or local app data. A token in Keychain or Keystore-backed storage has stronger protection and access controls.
Secure storage is appropriate for values such as:
- Refresh tokens.
- Small access tokens that must survive app restarts.
- Device or installation identifiers that must remain private.
- Small encryption keys used by another part of the app.
It is not a general-purpose database. It is also not a complete authentication system.
The threat model has limits
Secure storage protects data at rest. Data at rest is data saved while the application is not actively using it. Once your app reads a token, the token exists in the app's memory and can be sent over the network, logged accidentally, or exposed through a debugging mistake.
The package cannot fully protect a device that the user has deliberately compromised. A rooted Android device, jailbroken iPhone, attached debugger, malicious accessibility service, or injected code may observe values while your app uses them. Platform security reduces exposure, but it does not make a running application a sealed container.
The server must still support token expiration, revocation, rotation, and logout. If a refresh token is copied, local storage protection cannot revoke it by itself.
What it cannot replace
Do not put large application data in secure storage. User profiles, cached responses, images, and offline records belong in a database or file cache. Secure storage is intended for small key-value values. Large or frequent writes can add latency and make failures harder to diagnose.
Do not store a password merely because the package can store a string. A server should normally store a password verifier, not the user's password. The app should use a normal sign-in flow and retain only the credentials needed for its session policy.
Do not use secure storage as the only copy of data that must never be lost. Keychain and Keystore behavior can change after device migration, app removal, account changes, operating-system updates, or a failed restore.
Platform and lifecycle differences
The same Dart API can have different behavior on each platform:
| Risk or difference | Possible effect | Design response |
|---|---|---|
| Backup or device restore | Encrypted data may not be readable on the new device | Treat read failure as a new signed-out state |
| App uninstall or reinstall | Data may be deleted, retained, or restored differently by platform and settings | Do not assume uninstall always clears credentials |
| Keychain accessibility | A value may be unavailable before the first device unlock | Choose accessibility based on the app's startup needs |
| Web or desktop support | The backend and security guarantees differ from iOS and Android | Check the package documentation and platform threat model |
A successful write also does not mean that the server accepts the credential forever. The token can expire or be revoked immediately afterward. Your repository should handle an authentication failure by removing stale credentials and requiring sign-in again.
Choosing an alternative
Use ordinary preferences for non-secret settings such as a theme choice or a selected language. Use a database for structured and frequently queried data. Use a server-side session or token service when revocation, device management, or audit history matters. Add biometric authentication when the user must approve access at the moment of use. Biometrics add a user-presence check; they do not replace secure storage.
The right design often combines these pieces:
A practical decision checklist
Before storing a value, ask:
- Is it secret, or merely private?
- Does it need to survive an app restart?
- Can the app safely recover if the value becomes unreadable?
- Is the value small enough for key-value storage?
- Can the server expire or revoke it?
- Would a user action, such as a biometric check, be required before use?
- What should happen after sign-out, restore, uninstall, or device migration?
If the answers are unclear, store less data and make the recovery path explicit.
Exercise
Why is secure storage not enough to protect a refresh token on a rooted device?
Show answer
Secure storage protects the saved value, but a compromised device may observe the token after the app reads it into memory or sends it to a server. Token expiration, rotation, revocation, and careful logging are still required.