A password, token or key is stored in plain SharedPreferences

tx0/kotlin:secret-in-shared-preferencesrecommendedwarningsecurity/secretsdepth fastsecurity, android, cwe-312

What it finds

SharedPreferences are stored as a plain XML file in the app's data directory. A password, access token or API key put there can be read from a device backup, by anything with root access, or by the app itself after a path traversal bug (CWE-312). The Android keystore, or preferences encrypted with a key held there, keep the secret unreadable at rest.

Reported for putString on a preferences editor (in edit { }, on edit() or on a variable named as an editor) with a key, written as a string or a constant initialized with one, that names a password, secret, private or API key, or an access, refresh or authentication token, in a file that does not use EncryptedSharedPreferences. Push and registration tokens, which are not secrets, keys of secrets stored encrypted, tests and files that import nothing from Android are left alone.

From the rule's source, secret_in_preferences.rs.

Example from the rule's tests
import android.content.SharedPreferences const val KEY_TOKEN = "access_token" class Session(private val prefs: SharedPreferences) {    fun save(password: String, token: String, pushToken: String, name: String) {        prefs.edit().putString("password", password).apply()tx0 reports: the secret password is stored in plain preferences        prefs.edit().putString(KEY_TOKEN, token).apply()tx0 reports: the secret access_token        prefs.edit().putString("fcm_token", pushToken).apply()        prefs.edit().putString("user_name", name).apply()        prefs.edit().putString("password_hint", name).apply()        prefs.edit { putString("api_secret", token) }tx0 reports: the secret api_secret        prefs.edit { putString("encrypted_secret", token) }        bundle.putString("password", password)    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:secret-in-shared-preferences" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:secret-in-shared-preferences] reason="..."

tx0 explain tx0/kotlin:secret-in-shared-preferences shows the same in the terminal. Found a false positive? Report it.