You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On iOS, the Keychain item that holds the encryption secret is written without a kSecAttrAccessible attribute, so iOS applies its default, kSecAttrAccessibleWhenUnlocked. There is no way to choose a different accessibility class.
ios/Plugin/Models/KeychainServices.swift, storeGenericPasswordFor (around lines 56–68): the SecItemAdd query contains only kSecClass, kSecAttrAccount, kSecAttrService and kSecValueData.
Same file, updateGenericPasswordFor (around lines 131–143): the SecItemUpdate attributes contain only kSecValueData.
This has two consequences for encrypted databases:
The secret can't be read while the device is locked. An app that keeps working after the screen locks (finishing a write, a background task, handling a notification action) can't open its encrypted database.
WhenUnlocked is not a ThisDeviceOnly class, so the secret can migrate to another device through an encrypted backup or restore. The database file itself is excluded from backup, so the secret and its database end up handled differently.
A related detail: UtilsSecret.getPassphrase (ios/Plugin/Utils/UtilsSecret.swift, around lines 46–54) returns "" on any Keychain error. A read that fails because the device is locked looks the same as "no secret stored", so a caller can wrongly decide it's a first run and set a new secret, which locks it out of the existing database.
To reproduce
Create an encrypted database with setEncryptionSecret, then open it.
Lock the device and let the app keep running (for example a short background task that opens the database a few seconds after locking).
The Keychain read fails (errSecInteractionNotAllowed), and the database can't be opened.
Expected behavior
Apps can choose the accessibility class for the secret, for example kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, which allows reads after the first unlock since boot and never leaves the device.
Proposed change
An optional iOS setting in the plugin config, for example "CapacitorSQLite": { "iosKeychainAccessibility": "afterFirstUnlockThisDeviceOnly" }, applied to both the add query and the update attributes. Omitting it keeps today's default, so existing apps are unaffected.
Optionally, in getPassphrase, tell errSecItemNotFound apart from other errors (or surface the error), so callers don't mistake a locked Keychain for a missing secret.
Related
#522 asked for the same option in 2024. It was closed after the requester's use case went away, and 8.1.1 still doesn't set the attribute. See also #480 about background access.
Activity