skip to content

A law-firm portal client restores a backup onto a new phone, or reinstalls the app; what can the app find at first launch, and how must it treat it?

level: seniorimportance: should knowfreq 47%

answer

  1. files come back, keys may not
  2. ciphertext without its key
  3. locked, missing: two different errors
  4. a reinstall is not a clean slate
  5. an install marker kept out of backups

basics

~20 s

Backed-up files can arrive without the device-bound key that opens them, and on one platform old credentials can outlive an uninstall. Clear what a fresh install finds, and on a missing key delete the ciphertext, sign in again and re-download.

solid answer

~50 s

Both platforms back up ordinary app files by default, so a restore to a new phone brings back the encrypted database, the document cache and a signed-in flag, but not a key created in the hardware-backed store, which cannot leave the old phone, nor items marked this-device-only. The app finds ciphertext it cannot open. It must tell missing from locked, then delete the unreadable data, clear leftover tokens, send the client to sign in and download the documents again; a crash or a retry loop is the failure. Reinstall is not a clean slate either: on one platform, credential-store items can outlive an uninstall, possibly from a previous owner, and on the other a reinstall can restore backed-up files before the first launch. So a fresh install keeps an install marker excluded from backup and, when it is absent, wipes every credential-store item and local secret before trusting anything.

code

pseudocode · 23 lines
pseudocode
on launch:
  if not exists(install_marker):            # ordinary storage, excluded from backup
    credential_store.delete_all_items_of_this_app()
    delete(encrypted_document_cache)
    delete(wrapped_data_key)
    write(install_marker)

  if not exists(wrapped_data_key):
    route_to_sign_in()
    return

  result = key_store.unwrap(wrapped_data_key)
  if result.status == LOCKED:
    wait_for_unlock()                          # nothing destructive
    return
  if result.status == KEY_MISSING:
    delete(encrypted_document_cache)
    delete(wrapped_data_key)
    credential_store.delete("refresh_token")
    route_to_sign_in(message = "sign in again to restore offline documents")
    return

  open_document_cache(result.data_key)

go deeper

for a junior

Recall that a backup restores an app's ordinary files but not keys locked to the old phone, so the app can find data it cannot open.

for a middle

Explain which items move on a restore and which do not, and why that leaves ciphertext without its key on a new phone.

for a senior

Design the first launch: an install marker kept out of backups, locked versus missing, and a recovery path ending in sign-in and re-download, never a crash.

for a principal

Decide the firm's policy on new devices: every new phone signs in afresh, credentials never migrate, and what the backup may carry at all.

## Three ways an install starts A phone app's first launch is not always a fresh start. Three situations look alike to the code: 1. **A new phone restored from a backup** of the old one. 2. **The same phone restored** after it was reset. 3. **A reinstall** on the same phone, possibly by a different person after the phone changed hands. In each, some of the app's state comes back and some does not, and the platforms differ on which. The design rule follows: **trust nothing the first launch finds until the app has checked that it fits together.** ## What a restore carries | Item | Restored onto a new phone | |---|---| | Ordinary app files: database, documents, preferences | Yes by default on both platforms, unless the app excludes them; cache folders are left out | | Credential-store item readable after first unlock | On one platform, yes, through an encrypted backup | | Credential-store item marked this-device-only | No (it can come back only when restoring onto the same phone) | | Key created in the hardware-backed store | No: a key that cannot be exported cannot travel in a backup | The dangerous combination is the first row without the last: **ciphertext and a "signed in" flag, without the key that opens them.** An app that assumes its keys exist because its files do will crash, loop on an error, or show an empty document list with no explanation. ## The key-missing path 1. At launch, try to unwrap the data key with the device key. 2. If the store reports the phone **locked**, do nothing destructive; wait for the unlock. 3. If the key is **missing**, the local ciphertext can never be opened on this phone: delete it, delete any leftover tokens, and send the client to sign in. 4. After sign-in, create fresh keys and download the offline documents again from the server, which holds the originals. 5. Tell the client plainly, "Sign in again to restore your offline documents", rather than showing an error code. ## Reinstall is not a clean slate The two platforms surprise teams in opposite directions: - **On one platform**, items in the OS credential store can outlive an uninstall. A reinstall can find the previous install's refresh token, perhaps belonging to someone who sold or handed down the phone, and sign the new person straight into a stranger's case files. - **On the other**, when a backup exists, backed-up app files are restored as the app is installed, before it first runs. The app finds its old database and preferences, while the device-bound keys that protected them are gone. Apps built with a cross-platform framework inherit both behaviours. One guard handles them: - Keep an **install marker** in ordinary app storage, explicitly excluded from backup. - At launch, if the marker is absent, delete every credential-store item the app owns and any encrypted local data, then write the marker. - Only after that read tokens or unwrap keys. Because the marker is excluded from backup, a new phone also looks like a fresh install. For a law firm that is the intended outcome: every new device signs in and receives its own credentials. ## Keep the backup honest - **Exclude the encrypted document cache** from backup. It is large, the server can send it again, and on any other phone it is useless without a key that stays behind. - **Keep credentials device-only**, so they never arrive on a phone that did not sign in. The cost is one sign-in per new phone, which a client handling confidential matters will rarely resent. - **Treat phone-only user data deliberately.** A draft message to the lawyer that exists nowhere else is either synced to the server or protected in a way a new phone can open; encrypting it under a device-bound key and backing it up preserves nothing. ## What interviewers listen for The weak answer says "the backup restores everything" or "a reinstall starts clean", and its app crashes on the first launch after a client moves phones. The strong one names the mismatched state a restore produces, separates locked from missing, gives a recovery path that ends in sign-in and re-download rather than a crash, and knows that a leftover credential on a reinstalled app may belong to someone else.

  • Should the encrypted document cache be excluded from the device backup?
    Here, yes. Both platforms back up ordinary app files by default except cache folders, and these files cannot be opened on another phone because the wrapping key stays behind. They are large and the server can send them again, so the app excludes them explicitly. Data that exists only on the phone is a separate decision.
  • Why not let the refresh token migrate so the client stays signed in on the new phone?
    A token that migrates is a credential that left the device it was issued to; the server cannot tell the new phone from a copy elsewhere, and the client never proves who they are on the new device. For confidential case files, a new phone signs in and gets its own session and keys. The cost is one sign-in per new phone.

  • A reinstall always starts the app from a clean slate.
  • Restoring a backup brings back every key the app had on the old phone.
  • If the key is missing, the client's documents are lost for good.
  • Back up the encrypted cache so a new phone works offline at once.
  • Device backups leave out an app's database unless the app opts in.