@open-e2ee/signal-protocol-sdk 13.5.2 → 14.0.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.
- package/CHANGELOG.md +323 -0
- package/README.md +1 -1
- package/SECURITY.md +2 -2
- package/device/README.md +4 -3
- package/dist/client/client.d.ts +20 -55
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +133 -165
- package/dist/client/client.js.map +1 -1
- package/dist/client/content-adapter.d.ts +35 -6
- package/dist/client/content-adapter.d.ts.map +1 -1
- package/dist/client/content-adapter.js +28 -0
- package/dist/client/content-adapter.js.map +1 -1
- package/dist/client/event-hooks.d.ts +22 -11
- package/dist/client/event-hooks.d.ts.map +1 -1
- package/dist/client/event-hooks.js.map +1 -1
- package/dist/client/group-fetcher.d.ts.map +1 -1
- package/dist/client/group-fetcher.js +43 -0
- package/dist/client/group-fetcher.js.map +1 -1
- package/dist/client/group-key-sync.d.ts +65 -0
- package/dist/client/group-key-sync.d.ts.map +1 -0
- package/dist/client/group-key-sync.js +286 -0
- package/dist/client/group-key-sync.js.map +1 -0
- package/dist/client/group-outbox.d.ts +20 -6
- package/dist/client/group-outbox.d.ts.map +1 -1
- package/dist/client/group-outbox.js +153 -80
- package/dist/client/group-outbox.js.map +1 -1
- package/dist/client/group-receive-authorization.d.ts +16 -9
- package/dist/client/group-receive-authorization.d.ts.map +1 -1
- package/dist/client/group-receive-authorization.js +25 -16
- package/dist/client/group-receive-authorization.js.map +1 -1
- package/dist/client/hosted.d.ts +7 -7
- package/dist/client/hosted.js +7 -7
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/receive-queue.d.ts +3 -4
- package/dist/client/receive-queue.d.ts.map +1 -1
- package/dist/client/receive-queue.js +24 -32
- package/dist/client/receive-queue.js.map +1 -1
- package/dist/client/relay-subscription.d.ts.map +1 -1
- package/dist/client/relay-subscription.js +4 -9
- package/dist/client/relay-subscription.js.map +1 -1
- package/dist/client/retry-request-envelope.d.ts +4 -2
- package/dist/client/retry-request-envelope.d.ts.map +1 -1
- package/dist/client/retry-request-envelope.js +6 -4
- package/dist/client/retry-request-envelope.js.map +1 -1
- package/dist/client/retry-request-lifecycle.d.ts +7 -8
- package/dist/client/retry-request-lifecycle.d.ts.map +1 -1
- package/dist/client/retry-request-lifecycle.js +10 -7
- package/dist/client/retry-request-lifecycle.js.map +1 -1
- package/dist/client/retry-terminal-recovery.d.ts +14 -15
- package/dist/client/retry-terminal-recovery.d.ts.map +1 -1
- package/dist/client/retry-terminal-recovery.js +16 -17
- package/dist/client/retry-terminal-recovery.js.map +1 -1
- package/dist/client/retry-utils.d.ts +22 -0
- package/dist/client/retry-utils.d.ts.map +1 -1
- package/dist/client/retry-utils.js +30 -0
- package/dist/client/retry-utils.js.map +1 -1
- package/dist/client/send-deadline.js +1 -1
- package/dist/client/send-deadline.js.map +1 -1
- package/dist/client/send-timestamp.d.ts +71 -14
- package/dist/client/send-timestamp.d.ts.map +1 -1
- package/dist/client/send-timestamp.js +87 -19
- package/dist/client/send-timestamp.js.map +1 -1
- package/dist/client/signal-service-cipher.d.ts +125 -13
- package/dist/client/signal-service-cipher.d.ts.map +1 -1
- package/dist/client/signal-service-cipher.js +413 -102
- package/dist/client/signal-service-cipher.js.map +1 -1
- package/dist/client/types.d.ts +21 -16
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/groups/key-sync-ledger.d.ts +65 -0
- package/dist/internal/groups/key-sync-ledger.d.ts.map +1 -0
- package/dist/internal/groups/key-sync-ledger.js +104 -0
- package/dist/internal/groups/key-sync-ledger.js.map +1 -0
- package/dist/internal/groups/receive-record.d.ts +4 -1
- package/dist/internal/groups/receive-record.d.ts.map +1 -1
- package/dist/internal/groups/receive-record.js.map +1 -1
- package/dist/internal/session/cipher.d.ts.map +1 -1
- package/dist/internal/session/cipher.js +14 -0
- package/dist/internal/session/cipher.js.map +1 -1
- package/dist/local/store/conformance/cases/durability.d.ts.map +1 -1
- package/dist/local/store/conformance/cases/durability.js +6 -0
- package/dist/local/store/conformance/cases/durability.js.map +1 -1
- package/dist/local/store/conformance/records.d.ts.map +1 -1
- package/dist/local/store/conformance/records.js +3 -0
- package/dist/local/store/conformance/records.js.map +1 -1
- package/dist/local/store/expo/adapter.js +2 -2
- package/dist/local/store/expo/adapter.js.map +1 -1
- package/dist/local/store/key-value/adapter.d.ts +6 -5
- package/dist/local/store/key-value/adapter.d.ts.map +1 -1
- package/dist/local/store/key-value/adapter.js +21 -19
- package/dist/local/store/key-value/adapter.js.map +1 -1
- package/dist/local/store/key-value/receive-work.d.ts +0 -5
- package/dist/local/store/key-value/receive-work.d.ts.map +1 -1
- package/dist/local/store/key-value/receive-work.js +0 -16
- package/dist/local/store/key-value/receive-work.js.map +1 -1
- package/dist/local/store/node/adapter.js +2 -2
- package/dist/local/store/node/adapter.js.map +1 -1
- package/dist/local/store/react-native/adapter.js +2 -2
- package/dist/local/store/react-native/adapter.js.map +1 -1
- package/dist/local/store/receive-work.d.ts +32 -32
- package/dist/local/store/receive-work.d.ts.map +1 -1
- package/dist/local/store/receive-work.js +112 -65
- package/dist/local/store/receive-work.js.map +1 -1
- package/dist/local/store/received-content.d.ts +0 -5
- package/dist/local/store/received-content.d.ts.map +1 -1
- package/dist/local/store/received-content.js +0 -17
- package/dist/local/store/received-content.js.map +1 -1
- package/dist/local/store/reliability.d.ts +191 -53
- package/dist/local/store/reliability.d.ts.map +1 -1
- package/dist/local/store/reliability.js +517 -37
- package/dist/local/store/reliability.js.map +1 -1
- package/dist/local/store/sqlite/key-storage/metadata.d.ts +1 -8
- package/dist/local/store/sqlite/key-storage/metadata.d.ts.map +1 -1
- package/dist/local/store/sqlite/key-storage/metadata.js +1 -18
- package/dist/local/store/sqlite/key-storage/metadata.js.map +1 -1
- package/dist/local/store/sqlite/migrations.d.ts.map +1 -1
- package/dist/local/store/sqlite/migrations.js +8 -0
- package/dist/local/store/sqlite/migrations.js.map +1 -1
- package/dist/local/store/sqlite/store.d.ts +5 -5
- package/dist/local/store/sqlite/store.d.ts.map +1 -1
- package/dist/local/store/sqlite/store.js +29 -9
- package/dist/local/store/sqlite/store.js.map +1 -1
- package/dist/local/store/web/adapter.d.ts +11 -10
- package/dist/local/store/web/adapter.d.ts.map +1 -1
- package/dist/local/store/web/adapter.js +69 -51
- package/dist/local/store/web/adapter.js.map +1 -1
- package/dist/local/store/web-sqlite/adapter.js +2 -2
- package/dist/local/store/web-sqlite/adapter.js.map +1 -1
- package/dist/types/api.d.ts +50 -71
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/errors.d.ts +52 -18
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +58 -17
- package/dist/types/errors.js.map +1 -1
- package/docs/ASSURANCE.md +5 -5
- package/docs/DEVIATIONS.md +126 -9
- package/docs/ERROR_HANDLING.md +38 -23
- package/docs/GETTING_STARTED.md +17 -0
- package/local/store/README.md +32 -8
- package/package.json +1 -1
package/dist/client/client.js
CHANGED
|
@@ -69,11 +69,13 @@ import { removeDistributionsFromMessageRecords } from './distribution-records.js
|
|
|
69
69
|
import { GroupSenderKeyOperations } from './group-sender-key-operations.js';
|
|
70
70
|
import { GroupReceiveRefusedError, classifyGroupContent, createGroupMembershipChecker, createGroupReceiveEvaluator, groupOfContext, } from './group-receive-authorization.js';
|
|
71
71
|
import { GroupFetcher } from './group-fetcher.js';
|
|
72
|
+
import { GroupKeySync, deleteGroupKeySyncWithRecord } from './group-key-sync.js';
|
|
73
|
+
import { bytesToHex } from '../encoding/hex.js';
|
|
72
74
|
import { envelopeCiphertextBytes, receiveRefusal, reportReceiveRefusal } from './delivery-error.js';
|
|
73
75
|
import { profileKeyExchange } from './profile-key-exchange.js';
|
|
74
76
|
import { attachClientMessageId, SignalProtocolServiceCipher, sortEnvelopesForDecryption, } from './signal-service-cipher.js';
|
|
75
77
|
import { establishMultiDeviceSessions } from '../internal/sesame/device-registry.js';
|
|
76
|
-
import { isRetryableDecryptionError } from './retry-utils.js';
|
|
78
|
+
import { isPartialCommitKyberPreKeyUse, isRetryableDecryptionError } from './retry-utils.js';
|
|
77
79
|
import { envelopeTypeForContent, SealedRetryRequestReceived, unsealMessage } from './sealed-sender.js';
|
|
78
80
|
import { ResendableContent } from './resendable-content.js';
|
|
79
81
|
import { decideContentRecovery, readLocalStoreProtection, resolveContentRecoveryPolicy, resolveReceiveQueuePolicy, } from './storage-protection.js';
|
|
@@ -89,12 +91,12 @@ import { receiveInRetryFamily } from './retry-family.js';
|
|
|
89
91
|
import { RetryRequestScheduler } from './retry-request-lifecycle.js';
|
|
90
92
|
import { ResendJobDriver } from './recovery-driver.js';
|
|
91
93
|
import { retryRequestPost } from './retry-request-envelope.js';
|
|
92
|
-
import { getOutgoingMessageIntent, migrateOutgoingIntentRecords, migrateRetryRequestRecords, } from '../local/store/reliability.js';
|
|
94
|
+
import { getOutgoingMessageIntent, migrateOutgoingIntentRecords, migrateReceivedTimestampRecords, migrateRetryRequestRecords, } from '../local/store/reliability.js';
|
|
93
95
|
import { manualResendOutcomeEvent, SEND_END_REPORT, sendOutcomeEvent, } from './send-deadline.js';
|
|
94
96
|
import { intentContent, SendIntentDriver } from './send-intent-driver.js';
|
|
95
|
-
import { checkNewSendTimestamp, SEND_TIMESTAMP_ORIGIN, } from './send-timestamp.js';
|
|
97
|
+
import { checkNewSendTimestamp, releaseNewSendTimestampWithLock, reserveNewSendTimestamp, SEND_TIMESTAMP_ORIGIN, } from './send-timestamp.js';
|
|
96
98
|
import { deliveredEnvelopeFingerprint } from '../local/store/received-content.js';
|
|
97
|
-
import { RECEIVE_WORK_DEFAULT_LIMIT
|
|
99
|
+
import { RECEIVE_WORK_DEFAULT_LIMIT } from '../local/store/receive-work.js';
|
|
98
100
|
import { awaitReceiveCompletion, blockReceiveWork, completeReceiveWork, denyReceiveWork, ReceiveQueue, } from './receive-queue.js';
|
|
99
101
|
import { receiveExpiresAt } from './receive-expiry.js';
|
|
100
102
|
import { boundReceiveHooks } from './receive-hook-bound.js';
|
|
@@ -104,7 +106,6 @@ import { relayReceiptJoinOf } from './relay-receipt-join.js';
|
|
|
104
106
|
import { SignalProtocolClientState, SIGNAL_PROTOCOL_CLIENT_CONSTANTS } from './state.js';
|
|
105
107
|
import { clientRelayFanOut, rethrowRelayWorkStopped, RelayWorkStopped, } from './relay-work.js';
|
|
106
108
|
import { StaleSessionRefreshError } from './relay-device-refusal.js';
|
|
107
|
-
import { createKeyedLocks, withKeyedLock } from '../utils/keyed-lock.js';
|
|
108
109
|
import { deleteMediaAttachment, resolveMediaAttachment, } from '../media/index.js';
|
|
109
110
|
import { StorageBackedSignalProtocolClientMedia } from './media.js';
|
|
110
111
|
// Group state (Signal Private Group System)
|
|
@@ -121,12 +122,6 @@ const storageLock = new AsyncLock({
|
|
|
121
122
|
timeout: 30000, // 30 second timeout for storage initialization
|
|
122
123
|
maxPending: 100, // Limit pending operations
|
|
123
124
|
});
|
|
124
|
-
/**
|
|
125
|
-
* `decryptGroupMessage` calls for one group message run one at a time per
|
|
126
|
-
* local store, from the read of its receive record to its completion, so
|
|
127
|
-
* one content is given once.
|
|
128
|
-
*/
|
|
129
|
-
const groupMessageReceiveLocks = createKeyedLocks();
|
|
130
125
|
/** The reason of a `denied` receive outcome. */
|
|
131
126
|
function receiveDenialReason(error) {
|
|
132
127
|
if (error instanceof GroupReceiveRefusedError)
|
|
@@ -204,6 +199,7 @@ export class DefaultSignalProtocolClient {
|
|
|
204
199
|
groupReceiveEvaluator;
|
|
205
200
|
/** The single fetcher that brings each group up to its claimed revision. */
|
|
206
201
|
groupFetcher;
|
|
202
|
+
groupKeySync;
|
|
207
203
|
/** The current membership that a retry resend of group content needs. */
|
|
208
204
|
groupMembership;
|
|
209
205
|
/** The processor of the stored receive records. */
|
|
@@ -459,7 +455,8 @@ export class DefaultSignalProtocolClient {
|
|
|
459
455
|
if (config?.groups) {
|
|
460
456
|
const groupConfig = config.groups;
|
|
461
457
|
// Each client write to a group record reports a changed init status.
|
|
462
|
-
|
|
458
|
+
// A deleted group record also deletes its key sync ledger value.
|
|
459
|
+
this.groupStore = observeGroupInitStatus(deleteGroupKeySyncWithRecord(config.groups.store ?? new SignalProtocolGroupStateStore(this._storage), this._storage), {
|
|
463
460
|
isObserved: () => typeof this.hooks.onGroupInitStatus === 'function',
|
|
464
461
|
report: (groupId, status) => callHook(this.hooks, 'onGroupInitStatus', createGroupId(groupId), status),
|
|
465
462
|
logger: this.logger,
|
|
@@ -589,6 +586,26 @@ export class DefaultSignalProtocolClient {
|
|
|
589
586
|
logger: this.logger,
|
|
590
587
|
});
|
|
591
588
|
this.groupFetcher = fetcher;
|
|
589
|
+
// Each own device gets the key of each group in a GroupKeySync.
|
|
590
|
+
const keySync = new GroupKeySync({
|
|
591
|
+
storage: this._storage,
|
|
592
|
+
store,
|
|
593
|
+
...(config.aci?.uuid && { localAci: config.aci.uuid }),
|
|
594
|
+
ownDevices: () => this.cipher.localOwnSyncDevices(),
|
|
595
|
+
send: (input, deviceIds, stopSignal) => this.cipher.sendGroupKeySyncToLocalOtherDevices(input, deviceIds, stopSignal),
|
|
596
|
+
runInBackground: (job) => this.state.relayWork.run((work) => job(work.stopSignal)),
|
|
597
|
+
logger: this.logger,
|
|
598
|
+
});
|
|
599
|
+
this.groupKeySync = keySync;
|
|
600
|
+
this.cipher.setOwnDevicesFirstSeenListener((deviceIds) => {
|
|
601
|
+
void keySync.devicesFirstSeen(deviceIds);
|
|
602
|
+
});
|
|
603
|
+
this.cipher.setGroupKeySyncReceiver(async ({ masterKey, revision }, sender) => {
|
|
604
|
+
const groupId = bytesToHex(deriveGroupSecretParams(masterKey).groupId);
|
|
605
|
+
await fetcher.admitSync(groupId, masterKey, revision);
|
|
606
|
+
// The sender has the key, so this device does not send it back.
|
|
607
|
+
await keySync.receivedFrom(groupId, sender);
|
|
608
|
+
});
|
|
592
609
|
this.groupReceiveEvaluator = createGroupReceiveEvaluator({
|
|
593
610
|
store,
|
|
594
611
|
fetcher,
|
|
@@ -944,7 +961,7 @@ export class DefaultSignalProtocolClient {
|
|
|
944
961
|
}
|
|
945
962
|
/**
|
|
946
963
|
* The session reset steps of a retry request that ended as
|
|
947
|
-
* `
|
|
964
|
+
* `posting-deadline`. Their session hooks run through
|
|
948
965
|
* the given context, and its stop signal stops their lock waits and relay
|
|
949
966
|
* requests. There are none without a relay.
|
|
950
967
|
*/
|
|
@@ -1239,6 +1256,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1239
1256
|
await client.initialize(retained);
|
|
1240
1257
|
await client.hydratePersistedState();
|
|
1241
1258
|
await migrateRetryRequestRecords(client._storage);
|
|
1259
|
+
await migrateReceivedTimestampRecords(client._storage, client.logger);
|
|
1242
1260
|
await migrateOutgoingIntentRecords(client._storage);
|
|
1243
1261
|
await client.applyContentRecoveryDecision();
|
|
1244
1262
|
// A failed sweep fails the start and runs again at the next start.
|
|
@@ -1764,10 +1782,11 @@ export class DefaultSignalProtocolClient {
|
|
|
1764
1782
|
* - `duplicate`: this client processed the envelope, or the application
|
|
1765
1783
|
* already has the content of the failed envelope of a retry request.
|
|
1766
1784
|
* The client acknowledged the envelope.
|
|
1767
|
-
* - `null-message`: the envelope carries no content
|
|
1768
|
-
*
|
|
1769
|
-
*
|
|
1770
|
-
*
|
|
1785
|
+
* - `null-message`: the envelope carries no content for the application.
|
|
1786
|
+
* It is a null message, or a `GroupKeySync` from an own device that the
|
|
1787
|
+
* client consumed. The client acknowledged it. A null message for a
|
|
1788
|
+
* failed envelope marks that envelope processed, but a content resend
|
|
1789
|
+
* for it that comes later still gives its content.
|
|
1771
1790
|
* - `denied`: the client refused the content. The ratchet step is spent,
|
|
1772
1791
|
* and the client acknowledged the envelope. Content from another
|
|
1773
1792
|
* account that carries a sync message is denied in any envelope.
|
|
@@ -1812,7 +1831,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1812
1831
|
...(after !== undefined && { after }),
|
|
1813
1832
|
limit: RECEIVE_WORK_DEFAULT_LIMIT,
|
|
1814
1833
|
});
|
|
1815
|
-
const record = page.find((item) => item.origin === 'caller' &&
|
|
1834
|
+
const record = page.find((item) => item.origin === 'caller' && item.messageId === messageId);
|
|
1816
1835
|
if (record) {
|
|
1817
1836
|
await this.completeCallerRecord(record.workId);
|
|
1818
1837
|
return;
|
|
@@ -1893,7 +1912,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1893
1912
|
...(after !== undefined && { after }),
|
|
1894
1913
|
limit: RECEIVE_WORK_DEFAULT_LIMIT,
|
|
1895
1914
|
});
|
|
1896
|
-
record = page.find((item) => item.stage !== 'receipt-due' &&
|
|
1915
|
+
record = page.find((item) => item.stage !== 'receipt-due' && item.messageId === messageId);
|
|
1897
1916
|
if (record === undefined && page.length < RECEIVE_WORK_DEFAULT_LIMIT)
|
|
1898
1917
|
return null;
|
|
1899
1918
|
const last = page[page.length - 1];
|
|
@@ -2001,25 +2020,22 @@ export class DefaultSignalProtocolClient {
|
|
|
2001
2020
|
* Receive one envelope that the application passed. The queue skips its
|
|
2002
2021
|
* record while the receive runs, and runs a pass after it.
|
|
2003
2022
|
*
|
|
2004
|
-
* @param direct - The group and the receive record ID of a framed group
|
|
2005
|
-
* message that `decryptGroupMessage` passed. A decrypt failure then
|
|
2006
|
-
* throws with no retry request and no report.
|
|
2007
2023
|
* @returns The outcome, and the error behind a `pending` or `denied`
|
|
2008
2024
|
* outcome
|
|
2009
2025
|
*/
|
|
2010
|
-
async receiveForCaller(envelope, options
|
|
2026
|
+
async receiveForCaller(envelope, options) {
|
|
2011
2027
|
if (envelope.messageType === 'retry_request') {
|
|
2012
2028
|
throw new EncryptionError('A retry_request envelope holds no plaintext; give it to receiveIncomingEnvelopes', EncryptionErrorCode.INVALID_CIPHERTEXT, { messageType: envelope.messageType, senderId: envelope.senderUserId });
|
|
2013
2029
|
}
|
|
2014
2030
|
// The fingerprint covers the envelope as the Relay delivered it, before
|
|
2015
2031
|
// any unseal, so every receive path stores one value for it.
|
|
2016
|
-
const workId =
|
|
2032
|
+
const workId = await deliveredEnvelopeFingerprint(this.userId, this.deviceId, envelope);
|
|
2017
2033
|
const release = this.receiveQueue.hold(workId);
|
|
2018
2034
|
try {
|
|
2019
2035
|
// The record sorts after the stored records once the resume ends. The
|
|
2020
2036
|
// wait comes before the retry family lock, which the resume also takes.
|
|
2021
2037
|
await this.receiveQueue.seeded();
|
|
2022
|
-
return await receiveInRetryFamily(this._storage, this.logger, this.hooks, envelope, (family) => this.receiveForCallerLocked(envelope, family, workId, options
|
|
2038
|
+
return await receiveInRetryFamily(this._storage, this.logger, this.hooks, envelope, (family) => this.receiveForCallerLocked(envelope, family, workId, options));
|
|
2023
2039
|
}
|
|
2024
2040
|
finally {
|
|
2025
2041
|
release();
|
|
@@ -2027,7 +2043,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2027
2043
|
}
|
|
2028
2044
|
}
|
|
2029
2045
|
/** Receive one envelope that the application passed, under its retry family lock. */
|
|
2030
|
-
async receiveForCallerLocked(envelope, family, workId, options
|
|
2046
|
+
async receiveForCallerLocked(envelope, family, workId, options) {
|
|
2031
2047
|
// The processed record of an envelope with no ID has no key.
|
|
2032
2048
|
const receiveId = envelope.id ? workId : undefined;
|
|
2033
2049
|
let record = await this._storage.getReceiveWork(workId);
|
|
@@ -2037,22 +2053,15 @@ export class DefaultSignalProtocolClient {
|
|
|
2037
2053
|
await this.markMessageDeliveredSilently(envelope.id, options);
|
|
2038
2054
|
return { outcome: { kind: 'duplicate' } };
|
|
2039
2055
|
}
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
};
|
|
2046
|
-
}
|
|
2047
|
-
// A redelivery of content that waits for completeReceive.
|
|
2048
|
-
if (record.stage === 'awaiting-completion') {
|
|
2049
|
-
return { outcome: await this.processedReceive(record, options) };
|
|
2050
|
-
}
|
|
2056
|
+
// The receive queue delivers a Relay record to onMessageDecrypted.
|
|
2057
|
+
if (record.origin === 'relay') {
|
|
2058
|
+
return {
|
|
2059
|
+
outcome: { kind: 'pending', messageId: record.messageId, reason: 'queued' },
|
|
2060
|
+
};
|
|
2051
2061
|
}
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
record = await this.fillAwaitingRecord(record, family, workId, opened);
|
|
2062
|
+
// A redelivery of content that waits for completeReceive.
|
|
2063
|
+
if (record.stage === 'awaiting-completion') {
|
|
2064
|
+
return { outcome: await this.processedReceive(record, options) };
|
|
2056
2065
|
}
|
|
2057
2066
|
}
|
|
2058
2067
|
else {
|
|
@@ -2061,7 +2070,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2061
2070
|
return { outcome: { kind: 'duplicate' } };
|
|
2062
2071
|
}
|
|
2063
2072
|
const opened = await this.openForCaller(envelope, family, receiveId, options);
|
|
2064
|
-
const decrypted = await this.decryptForCaller(envelope, opened, family, workId, options
|
|
2073
|
+
const decrypted = await this.decryptForCaller(envelope, opened, family, workId, options);
|
|
2065
2074
|
if ('outcome' in decrypted)
|
|
2066
2075
|
return decrypted;
|
|
2067
2076
|
record = decrypted.record;
|
|
@@ -2095,6 +2104,13 @@ export class DefaultSignalProtocolClient {
|
|
|
2095
2104
|
content.inspected.senderKeyDistributionOnly === false,
|
|
2096
2105
|
...RelaySubscriptionOps.groupOfFulfill(content),
|
|
2097
2106
|
});
|
|
2107
|
+
// A GroupKeySync from an own device is SDK content, so the caller
|
|
2108
|
+
// never gets it.
|
|
2109
|
+
if (await this.cipher.consumeGroupKeySync(content)) {
|
|
2110
|
+
await completeReceiveWork(this.ctx, record);
|
|
2111
|
+
await this.markMessageDeliveredSilently(envelope.id, options);
|
|
2112
|
+
return { outcome: { kind: 'null-message' } };
|
|
2113
|
+
}
|
|
2098
2114
|
const decision = await this.cipher.decideGroupContent(content);
|
|
2099
2115
|
if (decision.kind === 'denied') {
|
|
2100
2116
|
await denyReceiveWork(this.ctx, record, content, decision.error);
|
|
@@ -2172,34 +2188,6 @@ export class DefaultSignalProtocolClient {
|
|
|
2172
2188
|
receiptJoin: false,
|
|
2173
2189
|
});
|
|
2174
2190
|
}
|
|
2175
|
-
/**
|
|
2176
|
-
* Fill the sender fields of a converted legacy record from the envelope
|
|
2177
|
-
* that the application passed. The record keeps its age and its arrival
|
|
2178
|
-
* order, and becomes a helper record.
|
|
2179
|
-
*/
|
|
2180
|
-
async fillAwaitingRecord(existing, family, workId, opened) {
|
|
2181
|
-
const { inner, arrivedSealed } = opened;
|
|
2182
|
-
const groupId = existing.groupId ??
|
|
2183
|
-
(inner.messageType === 'sender_key'
|
|
2184
|
-
? ((await this.senderKeyManager.resolveGroupForFramedMessage(envelopeCiphertextBytes(inner.ciphertext), inner.senderUserId, inner.senderDeviceId)) ?? undefined)
|
|
2185
|
-
: undefined);
|
|
2186
|
-
const seed = this.callerReceiveCommit(family, workId).commit.seed(this.relayEnvelopeOf(inner), arrivedSealed);
|
|
2187
|
-
const filled = sizedReceiveWork({
|
|
2188
|
-
...seed({ plaintext: existing.plaintext, ...(groupId !== undefined && { groupId }) }),
|
|
2189
|
-
workId: existing.workId,
|
|
2190
|
-
revision: existing.revision + 1,
|
|
2191
|
-
stage: existing.stage,
|
|
2192
|
-
arrivalSeq: existing.arrivalSeq,
|
|
2193
|
-
receivedAt: existing.receivedAt,
|
|
2194
|
-
dueAt: existing.dueAt,
|
|
2195
|
-
plaintext: existing.plaintext,
|
|
2196
|
-
...(groupId !== undefined && { groupId }),
|
|
2197
|
-
...(existing.blockedOn !== undefined && { blockedOn: existing.blockedOn }),
|
|
2198
|
-
});
|
|
2199
|
-
if (await this._storage.putReceiveWork(filled, existing.revision))
|
|
2200
|
-
return filled;
|
|
2201
|
-
return (await this._storage.getReceiveWork(workId)) ?? existing;
|
|
2202
|
-
}
|
|
2203
2191
|
/**
|
|
2204
2192
|
* Decrypt one opened envelope under the receive admission, and commit its
|
|
2205
2193
|
* receive record with the ratchet step.
|
|
@@ -2210,11 +2198,13 @@ export class DefaultSignalProtocolClient {
|
|
|
2210
2198
|
* the outcome is `denied`. A retryable failure (per SESAME spec §4.1)
|
|
2211
2199
|
* sends a retry request, which acknowledges the envelope, and throws. A
|
|
2212
2200
|
* failure after the decrypt built the record is a store failure: it
|
|
2213
|
-
* throws with no request, and the ratchet step rolled back.
|
|
2201
|
+
* throws with no request, and the ratchet step rolled back. A refused
|
|
2202
|
+
* Kyber prekey use with no session for its base key is retryable, as
|
|
2203
|
+
* `isPartialCommitKyberPreKeyUse` tells.
|
|
2214
2204
|
*
|
|
2215
2205
|
* @returns The committed record, or the outcome when nothing committed
|
|
2216
2206
|
*/
|
|
2217
|
-
async decryptForCaller(envelope, opened, family, workId, options
|
|
2207
|
+
async decryptForCaller(envelope, opened, family, workId, options) {
|
|
2218
2208
|
const { inner, arrivedSealed } = opened;
|
|
2219
2209
|
const receiveId = envelope.id ? workId : undefined;
|
|
2220
2210
|
const handle = this.callerReceiveCommit(family, workId);
|
|
@@ -2227,9 +2217,8 @@ export class DefaultSignalProtocolClient {
|
|
|
2227
2217
|
// does not say which group, so the group comes from the frame's
|
|
2228
2218
|
// distribution identifier resolved against the local sender key
|
|
2229
2219
|
// store.
|
|
2230
|
-
const framed =
|
|
2231
|
-
const groupId =
|
|
2232
|
-
(await this.senderKeyManager.resolveGroupForFramedMessage(framed, inner.senderUserId, inner.senderDeviceId));
|
|
2220
|
+
const framed = envelopeCiphertextBytes(inner.ciphertext);
|
|
2221
|
+
const groupId = await this.senderKeyManager.resolveGroupForFramedMessage(framed, inner.senderUserId, inner.senderDeviceId);
|
|
2233
2222
|
if (groupId === null) {
|
|
2234
2223
|
throw new EncryptionError(`No sender key from ${inner.senderUserId} matches this group message - request key distribution`, EncryptionErrorCode.SESSION_NOT_FOUND);
|
|
2235
2224
|
}
|
|
@@ -2252,7 +2241,9 @@ export class DefaultSignalProtocolClient {
|
|
|
2252
2241
|
}
|
|
2253
2242
|
}
|
|
2254
2243
|
catch (error) {
|
|
2255
|
-
|
|
2244
|
+
// A Kyber prekey use without a session is a partial-commit artifact,
|
|
2245
|
+
// not a store failure that a redelivery repairs.
|
|
2246
|
+
if (handle.seeded() && !isPartialCommitKyberPreKeyUse(error))
|
|
2256
2247
|
throw error;
|
|
2257
2248
|
if (receiveRefusal(error) !== null) {
|
|
2258
2249
|
await reportReceiveRefusal(this._storage, this.hooks, inner, error, receiveId, failure.reported);
|
|
@@ -2287,7 +2278,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2287
2278
|
kind: 'processed',
|
|
2288
2279
|
// The caller gets the content as its sender gave it.
|
|
2289
2280
|
plaintext: this.contentAdapter.withoutGroupContext(record.plaintext),
|
|
2290
|
-
messageId: record.messageId
|
|
2281
|
+
messageId: record.messageId,
|
|
2291
2282
|
...(record.groupId !== undefined && { groupId: extractGroupId(record.groupId) }),
|
|
2292
2283
|
};
|
|
2293
2284
|
if (options?.onPersist) {
|
|
@@ -2326,14 +2317,12 @@ export class DefaultSignalProtocolClient {
|
|
|
2326
2317
|
pendingReceiveOf(record) {
|
|
2327
2318
|
const ready = record.stage === 'awaiting-completion';
|
|
2328
2319
|
return {
|
|
2329
|
-
|
|
2330
|
-
// record ID, as `completeReceive` matches.
|
|
2331
|
-
messageId: record.messageId ?? record.workId,
|
|
2320
|
+
messageId: record.messageId,
|
|
2332
2321
|
stage: ready ? 'awaiting-completion' : 'decrypted',
|
|
2333
2322
|
origin: record.origin,
|
|
2334
2323
|
receivedAt: record.receivedAt,
|
|
2335
|
-
|
|
2336
|
-
|
|
2324
|
+
senderId: record.senderUserId,
|
|
2325
|
+
senderDeviceId: record.senderDeviceId,
|
|
2337
2326
|
...(record.groupId !== undefined && { groupId: extractGroupId(record.groupId) }),
|
|
2338
2327
|
...(ready &&
|
|
2339
2328
|
record.plaintext !== undefined && {
|
|
@@ -2610,7 +2599,11 @@ export class DefaultSignalProtocolClient {
|
|
|
2610
2599
|
// that repeats a stored intent keeps the stored timestamp, which can be
|
|
2611
2600
|
// outside the window, so a refused value passes only when a stored intent
|
|
2612
2601
|
// for this identifier holds the same timestamp. The mark stops the inner
|
|
2613
|
-
// checks of this send.
|
|
2602
|
+
// checks of this send. A new DataMessage send also reserves its timestamp
|
|
2603
|
+
// here, because the serialized message holds it. A new send with an
|
|
2604
|
+
// application timestamp reserves it here too, before the profile key
|
|
2605
|
+
// offer, so the profile key update cannot take that value. The inner
|
|
2606
|
+
// reservation of the send then finds the entry with its own ID.
|
|
2614
2607
|
const applicationTimestamp = isDataMessage(content)
|
|
2615
2608
|
? content.timestamp
|
|
2616
2609
|
: options?.timestamp;
|
|
@@ -2622,6 +2615,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2622
2615
|
: profileKeyExchange(this);
|
|
2623
2616
|
let profileKey;
|
|
2624
2617
|
let timestampOrigin;
|
|
2618
|
+
let reservation;
|
|
2625
2619
|
let result;
|
|
2626
2620
|
// A receipt for this send waits until the send resolves. The hold starts
|
|
2627
2621
|
// before the stored-intent read, so a receipt for a replay of a sent
|
|
@@ -2649,9 +2643,20 @@ export class DefaultSignalProtocolClient {
|
|
|
2649
2643
|
throw error;
|
|
2650
2644
|
}
|
|
2651
2645
|
}
|
|
2646
|
+
if (!stored && (isDataMessage(content) || applicationTimestamp != null)) {
|
|
2647
|
+
reservation = await reserveNewSendTimestamp(this._storage, {
|
|
2648
|
+
clientMessageId,
|
|
2649
|
+
timestamp: applicationTimestamp,
|
|
2650
|
+
[SEND_TIMESTAMP_ORIGIN]: timestampOrigin,
|
|
2651
|
+
});
|
|
2652
|
+
timestampOrigin = 'checked';
|
|
2653
|
+
}
|
|
2652
2654
|
if (isDataMessage(content)) {
|
|
2653
2655
|
// DataMessage path: build Content, set timestamp, serialize to protobuf (application send-pipeline ordering)
|
|
2654
|
-
const timestamp =
|
|
2656
|
+
const timestamp = reservation?.timestamp ??
|
|
2657
|
+
content.timestamp ??
|
|
2658
|
+
stored?.clientTimestamp ??
|
|
2659
|
+
Date.now();
|
|
2655
2660
|
clientTimestamp = timestamp;
|
|
2656
2661
|
profileKey = await profileKeys?.outgoing();
|
|
2657
2662
|
const dm = { ...content, timestamp, ...(profileKey && { profileKey }) };
|
|
@@ -2688,6 +2693,23 @@ export class DefaultSignalProtocolClient {
|
|
|
2688
2693
|
? this.withDeferredHooks(context, encrypt)
|
|
2689
2694
|
: encrypt(context));
|
|
2690
2695
|
}
|
|
2696
|
+
catch (error) {
|
|
2697
|
+
// A send that failed before its intent write removes its reservation.
|
|
2698
|
+
// A stored intent with the reserved timestamp keeps it. A failed
|
|
2699
|
+
// release does not replace the error of the send.
|
|
2700
|
+
if (reservation) {
|
|
2701
|
+
try {
|
|
2702
|
+
await releaseNewSendTimestampWithLock(this._storage, reservation);
|
|
2703
|
+
}
|
|
2704
|
+
catch (releaseError) {
|
|
2705
|
+
this.logger.warn('Failed to release the send timestamp reservation', {
|
|
2706
|
+
category: 'E2EE',
|
|
2707
|
+
data: { clientMessageId, error: releaseError.message },
|
|
2708
|
+
});
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
throw error;
|
|
2712
|
+
}
|
|
2691
2713
|
finally {
|
|
2692
2714
|
this.deliveries.endSend(clientMessageId);
|
|
2693
2715
|
}
|
|
@@ -2699,7 +2721,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2699
2721
|
await profileKeys?.delivered(recipientId, profileKey);
|
|
2700
2722
|
if (protocolError)
|
|
2701
2723
|
await callHook(context.hooks, 'onRelayProtocolError', protocolError);
|
|
2702
|
-
return
|
|
2724
|
+
return result;
|
|
2703
2725
|
}
|
|
2704
2726
|
/** @see SignalProtocolClient.resendMessage */
|
|
2705
2727
|
async resendMessage(clientMessageId) {
|
|
@@ -2967,6 +2989,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2967
2989
|
data: { behavior: 'GROUP_FETCH_START_FAILED', error: error.message },
|
|
2968
2990
|
});
|
|
2969
2991
|
});
|
|
2992
|
+
void this.groupKeySync?.start();
|
|
2970
2993
|
void this.resendJobs.start();
|
|
2971
2994
|
const relay = this.relay;
|
|
2972
2995
|
if (this.resendJobsOnConnect === undefined && relay) {
|
|
@@ -3257,6 +3280,7 @@ export class DefaultSignalProtocolClient {
|
|
|
3257
3280
|
this.resendJobs.stop();
|
|
3258
3281
|
this.sendIntents.stop();
|
|
3259
3282
|
this.groupFetcher?.stop();
|
|
3283
|
+
this.groupKeySync?.stop();
|
|
3260
3284
|
this.receiveQueue.stop();
|
|
3261
3285
|
this.resendJobsOnConnect?.();
|
|
3262
3286
|
this.resendJobsOnConnect = undefined;
|
|
@@ -3492,6 +3516,16 @@ export class DefaultSignalProtocolClient {
|
|
|
3492
3516
|
* use this to encrypt messages. All group members can decrypt
|
|
3493
3517
|
* the same ciphertext, making it efficient for large groups.
|
|
3494
3518
|
*
|
|
3519
|
+
* This primitive adds no group context and checks no group master key.
|
|
3520
|
+
* Unless the plaintext carries a group context, a
|
|
3521
|
+
* `processIncomingEnvelope` receiver denies the output for a managed
|
|
3522
|
+
* group. A receiver with a `groups` config gives the reason
|
|
3523
|
+
* `legacy-no-context`, or `blocked` when the group is blocked. A receiver
|
|
3524
|
+
* with no `groups` config refuses the output with a
|
|
3525
|
+
* `GroupReceiveRefusedError` of reason `configuration`. Send managed group
|
|
3526
|
+
* content with `send`. A caller that delivers the bytes itself decrypts
|
|
3527
|
+
* them with `decryptEnvelope`, as a `sender_key` envelope.
|
|
3528
|
+
*
|
|
3495
3529
|
* @param groupId - Group identifier
|
|
3496
3530
|
* @param plaintext - Message to encrypt
|
|
3497
3531
|
* @returns Framed SenderKeyMessage bytes
|
|
@@ -3512,83 +3546,6 @@ export class DefaultSignalProtocolClient {
|
|
|
3512
3546
|
? (candidateGroupId) => this.groupManager.assertGroupSendAllowed(candidateGroupId)
|
|
3513
3547
|
: undefined));
|
|
3514
3548
|
}
|
|
3515
|
-
/**
|
|
3516
|
-
* Decrypt a group message from a sender
|
|
3517
|
-
*
|
|
3518
|
-
* Use this to decrypt messages from other group members.
|
|
3519
|
-
* You must process the sender's distribution message first.
|
|
3520
|
-
*
|
|
3521
|
-
* The message passes the group decision of `processIncomingEnvelope`.
|
|
3522
|
-
* Content whose group state must update first stays stored, and a later
|
|
3523
|
-
* call with the same message decides it with no second decrypt.
|
|
3524
|
-
*
|
|
3525
|
-
* @deprecated Use `processIncomingEnvelope`, which returns a receive
|
|
3526
|
-
* outcome and keeps the content until the application completes the
|
|
3527
|
-
* receive. This method completes the receive before it returns, so a
|
|
3528
|
-
* plaintext that the application loses is gone.
|
|
3529
|
-
*
|
|
3530
|
-
* @param groupId - Group identifier
|
|
3531
|
-
* @param senderId - Sender's user ID
|
|
3532
|
-
* @param senderDeviceId - Sender's device ID
|
|
3533
|
-
* @param framedMessage - Framed SenderKeyMessage bytes
|
|
3534
|
-
* @returns Decrypted plaintext
|
|
3535
|
-
* @throws GroupReceiveUnavailableError when the group state of the content
|
|
3536
|
-
* is not current. Its `messageId` names the stored receive. Call again
|
|
3537
|
-
* later with the same message.
|
|
3538
|
-
* @throws GroupReceiveRefusedError when the client refuses the content
|
|
3539
|
-
* @throws EncryptionError MESSAGE_DUPLICATE for a message that this client
|
|
3540
|
-
* processed, or that carries no content, and STORAGE_QUOTA_EXCEEDED when
|
|
3541
|
-
* the receive queue has no room
|
|
3542
|
-
*
|
|
3543
|
-
* @example
|
|
3544
|
-
* ```typescript
|
|
3545
|
-
* const plaintext = await signal.decryptGroupMessage(
|
|
3546
|
-
* 'group-123',
|
|
3547
|
-
* senderId,
|
|
3548
|
-
* senderDeviceId,
|
|
3549
|
-
* encryptedMessage
|
|
3550
|
-
* );
|
|
3551
|
-
* console.log('Message:', plaintext);
|
|
3552
|
-
* ```
|
|
3553
|
-
*/
|
|
3554
|
-
async decryptGroupMessage(groupId, senderId, senderDeviceId, framedMessage) {
|
|
3555
|
-
// The message has no envelope ID, so its receive record ID binds the
|
|
3556
|
-
// sender and the framed bytes.
|
|
3557
|
-
const ciphertext = bytesToBase64(framedMessage);
|
|
3558
|
-
const workId = await deliveredEnvelopeFingerprint(this.userId, this.deviceId, {
|
|
3559
|
-
senderUserId: senderId,
|
|
3560
|
-
senderDeviceId,
|
|
3561
|
-
messageType: 'sender_key',
|
|
3562
|
-
ciphertext,
|
|
3563
|
-
});
|
|
3564
|
-
// A concurrent call with the same message waits here, and then reads
|
|
3565
|
-
// the record that the first call left, as a later call does.
|
|
3566
|
-
return withKeyedLock(groupMessageReceiveLocks, this._storage, workId, async () => {
|
|
3567
|
-
const { outcome, error } = await this.receiveForCaller({
|
|
3568
|
-
id: '',
|
|
3569
|
-
senderUserId: senderId,
|
|
3570
|
-
senderDeviceId,
|
|
3571
|
-
ciphertext,
|
|
3572
|
-
timestamp: Date.now(),
|
|
3573
|
-
messageType: 'sender_key',
|
|
3574
|
-
}, undefined, { groupId, workId, framed: framedMessage });
|
|
3575
|
-
switch (outcome.kind) {
|
|
3576
|
-
case 'processed':
|
|
3577
|
-
await this.completeCallerRecord(workId);
|
|
3578
|
-
return outcome.plaintext;
|
|
3579
|
-
case 'pending':
|
|
3580
|
-
case 'denied':
|
|
3581
|
-
if (error)
|
|
3582
|
-
throw error;
|
|
3583
|
-
break;
|
|
3584
|
-
case 'refused':
|
|
3585
|
-
throw new EncryptionError('The receive queue has no room for this group message', EncryptionErrorCode.STORAGE_QUOTA_EXCEEDED, { senderId });
|
|
3586
|
-
default:
|
|
3587
|
-
break;
|
|
3588
|
-
}
|
|
3589
|
-
throw new EncryptionError('This group message gives no content', EncryptionErrorCode.MESSAGE_DUPLICATE, { senderId });
|
|
3590
|
-
});
|
|
3591
|
-
}
|
|
3592
3549
|
/**
|
|
3593
3550
|
* Rotate sender key for a group (forward secrecy on membership changes).
|
|
3594
3551
|
*
|
|
@@ -3897,7 +3854,9 @@ export class DefaultSignalProtocolClient {
|
|
|
3897
3854
|
* Create a new group.
|
|
3898
3855
|
*/
|
|
3899
3856
|
async createGroup(creatorAci, creatorProfileKey, members, title, options) {
|
|
3900
|
-
|
|
3857
|
+
const created = await GroupOps.createGroup(this.ctx, await this.groups, creatorAci, creatorProfileKey, members, title, options);
|
|
3858
|
+
void this.groupKeySync?.groupJoined(extractGroupId(created.groupId));
|
|
3859
|
+
return created;
|
|
3901
3860
|
}
|
|
3902
3861
|
/**
|
|
3903
3862
|
* Get decrypted group state (from cache or server).
|
|
@@ -3936,7 +3895,8 @@ export class DefaultSignalProtocolClient {
|
|
|
3936
3895
|
}
|
|
3937
3896
|
/** Accept this client's pending profile-key invitation. */
|
|
3938
3897
|
async acceptGroupMemberInvitation(groupId) {
|
|
3939
|
-
|
|
3898
|
+
await GroupOps.acceptGroupMemberInvitation(this.ctx, await this.groups, groupId);
|
|
3899
|
+
void this.groupKeySync?.groupJoined(extractGroupId(groupId));
|
|
3940
3900
|
}
|
|
3941
3901
|
/**
|
|
3942
3902
|
* Decline this account's ACI- or PNI-keyed pending invitation.
|
|
@@ -3984,7 +3944,15 @@ export class DefaultSignalProtocolClient {
|
|
|
3984
3944
|
* Join a group via invite link.
|
|
3985
3945
|
*/
|
|
3986
3946
|
async joinGroupViaInviteLink(url, userAci, userProfileKey) {
|
|
3987
|
-
|
|
3947
|
+
const joined = await GroupOps.joinGroupViaInviteLink(this.ctx, await this.groups, url, userAci, userProfileKey);
|
|
3948
|
+
// Only a join with membership sends the key now. A join that waits for
|
|
3949
|
+
// approval or fails stores the master key and no state, so no pass sends
|
|
3950
|
+
// its key until its state arrives. The next start, join or first sight
|
|
3951
|
+
// of an own device after that sends it.
|
|
3952
|
+
if (joined.status === 'joined') {
|
|
3953
|
+
void this.groupKeySync?.groupJoined(extractGroupId(joined.groupId));
|
|
3954
|
+
}
|
|
3955
|
+
return joined;
|
|
3988
3956
|
}
|
|
3989
3957
|
// ============================================================================
|
|
3990
3958
|
// PRIVATE METHODS
|