-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[shared_preferences] update List<String> encode/decode #8335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
e772224
089d11d
29f3662
c0c08df
2ef0a01
bad5268
7a013b1
2c10fb7
614cbe4
e622f04
d317354
79ebe43
3a45c25
a49fb9f
e1f8fab
96d129b
e89421c
88ce597
7e28547
1cdce8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,24 +19,24 @@ | |
| import java.io.IOException; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.math.BigInteger; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** LegacySharedPreferencesPlugin */ | ||
| public class LegacySharedPreferencesPlugin implements FlutterPlugin, SharedPreferencesApi { | ||
| private static final String TAG = "SharedPreferencesPlugin"; | ||
| private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences"; | ||
| private static final String JSON_LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu!"; | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final String LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu"; | ||
| private static final String BIG_INTEGER_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy"; | ||
| private static final String DOUBLE_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu"; | ||
|
|
||
| private SharedPreferences preferences; | ||
| private SharedPreferencesListEncoder listEncoder; | ||
| private final SharedPreferencesListEncoder listEncoder; | ||
|
|
||
| public LegacySharedPreferencesPlugin() { | ||
| this(new ListEncoder()); | ||
|
|
@@ -73,13 +73,6 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin | |
|
|
||
| @Override | ||
| public @NonNull Boolean setString(@NonNull String key, @NonNull String value) { | ||
| // TODO (tarrinneal): Move this string prefix checking logic to dart code and make it an Argument Error. | ||
| if (value.startsWith(LIST_IDENTIFIER) | ||
| || value.startsWith(BIG_INTEGER_PREFIX) | ||
| || value.startsWith(DOUBLE_PREFIX)) { | ||
| throw new RuntimeException( | ||
| "StorageError: This string cannot be stored as it clashes with special identifier prefixes"); | ||
| } | ||
| return preferences.edit().putString(key, value).commit(); | ||
| } | ||
|
|
||
|
|
@@ -99,6 +92,7 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin | |
| return preferences.edit().remove(key).commit(); | ||
| } | ||
|
|
||
| // Deprecated, for testing purposes only. | ||
tarrinneal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public @NonNull Boolean setStringList(@NonNull String key, @NonNull List<String> value) | ||
| throws RuntimeException { | ||
|
|
@@ -131,14 +125,13 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin | |
|
|
||
| // Gets all shared preferences, filtered to only those set with the given prefix. | ||
| // Optionally filtered also to only those items in the optional [allowList]. | ||
| @SuppressWarnings("unchecked") | ||
| private @NonNull Map<String, Object> getAllPrefs( | ||
| @NonNull String prefix, @Nullable Set<String> allowList) throws RuntimeException { | ||
| Map<String, ?> allPrefs = preferences.getAll(); | ||
| Map<String, Object> filteredPrefs = new HashMap<>(); | ||
| for (String key : allPrefs.keySet()) { | ||
| if (key.startsWith(prefix) && (allowList == null || allowList.contains(key))) { | ||
| filteredPrefs.put(key, transformPref(key, allPrefs.get(key))); | ||
| filteredPrefs.put(key, transformPref(key, Objects.requireNonNull(allPrefs.get(key)))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was requireNonNull needed here? Also this code is getting a bit difficult to parse. (A loop with a conditional that sets some transformed filtered data) Consider breaking parts out into methods or adding comments.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a change suggested by a warning I was getting in Android Studio. Since the method being called requires nonnull arguments and getting a value from a map doesn't programmatically guarantee a value. In this context, there would always be a value though. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -148,32 +141,14 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin | |
| private Object transformPref(@NonNull String key, @NonNull Object value) { | ||
| if (value instanceof String) { | ||
| String stringValue = (String) value; | ||
| if (stringValue.startsWith(LIST_IDENTIFIER)) { | ||
| if (stringValue.startsWith(JSON_LIST_IDENTIFIER)) { | ||
|
||
| return value; | ||
| } else if (stringValue.startsWith(LIST_IDENTIFIER)) { | ||
| return listEncoder.decode(stringValue.substring(LIST_IDENTIFIER.length())); | ||
| } else if (stringValue.startsWith(BIG_INTEGER_PREFIX)) { | ||
| // TODO (tarrinneal): Remove all BigInt code. | ||
| // https://github.com/flutter/flutter/issues/124420 | ||
| String encoded = stringValue.substring(BIG_INTEGER_PREFIX.length()); | ||
| return new BigInteger(encoded, Character.MAX_RADIX); | ||
| } else if (stringValue.startsWith(DOUBLE_PREFIX)) { | ||
| String doubleStr = stringValue.substring(DOUBLE_PREFIX.length()); | ||
| return Double.valueOf(doubleStr); | ||
| } | ||
| } else if (value instanceof Set) { | ||
tarrinneal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO (tarrinneal): Remove Set code. | ||
| // https://github.com/flutter/flutter/issues/124420 | ||
|
|
||
| // This only happens for previous usage of setStringSet. The app expects a list. | ||
| @SuppressWarnings("unchecked") | ||
| List<String> listValue = new ArrayList<>((Set<String>) value); | ||
| // Let's migrate the value too while we are at it. | ||
| preferences | ||
| .edit() | ||
| .remove(key) | ||
| .putString(key, LIST_IDENTIFIER + listEncoder.encode(listValue)) | ||
| .apply(); | ||
|
|
||
| return listValue; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the breaking changes required? if not consider fixing/mitigating the security issue independent of the breaking changes. It makes it more likely for them to be adopted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking changes to
shared_preferenceshave the potential to be quite disruptive to the ecosystem; I would strongly prefer we keep the legacy type support around to making a breaking change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I originally expected this change was going to end up being "technically" breaking due to the new prefix (before we discussed the prefix extension plan) so I wanted to wrap these long awaited breaking changes in as well. I'll revert them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I revert the error handling changes as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this.