Skip to content

Commit f1c08bc

Browse files
committed
Add value-preserving steps for SharedPreferences
1 parent ebdf553 commit f1c08bc

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private module Frameworks {
8282
private import semmle.code.java.frameworks.android.ContentProviders
8383
private import semmle.code.java.frameworks.android.Intent
8484
private import semmle.code.java.frameworks.android.Notifications
85+
private import semmle.code.java.frameworks.android.SharedPreferences
8586
private import semmle.code.java.frameworks.android.Slice
8687
private import semmle.code.java.frameworks.android.SQLite
8788
private import semmle.code.java.frameworks.android.Widget

java/ql/lib/semmle/code/java/frameworks/android/SharedPreferences.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes related to `android.content.SharedPreferences`. */
22

33
import java
4+
import semmle.code.java.dataflow.ExternalFlow
45

56
/** The interface `android.content.SharedPreferences`. */
67
class SharedPreferences extends Interface {
@@ -55,3 +56,19 @@ class StoreSharedPreferenceMethod extends Method {
5556
this.hasName(["commit", "apply"])
5657
}
5758
}
59+
60+
private class SharedPreferencesSummaries extends SummaryModelCsv {
61+
override predicate row(string row) {
62+
row =
63+
[
64+
"android.content;SharedPreferences$Editor;true;clear;;;Argument[-1];ReturnValue;value",
65+
"android.content;SharedPreferences$Editor;true;putBoolean;;;Argument[-1];ReturnValue;value",
66+
"android.content;SharedPreferences$Editor;true;putFloat;;;Argument[-1];ReturnValue;value",
67+
"android.content;SharedPreferences$Editor;true;putInt;;;Argument[-1];ReturnValue;value",
68+
"android.content;SharedPreferences$Editor;true;putLong;;;Argument[-1];ReturnValue;value",
69+
"android.content;SharedPreferences$Editor;true;putString;;;Argument[-1];ReturnValue;value",
70+
"android.content;SharedPreferences$Editor;true;putStringSet;;;Argument[-1];ReturnValue;value",
71+
"android.content;SharedPreferences$Editor;true;remove;;;Argument[-1];ReturnValue;value"
72+
]
73+
}
74+
}

java/ql/test/query-tests/security/CWE-312/CleartextStorageSharedPrefsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ public void testSetSharedPrefs6(Context context, String name, String password)
8989
.create(context, "secret_shared_prefs", masterKey,
9090
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
9191
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)
92-
.edit().putString("name", name) /// Safe
92+
.edit().putString("name", name) // Safe
9393
.putString("password", password); // Safe
9494

9595
editor.commit();
9696
}
97+
98+
public void testSetSharedPrefs7(Context context, String name, String password) {
99+
SharedPreferences sharedPrefs =
100+
context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
101+
sharedPrefs.edit().putString("name", name).apply(); // Safe
102+
sharedPrefs.edit().putString("password", password).apply(); // $hasCleartextStorageSharedPrefs
103+
}
97104
}

0 commit comments

Comments
 (0)