gpt4 book ai didi

android - 在 React Native 中保存敏感数据

转载 作者:IT王子 更新时间:2023-10-28 23:59:00 26 4
gpt4 key购买 nike

我正在构建一个 React Native 应用程序,我需要保存一些敏感数据,例如 token 和刷新 token 。显而易见的解决方案是使用 AsyncStorage 保存该信息。 .问题在于 AsyncStorage 的安全级别。

AsyncStorage provides a way to locally store tokens and data. It can be, in some ways, compared to a LocalStorage option. In full production applications, it is recommended to not access AsyncStorage directly, but instead, to use an abstraction layer, as AsyncStorage is shared with other apps using the same browser, and thus an ill-conceieved removal of all items from storage could impair the functioning of neighboring apps.

https://auth0.com/blog/adding-authentication-to-react-native-using-jwt/

在 native 应用程序中,我会在 iOS 中选择 Keychain,在 private 模式 中选择 Shared Preferences 安卓

对于我在 React Native 提供的文档中读到的内容:

On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.

https://facebook.github.io/react-native/docs/asyncstorage.html

他们从不谈论这些数据的安全性。

最好的解决方案是为 Android 创建一个模块(在 private 模式 中使用 Shared Preferences)和另一个为 iOS (使用Keychain)来保存敏感数据?或者使用提供的 AsyncStorage 方法是否安全?

最佳答案

刚刚深入研究了 React Native 代码,我找到了答案。

安卓

React Native AsyncStorage模块实现基于SQLiteOpenHelper。处理所有数据类的包:https://github.com/facebook/react-native/tree/master/ReactAndroid/src/main/java/com/facebook/react/modules/storage

具有创建数据库说明的类:https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/storage/ReactDatabaseSupplier.java

根据 Android 文档,应用程序创建的数据库保存在与应用程序关联的私有(private)磁盘空间中,因此是安全的。

Just like files that you save on the device's internal storage, Android stores your database in private disk space that's associated application. Your data is secure, because by default this area is not accessible to other applications.

Source

iOS

在 iOS 中,AsyncStorage 值保存在序列化的字典文件中。这些文件保存在应用程序 NSDocumentDirectory 中。在 iOS 中,所有应用程序都存在于它们自己的沙盒中,因此一个应用程序的所有文件都是安全的,其他应用程序无法访问它们。

iOS 中处理 AsyncStorage 模块的代码可以在这里找到:https://github.com/facebook/react-native/blob/master/React/Modules/RCTAsyncLocalStorage.m

正如我们所见 here用于存储 AsyncStorage 保存的值的文件保存在 NSDocumentDirectory 下(在应用程序沙箱环境中)。

Every App Is an Island An iOS app’s interactions with the file system are limited mostly to the directories inside the app’s sandbox. During installation of a new app, the installer creates a number of containers for the app. Each container has a specific role. The bundle container holds the app’s bundle, whereas the data container holds data for both the application and the user. The data container is further divided into a number of directories that the app can use to sort and organize its data. The app may also request access to additional containers—for example, the iCloud container—at runtime.

Source

结论

使用 AsyncStorage 保存用户 token 是安全的,因为它们保存在安全上下文中。

请注意,这仅适用于没有 root 的 Android 设备和没有 越狱 的 iOS 设备。另请注意,如果攻击者对设备具有物理访问权限,并且该设备不 protected 。他可以将设备连接到 mac 笔记本电脑和 extract the documents目录并查看documents目录下保存的所有内容。

关于android - 在 React Native 中保存敏感数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39028755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com