# Device storage: private snapshots sealed with a device key

The repository's architectural decision, rationale and consequences.

# Device storage: private snapshots sealed with a device key

**Status:** accepted · **Date:** 2026-11-28

**Enforcement:** `apps/mobile/src/platform/offline/sealed-store.test.ts` (format, key, store, sign-out order), `packages/env/src/native.test.ts` (the switch). Nothing proves the native adapters on a device yet; see "Not proven".

The native app keeps the last value a reader saw, per server, User and Organization, as plain JSON in the cache folder (`platform/offline`, [ADR-0013](0013-mobile-features-mirror-the-backend-shape.md)). Four projects created from the template needed more than that and each built on-device storage with its own crypto: two encrypt a whole SQLite database with SQLCipher, one adds a PIN-wrapped key hierarchy on OpenSSL, one stores a mutation queue in plain files. Three of the four generate a random key and keep it in the platform key store; two seal files with the AES-GCM that `expo-crypto` already ships. The template had no decision, so each project chose its own key handling and its own answer to sign-out.

## Decision [#decision]

1. **The key.** One random 256-bit key per installation, generated on first use and stored in `expo-secure-store` as `private_snapshot_key_v1` with `WHEN_UNLOCKED_THIS_DEVICE_ONLY`: it stays in the Keychain or Keystore of this device, is not in a backup and is readable only while the device is unlocked. It is not derived from the account, a password or a PIN, and it is not biometric-gated; the app lock keeps protecting what is shown.
2. **The store.** The existing private snapshot files: one file per namespace and scope under the cache folder (`private-snapshots-v1/<namespace>/<scope>.sealed`), with the limits they already have (2 MiB, 24 hours). The cache folder is not part of a device backup, and the system may evict it; a snapshot is a cache, never the only copy.
3. **What is encrypted.** The whole snapshot (value, scope and time) with AES-256-GCM from `expo-crypto` (CryptoKit on iOS, `javax.crypto` on Android), a fresh 12-byte nonce per write, in the format `[version 1][nonce][ciphertext][16-byte tag]`. The namespace and the scope are the additional authenticated data, so a file moved into another account's or Organization's place does not open. In clear: the file name (server origin, User id, Organization id), its size and its time; preferences and the session stay where they are, in the key store.
4. **Sign-out.** Sign-out, a 401 or 403, account deletion and a password reset call `clearPrivateCaches()`, which now destroys the key first and deletes the folder second. A file the deletion misses cannot be opened again. The next sign-in generates a new key.
5. **Organization switch.** Nothing is deleted, as today: each Organization has its own file, a file opens only under its own scope, and the 24-hour limit ends what is not revisited. Deleting on a switch would cost the offline view of the Organization a person comes back to.
6. **A lost key.** A file that does not authenticate (key replaced, bytes changed, another slot) is deleted and read as "no snapshot". Nothing is set aside or recovered.
7. **An option, off by default.** `EXPO_PUBLIC_ENCRYPT_SNAPSHOTS=1` turns sealing on at build time; any value other than `0`, `1` or empty fails the environment check. Off, the store writes the plain files it always wrote. On, a snapshot is available one asynchronous step after the first render instead of during it, because the cipher is asynchronous: `ScopedSnapshot.restore(scope)` delivers it and the todo list hook hands it to its query.
8. **The web keeps no private snapshot,** sealed or plain.

No new native module: `expo-crypto`, `expo-secure-store` and `expo-file-system` are already in the development client.

## Not proven [#not-proven]

The format, the key handling, the store and the sign-out order are tested in Node with WebCrypto standing in for the cipher. The three native adapters (`expo-aes-gcm.ts`, the SecureStore key and the binary file calls in `private-caches.ts`) were type-checked but not run on a device in this change; the same `expo-crypto` calls run in production in a sibling project. That is why the option ships off. Turn it on for a project after the device check of issue #243, and make it the default once that check has passed on iOS and Android.

## Out of scope, and what would need a new native module [#out-of-scope-and-what-would-need-a-new-native-module]

- **An encrypted database and an offline outbox.** A device-first product that writes offline needs a database (SQLCipher through `expo-sqlite` and its config plugin), which is a new native module and a new store build, and decisions this record does not take: what a 401 does to queued writes, one database per scope or one wiped on a switch, and backup exclusion. The offline-first reviews of 2026-11-28 list them. The key, the cipher and the sign-out order above are the part such a database would reuse.
- **Files in the documents folder.** iOS backs that folder up, and excluding a file needs native code. Sealed snapshots stay in the cache folder for that reason.
- **A PIN or biometric-wrapped key.** It protects against someone holding the unlocked phone, which the app lock already addresses at the screen. A clinical project that needs it builds it on top of this key.

## Alternatives [#alternatives]

- SQLCipher for the snapshots. It encrypts the file names too, but needs the new native module for a cache that holds a few JSON files.
- A key per User or per Organization. The scope in the additional data already separates them, and sign-out destroys the one key; more keys add states (which key is missing?) without adding a boundary.
- Sealing always. It changes the first read of every installed app from synchronous to asynchronous before a device has shown the native adapters to work.
- A pure JavaScript cipher. It would run without the native call, but slower on a 2 MiB value and with a second implementation to trust; the platform's own AES-GCM is already linked.

## Migration from plaintext snapshots [#migration-from-plaintext-snapshots]

When `EXPO_PUBLIC_ENCRYPT_SNAPSHOTS=1` is turned on, opening a sealed store immediately deletes every legacy `.json` plaintext snapshot in that namespace (across all Organizations and scopes) while leaving `.sealed` files intact, so no private data remains unsealed on disk even if a scope is never written again.
