@kodelyth/matrix 2026.5.39 → 2026.5.42
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.
- package/CHANGELOG.md +321 -0
- package/SPEC-SUPPORT.md +116 -0
- package/api.ts +38 -0
- package/auth-presence.ts +56 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/dist/account-selection-Y50DNJ2l.js +158 -0
- package/dist/active-client-CmFdvPdO.js +20 -0
- package/dist/api.js +12 -0
- package/dist/approval-handler.runtime-BIi4fL0R.js +377 -0
- package/dist/approval-ids-BGHK7PnZ.js +7 -0
- package/dist/approval-reaction-auth-CL0-nCNV.js +27 -0
- package/dist/approval-reactions-nDm2x-K5.js +162 -0
- package/dist/async-lock-SsmtFXtt.js +19 -0
- package/dist/auth-presence.js +26 -0
- package/dist/backup-health-3BHbHxyd.js +60 -0
- package/dist/channel-C0kCyTNB.js +1380 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-CdrdEN-0.js +250 -0
- package/dist/cli-FtY6Nuzw.js +1338 -0
- package/dist/cli-metadata-Dkwua7CB.js +22 -0
- package/dist/cli-metadata.js +2 -0
- package/dist/client-BnohYygh.js +25 -0
- package/dist/client-PhrTwuC4.js +30 -0
- package/dist/client-bootstrap-Mcj8ChJ5.js +114 -0
- package/dist/config-paths-DVvt6vM3.js +114 -0
- package/dist/config-schema-BMGOlhdI.js +308 -0
- package/dist/config-secret-input.runtime-Dv_4Br_f.js +2 -0
- package/dist/contract-api.js +8 -0
- package/dist/create-client-J0htTaRj.js +64 -0
- package/dist/credentials-B7GsBbgQ.js +56 -0
- package/dist/credentials-read-8fE4qoWs.js +112 -0
- package/dist/credentials-write.runtime-BibplB4Y.js +17 -0
- package/dist/crypto-node.runtime-D9qxgRPa.js +12 -0
- package/dist/crypto-runtime-1pKW4O2F.js +1214 -0
- package/dist/deps-DVpDS81G.js +208 -0
- package/dist/device-health-Ct2wDSPG.js +16 -0
- package/dist/directory-live-i3T8uORc.js +150 -0
- package/dist/doctor-contract-BLzYHl_9.js +246 -0
- package/dist/doctor-contract-api.js +2 -0
- package/dist/doctor-diR5gE7D.js +153 -0
- package/dist/draft-stream-HpPJ_VJt.js +143 -0
- package/dist/encryption-guidance-BNEgckrZ.js +15 -0
- package/dist/env-auth-UFiTGkDM.js +63 -0
- package/dist/env-vars-EQKQv-FE.js +63 -0
- package/dist/errors-BETj3zr9.js +17 -0
- package/dist/exec-approval-resolver-BxPorU_t.js +15 -0
- package/dist/helper-api.js +4 -0
- package/dist/http-client-DoQgbQsU.js +331 -0
- package/dist/index.js +46 -0
- package/dist/legacy-crypto-inspector-zK0hDCbt.js +41 -0
- package/dist/legacy-crypto-restore-DSFIXuDo.js +85 -0
- package/dist/logging-Df7aPD1z.js +99 -0
- package/dist/matrix-migration.runtime-BNoT1Prt.js +525 -0
- package/dist/media-text-ZhGA8Pcs.js +146 -0
- package/dist/messages-CRA9WGg0.js +140 -0
- package/dist/migration-snapshot-backup-BR-xD7Ew.js +69 -0
- package/dist/migration-snapshot.runtime-BLcy_Nvw.js +2 -0
- package/dist/monitor-DQm7_13y.js +4331 -0
- package/dist/plugin-entry.handlers.runtime.js +51 -0
- package/dist/probe.runtime-CjJS53Kz.js +3 -0
- package/dist/profile-update-DqkPgZ1P.js +68 -0
- package/dist/reaction-common-CmVLzP-u.js +71 -0
- package/dist/reaction-events-D0nUJuZV.js +121 -0
- package/dist/record-shared-DGvSFn5M.js +2 -0
- package/dist/resolve-targets-ChECUzD2.js +140 -0
- package/dist/resolver.runtime-hdY3n0GO.js +5 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/route-xRKj_ESW.js +161 -0
- package/dist/runtime-B-Fyrmxo.js +8 -0
- package/dist/runtime-api-BYXXkxq2.js +24 -0
- package/dist/runtime-api.js +25 -0
- package/dist/runtime-heavy-api.js +3 -0
- package/dist/runtime-lwTSy9Yt.js +6 -0
- package/dist/runtime-setter-api.js +2 -0
- package/dist/sdk-Jhq7mLtD.js +1704 -0
- package/dist/secret-contract-DEMcDsjl.js +120 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/send-CJunc6QM.js +1517 -0
- package/dist/setup-bootstrap-rJ0qZWPe.js +62 -0
- package/dist/setup-core-BEYoXF3J.js +677 -0
- package/dist/setup-entry.js +19 -0
- package/dist/setup-plugin-api.js +43 -0
- package/dist/setup-surface-c28ON6jq.js +537 -0
- package/dist/shared-D6MFMnpG.js +642 -0
- package/dist/startup-abort-B2J3MU_h.js +109 -0
- package/dist/startup-verification-CkD4Cwce.js +132 -0
- package/dist/storage-nyO0DOFE.js +281 -0
- package/dist/storage-paths-BTAketfg.js +52 -0
- package/dist/subagent-hooks-api-Dr_xnMRG.js +170 -0
- package/dist/subagent-hooks-api.js +2 -0
- package/dist/sync-state-Bx0gPaGA.js +12 -0
- package/dist/target-ids-Bsazo8si.js +77 -0
- package/dist/test-api.js +4 -0
- package/dist/thread-binding-api-IGU0-L70.js +17 -0
- package/dist/thread-binding-api.js +2 -0
- package/dist/thread-bindings-FjAZmDUP.js +352 -0
- package/dist/thread-bindings-runtime.js +2 -0
- package/dist/thread-bindings-shared-fvfP7jVs.js +97 -0
- package/dist/timeout-abort-signal-DpSHDHhR.js +2 -0
- package/dist/tool-actions.runtime-Cbo7YcYZ.js +532 -0
- package/dist/url-validation-DlrXNjAE.js +36 -0
- package/dist/verification-7tDPRpJU.js +345 -0
- package/doctor-contract-api.ts +1 -0
- package/helper-api.ts +3 -0
- package/index.ts +55 -0
- package/klaw.plugin.json +3 -891
- package/package.json +4 -4
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +72 -0
- package/runtime-heavy-api.ts +1 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +17 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-selection.ts +223 -0
- package/src/actions.ts +346 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.ts +592 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.ts +345 -0
- package/src/approval-reaction-auth.ts +45 -0
- package/src/approval-reactions.ts +313 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +97 -0
- package/src/channel.runtime.ts +17 -0
- package/src/channel.setup.ts +48 -0
- package/src/channel.ts +667 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.ts +2298 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.ts +159 -0
- package/src/config-ui-hints.ts +56 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.ts +287 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.ts +531 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +175 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.ts +129 -0
- package/src/matrix/actions/pins.ts +63 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +63 -0
- package/src/matrix/actions/verification.ts +589 -0
- package/src/matrix/actions.ts +37 -0
- package/src/matrix/active-client.ts +26 -0
- package/src/matrix/async-lock.ts +18 -0
- package/src/matrix/backup-health.ts +124 -0
- package/src/matrix/client/config-runtime-api.ts +9 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +853 -0
- package/src/matrix/client/create-client.ts +105 -0
- package/src/matrix/client/env-auth.ts +95 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +140 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +1 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.ts +316 -0
- package/src/matrix/client/storage.ts +543 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client/url-validation.ts +73 -0
- package/src/matrix/client-bootstrap.ts +173 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.ts +292 -0
- package/src/matrix/credentials-read.ts +208 -0
- package/src/matrix/credentials-write.runtime.ts +35 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.ts +309 -0
- package/src/matrix/device-health.ts +29 -0
- package/src/matrix/direct-management.ts +349 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.ts +225 -0
- package/src/matrix/encryption-guidance.ts +24 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.ts +426 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +162 -0
- package/src/matrix/monitor/access-state.ts +145 -0
- package/src/matrix/monitor/ack-config.ts +27 -0
- package/src/matrix/monitor/allowlist.ts +89 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.ts +569 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.ts +296 -0
- package/src/matrix/monitor/events.ts +397 -0
- package/src/matrix/monitor/handler.ts +2266 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.ts +540 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +108 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.ts +256 -0
- package/src/matrix/monitor/reaction-events.ts +197 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.ts +179 -0
- package/src/matrix/monitor/runtime-api.ts +28 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +120 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/test-events.ts +21 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +643 -0
- package/src/matrix/monitor/verification-utils.ts +46 -0
- package/src/matrix/outbound-media-runtime.ts +1 -0
- package/src/matrix/poll-summary.ts +110 -0
- package/src/matrix/poll-types.ts +429 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.ts +97 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +438 -0
- package/src/matrix/sdk/crypto-facade.ts +242 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +17 -0
- package/src/matrix/sdk/crypto-runtime.ts +14 -0
- package/src/matrix/sdk/decrypt-bridge.ts +410 -0
- package/src/matrix/sdk/event-helpers.ts +83 -0
- package/src/matrix/sdk/http-client.ts +87 -0
- package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
- package/src/matrix/sdk/idb-persistence.ts +288 -0
- package/src/matrix/sdk/logger.ts +108 -0
- package/src/matrix/sdk/read-response-with-limit.ts +19 -0
- package/src/matrix/sdk/recovery-key-store.ts +453 -0
- package/src/matrix/sdk/timeout-abort-signal.ts +1 -0
- package/src/matrix/sdk/transport-runtime-api.ts +18 -0
- package/src/matrix/sdk/transport.ts +352 -0
- package/src/matrix/sdk/types.ts +245 -0
- package/src/matrix/sdk/verification-manager.ts +795 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.ts +2152 -0
- package/src/matrix/send/client.ts +93 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +131 -0
- package/src/matrix/send.ts +660 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/subagent-hooks.ts +308 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +79 -0
- package/src/matrix/thread-bindings-shared.ts +206 -0
- package/src/matrix/thread-bindings.ts +580 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +116 -0
- package/src/migration-snapshot.ts +53 -0
- package/src/onboarding.ts +775 -0
- package/src/outbound.ts +248 -0
- package/src/plugin-entry.runtime.js +115 -0
- package/src/plugin-entry.runtime.ts +70 -0
- package/src/profile-update.ts +71 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.runtime.ts +5 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +106 -0
- package/src/runtime.ts +13 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.ts +126 -0
- package/src/setup-bootstrap.ts +102 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +90 -0
- package/src/setup-core.ts +146 -0
- package/src/setup-dm-policy.ts +15 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/thread-binding-api.ts +23 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +257 -0
- package/subagent-hooks-api.ts +31 -0
- package/test-api.ts +21 -0
- package/thread-binding-api.ts +4 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/auth-presence.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/cli-metadata.js +0 -7
- package/contract-api.js +0 -7
- package/doctor-contract-api.js +0 -7
- package/helper-api.js +0 -7
- package/index.js +0 -7
- package/plugin-entry.handlers.runtime.js +0 -7
- package/runtime-api.js +0 -7
- package/runtime-heavy-api.js +0 -7
- package/runtime-setter-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/subagent-hooks-api.js +0 -7
- package/test-api.js +0 -7
- package/thread-binding-api.js +0 -7
- package/thread-bindings-runtime.js +0 -7
|
@@ -0,0 +1,453 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { buildTimeoutAbortSignal } from "klaw/plugin-sdk/extension-shared";
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
};
|