Skip to content

secret_in_plain_preferences

Reports a credential kept with shared_preferences, which stores it as plain text. Keep it in the platform’s secure store instead.

Tier
advisory
Scope
Flutter
Lowest preset
recommended
Fix
none
Security class
CWE-312, CWE-922 · mobile risk M9 (insecure data storage) · severity 5.5

A setString or setStringList of SharedPreferences, SharedPreferencesAsync or SharedPreferencesWithCache that keeps a credential. A call counts when:

  • its key is a credential name, with its words split as a name’s are: 'refresh_token', 'user.password', 'credentials'. The key may be a literal or a constant: kRefreshTokenKey, Keys.password.
  • its value comes from a credential, within the function: a text read by a credential name, such as token or session.accessToken. For setStringList, an element of the list literal counts.

The message names what the call keeps.

It does not report:

  • a push or page token: fcm_token, deviceToken, nextPageToken. These are not credentials.
  • anything kept under a key that names one of those, whatever words follow its token: 'fcm_token', 'deviceToken', 'push_token_key', 'next_page_token'. The key tells what is kept, so setString('fcm_token', token!) is silent though the value is named token.
  • setBool, setInt and setDouble: rememberPassword: true keeps no secret.
  • a secret passed to your own code first, such as seal(token);
  • debug-only code, such as code under kDebugMode, and tests.

shared_preferences writes its values to a plain-text file of the app: an XML file on Android, a property list on iOS and macOS. A device backup, a rooted or jailbroken device, or a lost phone with its storage read out gives them away. A token kept there opens the account it belongs to.

The platform’s secure store keeps them encrypted with the device’s keys: the Keychain on iOS and macOS, the Keystore on Android.

Violating
await preferences.setString('refresh_token', session.refreshToken);
Corrected
await const FlutterSecureStorage().write(key: 'refresh_token', value: session.refreshToken);

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

In SARIF, the rule’s descriptor carries the tags security, external/cwe/cwe-312, external/cwe/cwe-922 and external/owasp-mobile-2024/m9, and the security-severity 5.5. No other report changes. See Security.

Write why on the line directly above the suppression:

// The token of the public demo account, shown on the store page.
// ignore: assay/secret_in_plain_preferences

// ignore_for_file: assay/secret_in_plain_preferences silences it in a whole file, and // ignore: secret_in_plain_preferences without the prefix works too. See Suppressions.