Skip to content

Commit

Permalink
[shared_preferences] Limit Android decoding (#8187)
Browse files Browse the repository at this point in the history
When decoding stored preferences for `List<String>` support, only allow lists and strings to avoid potentially instantiating other object types.
  • Loading branch information
stuartmorgan authored Dec 2, 2024
1 parent a4ac811 commit 15501ec
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.4

* Restrict types when decoding preferences.

## 2.3.3

* Updates Java compatibility version to 11.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static class ListEncoder implements SharedPreferencesListEncoder {
public @NonNull List<String> decode(@NonNull String listString) throws RuntimeException {
try {
ObjectInputStream stream =
new ObjectInputStream(new ByteArrayInputStream(Base64.decode(listString, 0)));
new StringListObjectInputStream(new ByteArrayInputStream(Base64.decode(listString, 0)));
return (List<String>) stream.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
Expand Down Expand Up @@ -250,25 +249,17 @@ class SharedPreferencesPlugin() : FlutterPlugin, SharedPreferencesAsyncApi {
/** Class that provides tools for encoding and decoding List<String> to String and back. */
class ListEncoder : SharedPreferencesListEncoder {
override fun encode(list: List<String>): String {
try {
val byteStream = ByteArrayOutputStream()
val stream = ObjectOutputStream(byteStream)
stream.writeObject(list)
stream.flush()
return Base64.encodeToString(byteStream.toByteArray(), 0)
} catch (e: RuntimeException) {
throw RuntimeException(e)
}
val byteStream = ByteArrayOutputStream()
val stream = ObjectOutputStream(byteStream)
stream.writeObject(list)
stream.flush()
return Base64.encodeToString(byteStream.toByteArray(), 0)
}

override fun decode(listString: String): List<String> {
try {
val byteArray = Base64.decode(listString, 0)
val stream = ObjectInputStream(ByteArrayInputStream(byteArray))
return (stream.readObject() as List<*>).filterIsInstance<String>()
} catch (e: RuntimeException) {
throw RuntimeException(e)
}
val byteArray = Base64.decode(listString, 0)
val stream = StringListObjectInputStream(ByteArrayInputStream(byteArray))
return (stream.readObject() as List<*>).filterIsInstance<String>()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.sharedpreferences

import java.io.IOException
import java.io.InputStream
import java.io.ObjectInputStream
import java.io.ObjectStreamClass

/**
* An ObjectInputStream that only allows string lists, to prevent injected prefs from instantiating
* arbitrary objects.
*/
class StringListObjectInputStream(input: InputStream) : ObjectInputStream(input) {
@Throws(ClassNotFoundException::class, IOException::class)
override fun resolveClass(desc: ObjectStreamClass?): Class<*>? {
val allowList =
setOf(
"java.util.Arrays\$ArrayList",
"java.util.ArrayList",
"java.lang.String",
"[Ljava.lang.String;")
val name = desc?.name
if (name != null && !allowList.contains(name)) {
throw ClassNotFoundException(desc.name)
}
return super.resolveClass(desc)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package io.flutter.plugins.sharedpreferences

import android.content.Context
import android.util.Base64
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.mockk.every
import io.mockk.mockk
import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import org.junit.Assert
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.runner.RunWith

Expand Down Expand Up @@ -48,6 +52,7 @@ internal class SharedPreferencesTest {
every { flutterPluginBinding.binaryMessenger } returns binaryMessenger
every { flutterPluginBinding.applicationContext } returns testContext
plugin.onAttachedToEngine(flutterPluginBinding)
plugin.clear(null, emptyOptions)
return plugin
}

Expand Down Expand Up @@ -171,4 +176,24 @@ internal class SharedPreferencesTest {
Assert.assertNull(all[doubleKey])
Assert.assertNull(all[listKey])
}

@Test
fun testUnexpectedClassDecodeThrows() {
// Only String should be allowed in an encoded list.
val badList = listOf(1, 2, 3)
// Replicate the behavior of ListEncoder.encode, but with a non-List<String> list.
val byteStream = ByteArrayOutputStream()
val stream = ObjectOutputStream(byteStream)
stream.writeObject(badList)
stream.flush()
val badPref = LIST_PREFIX + Base64.encodeToString(byteStream.toByteArray(), 0)

val plugin = pluginSetup()
val badListKey = "badList"
// Inject the bad pref as a string, as that is how string lists are stored internally.
plugin.setString(badListKey, badPref, emptyOptions)
assertThrows(ClassNotFoundException::class.java) {
plugin.getStringList(badListKey, emptyOptions)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_android
description: Android implementation of the shared_preferences plugin
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.3.3
version: 2.3.4

environment:
sdk: ^3.5.0
Expand Down

0 comments on commit 15501ec

Please # to comment.