@kodelyth/matrix 2026.5.42 → 2026.6.2

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 (205) hide show
  1. package/klaw.plugin.json +891 -3
  2. package/package.json +18 -6
  3. package/CHANGELOG.md +0 -321
  4. package/SPEC-SUPPORT.md +0 -116
  5. package/api.ts +0 -38
  6. package/auth-presence.ts +0 -56
  7. package/channel-plugin-api.ts +0 -3
  8. package/cli-metadata.ts +0 -11
  9. package/contract-api.ts +0 -17
  10. package/doctor-contract-api.ts +0 -1
  11. package/helper-api.ts +0 -3
  12. package/index.ts +0 -55
  13. package/plugin-entry.handlers.runtime.ts +0 -1
  14. package/runtime-api.ts +0 -72
  15. package/runtime-heavy-api.ts +0 -1
  16. package/runtime-setter-api.ts +0 -3
  17. package/secret-contract-api.ts +0 -5
  18. package/setup-entry.ts +0 -17
  19. package/setup-plugin-api.ts +0 -3
  20. package/src/account-selection.ts +0 -223
  21. package/src/actions.ts +0 -346
  22. package/src/approval-auth.ts +0 -25
  23. package/src/approval-handler.runtime.ts +0 -592
  24. package/src/approval-ids.ts +0 -6
  25. package/src/approval-native.ts +0 -345
  26. package/src/approval-reaction-auth.ts +0 -45
  27. package/src/approval-reactions.ts +0 -313
  28. package/src/auth-precedence.ts +0 -61
  29. package/src/channel-account-paths.ts +0 -97
  30. package/src/channel.runtime.ts +0 -17
  31. package/src/channel.setup.ts +0 -48
  32. package/src/channel.ts +0 -667
  33. package/src/cli-metadata.ts +0 -19
  34. package/src/cli.ts +0 -2298
  35. package/src/config-adapter.ts +0 -41
  36. package/src/config-schema.ts +0 -159
  37. package/src/config-ui-hints.ts +0 -56
  38. package/src/directory-live.ts +0 -238
  39. package/src/doctor-contract.ts +0 -287
  40. package/src/doctor.ts +0 -262
  41. package/src/env-vars.ts +0 -92
  42. package/src/exec-approval-resolver.ts +0 -23
  43. package/src/exec-approvals.ts +0 -287
  44. package/src/group-mentions.ts +0 -41
  45. package/src/legacy-crypto-inspector-availability.ts +0 -60
  46. package/src/legacy-crypto.ts +0 -531
  47. package/src/legacy-state.ts +0 -156
  48. package/src/matrix/account-config.ts +0 -175
  49. package/src/matrix/accounts.ts +0 -194
  50. package/src/matrix/actions/client.ts +0 -31
  51. package/src/matrix/actions/devices.ts +0 -34
  52. package/src/matrix/actions/limits.ts +0 -6
  53. package/src/matrix/actions/messages.ts +0 -129
  54. package/src/matrix/actions/pins.ts +0 -63
  55. package/src/matrix/actions/polls.ts +0 -109
  56. package/src/matrix/actions/profile.ts +0 -37
  57. package/src/matrix/actions/reactions.ts +0 -59
  58. package/src/matrix/actions/room.ts +0 -71
  59. package/src/matrix/actions/summary.ts +0 -88
  60. package/src/matrix/actions/types.ts +0 -63
  61. package/src/matrix/actions/verification.ts +0 -589
  62. package/src/matrix/actions.ts +0 -37
  63. package/src/matrix/active-client.ts +0 -26
  64. package/src/matrix/async-lock.ts +0 -18
  65. package/src/matrix/backup-health.ts +0 -124
  66. package/src/matrix/client/config-runtime-api.ts +0 -9
  67. package/src/matrix/client/config-secret-input.runtime.ts +0 -1
  68. package/src/matrix/client/config.ts +0 -853
  69. package/src/matrix/client/create-client.ts +0 -105
  70. package/src/matrix/client/env-auth.ts +0 -95
  71. package/src/matrix/client/file-sync-store.ts +0 -289
  72. package/src/matrix/client/logging.ts +0 -140
  73. package/src/matrix/client/migration-snapshot.runtime.ts +0 -1
  74. package/src/matrix/client/private-network-host.ts +0 -1
  75. package/src/matrix/client/runtime.ts +0 -4
  76. package/src/matrix/client/shared.ts +0 -316
  77. package/src/matrix/client/storage.ts +0 -543
  78. package/src/matrix/client/types.ts +0 -50
  79. package/src/matrix/client/url-validation.ts +0 -73
  80. package/src/matrix/client-bootstrap.ts +0 -173
  81. package/src/matrix/client.ts +0 -23
  82. package/src/matrix/config-paths.ts +0 -31
  83. package/src/matrix/config-update.ts +0 -292
  84. package/src/matrix/credentials-read.ts +0 -208
  85. package/src/matrix/credentials-write.runtime.ts +0 -35
  86. package/src/matrix/credentials.ts +0 -95
  87. package/src/matrix/deps.ts +0 -309
  88. package/src/matrix/device-health.ts +0 -29
  89. package/src/matrix/direct-management.ts +0 -349
  90. package/src/matrix/direct-room.ts +0 -128
  91. package/src/matrix/draft-stream.ts +0 -225
  92. package/src/matrix/encryption-guidance.ts +0 -24
  93. package/src/matrix/errors.ts +0 -21
  94. package/src/matrix/format.ts +0 -426
  95. package/src/matrix/legacy-crypto-inspector.ts +0 -95
  96. package/src/matrix/media-errors.ts +0 -20
  97. package/src/matrix/media-text.ts +0 -162
  98. package/src/matrix/monitor/access-state.ts +0 -145
  99. package/src/matrix/monitor/ack-config.ts +0 -27
  100. package/src/matrix/monitor/allowlist.ts +0 -89
  101. package/src/matrix/monitor/auto-join.ts +0 -86
  102. package/src/matrix/monitor/config.ts +0 -569
  103. package/src/matrix/monitor/context-summary.ts +0 -43
  104. package/src/matrix/monitor/direct.ts +0 -296
  105. package/src/matrix/monitor/events.ts +0 -397
  106. package/src/matrix/monitor/handler.ts +0 -2266
  107. package/src/matrix/monitor/inbound-dedupe.ts +0 -267
  108. package/src/matrix/monitor/index.ts +0 -540
  109. package/src/matrix/monitor/legacy-crypto-restore.ts +0 -139
  110. package/src/matrix/monitor/location.ts +0 -108
  111. package/src/matrix/monitor/media.ts +0 -119
  112. package/src/matrix/monitor/mentions.ts +0 -256
  113. package/src/matrix/monitor/reaction-events.ts +0 -197
  114. package/src/matrix/monitor/recent-invite.ts +0 -30
  115. package/src/matrix/monitor/replies.ts +0 -136
  116. package/src/matrix/monitor/reply-context.ts +0 -92
  117. package/src/matrix/monitor/room-history.ts +0 -301
  118. package/src/matrix/monitor/room-info.ts +0 -126
  119. package/src/matrix/monitor/rooms.ts +0 -52
  120. package/src/matrix/monitor/route.ts +0 -179
  121. package/src/matrix/monitor/runtime-api.ts +0 -28
  122. package/src/matrix/monitor/startup-verification.ts +0 -237
  123. package/src/matrix/monitor/startup.ts +0 -218
  124. package/src/matrix/monitor/status.ts +0 -120
  125. package/src/matrix/monitor/sync-lifecycle.ts +0 -91
  126. package/src/matrix/monitor/task-runner.ts +0 -38
  127. package/src/matrix/monitor/test-events.ts +0 -21
  128. package/src/matrix/monitor/thread-context.ts +0 -108
  129. package/src/matrix/monitor/threads.ts +0 -85
  130. package/src/matrix/monitor/types.ts +0 -30
  131. package/src/matrix/monitor/verification-events.ts +0 -643
  132. package/src/matrix/monitor/verification-utils.ts +0 -46
  133. package/src/matrix/outbound-media-runtime.ts +0 -1
  134. package/src/matrix/poll-summary.ts +0 -110
  135. package/src/matrix/poll-types.ts +0 -429
  136. package/src/matrix/probe.runtime.ts +0 -4
  137. package/src/matrix/probe.ts +0 -97
  138. package/src/matrix/profile.ts +0 -184
  139. package/src/matrix/reaction-common.ts +0 -147
  140. package/src/matrix/sdk/crypto-bootstrap.ts +0 -438
  141. package/src/matrix/sdk/crypto-facade.ts +0 -242
  142. package/src/matrix/sdk/crypto-node.runtime.ts +0 -17
  143. package/src/matrix/sdk/crypto-runtime.ts +0 -14
  144. package/src/matrix/sdk/decrypt-bridge.ts +0 -410
  145. package/src/matrix/sdk/event-helpers.ts +0 -83
  146. package/src/matrix/sdk/http-client.ts +0 -87
  147. package/src/matrix/sdk/idb-persistence-lock.ts +0 -51
  148. package/src/matrix/sdk/idb-persistence.ts +0 -288
  149. package/src/matrix/sdk/logger.ts +0 -108
  150. package/src/matrix/sdk/read-response-with-limit.ts +0 -19
  151. package/src/matrix/sdk/recovery-key-store.ts +0 -453
  152. package/src/matrix/sdk/timeout-abort-signal.ts +0 -1
  153. package/src/matrix/sdk/transport-runtime-api.ts +0 -18
  154. package/src/matrix/sdk/transport.ts +0 -352
  155. package/src/matrix/sdk/types.ts +0 -245
  156. package/src/matrix/sdk/verification-manager.ts +0 -795
  157. package/src/matrix/sdk/verification-status.ts +0 -23
  158. package/src/matrix/sdk.ts +0 -2152
  159. package/src/matrix/send/client.ts +0 -93
  160. package/src/matrix/send/formatting.ts +0 -189
  161. package/src/matrix/send/media.ts +0 -244
  162. package/src/matrix/send/targets.ts +0 -104
  163. package/src/matrix/send/types.ts +0 -131
  164. package/src/matrix/send.ts +0 -660
  165. package/src/matrix/session-store-metadata.ts +0 -108
  166. package/src/matrix/startup-abort.ts +0 -44
  167. package/src/matrix/subagent-hooks.ts +0 -308
  168. package/src/matrix/sync-state.ts +0 -27
  169. package/src/matrix/target-ids.ts +0 -79
  170. package/src/matrix/thread-bindings-shared.ts +0 -206
  171. package/src/matrix/thread-bindings.ts +0 -580
  172. package/src/matrix-migration.runtime.ts +0 -9
  173. package/src/migration-config.ts +0 -243
  174. package/src/migration-snapshot-backup.ts +0 -116
  175. package/src/migration-snapshot.ts +0 -53
  176. package/src/onboarding.ts +0 -775
  177. package/src/outbound.ts +0 -248
  178. package/src/plugin-entry.runtime.js +0 -115
  179. package/src/plugin-entry.runtime.ts +0 -70
  180. package/src/profile-update.ts +0 -71
  181. package/src/record-shared.ts +0 -3
  182. package/src/resolve-targets.ts +0 -175
  183. package/src/resolver.runtime.ts +0 -5
  184. package/src/resolver.ts +0 -21
  185. package/src/runtime-api.ts +0 -106
  186. package/src/runtime.ts +0 -13
  187. package/src/secret-contract.ts +0 -174
  188. package/src/session-route.ts +0 -126
  189. package/src/setup-bootstrap.ts +0 -102
  190. package/src/setup-config.ts +0 -222
  191. package/src/setup-contract.ts +0 -90
  192. package/src/setup-core.ts +0 -146
  193. package/src/setup-dm-policy.ts +0 -15
  194. package/src/setup-surface.ts +0 -4
  195. package/src/startup-maintenance.ts +0 -114
  196. package/src/storage-paths.ts +0 -92
  197. package/src/thread-binding-api.ts +0 -23
  198. package/src/tool-actions.runtime.ts +0 -1
  199. package/src/tool-actions.ts +0 -498
  200. package/src/types.ts +0 -257
  201. package/subagent-hooks-api.ts +0 -31
  202. package/test-api.ts +0 -21
  203. package/thread-binding-api.ts +0 -4
  204. package/thread-bindings-runtime.ts +0 -4
  205. package/tsconfig.json +0 -16
@@ -1,453 +0,0 @@
1
- import { loadJsonFile, saveJsonFile } from "klaw/plugin-sdk/json-store";
2
- import { decodeRecoveryKey } from "matrix-js-sdk/lib/crypto-api/recovery-key.js";
3
- import { formatMatrixErrorMessage, formatMatrixErrorReason } from "../errors.js";
4
- import { LogService } from "./logger.js";
5
- import type {
6
- MatrixCryptoBootstrapApi,
7
- MatrixCryptoCallbacks,
8
- MatrixGeneratedSecretStorageKey,
9
- MatrixSecretStorageStatus,
10
- MatrixStoredRecoveryKey,
11
- } from "./types.js";
12
-
13
- export function isRepairableSecretStorageAccessError(err: unknown): boolean {
14
- const message = formatMatrixErrorReason(err);
15
- if (!message) {
16
- return false;
17
- }
18
- if (message.includes("getsecretstoragekey callback returned falsey")) {
19
- return true;
20
- }
21
- // The homeserver still has secret storage, but the local recovery key cannot
22
- // authenticate/decrypt a required secret. During explicit bootstrap we can
23
- // recreate secret storage and continue with a new local baseline.
24
- if (message.includes("decrypting secret") && message.includes("bad mac")) {
25
- return true;
26
- }
27
- return false;
28
- }
29
-
30
- export class MatrixRecoveryKeyStore {
31
- private readonly secretStorageKeyCache = new Map<
32
- string,
33
- { key: Uint8Array; keyInfo?: MatrixStoredRecoveryKey["keyInfo"] }
34
- >();
35
- private stagedRecoveryKey: MatrixStoredRecoveryKey | null = null;
36
- private stagedRecoveryKeyUsed = false;
37
- private readonly stagedCacheKeyIds = new Set<string>();
38
-
39
- constructor(private readonly recoveryKeyPath?: string) {}
40
-
41
- buildCryptoCallbacks(): MatrixCryptoCallbacks {
42
- return {
43
- getSecretStorageKey: async ({ keys }) => {
44
- const requestedKeyIds = Object.keys(keys ?? {});
45
- if (requestedKeyIds.length === 0) {
46
- return null;
47
- }
48
-
49
- const staged = this.resolveStagedSecretStorageKey(requestedKeyIds);
50
- if (staged) {
51
- return staged;
52
- }
53
-
54
- for (const keyId of requestedKeyIds) {
55
- const cached = this.secretStorageKeyCache.get(keyId);
56
- if (cached) {
57
- return [keyId, new Uint8Array(cached.key)];
58
- }
59
- }
60
-
61
- const stored = this.loadStoredRecoveryKey();
62
- if (!stored?.privateKeyBase64) {
63
- return null;
64
- }
65
- const privateKey = new Uint8Array(Buffer.from(stored.privateKeyBase64, "base64"));
66
- if (privateKey.length === 0) {
67
- return null;
68
- }
69
-
70
- if (stored.keyId && requestedKeyIds.includes(stored.keyId)) {
71
- this.rememberSecretStorageKey(stored.keyId, privateKey, stored.keyInfo);
72
- return [stored.keyId, privateKey];
73
- }
74
-
75
- const firstRequestedKeyId = requestedKeyIds[0];
76
- if (!firstRequestedKeyId) {
77
- return null;
78
- }
79
- this.rememberSecretStorageKey(firstRequestedKeyId, privateKey, stored.keyInfo);
80
- return [firstRequestedKeyId, privateKey];
81
- },
82
- cacheSecretStorageKey: (keyId, keyInfo, key) => {
83
- const privateKey = new Uint8Array(key);
84
- const normalizedKeyInfo: MatrixStoredRecoveryKey["keyInfo"] = {
85
- passphrase: keyInfo?.passphrase,
86
- name: typeof keyInfo?.name === "string" ? keyInfo.name : undefined,
87
- };
88
- this.rememberSecretStorageKey(keyId, privateKey, normalizedKeyInfo);
89
-
90
- const stored = this.loadStoredRecoveryKey();
91
- this.saveRecoveryKeyToDisk({
92
- keyId,
93
- keyInfo: normalizedKeyInfo,
94
- privateKey,
95
- encodedPrivateKey: stored?.encodedPrivateKey,
96
- });
97
- },
98
- };
99
- }
100
-
101
- getRecoveryKeySummary(): {
102
- encodedPrivateKey?: string;
103
- keyId?: string | null;
104
- createdAt?: string;
105
- } | null {
106
- const stored = this.loadStoredRecoveryKey();
107
- if (!stored) {
108
- return null;
109
- }
110
- return {
111
- encodedPrivateKey: stored.encodedPrivateKey,
112
- keyId: stored.keyId,
113
- createdAt: stored.createdAt,
114
- };
115
- }
116
-
117
- private resolveEncodedRecoveryKeyInput(params: {
118
- encodedPrivateKey: string;
119
- keyId?: string | null;
120
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"];
121
- }): {
122
- encodedPrivateKey: string;
123
- privateKey: Uint8Array;
124
- keyId: string | null;
125
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"];
126
- } {
127
- const encodedPrivateKey = params.encodedPrivateKey.trim();
128
- if (!encodedPrivateKey) {
129
- throw new Error("Matrix recovery key is required");
130
- }
131
- let privateKey: Uint8Array;
132
- try {
133
- privateKey = decodeRecoveryKey(encodedPrivateKey);
134
- } catch (err) {
135
- throw new Error(`Invalid Matrix recovery key: ${formatMatrixErrorMessage(err)}`, {
136
- cause: err,
137
- });
138
- }
139
- const keyId =
140
- typeof params.keyId === "string" && params.keyId.trim() ? params.keyId.trim() : null;
141
- return {
142
- encodedPrivateKey,
143
- privateKey,
144
- keyId,
145
- keyInfo: params.keyInfo ?? this.loadStoredRecoveryKey()?.keyInfo,
146
- };
147
- }
148
-
149
- storeEncodedRecoveryKey(params: {
150
- encodedPrivateKey: string;
151
- keyId?: string | null;
152
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"];
153
- }): {
154
- encodedPrivateKey?: string;
155
- keyId?: string | null;
156
- createdAt?: string;
157
- } {
158
- const prepared = this.resolveEncodedRecoveryKeyInput(params);
159
- this.saveRecoveryKeyToDisk({
160
- keyId: prepared.keyId,
161
- keyInfo: prepared.keyInfo,
162
- privateKey: prepared.privateKey,
163
- encodedPrivateKey: prepared.encodedPrivateKey,
164
- });
165
- if (prepared.keyId) {
166
- this.rememberSecretStorageKey(prepared.keyId, prepared.privateKey, prepared.keyInfo);
167
- }
168
- return this.getRecoveryKeySummary() ?? {};
169
- }
170
-
171
- stageEncodedRecoveryKey(params: {
172
- encodedPrivateKey: string;
173
- keyId?: string | null;
174
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"];
175
- }): void {
176
- const prepared = this.resolveEncodedRecoveryKeyInput(params);
177
- this.discardStagedRecoveryKey();
178
- this.stagedRecoveryKey = {
179
- version: 1,
180
- createdAt: new Date().toISOString(),
181
- keyId: prepared.keyId,
182
- encodedPrivateKey: prepared.encodedPrivateKey,
183
- privateKeyBase64: Buffer.from(prepared.privateKey).toString("base64"),
184
- keyInfo: prepared.keyInfo,
185
- };
186
- }
187
-
188
- hasStagedRecoveryKeyBeenUsed(): boolean {
189
- return this.stagedRecoveryKeyUsed;
190
- }
191
-
192
- commitStagedRecoveryKey(params?: {
193
- keyId?: string | null;
194
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"];
195
- }): {
196
- encodedPrivateKey?: string;
197
- keyId?: string | null;
198
- createdAt?: string;
199
- } | null {
200
- if (!this.stagedRecoveryKey) {
201
- return this.getRecoveryKeySummary();
202
- }
203
- const staged = this.stagedRecoveryKey;
204
- const privateKey = new Uint8Array(Buffer.from(staged.privateKeyBase64, "base64"));
205
- const keyId =
206
- typeof params?.keyId === "string" && params.keyId.trim() ? params.keyId.trim() : staged.keyId;
207
- this.saveRecoveryKeyToDisk({
208
- keyId,
209
- keyInfo: params?.keyInfo ?? staged.keyInfo,
210
- privateKey,
211
- encodedPrivateKey: staged.encodedPrivateKey,
212
- });
213
- this.clearStagedRecoveryKeyTracking();
214
- return this.getRecoveryKeySummary();
215
- }
216
-
217
- discardStagedRecoveryKey(): void {
218
- for (const keyId of this.stagedCacheKeyIds) {
219
- this.secretStorageKeyCache.delete(keyId);
220
- }
221
- this.clearStagedRecoveryKeyTracking();
222
- }
223
-
224
- async bootstrapSecretStorageWithRecoveryKey(
225
- crypto: MatrixCryptoBootstrapApi,
226
- options: {
227
- setupNewKeyBackup?: boolean;
228
- allowSecretStorageRecreateWithoutRecoveryKey?: boolean;
229
- forceNewSecretStorage?: boolean;
230
- forceNewRecoveryKey?: boolean;
231
- } = {},
232
- ): Promise<void> {
233
- let status: MatrixSecretStorageStatus | null = null;
234
- const getSecretStorageStatus = crypto.getSecretStorageStatus; // pragma: allowlist secret
235
- if (typeof getSecretStorageStatus === "function") {
236
- try {
237
- status = await getSecretStorageStatus.call(crypto);
238
- } catch (err) {
239
- LogService.warn("MatrixClientLite", "Failed to read secret storage status:", err);
240
- }
241
- }
242
-
243
- const hasDefaultSecretStorageKey = Boolean(status?.defaultKeyId);
244
- const hasKnownInvalidSecrets = Object.values(status?.secretStorageKeyValidityMap ?? {}).some(
245
- (valid) => !valid,
246
- );
247
- let generatedRecoveryKey = false;
248
- const storedRecovery = this.loadStoredRecoveryKey();
249
- const stagedRecovery = this.stagedRecoveryKey;
250
- const sourceRecovery =
251
- options.forceNewRecoveryKey === true ? null : (stagedRecovery ?? storedRecovery);
252
- let recoveryKey: MatrixGeneratedSecretStorageKey | null = sourceRecovery
253
- ? {
254
- keyInfo: sourceRecovery.keyInfo,
255
- privateKey: new Uint8Array(Buffer.from(sourceRecovery.privateKeyBase64, "base64")),
256
- encodedPrivateKey: sourceRecovery.encodedPrivateKey,
257
- }
258
- : null;
259
-
260
- if (recoveryKey && status?.defaultKeyId) {
261
- const defaultKeyId = status.defaultKeyId;
262
- if (!stagedRecovery) {
263
- this.rememberSecretStorageKey(defaultKeyId, recoveryKey.privateKey, recoveryKey.keyInfo);
264
- if (storedRecovery && storedRecovery.keyId !== defaultKeyId) {
265
- this.saveRecoveryKeyToDisk({
266
- keyId: defaultKeyId,
267
- keyInfo: recoveryKey.keyInfo,
268
- privateKey: recoveryKey.privateKey,
269
- encodedPrivateKey: recoveryKey.encodedPrivateKey,
270
- });
271
- }
272
- }
273
- }
274
-
275
- const ensureRecoveryKey = async (): Promise<MatrixGeneratedSecretStorageKey> => {
276
- if (recoveryKey) {
277
- if (stagedRecovery) {
278
- this.stagedRecoveryKeyUsed = true;
279
- }
280
- return recoveryKey;
281
- }
282
- if (typeof crypto.createRecoveryKeyFromPassphrase !== "function") {
283
- throw new Error(
284
- "Matrix crypto backend does not support recovery key generation (createRecoveryKeyFromPassphrase missing)",
285
- );
286
- }
287
- recoveryKey = await crypto.createRecoveryKeyFromPassphrase();
288
- this.saveRecoveryKeyToDisk(recoveryKey);
289
- generatedRecoveryKey = true;
290
- return recoveryKey;
291
- };
292
-
293
- const shouldRecreateSecretStorage =
294
- options.forceNewSecretStorage === true ||
295
- !hasDefaultSecretStorageKey ||
296
- (!recoveryKey && status?.ready === false) ||
297
- hasKnownInvalidSecrets;
298
-
299
- if (hasKnownInvalidSecrets) {
300
- // Existing secret storage keys can't decrypt required secrets. Generate a fresh recovery key.
301
- recoveryKey = null;
302
- }
303
-
304
- const secretStorageOptions: {
305
- createSecretStorageKey?: () => Promise<MatrixGeneratedSecretStorageKey>;
306
- setupNewSecretStorage?: boolean;
307
- setupNewKeyBackup?: boolean;
308
- } = {
309
- setupNewKeyBackup: options.setupNewKeyBackup === true,
310
- };
311
-
312
- if (shouldRecreateSecretStorage) {
313
- secretStorageOptions.setupNewSecretStorage = true;
314
- secretStorageOptions.createSecretStorageKey = ensureRecoveryKey;
315
- }
316
-
317
- try {
318
- await crypto.bootstrapSecretStorage(secretStorageOptions);
319
- } catch (err) {
320
- const shouldRecreateWithoutRecoveryKey =
321
- options.allowSecretStorageRecreateWithoutRecoveryKey === true &&
322
- hasDefaultSecretStorageKey &&
323
- isRepairableSecretStorageAccessError(err);
324
- if (!shouldRecreateWithoutRecoveryKey) {
325
- throw err;
326
- }
327
-
328
- recoveryKey = null;
329
- LogService.warn(
330
- "MatrixClientLite",
331
- "Secret storage exists on the server but local recovery material cannot unlock it; recreating secret storage during explicit bootstrap.",
332
- );
333
- await crypto.bootstrapSecretStorage({
334
- setupNewSecretStorage: true,
335
- setupNewKeyBackup: options.setupNewKeyBackup === true,
336
- createSecretStorageKey: ensureRecoveryKey,
337
- });
338
- }
339
-
340
- if (generatedRecoveryKey && this.recoveryKeyPath) {
341
- LogService.warn(
342
- "MatrixClientLite",
343
- `Generated Matrix recovery key and saved it to ${this.recoveryKeyPath}. Keep this file secure.`,
344
- );
345
- }
346
- }
347
-
348
- private clearStagedRecoveryKeyTracking(): void {
349
- this.stagedRecoveryKey = null;
350
- this.stagedRecoveryKeyUsed = false;
351
- this.stagedCacheKeyIds.clear();
352
- }
353
-
354
- private resolveStagedSecretStorageKey(requestedKeyIds: string[]): [string, Uint8Array] | null {
355
- const staged = this.stagedRecoveryKey;
356
- if (!staged?.privateKeyBase64) {
357
- return null;
358
- }
359
- const privateKey = new Uint8Array(Buffer.from(staged.privateKeyBase64, "base64"));
360
- if (privateKey.length === 0) {
361
- return null;
362
- }
363
- const keyId =
364
- staged.keyId && requestedKeyIds.includes(staged.keyId) ? staged.keyId : requestedKeyIds[0];
365
- if (!keyId) {
366
- return null;
367
- }
368
- this.rememberStagedSecretStorageKey(keyId, privateKey, staged.keyInfo);
369
- this.stagedCacheKeyIds.add(keyId);
370
- return [keyId, privateKey];
371
- }
372
-
373
- private rememberStagedSecretStorageKey(
374
- keyId: string,
375
- key: Uint8Array,
376
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"],
377
- ): void {
378
- this.stagedRecoveryKeyUsed = true;
379
- this.rememberSecretStorageKey(keyId, key, keyInfo);
380
- }
381
-
382
- private rememberSecretStorageKey(
383
- keyId: string,
384
- key: Uint8Array,
385
- keyInfo?: MatrixStoredRecoveryKey["keyInfo"],
386
- ): void {
387
- if (!keyId.trim()) {
388
- return;
389
- }
390
- this.secretStorageKeyCache.set(keyId, {
391
- key: new Uint8Array(key),
392
- keyInfo,
393
- });
394
- }
395
-
396
- private loadStoredRecoveryKey(): MatrixStoredRecoveryKey | null {
397
- if (!this.recoveryKeyPath) {
398
- return null;
399
- }
400
- try {
401
- const parsed = loadJsonFile<Partial<MatrixStoredRecoveryKey>>(this.recoveryKeyPath);
402
- if (
403
- parsed?.version !== 1 ||
404
- typeof parsed.createdAt !== "string" ||
405
- typeof parsed.privateKeyBase64 !== "string" || // pragma: allowlist secret
406
- !parsed.privateKeyBase64.trim()
407
- ) {
408
- return null;
409
- }
410
- return {
411
- version: 1,
412
- createdAt: parsed.createdAt,
413
- keyId: typeof parsed.keyId === "string" ? parsed.keyId : null,
414
- encodedPrivateKey:
415
- typeof parsed.encodedPrivateKey === "string" ? parsed.encodedPrivateKey : undefined,
416
- privateKeyBase64: parsed.privateKeyBase64,
417
- keyInfo:
418
- parsed.keyInfo && typeof parsed.keyInfo === "object"
419
- ? {
420
- passphrase: parsed.keyInfo.passphrase,
421
- name: typeof parsed.keyInfo.name === "string" ? parsed.keyInfo.name : undefined,
422
- }
423
- : undefined,
424
- };
425
- } catch {
426
- return null;
427
- }
428
- }
429
-
430
- private saveRecoveryKeyToDisk(params: MatrixGeneratedSecretStorageKey): void {
431
- if (!this.recoveryKeyPath) {
432
- return;
433
- }
434
- try {
435
- const payload: MatrixStoredRecoveryKey = {
436
- version: 1,
437
- createdAt: new Date().toISOString(),
438
- keyId: typeof params.keyId === "string" ? params.keyId : null,
439
- encodedPrivateKey: params.encodedPrivateKey,
440
- privateKeyBase64: Buffer.from(params.privateKey).toString("base64"),
441
- keyInfo: params.keyInfo
442
- ? {
443
- passphrase: params.keyInfo.passphrase,
444
- name: params.keyInfo.name,
445
- }
446
- : undefined,
447
- };
448
- saveJsonFile(this.recoveryKeyPath, payload);
449
- } catch (err) {
450
- LogService.warn("MatrixClientLite", "Failed to persist recovery key:", err);
451
- }
452
- }
453
- }
@@ -1 +0,0 @@
1
- export { buildTimeoutAbortSignal } from "klaw/plugin-sdk/extension-shared";
@@ -1,18 +0,0 @@
1
- import { fetchWithRuntimeDispatcherOrMockedGlobal } from "klaw/plugin-sdk/runtime-fetch";
2
- import {
3
- closeDispatcher,
4
- createPinnedDispatcher,
5
- resolvePinnedHostnameWithPolicy,
6
- type PinnedDispatcherPolicy,
7
- type SsrFPolicy,
8
- } from "klaw/plugin-sdk/ssrf-dispatcher";
9
- export { buildTimeoutAbortSignal } from "./timeout-abort-signal.js";
10
-
11
- export {
12
- closeDispatcher,
13
- createPinnedDispatcher,
14
- fetchWithRuntimeDispatcherOrMockedGlobal,
15
- resolvePinnedHostnameWithPolicy,
16
- type PinnedDispatcherPolicy,
17
- type SsrFPolicy,
18
- };