Reviewing a SwiftUI pull request that keeps an auth token in @AppStorage and a whole article list in @SceneStorage, what do you flag and where should each go?
answer
- preferences, not secrets
- lightweight per-scene UI state
- store the ID, not the model
- values outlive sign-out
basics
~20 sBoth are misuse. @AppStorage is a UserDefaults preference without Keychain protection, so the token belongs in the Keychain. @SceneStorage is for lightweight per-scene UI state, so keep only the open article's ID there and the list in a database or files.
solid answer
~40 sI would block both. `@AppStorage` writes to `UserDefaults`, a preferences store with none of the Keychain's access control, and the value outlives sign-out until code removes it, so the token moves to the Keychain behind an auth service and the view never sees it. `@SceneStorage` is meant for lightweight UI state: Apple's documentation says large data such as model data should not be stored there because performance may suffer, and that it must not hold sensitive data. The system also decides when to save it and discards it when the scene is destroyed, so it is not a store of record. The fix is to keep the selected article's ID in `@SceneStorage`, load the articles from the app's database or files, and resolve the ID on restore.
go deeper
Recall the rule: @AppStorage for small settings, @SceneStorage for what a window was showing, never passwords or tokens in either.
Explain why: UserDefaults is a preferences store without Keychain protection, and Apple limits @SceneStorage to lightweight, non-sensitive values it saves at times it chooses.
Review like an owner: name each misuse with its consequence, propose the ID-plus-lookup fix, remove the leaked key on upgrade, and clear user-specific keys on sign-out.
Turn the review into a team rule: one table that maps each kind of data to its store, enforced in code review and in shared key definitions.
## What each wrapper is for Both SwiftUI wrappers persist a value for a view, which makes them tempting for anything that "needs to be saved". Their documented purposes are narrow: | | `@AppStorage` | `@SceneStorage` | |---|---|---| | Backed by | `UserDefaults` | Per-scene storage the system manages | | Meant for | Small user preferences | Lightweight UI state to restore a scene | | Limit | A preferences store, not a secure one | Apple: not for sensitive data, not for large data such as model data | | Survives | Relaunch, sign-out, until removed | Relaunch, while the scene is not destroyed | ## Why the token cannot live in @AppStorage - **It is the wrong kind of store.** `UserDefaults` is for preferences: plain values in the app's preferences file, with none of the access control the Keychain gives credentials. - **It is visible to every view.** Any view can declare the same key and read the token, so the secret leaks into the UI layer. - **It outlives the session.** `UserDefaults` is not tied to a signed-in user; signing out does nothing to the value unless code removes it. - **A shared suite makes it worse.** If the app later points `@AppStorage` at an App Group suite, every extension in the group can read the token. The fix is to move the token into the Keychain behind an authentication service that the view layer never sees; the view only learns "signed in or not". ## Why the article list cannot live in @SceneStorage Apple's documentation for `SceneStorage` says to keep the data **lightweight**, that "data of a large size, such as model data, should not be stored in `SceneStorage`, as poor performance may result", and not to use it with sensitive data. It also says: - The system makes **no guarantee when or how often** the data is persisted. - The data is **destroyed with the scene**, for example when the person removes the window's snapshot from the iPad app switcher. - The backing data is **not available to you** except through the wrapper. So the list would be slow to save and restore, could be lost at any moment, and would be duplicated in every window. Articles are model data; they belong in a database or in files. ## The fix in the pull request 1. Replace the token property with a call to an authentication service that stores the token in the Keychain. 2. Delete the old `@AppStorage` key on upgrade, so the token does not linger on devices that ran the old build. 3. Replace the article-list `@SceneStorage` with `@SceneStorage("reader.openArticleID") var openArticleID: String?`. 4. Load articles from the data layer, and resolve the restored ID against it, falling back to the list when the article no longer exists. 5. Add a review note to the team's guidelines: preferences in `@AppStorage`, per-window UI state in `@SceneStorage`, secrets in the Keychain, content in the database. ## How to tell a preference from data A quick test helps reviewers who see these wrappers often: - **Would a person describe it as a setting?** Font size, theme, "show images": a preference, so `@AppStorage`. - **Does it describe what one window is showing right now?** Selected article, selected tab: UI state, so `@SceneStorage`, as an identifier. - **Would losing it lose the person's work or expose them?** Saved articles, notes, tokens: data or secrets, so a database, files or the Keychain. - **Does it grow with use?** Anything that grows is not a preference, however small it starts. ## Other review smells in the same area - **User-specific preferences** in `@AppStorage` with no clearing on sign-out, so the next person on a shared iPad inherits them. - **Large encoded `Data` blobs** in `@AppStorage`: every write goes to the preferences store and updates every view reading that key. - **Derived values** persisted next to their source, which drift apart after an update. - **Unscoped keys** such as `"selected"` reused by two features that then overwrite each other. A strong review comment names the rule and the consequence in one line each: "token in UserDefaults: not secure storage and survives sign-out; move to the Keychain" and "model data in SceneStorage: Apple says lightweight only and saving is not guaranteed; store the ID".
- A user signs out of a SwiftUI app; what happens to the preferences they set through @AppStorage?Nothing on its own: `UserDefaults` is not tied to a session, so the values stay until code removes them. On a shared device the next person inherits them. Remove user-specific keys on sign-out, or namespace them by account and keep only device-wide preferences, such as text size, unscoped.
- Is a large encoded Data value in @AppStorage a problem even if it is not secret?Yes. `UserDefaults` is meant for small preferences: each write rewrites the stored value and invalidates every view that reads the key, and the payload has no schema, migrations or partial reads. Once a value grows into a collection or user content, it belongs in a database or a file, with `@AppStorage` keeping at most a small pointer or flag.
- A token in @AppStorage is fine because the sandbox protects it.
- @SceneStorage is safe for secrets because the system manages it.
- Saving the whole model in @SceneStorage makes restoration more reliable.
- Signing out clears @AppStorage values automatically.
- @SceneStorage can be the app's store of record for content.