As iOS lead, what Keychain Services standard would you set for each secret's accessibility class, iCloud sync, access group and biometric flag, and how should the app handle reinstall?
answer
- four choices per item
- when, where, who, what proof
- ThisDeviceOnly cannot sync
- current set costs sign-ins
- reinstall leftovers: observed, not promised
basics
~20 sFix four choices per secret in one wrapper: an explicit kSecAttrAccessible class, sync only for user-visible passwords, a named access group only where an extension needs it, and biometryCurrentSet only for high-value secrets. Detect reinstall leftovers with a first-launch marker.
solid answer
~40 sI would write the standard as a table and enforce it through one keychain wrapper. Background-read tokens get `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly`, no sync, and the app's own group unless an extension reads them, in which case a named keychain access group. High-value secrets such as a digital key get `kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly` with `.biometryCurrentSet`, accepting forced sign-ins after re-enrolment. Only passwords the user expects on every device set `kSecAttrSynchronizable`, which rules out `ThisDeviceOnly` classes and makes every update or delete hit all copies. Every add sets its class explicitly and the deprecated `Always` classes are banned. On reinstall, items commonly survive app deletion though Apple does not promise it, so a first-launch marker in the app container tells the app to delete leftovers by service before anyone is signed in silently.
go deeper
Recall the four settings every keychain item has: accessibility class, sync flag, access group and an optional biometric access control.
Explain what each setting does in the API: ThisDeviceOnly versus syncing, default access groups, and how biometryCurrentSet differs from biometryAny.
Show the operational edges: background reads before first unlock, items stranded in the wrong group, leftovers after reinstall, and deletes that reach shared or synced copies.
Own the tradeoffs per secret, security against forced sign-ins and convenience against exposure, and make them enforceable through a wrapper, lint rules and a device test matrix.
## What the standard decides A team storing secrets in the keychain makes the same four choices for every item: **when** its data can be read (`kSecAttrAccessible`), **where** it may travel (`kSecAttrSynchronizable` and the `ThisDeviceOnly` suffix), **who** may read it (`kSecAttrAccessGroup`), and **what the user must prove** first (`SecAccessControl` flags). Left to each feature these drift: one engineer relies on the default class, another adds a synchronizable copy, a third deletes by class alone. A written standard, enforced by one wrapper type, keeps them consistent and reviewable. ## A starting table | Secret | Accessibility | Sync | Access group | Flag | |---|---|---|---|---| | Refresh token read by background refresh | `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly` | No | App's own | None | | Secret a widget or other extension reads | `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly` | No | Named keychain access group | None | | Digital key or payment secret | `kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly` inside a `SecAccessControl` | No | App's own | `.biometryCurrentSet` | | Password the user expects on all devices | `kSecAttrAccessibleAfterFirstUnlock` or `kSecAttrAccessibleWhenUnlocked` | Yes | Named group if several apps use it | None | The judgment lives in the rows, not in the API: each row is a tradeoff the lead signs off. ## The tradeoffs a lead owns - **`.biometryCurrentSet` or `.biometryAny`.** The current-set flag invalidates the item when a face or finger is enrolled, which stops someone who learned the passcode from adding their own; the price is a forced sign-in whenever a member re-enrols, which arrives as support requests. Reserve it for secrets that move money or open things. - **`ThisDeviceOnly` or migrating.** Without the suffix, items move to a new phone restored from an encrypted backup, so members stay signed in. With it, a backup restored onto another phone does not carry them. Session tokens rarely need to move. - **iCloud sync.** A synchronizable item cannot use a `ThisDeviceOnly` class, and updating or deleting it changes every copy on every device. Since iOS 14 the keychain syncs keys and certificates as well as passwords. Sync only what the user would expect to see on another device. - **Keychain access group or App Group.** Both share items with extensions; an App Group also shares files and `UserDefaults` and can never be the default group. A dedicated keychain group keeps the sharing explicit. ## Reinstall: observed, not promised It is commonly observed that keychain items remain after a user deletes and reinstalls an app, but Apple does not document this as guaranteed, so the standard must work whether or not they remain: 1. On launch, look for a first-launch marker kept in the app container, such as a `UserDefaults` flag, which is removed together with the app. 2. If the marker is missing, treat any keychain items as leftovers from an earlier install: delete them with queries scoped by `kSecAttrService` and access group, then write the marker. 3. Never let a token found on a first launch sign someone in silently; it may belong to an account the member has since left, or to a session the server already revoked. Scope those deletes carefully. A query holding only `kSecClass` searches every access group the app belongs to, so it would also remove items shared with the team's other apps. A query without `kSecAttrSynchronizable` matches only non-synchronizable items, so synced passwords need their own decision, remembering that deleting one removes it from every device. ## Making the standard stick - **One wrapper owns every SecItem call.** It builds queries, sets an explicit `kSecAttrAccessible` on every add, performs update-or-add, and maps `OSStatus` to typed results, with `errSecInteractionNotAllowed` meaning "retry later". - **Ban the deprecated classes.** `kSecAttrAccessibleAlways` and `kSecAttrAccessibleAlwaysThisDeviceOnly` are deprecated since iOS 12; a lint rule or a code search in CI catches them. - **Name services centrally,** so sign-out and the reinstall cleanup delete exactly the app's items. - **Ship migrations with standard changes.** A new accessibility class needs `SecItemUpdate`; a new access group needs copy-then-delete, because the group is part of the item's identity. - **Test the hard states on devices:** locked, freshly rebooted, Face ID re-enrolled, restored from a backup, and deleted then reinstalled. ## Reviewing a pull request against it A reviewer holding the standard asks a short list of questions of any change that touches the keychain: - Does every `SecItemAdd` go through the wrapper, with an explicit class from the table? - Does a new `kSecAttrAccessGroup` appear in the entitlements of exactly the targets that need it, and no others? - Does a new synchronizable item have an owner who accepts that its deletes reach every device? - Does a biometric flag come with a recovery path for members who re-enrol or get locked out? - Does the change alter an existing item's class or group, and if so, does it ship the migration?
- Why not make every secret synchronizable so members never sign in again on a new iPhone?A synchronizable item cannot use a `ThisDeviceOnly` class, and updating or deleting it affects every copy, so a sign-out on one phone signs the member out everywhere. A session the server issued to one device would also appear on the member's other devices. Sync suits passwords the user owns, not session or device keys.
- What should the first-launch cleanup do with items in a keychain access group shared with the team's other apps?Leave them unless this app owns them. A missing marker proves only that this app was reinstalled, not that the sibling apps were. Scope the cleanup by `kSecAttrService` and the app's own group, and let shared items follow the owning app's rules.
- Deleting the app always deletes its keychain items, so reinstall needs no handling.
- A ThisDeviceOnly item can still sync through iCloud Keychain.
- biometryCurrentSet should be the default for every keychain item.
- Relying on the default accessibility class is fine for background work.
- Deleting a synchronizable item affects only the copy on this device.