Skip to content

iOS: encryption secret stored with default Keychain accessibility (WhenUnlocked); allow AfterFirstUnlockThisDeviceOnly #702

Open

Description

@XavierGreer

Plugin version: 8.1.1 (iOS, Capacitor 8)

Describe the bug

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:

  1. 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.
  2. 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

  1. Create an encrypted database with setEncryptionSecret, then open it.
  2. Lock the device and let the app keep running (for example a short background task that opens the database a few seconds after locking).
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureplatform: ios

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions