Sub topic 1
What you are protecting against
Before you add a package, decide what you are defending. "Secure storage" does not mean "safe from everyone". It protects against some attackers and does nothing against others. This chapter builds a threat model for secrets stored on a phone. The rest of the tutorial relies on it.
Prerequisites
This chapter assumes you already have the following:
- Basic Dart, including
async/awaitandFuture. - A Flutter app that builds for Android or iOS.
- Experience adding packages with
pub. - A basic idea of authentication tokens, meaning a short string your server gives the app after login that proves who the user is.
You do not need to know how the iOS Keychain or the Android Keystore work. Chapter 3 covers them. This chapter only needs the idea that the operating system offers a protected place for small secrets.
Why a threat model comes first
A threat model is a short, written answer to four questions:
- What are we protecting (the asset)?
- Who might attack it (the attacker)?
- How could they reach it (the attack path)?
- What does a successful attack cost the user?
Without it, you cannot tell whether a storage choice is adequate. A refresh token that gives access to a banking account needs more care than a "last viewed tab" preference. Treating both the same wastes effort on one and under-protects the other.
The asset: secrets that live on the device
A mobile app holds data in three places: in memory while it runs, on disk, and on the server. This tutorial covers only the disk. The question is where a value goes when the app is not running, so that it survives a restart.
Typical secrets that need to survive a restart:
- A refresh token, which lets the app get new access tokens without asking for a password again.
- An API key issued to one user (not one baked into the app for everybody).
- A local database encryption key.
- A session identifier for a service with no short-lived token flow.
Chapter 2 sorts values into "belongs" and "does not belong". For now, treat any value that would let someone act as the user as a secret.
Attackers and attack paths
Different attackers need different levels of effort. Order them by how much they need.
Another app on the same device
On both Android and iOS, apps run in sandboxes. Each app has its own private storage that other apps cannot read under normal conditions. Plain files and SharedPreferences (Android) or UserDefaults (iOS) are protected by this sandbox alone. If the sandbox holds, another app cannot read them.
The sandbox is the first line of defence, and a good one. Secure storage adds a second layer. It does not replace the first.
Someone who has the device, but not the passcode
A lost or stolen phone is the most realistic attack on an ordinary user. The attacker may try to pull data off the storage chip, or boot the phone in a special mode and copy files.
Here, plain files are weak. If the phone is unlocked or the storage is not fully encrypted, the attacker can read a token written in plain text. Modern phones encrypt storage by default, but the protection depends on the lock state and the OS version. Secure storage is designed for this case. Values are encrypted with keys that the operating system guards and ties to the device and, depending on settings, to the device being unlocked.
Someone with a backup
Backups copy app data off the device to a cloud account or a computer. An attacker who gets a backup file, or the account that owns it, can read whatever the backup contains. Plain preferences files often end up in backups. Keychain items and Keystore-protected data behave differently, and not always in the way you would expect. Chapter 9 covers this in detail. For now, remember that backups are an attack path and a source of bugs.
Someone who can read your app package
Anyone can download your app and unpack it. Anything compiled into the app, such as a hard-coded key or a "secret" string constant, is readable. Obfuscation slows this down but does not stop it. Secure storage cannot help here, because the secret never goes through storage. It is already in the binary.
An attacker with root or jailbreak access
On a rooted Android phone or a jailbroken iPhone, the attacker can bypass sandbox rules. They can also attach tools such as Frida to your running app and call functions inside it. If your app can read the token, so can code running inside your app's process.
This is the hard limit of on-device storage. It is covered under "Limits" in the last chapter. For now, accept this: secure storage protects data at rest, not data in use.
Malware and screen capture
Malware that runs with the user's permissions can sometimes capture screens, log keystrokes, or abuse accessibility services. Secure storage does not defend against these, because the secret is exposed once the app reads it.
How the attackers compare
| Attacker | Plain files / SharedPreferences | Secure storage |
|---|---|---|
| Another normal app | Blocked by sandbox | Blocked by sandbox and encryption |
| Thief with a locked device | Often protected by device encryption, weaker in some states | Stronger, keys are OS-managed |
| Person with a backup file | Often readable | Depends on platform and options |
| Someone unpacking your APK/IPA | Not relevant to stored data | Not relevant to stored data |
| Root or jailbreak plus instrumentation | Readable | Readable through your running app |
| Malware that runs inside the user session | Depends on permissions | Does not help once the app reads the value |
The table shows a pattern. Secure storage helps most with offline attacks, where someone has the device or its files but is not running code inside your app.
What you can and cannot promise
From the table you can write honest security claims for your app:
- "Tokens are encrypted at rest with keys managed by the operating system."
- "Tokens are not stored in plain files or preferences."
- "Tokens do not appear in logs or crash reports."
You cannot honestly claim these:
- "Tokens cannot be stolen."
- "Rooted devices are safe."
- "The app is secure because it uses a secure storage package."
Claims like these fail in security reviews and mislead users. Write the narrow claim you can verify.
A small illustration: plain versus secure
The two snippets below store the same token. The first uses shared_preferences. The second uses flutter_secure_storage. This chapter does not explain the package API (Chapter 5 does). The point is that the calls look similar while the protection differs.
import 'package:shared_preferences/shared_preferences.dart';
Future<void> saveTokenPlain(String token) async {
final prefs = await SharedPreferences.getInstance();
// Stored as readable text in an app-private file.
await prefs.setString('refresh_token', token);
}import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const _storage = FlutterSecureStorage();
Future<void> saveTokenSecure(String token) async {
// Stored encrypted, with a key held by the platform's secure facility.
await _storage.write(key: 'refresh_token', value: token);
}The code is almost the same. The difference is in what an attacker finds on disk. In the first case they find the token as readable text. In the second they find ciphertext, and decrypting it needs a key that the operating system controls.
Reducing the damage of a stolen secret
You cannot stop every theft, so limit what a stolen secret is worth. These measures belong on the server and in your token design, not in storage:
- Use short-lived access tokens and keep only the refresh token on disk.
- Make refresh tokens revocable, so a "sign out everywhere" action works.
- Rotate refresh tokens on use, so a copied token stops working once the real app refreshes.
- Never store the user's password. If you can store a token instead, do.
- Scope tokens narrowly, so one token cannot do everything.
Storage and token design work together. Good storage makes theft harder. Good token design makes theft less useful.
Common mistakes at this stage
- Mixing up the attacker. Teams add secure storage to defend against an attacker who already controls the device. It protects the wrong case.
- Storing too much. Putting entire user profiles in secure storage slows reads and does not make the data less sensitive to handle.
- Logging secrets. A token printed with
printduring debugging ends up in device logs. Remove those lines before release. - Treating the package as a compliance answer. Regulations talk about outcomes and risk, not about using one package.
Summary
Secure storage defends stored secrets against offline attackers: other apps, people who have the device, and people who read raw files. It does not defend against code running inside your app, rooted or jailbroken devices with instrumentation, malware, or secrets compiled into the binary. Write a short threat model before you store anything. Then limit what a stolen token can do through server-side design. The next chapter applies this model to decide which values belong in secure storage.
Exercise
A teammate says, "We use fluttersecurestorage, so a user on a rooted phone cannot leak their refresh token." Explain why the claim is too strong, and name one server-side measure that reduces the harm if the token leaks.
Show answer
Secure storage protects data at rest. On a rooted device an attacker can run instrumentation inside the app process and call the same code that reads the token, so the app decrypts it for them. The claim is therefore false. A server-side measure that reduces harm is refresh token rotation, short-lived access tokens, narrowly scoped tokens, or revocation (a "sign out everywhere" action). Any one of these is a valid answer.
Exercise
You find a hard-coded API key in your Dart source that your server requires from every client. Would moving it into fluttersecurestorage keep it secret from users? Why or why not?
Show answer
No. The key would still have to be compiled into the app or downloaded to it before it could be written to storage, so anyone who unpacks the app or watches its network traffic can get it. Secure storage protects values after they are on the device, not values that ship in the binary. A secret that must stay hidden from users must stay on your server.