Skip to content

Commit

Permalink
fix: Use Mutex to ensure no concurrent encryption operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ovitrif committed Nov 5, 2024
1 parent e096a73 commit eb6df3b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion android/src/main/java/com/oblador/keychain/KeychainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

@ReactModule(name = KeychainModule.KEYCHAIN_MODULE)
@Suppress("unused")
Expand Down Expand Up @@ -132,6 +134,9 @@ class KeychainModule(reactContext: ReactApplicationContext) :
/** Launches a coroutine to perform non-blocking UI operations */
private val coroutineScope = CoroutineScope(Dispatchers.Default)

/** Mutex to prevent concurrent calls to Cipher, which doesn't support multi-threading */
private val mutex = Mutex()

// endregion
// region Initialization
/** Default constructor. */
Expand Down Expand Up @@ -207,7 +212,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
val storage = getSelectedStorage(options)
throwIfInsufficientLevel(storage, level)

val result = storage.encrypt(alias, username, password, level)
val result = mutex.withLock { storage.encrypt(alias, username, password, level) }
prefsStorage.storeEncryptedEntry(alias, result)
val results = Arguments.createMap()
results.putString(Maps.SERVICE, alias)
Expand Down

0 comments on commit eb6df3b

Please sign in to comment.