@open-e2ee/signal-protocol-sdk 12.0.1 → 13.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/ADAPTERS.md +3 -0
  2. package/ARCHITECTURE.md +4 -4
  3. package/CHANGELOG.md +141 -0
  4. package/README.md +1 -1
  5. package/SECURITY.md +2 -2
  6. package/client/README.md +40 -0
  7. package/device/README.md +40 -4
  8. package/dist/client/client.d.ts +8 -0
  9. package/dist/client/client.d.ts.map +1 -1
  10. package/dist/client/client.js +19 -1
  11. package/dist/client/client.js.map +1 -1
  12. package/dist/client/config.d.ts +6 -2
  13. package/dist/client/config.d.ts.map +1 -1
  14. package/dist/client/config.js.map +1 -1
  15. package/dist/client/event-hooks.d.ts +28 -0
  16. package/dist/client/event-hooks.d.ts.map +1 -1
  17. package/dist/client/event-hooks.js +1 -0
  18. package/dist/client/event-hooks.js.map +1 -1
  19. package/dist/client/group-init-status.d.ts +21 -0
  20. package/dist/client/group-init-status.d.ts.map +1 -0
  21. package/dist/client/group-init-status.js +156 -0
  22. package/dist/client/group-init-status.js.map +1 -0
  23. package/dist/client/hosted-device-link-transport.d.ts +11 -0
  24. package/dist/client/hosted-device-link-transport.d.ts.map +1 -0
  25. package/dist/client/hosted-device-link-transport.js +25 -0
  26. package/dist/client/hosted-device-link-transport.js.map +1 -0
  27. package/dist/client/hosted-device-link.d.ts +97 -0
  28. package/dist/client/hosted-device-link.d.ts.map +1 -0
  29. package/dist/client/hosted-device-link.js +149 -0
  30. package/dist/client/hosted-device-link.js.map +1 -0
  31. package/dist/client/hosted-http-error.d.ts +30 -0
  32. package/dist/client/hosted-http-error.d.ts.map +1 -0
  33. package/dist/client/hosted-http-error.js +34 -0
  34. package/dist/client/hosted-http-error.js.map +1 -0
  35. package/dist/client/hosted-presence.js +1 -1
  36. package/dist/client/hosted-presence.js.map +1 -1
  37. package/dist/client/hosted-profile-keys.js +1 -1
  38. package/dist/client/hosted-profile-keys.js.map +1 -1
  39. package/dist/client/hosted-sandbox-identity.d.ts +3 -3
  40. package/dist/client/hosted-sandbox-identity.js +3 -3
  41. package/dist/client/hosted-transport.d.ts +12 -13
  42. package/dist/client/hosted-transport.d.ts.map +1 -1
  43. package/dist/client/hosted-transport.js +111 -17
  44. package/dist/client/hosted-transport.js.map +1 -1
  45. package/dist/client/hosted.d.ts +57 -8
  46. package/dist/client/hosted.d.ts.map +1 -1
  47. package/dist/client/hosted.js +170 -19
  48. package/dist/client/hosted.js.map +1 -1
  49. package/dist/client/index.d.ts +2 -1
  50. package/dist/client/index.d.ts.map +1 -1
  51. package/dist/client/index.js +1 -1
  52. package/dist/client/index.js.map +1 -1
  53. package/dist/client/receive-queue.d.ts +4 -2
  54. package/dist/client/receive-queue.d.ts.map +1 -1
  55. package/dist/client/receive-queue.js +26 -22
  56. package/dist/client/receive-queue.js.map +1 -1
  57. package/dist/index.d.ts +2 -1
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +1 -1
  60. package/dist/index.js.map +1 -1
  61. package/dist/local/store/conformance/cases/ciphertext.d.ts +7 -0
  62. package/dist/local/store/conformance/cases/ciphertext.d.ts.map +1 -0
  63. package/dist/local/store/conformance/cases/ciphertext.js +55 -0
  64. package/dist/local/store/conformance/cases/ciphertext.js.map +1 -0
  65. package/dist/local/store/conformance/cases/durability.d.ts +6 -0
  66. package/dist/local/store/conformance/cases/durability.d.ts.map +1 -0
  67. package/dist/local/store/conformance/cases/durability.js +34 -0
  68. package/dist/local/store/conformance/cases/durability.js.map +1 -0
  69. package/dist/local/store/conformance/cases/identity.d.ts +7 -0
  70. package/dist/local/store/conformance/cases/identity.d.ts.map +1 -0
  71. package/dist/local/store/conformance/cases/identity.js +84 -0
  72. package/dist/local/store/conformance/cases/identity.js.map +1 -0
  73. package/dist/local/store/conformance/cases/metadata.d.ts +7 -0
  74. package/dist/local/store/conformance/cases/metadata.d.ts.map +1 -0
  75. package/dist/local/store/conformance/cases/metadata.js +48 -0
  76. package/dist/local/store/conformance/cases/metadata.js.map +1 -0
  77. package/dist/local/store/conformance/cases/protection.d.ts +7 -0
  78. package/dist/local/store/conformance/cases/protection.d.ts.map +1 -0
  79. package/dist/local/store/conformance/cases/protection.js +14 -0
  80. package/dist/local/store/conformance/cases/protection.js.map +1 -0
  81. package/dist/local/store/conformance/cases/receive-work.d.ts +8 -0
  82. package/dist/local/store/conformance/cases/receive-work.d.ts.map +1 -0
  83. package/dist/local/store/conformance/cases/receive-work.js +152 -0
  84. package/dist/local/store/conformance/cases/receive-work.js.map +1 -0
  85. package/dist/local/store/conformance/cases/sender-key.d.ts +7 -0
  86. package/dist/local/store/conformance/cases/sender-key.d.ts.map +1 -0
  87. package/dist/local/store/conformance/cases/sender-key.js +59 -0
  88. package/dist/local/store/conformance/cases/sender-key.js.map +1 -0
  89. package/dist/local/store/conformance/cases/session-trust.d.ts +7 -0
  90. package/dist/local/store/conformance/cases/session-trust.d.ts.map +1 -0
  91. package/dist/local/store/conformance/cases/session-trust.js +64 -0
  92. package/dist/local/store/conformance/cases/session-trust.js.map +1 -0
  93. package/dist/local/store/conformance/checks.d.ts +15 -0
  94. package/dist/local/store/conformance/checks.d.ts.map +1 -0
  95. package/dist/local/store/conformance/checks.js +67 -0
  96. package/dist/local/store/conformance/checks.js.map +1 -0
  97. package/dist/local/store/conformance/index.d.ts +30 -0
  98. package/dist/local/store/conformance/index.d.ts.map +1 -0
  99. package/dist/local/store/conformance/index.js +29 -0
  100. package/dist/local/store/conformance/index.js.map +1 -0
  101. package/dist/local/store/conformance/records.d.ts +36 -0
  102. package/dist/local/store/conformance/records.d.ts.map +1 -0
  103. package/dist/local/store/conformance/records.js +109 -0
  104. package/dist/local/store/conformance/records.js.map +1 -0
  105. package/dist/local/store/conformance/run.d.ts +24 -0
  106. package/dist/local/store/conformance/run.d.ts.map +1 -0
  107. package/dist/local/store/conformance/run.js +156 -0
  108. package/dist/local/store/conformance/run.js.map +1 -0
  109. package/dist/local/store/conformance/types.d.ts +55 -0
  110. package/dist/local/store/conformance/types.d.ts.map +1 -0
  111. package/dist/local/store/conformance/types.js +6 -0
  112. package/dist/local/store/conformance/types.js.map +1 -0
  113. package/dist/local/store/expo/adapter.d.ts +2 -1
  114. package/dist/local/store/expo/adapter.d.ts.map +1 -1
  115. package/dist/local/store/expo/adapter.js.map +1 -1
  116. package/dist/local/store/node/adapter.d.ts +2 -2
  117. package/dist/local/store/react-native/adapter.d.ts +2 -1
  118. package/dist/local/store/react-native/adapter.d.ts.map +1 -1
  119. package/dist/local/store/react-native/adapter.js.map +1 -1
  120. package/dist/local/store/sqlite/convert-plaintext.d.ts +62 -0
  121. package/dist/local/store/sqlite/convert-plaintext.d.ts.map +1 -0
  122. package/dist/local/store/sqlite/convert-plaintext.js +236 -0
  123. package/dist/local/store/sqlite/convert-plaintext.js.map +1 -0
  124. package/dist/local/store/sqlite/encryption.d.ts +21 -0
  125. package/dist/local/store/sqlite/encryption.d.ts.map +1 -1
  126. package/dist/local/store/sqlite/encryption.js.map +1 -1
  127. package/dist/local/store/sqlite/errors.d.ts.map +1 -1
  128. package/dist/local/store/sqlite/errors.js +2 -1
  129. package/dist/local/store/sqlite/errors.js.map +1 -1
  130. package/dist/local/store/sqlite/key-storage/identity-keys.d.ts +3 -1
  131. package/dist/local/store/sqlite/key-storage/identity-keys.d.ts.map +1 -1
  132. package/dist/local/store/sqlite/key-storage/identity-keys.js +3 -1
  133. package/dist/local/store/sqlite/key-storage/identity-keys.js.map +1 -1
  134. package/dist/local/store/sqlite/open-store.d.ts +36 -4
  135. package/dist/local/store/sqlite/open-store.d.ts.map +1 -1
  136. package/dist/local/store/sqlite/open-store.js +326 -21
  137. package/dist/local/store/sqlite/open-store.js.map +1 -1
  138. package/dist/local/store/sqlite/web/worker.d.ts +5 -0
  139. package/dist/local/store/sqlite/web/worker.d.ts.map +1 -1
  140. package/dist/local/store/sqlite/web/worker.js +99 -20
  141. package/dist/local/store/sqlite/web/worker.js.map +1 -1
  142. package/dist/local/store/web-sqlite/adapter.d.ts +3 -2
  143. package/dist/local/store/web-sqlite/adapter.d.ts.map +1 -1
  144. package/dist/local/store/web-sqlite/adapter.js +12 -2
  145. package/dist/local/store/web-sqlite/adapter.js.map +1 -1
  146. package/dist/remote/relay/types.d.ts +4 -2
  147. package/dist/remote/relay/types.d.ts.map +1 -1
  148. package/dist/types/api.d.ts +20 -4
  149. package/dist/types/api.d.ts.map +1 -1
  150. package/docs/ASSURANCE.md +25 -7
  151. package/docs/DEVIATIONS.md +35 -3
  152. package/docs/ERROR_HANDLING.md +8 -6
  153. package/docs/GETTING_STARTED.md +25 -8
  154. package/docs/INTERFACES.md +3 -1
  155. package/docs/PACKAGE_SURFACE.md +1 -0
  156. package/docs/RECIPES.md +44 -0
  157. package/docs/SECURITY.md +23 -5
  158. package/keys/README.md +3 -1
  159. package/local/store/README.md +92 -12
  160. package/local/store/expo/README.md +63 -6
  161. package/local/store/key-value/README.md +3 -2
  162. package/local/store/node/README.md +68 -6
  163. package/local/store/react-native/README.md +62 -4
  164. package/local/store/web-sqlite/README.md +101 -4
  165. package/package.json +5 -1
@@ -18,6 +18,15 @@
18
18
  * `encryptionAtRest: false` has no key, and its open and reset do not touch
19
19
  * the vault.
20
20
  *
21
+ * With `convertPlaintext: true`, an encrypted open converts a plaintext file
22
+ * and keeps its data. The driver seam has no rename, so the conversion copies
23
+ * twice: the plaintext file to a temporary encrypted file, then that file back
24
+ * to the store name. A pending vault slot holds the new key until the copy
25
+ * back is complete. Removing the plaintext file is the commit point. Before
26
+ * it, a failed conversion leaves the plaintext file, and the next conversion
27
+ * starts again with a new key. After it, the next encrypted open finishes the
28
+ * conversion, with or without the option.
29
+ *
21
30
  * In one process, the open and the reset of a database run one at a time, and
22
31
  * a database has at most one open store. Two first opens therefore cannot write
23
32
  * two different keys, and a reset or the driver's `exists` never runs on a
@@ -28,6 +37,8 @@
28
37
  import { generateRandomBytes } from '../../../internal/crypto/random.js';
29
38
  import { EncryptionError, EncryptionErrorCode } from '../../../types/errors.js';
30
39
  import { secretVaultCustody } from '../../vault/custody.js';
40
+ import { SQLITE_CONVERT_SUFFIX, copySqliteDatabase, openSqliteConversionSource, readSqliteUserVersion, sqliteConvertTempName, } from './convert-plaintext.js';
41
+ import { openSqliteDatabase } from './database.js';
31
42
  import { isPlaintextSqliteDatabase, readSqliteAtRest } from './encryption.js';
32
43
  import { SqliteKeyMismatchError } from './errors.js';
33
44
  import { openMigratedSqliteDatabase } from './migrations.js';
@@ -43,14 +54,26 @@ export function sqliteDatabaseKeySlot(identity) {
43
54
  return `signal_protocol_sqlite_key.${identity}`;
44
55
  }
45
56
  /**
46
- * Reject a name that is not safe as a file name on every platform, with the
47
- * error of {@link openSqliteStore}.
57
+ * The vault slot that holds the new key of the database `identity` while a
58
+ * conversion of it is incomplete. The prefix differs from the main slot.
59
+ */
60
+ function sqlitePendingKeySlot(identity) {
61
+ return `signal_protocol_sqlite_pending_key.${identity}`;
62
+ }
63
+ /**
64
+ * Reject a name that is not safe as a file name on every platform, or that
65
+ * names the temporary file of a conversion, with the error of
66
+ * {@link openSqliteStore}.
48
67
  */
49
68
  export function assertSqliteStoreName(name, operation) {
50
- if (STORE_NAME.test(name))
51
- return;
52
- throw new EncryptionError(`The SQLite store name ${JSON.stringify(name)} may contain only letters, digits, ` +
53
- '".", "_", and "-".', EncryptionErrorCode.INVALID_STATE, { operation });
69
+ if (!STORE_NAME.test(name)) {
70
+ throw new EncryptionError(`The SQLite store name ${JSON.stringify(name)} may contain only letters, digits, ` +
71
+ '".", "_", and "-".', EncryptionErrorCode.INVALID_STATE, { operation });
72
+ }
73
+ if (name.endsWith(SQLITE_CONVERT_SUFFIX)) {
74
+ throw new EncryptionError(`The SQLite store name ${JSON.stringify(name)} ends in ${SQLITE_CONVERT_SUFFIX}. ` +
75
+ 'The conversion to encryption uses that ending for its temporary file.', EncryptionErrorCode.INVALID_STATE, { operation });
76
+ }
54
77
  }
55
78
  /** The tail of the queued opens and resets of each database identity. */
56
79
  const pending = new Map();
@@ -61,11 +84,19 @@ const held = new Set();
61
84
  * migrations. On an encrypted store, the key comes from the vault. The key,
62
85
  * then the file, are created only when the file does not exist.
63
86
  *
87
+ * With `convertPlaintext: true`, an encrypted open of a plaintext file
88
+ * converts it. The store keeps every row and its schema version. An
89
+ * encrypted open also finishes a conversion that stopped after its commit
90
+ * point, with or without the option.
91
+ *
64
92
  * `close()` on the result closes the database and releases the name.
65
93
  *
66
94
  * @throws EncryptionError with `INVALID_STATE` when the name is not valid or
67
95
  * its database already has an open store in this process, or when a newer
68
- * SDK wrote the file.
96
+ * SDK wrote the file. Also when `convertPlaintext` comes with
97
+ * `encryptionAtRest: false`, when an open with `encryptionAtRest: false`
98
+ * finds an incomplete conversion, or when a plaintext file cannot be
99
+ * converted.
69
100
  * @throws EncryptionError with `LOCAL_STORE_KEY_LOST` when the file exists
70
101
  * and the vault holds no key for it.
71
102
  * @throws EncryptionError with `KEY_STORAGE_ERROR` when the vault fails or
@@ -76,20 +107,14 @@ export function openSqliteStore(options) {
76
107
  return oneAtATime(options, 'open', async () => {
77
108
  const { driver, name, vault } = options;
78
109
  if (options.encryptionAtRest === false) {
110
+ await refuseIncompleteConversion(driver, name, 'open');
79
111
  return openMigratedSqliteDatabase(driver, name, null, options);
80
112
  }
81
113
  const slot = sqliteDatabaseKeySlot(driver.identify(name));
82
114
  const storedKey = await readDatabaseKey(vault, slot);
83
115
  if (storedKey)
84
116
  return openMigratedSqliteDatabase(driver, name, storedKey, options);
85
- if (await driver.exists(name)) {
86
- if (await isPlaintextSqliteDatabase(driver, name)) {
87
- throw new SqliteKeyMismatchError(driver.name);
88
- }
89
- throw new EncryptionError(`The ${driver.name} database ${name} exists, but the secret vault holds no key for it. ` +
90
- 'Restore the vault entry, or reset the store to delete the database.', EncryptionErrorCode.LOCAL_STORE_KEY_LOST, { operation: 'open' });
91
- }
92
- return openMigratedSqliteDatabase(driver, name, await replaceDatabaseKey(vault, slot), options);
117
+ return openWithoutMainKey(options, slot);
93
118
  });
94
119
  }
95
120
  /**
@@ -101,19 +126,284 @@ export function openSqliteStore(options) {
101
126
  * {@link openSqliteStore} opens that state as a new store. The other order
102
127
  * would leave the old file under a key that no longer exists.
103
128
  *
129
+ * An encrypted reset also ends an incomplete conversion. It deletes the
130
+ * pending key first, then the temporary file, then the database. A process
131
+ * that stops after the key deletion cannot finish the conversion of the old
132
+ * data.
133
+ *
104
134
  * @throws EncryptionError with `INVALID_STATE` when the name is not valid or
105
- * its database already has an open store in this process.
135
+ * its database already has an open store in this process. Also when
136
+ * `convertPlaintext` comes with `encryptionAtRest: false`, or when a reset
137
+ * with `encryptionAtRest: false` finds an incomplete conversion.
106
138
  */
107
139
  export function resetSqliteStore(options) {
108
140
  return oneAtATime(options, 'reset', async () => {
109
141
  const { driver, name, vault } = options;
142
+ if (options.encryptionAtRest === false) {
143
+ await refuseIncompleteConversion(driver, name, 'reset');
144
+ await driver.remove(name);
145
+ return openMigratedSqliteDatabase(driver, name, null, options);
146
+ }
147
+ const identity = driver.identify(name);
148
+ await deleteDatabaseKey(vault, sqlitePendingKeySlot(identity));
149
+ const temp = sqliteConvertTempName(name);
150
+ if (await tempExists(driver, temp))
151
+ await driver.remove(temp);
110
152
  await driver.remove(name);
111
- const key = options.encryptionAtRest === false
112
- ? null
113
- : await replaceDatabaseKey(vault, sqliteDatabaseKeySlot(driver.identify(name)));
153
+ const key = await replaceDatabaseKey(vault, sqliteDatabaseKeySlot(identity));
114
154
  return openMigratedSqliteDatabase(driver, name, key, options);
115
155
  });
116
156
  }
157
+ /**
158
+ * The open of an encrypted store whose main slot holds no key. It creates a
159
+ * new store, converts a plaintext file, or finishes a conversion. Each state
160
+ * that a conversion can leave has one action.
161
+ *
162
+ * - The file is plaintext: convert it with the option, else report the
163
+ * setting mismatch.
164
+ * - The file is encrypted and the pending key opens it: the copy back ran.
165
+ * Copy back again when it did not commit, then finish.
166
+ * - Any other existing file: the key is lost.
167
+ * - No file, a pending key, and a complete temporary file: the plaintext
168
+ * file was removed. Copy back, then finish.
169
+ * - No file and no pending key, with a temporary file: refuse. Its key is
170
+ * not in this vault.
171
+ * - No file and no pending key: a new store.
172
+ * - No file, a pending key, and no complete temporary file: no data exists,
173
+ * so discard both and create a new store.
174
+ *
175
+ * The first read of the state takes no lock. Before the first write of a
176
+ * conversion or its recovery, the open takes the conversion lock and reads
177
+ * the state again.
178
+ */
179
+ async function openWithoutMainKey(options, slot, locked = false) {
180
+ const { driver, name, vault } = options;
181
+ const pendingSlot = sqlitePendingKeySlot(driver.identify(name));
182
+ const temp = sqliteConvertTempName(name);
183
+ const relock = () => withConversionLock(options, async () => {
184
+ const storedKey = await readDatabaseKey(vault, slot);
185
+ if (storedKey)
186
+ return openMigratedSqliteDatabase(driver, name, storedKey, options);
187
+ return openWithoutMainKey(options, slot, true);
188
+ });
189
+ if (await driver.exists(name)) {
190
+ if (await isPlaintextSqliteDatabase(driver, name)) {
191
+ await refuseCompleteCopy(options, pendingSlot);
192
+ if (!options.convertPlaintext)
193
+ throw new SqliteKeyMismatchError(driver.name);
194
+ if (!locked)
195
+ return relock();
196
+ const key = await convertPlaintextFile(options, pendingSlot);
197
+ return finish(options, slot, pendingSlot, key);
198
+ }
199
+ const pendingKey = await readDatabaseKey(vault, pendingSlot);
200
+ const version = pendingKey && (await readSqliteUserVersion(driver, name, pendingKey));
201
+ if (!pendingKey || version === null) {
202
+ throw new EncryptionError(`The ${driver.name} database ${name} exists, but the secret vault holds no key for it. ` +
203
+ 'Restore the vault entry, or reset the store to delete the database.', EncryptionErrorCode.LOCAL_STORE_KEY_LOST, { operation: 'open' });
204
+ }
205
+ if (!locked)
206
+ return relock();
207
+ if (version > 0)
208
+ return finish(options, slot, pendingSlot, pendingKey);
209
+ // The copy back did not commit, so the file holds no rows. The
210
+ // temporary file still holds them.
211
+ await driver.remove(name);
212
+ }
213
+ const pendingKey = await readDatabaseKey(vault, pendingSlot);
214
+ const tempPresent = await tempExists(driver, temp);
215
+ if (!pendingKey) {
216
+ if (tempPresent) {
217
+ throw new EncryptionError(`The temporary file ${temp} of a conversion to encryption exists without its database ` +
218
+ `${name}, and the secret vault holds no key for it. The store did not change it. ` +
219
+ 'Restore the vault entry, or reset the store to delete all data.', EncryptionErrorCode.INVALID_STATE, { operation: 'open' });
220
+ }
221
+ return openMigratedSqliteDatabase(driver, name, await replaceDatabaseKey(vault, slot), options);
222
+ }
223
+ if (!locked)
224
+ return relock();
225
+ if (tempPresent && ((await readSqliteUserVersion(driver, temp, pendingKey)) ?? 0) > 0) {
226
+ return finish(options, slot, pendingSlot, pendingKey);
227
+ }
228
+ // No step of a conversion leaves a pending key without the database and
229
+ // without a complete temporary file. Nothing holds data, so start anew.
230
+ await deleteDatabaseKey(vault, pendingSlot);
231
+ if (tempPresent)
232
+ await driver.remove(temp);
233
+ const key = await replaceDatabaseKey(vault, slot);
234
+ return openMigratedSqliteDatabase(driver, name, key, options);
235
+ }
236
+ /**
237
+ * Refuse to convert a plaintext file when a complete encrypted copy of a
238
+ * conversion exists beside it. This SDK removes the plaintext file right
239
+ * after the copy commits. An older SDK can create a new, empty plaintext
240
+ * file after that removal. The open does not choose between the two files.
241
+ */
242
+ async function refuseCompleteCopy({ driver, name, vault }, pendingSlot) {
243
+ const pendingKey = await readDatabaseKey(vault, pendingSlot);
244
+ if (!pendingKey)
245
+ return;
246
+ const temp = sqliteConvertTempName(name);
247
+ if (!(await tempExists(driver, temp)))
248
+ return;
249
+ if (((await readSqliteUserVersion(driver, temp, pendingKey)) ?? 0) === 0)
250
+ return;
251
+ throw new EncryptionError(`The plaintext database ${name} and the complete encrypted copy ${temp} of a conversion ` +
252
+ `both exist, so the store did not open. Remove ${name} to finish the conversion from ` +
253
+ 'the copy, or reset the store to delete all data.', EncryptionErrorCode.INVALID_STATE, { operation: 'open' });
254
+ }
255
+ /**
256
+ * Convert the plaintext file of the store to an encrypted temporary file, and
257
+ * remove the plaintext file. Return the pending key, which encrypts the
258
+ * temporary file. The removal is the commit point. The caller holds the
259
+ * conversion lock.
260
+ */
261
+ async function convertPlaintextFile(options, pendingSlot) {
262
+ const { driver, name, vault } = options;
263
+ const temp = sqliteConvertTempName(name);
264
+ let source = await openSqliteConversionSource(driver, name);
265
+ let committed = false;
266
+ try {
267
+ const tempPresent = await tempExistsForConversion(driver, temp);
268
+ // A conversion that stopped before its commit point leaves its key and
269
+ // its temporary file. Discard both, and start again with a new key.
270
+ await deleteDatabaseKey(vault, pendingSlot);
271
+ const pendingKey = await replaceDatabaseKey(vault, pendingSlot);
272
+ if (tempPresent)
273
+ await driver.remove(temp);
274
+ const target = await openSqliteDatabase(driver, temp, pendingKey, options);
275
+ try {
276
+ await copySqliteDatabase(source, target);
277
+ }
278
+ finally {
279
+ await target.close();
280
+ }
281
+ const closing = source;
282
+ source = null;
283
+ await closing.close();
284
+ await driver.remove(name);
285
+ committed = true;
286
+ return pendingKey;
287
+ }
288
+ catch (error) {
289
+ // The conversion error is the one to report. A failed close adds nothing.
290
+ await source?.close().catch(() => undefined);
291
+ if (!committed)
292
+ await discardConversion(options, pendingSlot);
293
+ throw error;
294
+ }
295
+ }
296
+ /**
297
+ * Copy the complete temporary file back to the store name, under the
298
+ * pending key that encrypts both.
299
+ */
300
+ async function copyBack(options, pendingKey) {
301
+ const { driver, name } = options;
302
+ const source = await openSqliteDatabase(driver, sqliteConvertTempName(name), pendingKey, options);
303
+ try {
304
+ const target = await openSqliteDatabase(driver, name, pendingKey, options);
305
+ try {
306
+ await copySqliteDatabase(source, target);
307
+ }
308
+ finally {
309
+ await target.close();
310
+ }
311
+ }
312
+ finally {
313
+ await source.close();
314
+ }
315
+ }
316
+ /**
317
+ * Finish a conversion whose copy back committed: remove the temporary file,
318
+ * move the pending key to the main slot, and open the store.
319
+ */
320
+ async function finish(options, slot, pendingSlot, key) {
321
+ const { driver, name, vault } = options;
322
+ if (!(await driver.exists(name)))
323
+ await copyBack(options, key);
324
+ const temp = sqliteConvertTempName(name);
325
+ if (await tempExists(driver, temp))
326
+ await driver.remove(temp);
327
+ // The vault has no atomic move. A stop between the two writes leaves the
328
+ // same key in both slots. The open ignores the pending slot then, and a
329
+ // reset deletes it.
330
+ await writeDatabaseKey(vault, slot, key);
331
+ await deleteDatabaseKey(vault, pendingSlot);
332
+ return openMigratedSqliteDatabase(driver, name, key, options);
333
+ }
334
+ /**
335
+ * Undo a conversion that failed before its commit point: remove the
336
+ * temporary file, then delete the pending key. A failure here leaves a state
337
+ * that the next conversion discards.
338
+ */
339
+ async function discardConversion({ driver, name, vault }, pendingSlot) {
340
+ try {
341
+ const temp = sqliteConvertTempName(name);
342
+ if (await tempExists(driver, temp))
343
+ await driver.remove(temp);
344
+ await vault.deleteSecret(pendingSlot);
345
+ }
346
+ catch {
347
+ // The conversion error is the one to report.
348
+ }
349
+ }
350
+ /** Run `task` while the entry holds the lock that a conversion needs. */
351
+ async function withConversionLock(options, task) {
352
+ const release = options.lockConversion ? await options.lockConversion() : undefined;
353
+ try {
354
+ return await task();
355
+ }
356
+ finally {
357
+ release?.();
358
+ }
359
+ }
360
+ /**
361
+ * Refuse an open or a reset with `encryptionAtRest: false` while a
362
+ * conversion to encryption is incomplete. Such an open would create a new
363
+ * plaintext file, and such a reset would leave the encrypted copy.
364
+ */
365
+ async function refuseIncompleteConversion(driver, name, operation) {
366
+ const temp = sqliteConvertTempName(name);
367
+ if (!(await tempExists(driver, temp)))
368
+ return;
369
+ throw new EncryptionError(`The SQLite store ${name} has an incomplete conversion to encryption: its temporary file ` +
370
+ `${temp} exists. Open the store with encryption to finish the conversion, or reset it ` +
371
+ 'with encryption to delete all data.', EncryptionErrorCode.INVALID_STATE, { operation });
372
+ }
373
+ /**
374
+ * Report whether the temporary file of a conversion exists. A driver that
375
+ * refuses the name, such as the web driver for a name that is too long, has
376
+ * no such file.
377
+ */
378
+ async function tempExists(driver, temp) {
379
+ try {
380
+ return await driver.exists(temp);
381
+ }
382
+ catch (error) {
383
+ if (isInvalidState(error))
384
+ return false;
385
+ throw error;
386
+ }
387
+ }
388
+ /**
389
+ * Report whether the temporary file of a conversion exists, and refuse a
390
+ * conversion on a driver that does not accept its name. The check runs
391
+ * before the first write.
392
+ */
393
+ async function tempExistsForConversion(driver, temp) {
394
+ try {
395
+ return await driver.exists(temp);
396
+ }
397
+ catch (error) {
398
+ if (!isInvalidState(error))
399
+ throw error;
400
+ throw new EncryptionError(`The ${driver.name} driver does not accept the name ${temp} of the temporary file of a ` +
401
+ 'conversion. Use a shorter store name.', EncryptionErrorCode.INVALID_STATE, { operation: 'open', originalError: error });
402
+ }
403
+ }
404
+ function isInvalidState(error) {
405
+ return error instanceof EncryptionError && error.code === EncryptionErrorCode.INVALID_STATE;
406
+ }
117
407
  /**
118
408
  * Read the protection of a database that {@link openSqliteStore} or
119
409
  * {@link resetSqliteStore} opened: the encryption that the database records,
@@ -137,9 +427,13 @@ export async function readSqliteStoreProtection(db, vault) {
137
427
  * it while the database has an open store. The database stays held until the
138
428
  * returned executor closes.
139
429
  */
140
- function oneAtATime({ driver, name }, operation, task) {
430
+ function oneAtATime({ driver, name, encryptionAtRest, convertPlaintext }, operation, task) {
141
431
  try {
142
432
  assertSqliteStoreName(name, operation);
433
+ if (convertPlaintext === true && encryptionAtRest === false) {
434
+ throw new EncryptionError('convertPlaintext converts a plaintext SQLite store to an encrypted one, so it does ' +
435
+ 'not apply with encryptionAtRest: false.', EncryptionErrorCode.INVALID_STATE, { operation });
436
+ }
143
437
  }
144
438
  catch (error) {
145
439
  return Promise.reject(error);
@@ -185,12 +479,23 @@ async function readDatabaseKey(vault, slot) {
185
479
  /** Create a new database key and write it to the vault, over any old key. */
186
480
  async function replaceDatabaseKey(vault, slot) {
187
481
  const key = await generateRandomBytes(DATABASE_KEY_SIZE);
482
+ await writeDatabaseKey(vault, slot, key);
483
+ return key;
484
+ }
485
+ async function writeDatabaseKey(vault, slot, key) {
188
486
  try {
189
487
  await vault.setSecret(slot, key);
190
488
  }
191
489
  catch (error) {
192
490
  throw new EncryptionError('Failed to write the database key to the secret vault', EncryptionErrorCode.KEY_STORAGE_ERROR, { operation: 'open', originalError: error });
193
491
  }
194
- return key;
492
+ }
493
+ async function deleteDatabaseKey(vault, slot) {
494
+ try {
495
+ await vault.deleteSecret(slot);
496
+ }
497
+ catch (error) {
498
+ throw new EncryptionError('Failed to delete a database key from the secret vault', EncryptionErrorCode.KEY_STORAGE_ERROR, { operation: 'open', originalError: error });
499
+ }
195
500
  }
196
501
  //# sourceMappingURL=open-store.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"open-store.js","sourceRoot":"","sources":["../../../../local/store/sqlite/open-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAEtE,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,yBAAyB,EAAE,gBAAgB,EAA0B,MAAM,cAAc,CAAC;AACnG,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAElD,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAE1D,8BAA8B;AAC9B,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,gFAAgF;AAChF,MAAM,UAAU,GAAG,mBAAmB,CAAC;AAcvC;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAgB;IACpD,OAAO,8BAA8B,QAAQ,EAAE,CAAC;AAClD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,SAA2B;IAC7E,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO;IAClC,MAAM,IAAI,eAAe,CACvB,yBAAyB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,qCAAqC;QAChF,oBAAoB,EACtB,mBAAmB,CAAC,aAAa,EACjC,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,CAAC,CAAC;IACzD,MAAM,gBAAgB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;IACzC,OAAO,GAAG,CAAC;AACb,CAAC;AAED,KAAK,UAAU,gBAAgB,CAC7B,KAAqC,EACrC,IAAY,EACZ,GAAe;IAEf,IAAI,CAAC;QACH,MAAM,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,eAAe,CACvB,sDAAsD,EACtD,mBAAmB,CAAC,iBAAiB,EACrC,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,KAAc,EAAE,CACrD,CAAC;IACJ,CAAC;AACH,CAAC;AAED,KAAK,UAAU,iBAAiB,CAC9B,KAAqC,EACrC,IAAY;IAEZ,IAAI,CAAC;QACH,MAAM,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,eAAe,CACvB,uDAAuD,EACvD,mBAAmB,CAAC,iBAAiB,EACrC,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,KAAc,EAAE,CACrD,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -32,6 +32,11 @@
32
32
  * `STORAGE_QUOTA_EXCEEDED`, and the next attach adds slot files up to the
33
33
  * two that an open needs.
34
34
  *
35
+ * An existence check and a removal attach the pool as every request does,
36
+ * so a full quota fails them before the core writes a key. After the check
37
+ * or the removal, a pool that holds no file goes, with its directory. So a
38
+ * check for a file that does not exist leaves nothing in OPFS.
39
+ *
35
40
  * The logical connection that the driver returns stays pinned to its file
36
41
  * across these physical reopens.
37
42
  *
@@ -1 +1 @@
1
- {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../../../local/store/sqlite/web/worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG"}
1
+ {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../../../local/store/sqlite/web/worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG"}
@@ -32,6 +32,11 @@
32
32
  * `STORAGE_QUOTA_EXCEEDED`, and the next attach adds slot files up to the
33
33
  * two that an open needs.
34
34
  *
35
+ * An existence check and a removal attach the pool as every request does,
36
+ * so a full quota fails them before the core writes a key. After the check
37
+ * or the removal, a pool that holds no file goes, with its directory. So a
38
+ * check for a file that does not exist leaves nothing in OPFS.
39
+ *
35
40
  * The logical connection that the driver returns stays pinned to its file
36
41
  * across these physical reopens.
37
42
  *
@@ -74,6 +79,14 @@ const MIN_POOL_CAPACITY = 2;
74
79
  */
75
80
  const HANDLE_WAIT_MS = 5_000;
76
81
  const HANDLE_RETRY_MS = 50;
82
+ /**
83
+ * An attach of the pool that fails with a transient error runs again, up to
84
+ * this many attempts in total, with this delay between them. WebKit can
85
+ * reject an OPFS call with `UnknownError` under load and accept it again a
86
+ * moment later.
87
+ */
88
+ const ATTACH_ATTEMPTS = 4;
89
+ const ATTACH_RETRY_MS = 100;
77
90
  const scope = globalThis;
78
91
  const engineConfiguration = globalThis;
79
92
  const slots = new Map();
@@ -108,6 +121,16 @@ function isQuotaExceededError(error) {
108
121
  error !== null &&
109
122
  error.name === 'QuotaExceededError');
110
123
  }
124
+ /**
125
+ * An OPFS call that failed with `UnknownError` failed for a transient reason,
126
+ * such as memory pressure, and can pass when it runs again. Checked by name,
127
+ * as `isQuotaExceededError` is.
128
+ */
129
+ function isTransientError(error) {
130
+ return (typeof error === 'object' &&
131
+ error !== null &&
132
+ error.name === 'UnknownError');
133
+ }
111
134
  function quotaExceeded(cause) {
112
135
  return new EncryptionError(`The origin storage quota is full, so the SQLite pool could not prepare its files. ` +
113
136
  `No database file changed. Free space and open again. Cause: ${cause}`, EncryptionErrorCode.STORAGE_QUOTA_EXCEEDED, { operation: 'attachPool' });
@@ -278,7 +301,8 @@ async function waitForFreeHandles(slot) {
278
301
  catch (error) {
279
302
  if (error instanceof DOMException && error.name === 'NotFoundError')
280
303
  return;
281
- throw fileBusy(detail(error));
304
+ // The caller maps this failure, and runs the attach again when it is transient.
305
+ throw error;
282
306
  }
283
307
  const deadline = Date.now() + HANDLE_WAIT_MS;
284
308
  // `FileSystemDirectoryHandle` is async iterable; the DOM library of this
@@ -306,28 +330,30 @@ async function waitForFreeHandles(slot) {
306
330
  * failed attach changes no database file and holds no access handle, so the
307
331
  * next call tries again. A full origin quota fails it with
308
332
  * `STORAGE_QUOTA_EXCEEDED`, and any other failure with `OPFS_FILE_BUSY`.
333
+ *
334
+ * A transient failure runs the attach again, up to `ATTACH_ATTEMPTS` in all.
335
+ * The retry is safe because the attach runs before the first statement of the
336
+ * request, so no database page and no journal has changed. A failed install
337
+ * or unpause releases every access handle, and a failed reservation pauses
338
+ * the pool here. So each attempt starts from the state that the request found.
309
339
  */
310
340
  async function attachPool(slot, sqlite3) {
311
341
  if (slot.pool !== null && !slot.pool.isPaused())
312
342
  return;
313
- await waitForFreeHandles(slot);
314
- try {
315
- if (slot.pool === null) {
316
- slot.pool = await sqlite3.installOpfsSAHPoolVfs({
317
- name: slot.poolName,
318
- directory: slot.poolDirectory,
319
- verbosity: 1,
320
- forceReinitIfPreviouslyFailed: true,
321
- });
343
+ for (let attempt = 1;; attempt += 1) {
344
+ try {
345
+ await attachPoolOnce(slot, sqlite3);
346
+ break;
322
347
  }
323
- else {
324
- await slot.pool.unpauseVfs();
348
+ catch (error) {
349
+ if (error instanceof EncryptionError)
350
+ throw error;
351
+ if (isQuotaExceededError(error))
352
+ throw quotaExceeded(detail(error));
353
+ if (!isTransientError(error) || attempt === ATTACH_ATTEMPTS)
354
+ throw fileBusy(detail(error));
325
355
  }
326
- // A failure here leaves the pool attached, and the caller detaches it.
327
- await slot.pool.reserveMinimumCapacity(MIN_POOL_CAPACITY);
328
- }
329
- catch (error) {
330
- throw isQuotaExceededError(error) ? quotaExceeded(detail(error)) : fileBusy(detail(error));
356
+ await delay(ATTACH_RETRY_MS);
331
357
  }
332
358
  if (!encryptingVfses.has(slot.poolName)) {
333
359
  const rc = sqlite3.capi.sqlite3mc_vfs_create(slot.poolName, 0);
@@ -336,6 +362,52 @@ async function attachPool(slot, sqlite3) {
336
362
  encryptingVfses.add(slot.poolName);
337
363
  }
338
364
  }
365
+ /**
366
+ * One attempt of `attachPool`. A failure leaves the pool detached: either not
367
+ * installed, or paused with no access handle.
368
+ */
369
+ async function attachPoolOnce(slot, sqlite3) {
370
+ await waitForFreeHandles(slot);
371
+ if (slot.pool === null) {
372
+ slot.pool = await sqlite3.installOpfsSAHPoolVfs({
373
+ name: slot.poolName,
374
+ directory: slot.poolDirectory,
375
+ verbosity: 1,
376
+ forceReinitIfPreviouslyFailed: true,
377
+ });
378
+ }
379
+ else {
380
+ await slot.pool.unpauseVfs();
381
+ }
382
+ try {
383
+ await slot.pool.reserveMinimumCapacity(MIN_POOL_CAPACITY);
384
+ }
385
+ catch (error) {
386
+ // The pool is attached and opens no file, so the pause releases its handles.
387
+ slot.pool.pauseVfs();
388
+ throw error;
389
+ }
390
+ }
391
+ /**
392
+ * Removes the attached pool of the slot and its directory when the pool holds
393
+ * no file. The caller holds the lock. The Multiple Ciphers VFS points at
394
+ * the pool's VFS, so it goes first. The engine keeps that VFS while a file of
395
+ * it is open, and then the pool stays too. The engine logs a failed removal
396
+ * of the directory and keeps the rest of it. The pool is gone in both cases,
397
+ * and the next attach installs it again.
398
+ */
399
+ async function removePool(slot, sqlite3) {
400
+ if (slot.pool.getFileNames().length > 0)
401
+ return;
402
+ const { capi } = sqlite3;
403
+ capi.sqlite3mc_vfs_destroy(slot.vfsName);
404
+ if (capi.sqlite3_vfs_find(slot.vfsName) !== 0)
405
+ return;
406
+ encryptingVfses.delete(slot.poolName);
407
+ const pool = slot.pool;
408
+ slot.pool = null;
409
+ await pool.removeVfs();
410
+ }
339
411
  // ===== Physical connection =====
340
412
  function applyKey(sqlite3, database, key) {
341
413
  database.exec("PRAGMA cipher = 'sqlcipher'");
@@ -516,7 +588,7 @@ function withFile(slot, operation) {
516
588
  if (slot.connection !== null && slot.database === null) {
517
589
  reopen(slot, sqlite3, slot.connection);
518
590
  }
519
- return operation(sqlite3);
591
+ return await operation(sqlite3);
520
592
  }
521
593
  finally {
522
594
  if (slot.connection === null || slot.database === null) {
@@ -605,14 +677,21 @@ async function handle(request) {
605
677
  const slot = slotFor(request.file, request.type);
606
678
  switch (request.type) {
607
679
  case 'exists':
608
- return withFile(slot, () => slot.pool.getFileNames().includes(slot.path));
680
+ return withFile(slot, async (sqlite3) => {
681
+ if (slot.pool.getFileNames().includes(slot.path))
682
+ return true;
683
+ await removePool(slot, sqlite3);
684
+ return false;
685
+ });
609
686
  case 'remove':
610
687
  if (slot.connection !== null) {
611
688
  throw invalidState('Close the SQLite connection before removing its file', 'remove');
612
689
  }
613
- return withFile(slot, () => {
690
+ // Each file has its own pool, so an empty pool goes with its file.
691
+ return withFile(slot, async (sqlite3) => {
614
692
  slot.pool.unlink(slot.path);
615
693
  slot.pool.unlink(`${slot.path}-journal`);
694
+ await removePool(slot, sqlite3);
616
695
  return undefined;
617
696
  });
618
697
  case 'open': {