Commit 111e21a
authored
feat: offline db encryption (#3780)
## 🎯 Goal
Let integrators encrypt the offline database at rest. The offline cache
stores channels, messages, members, drafts and reminders, and right now
we write all of it as plaintext `SQLite`.
It's opt-in. Apps that don't pass the new prop behave exactly as they do
today.
One thing to know up front, since it shapes the rest of the PR:
`op-sqlite` accepts an `encryptionKey` on a build without `SQLCipher`
and then ignores it. You get a plaintext database and no error at any
layer. So part of this change is detecting that and refusing to open the
database, instead of passing the key along and assuming it was used.
Accompanying docs PR:
GetStream/docs-content#1521
## 🛠 Implementation details
### API
`Chat` takes one new prop:
```tsx
<Chat client={client} enableOfflineSupport getEncryptionKey={getEncryptionKey}>
```
`getEncryptionKey?: () => Promise<string | undefined>` runs once per
database open, so once per launch and again after a sign-out. Its result
is passed to `SQLCipher` through `op-sqlite`. `SqliteClientError` and
`SqliteClientErrorCode` are exported too.
### We throw instead of recovering
When the database can't be opened with the encryption that was asked
for, `Chat` throws a `SqliteClientError` from render and the
integrator's error boundary handles it. We don't fall back to plaintext,
we don't switch offline support off, and we don't delete anything.
The reason is that all of those recoveries have a security consequence
and there's no default that's right for everyone. Falling back to
plaintext defeats the point of the feature and nothing tells you it
happened. Dropping the cache decides a compliance question for the
integrator. Deleting the file throws away offline actions that are still
queued. We also can't tell "the Keystore isn't unlocked yet, try again
shortly" from "something is wrong here, sign this device out". So we
detect the failure and classify it, and the app decides what to do about
it.
### Scenarios
| Scenario | What it means | What the SDK does | Recommended recovery |
| ------------------------------------------------------- |
------------------------------------------------------------ |
---------------------------------------------- |
------------------------------------------------------------------ |
| No `getEncryptionKey` passed | Encryption not requested | Opens
plaintext, same as today | n/a |
| Key supplied, fresh install | Nothing on disk yet | Creates the
database encrypted with that key | n/a |
| Key supplied, plaintext database already on disk | Integrator is
turning encryption on for an existing install | Throws
`OFFLINE_DB_UNREADABLE` | Delete the database, remount `Chat` |
| Key differs from the one the database was written with | Key rotated,
or read from the wrong place | Throws `OFFLINE_DB_UNREADABLE` | Delete
the database, remount `Chat` |
| `getEncryptionKey` removed, encrypted database on disk | Integrator is
turning encryption off again | Throws `OFFLINE_DB_UNREADABLE` | Delete
the database, remount `Chat` |
| `getEncryptionKey` throws | Key isn't available yet, e.g. Keystore
still locked | Throws `ENCRYPTION_KEY_UNAVAILABLE` | Remount to retry,
e.g. on next app foreground |
| `getEncryptionKey` resolves `undefined` | Same as above | Throws
`ENCRYPTION_KEY_UNAVAILABLE` | Remount to retry, e.g. on next app
foreground |
| Key supplied, native build has no `SQLCipher` | The key would be
ignored and the database left plaintext | Throws
`SQLCIPHER_BUILD_MISSING`, doesn't open | Not fixable at runtime,
remount with `enableOfflineSupport={false}` |
| Database file corrupted | Nothing to do with encryption | Throws
`OFFLINE_DB_UNREADABLE` | Delete the database, remount `Chat` |
Two of those rows need a closer look in review.
`OFFLINE_DB_UNREADABLE` is not gated on `getEncryptionKey` being set,
and that's on purpose, because of the "turning encryption off again"
row. If we only threw it when a key was supplied, an integrator removing
the prop would get a blank screen instead of an error they can recover
from. I hit that on device.
The last row is why the boundary is useful even for apps that never use
encryption. A corrupted database gives you the same code, so anything
using `enableOfflineSupport` can end up there.
### Where the error comes from
`AbstractOfflineDB.init` in the LLC catches whatever `initializeDB`
throws and doesn't re-throw it, so a caller can't find out why
initialisation failed. I left that alone, because changing it would tie
this PR to an LLC release. `OfflineDB` stores the reason on the instance
on the way out instead, and the new hook reads it back once `init` has
settled. No LLC changes needed for this.
- `SqliteClient` resolves the key, opens through `SQLCipher` and maps
failures onto the codes above. It also gets `preflightEncryption()`,
which runs before `setOfflineDBApi`. Without that ordering the client
attaches a database that's already dead, and the unguarded `await
this.offlineDb.upsertChannels(...)` inside `queryChannels` rejects. You
end up on a loading screen that never resolves.
- `useInitializeOfflineDb()` is new and does preflight, attach, init and
raise, with the init options behind an `options` param. It's pulled out
of `Chat`, which loses 72 lines.
- `OfflineDB` records `initializationError` and re-throws, so `init`
still marks the database uninitialised.
## 🎨 UI Changes
## 🧪 Testing
<!-- Explain how this change can be tested (or why it can't be tested)
-->
## ☑️ Checklist
- [x] I have signed the [Stream
CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform)
(required)
- [x] PR targets the `develop` branch
- [x] Documentation is updated
- [ ] New code is tested in main example apps, including all possible
scenarios
- [ ] SampleApp iOS and Android
- [ ] Expo iOS and Android1 parent 6d8ba12 commit 111e21a
11 files changed
Lines changed: 1006 additions & 40 deletions
File tree
- examples/SampleApp
- ios
- src/components
- package/src
- components/Chat
- __tests__
- hooks
- mock-builders/DB
- store
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
334 | 335 | | |
335 | 336 | | |
336 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
337 | 347 | | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
344 | 352 | | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
351 | 367 | | |
352 | 368 | | |
353 | 369 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
301 | | - | |
| 301 | + | |
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
| |||
2857 | 2857 | | |
2858 | 2858 | | |
2859 | 2859 | | |
2860 | | - | |
| 2860 | + | |
2861 | 2861 | | |
2862 | 2862 | | |
2863 | 2863 | | |
| |||
3291 | 3291 | | |
3292 | 3292 | | |
3293 | 3293 | | |
3294 | | - | |
| 3294 | + | |
3295 | 3295 | | |
3296 | 3296 | | |
3297 | 3297 | | |
| |||
3309 | 3309 | | |
3310 | 3310 | | |
3311 | 3311 | | |
3312 | | - | |
| 3312 | + | |
3313 | 3313 | | |
3314 | 3314 | | |
3315 | 3315 | | |
| |||
3399 | 3399 | | |
3400 | 3400 | | |
3401 | 3401 | | |
3402 | | - | |
| 3402 | + | |
3403 | 3403 | | |
3404 | 3404 | | |
3405 | 3405 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | | - | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
48 | 92 | | |
49 | 93 | | |
50 | 94 | | |
| |||
172 | 216 | | |
173 | 217 | | |
174 | 218 | | |
| 219 | + | |
175 | 220 | | |
176 | 221 | | |
177 | 222 | | |
| |||
241 | 286 | | |
242 | 287 | | |
243 | 288 | | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
261 | 295 | | |
262 | 296 | | |
263 | 297 | | |
| |||
0 commit comments