@open-e2ee/signal-protocol-sdk 9.4.0 → 9.5.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/ADAPTERS.md +1 -0
- package/CHANGELOG.md +176 -0
- package/README.md +1 -1
- package/SECURITY.md +2 -2
- package/dist/client/client.d.ts +61 -62
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +127 -169
- package/dist/client/client.js.map +1 -1
- package/dist/client/content-adapter.d.ts +8 -0
- package/dist/client/content-adapter.d.ts.map +1 -1
- package/dist/client/content-adapter.js +4 -0
- package/dist/client/content-adapter.js.map +1 -1
- package/dist/client/hosted-anonymous-delivery.d.ts +1 -1
- package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
- package/dist/client/hosted-anonymous-delivery.js +3 -3
- package/dist/client/hosted-anonymous-delivery.js.map +1 -1
- package/dist/client/hosted-transport.d.ts +1 -1
- package/dist/client/hosted-transport.d.ts.map +1 -1
- package/dist/client/hosted-transport.js +20 -5
- package/dist/client/hosted-transport.js.map +1 -1
- package/dist/client/relay-acceptance.d.ts +8 -0
- package/dist/client/relay-acceptance.d.ts.map +1 -1
- package/dist/client/relay-acceptance.js +7 -0
- package/dist/client/relay-acceptance.js.map +1 -1
- package/dist/client/relay-subscription.d.ts +12 -0
- package/dist/client/relay-subscription.d.ts.map +1 -1
- package/dist/client/relay-subscription.js +226 -80
- package/dist/client/relay-subscription.js.map +1 -1
- package/dist/client/retry-family.d.ts +119 -0
- package/dist/client/retry-family.d.ts.map +1 -0
- package/dist/client/retry-family.js +170 -0
- package/dist/client/retry-family.js.map +1 -0
- package/dist/client/retry-request-envelope.d.ts +106 -0
- package/dist/client/retry-request-envelope.d.ts.map +1 -0
- package/dist/client/retry-request-envelope.js +245 -0
- package/dist/client/retry-request-envelope.js.map +1 -0
- package/dist/client/retry.d.ts +63 -22
- package/dist/client/retry.d.ts.map +1 -1
- package/dist/client/retry.js +234 -136
- package/dist/client/retry.js.map +1 -1
- package/dist/client/sealed-sender.d.ts +15 -3
- package/dist/client/sealed-sender.d.ts.map +1 -1
- package/dist/client/sealed-sender.js +22 -3
- package/dist/client/sealed-sender.js.map +1 -1
- package/dist/client/signal-service-cipher.d.ts +52 -0
- package/dist/client/signal-service-cipher.d.ts.map +1 -1
- package/dist/client/signal-service-cipher.js +184 -16
- package/dist/client/signal-service-cipher.js.map +1 -1
- package/dist/client/state.d.ts +7 -60
- package/dist/client/state.d.ts.map +1 -1
- package/dist/client/state.js +8 -86
- package/dist/client/state.js.map +1 -1
- package/dist/client/types.d.ts +10 -2
- package/dist/client/types.d.ts.map +1 -1
- package/dist/internal/protocol/sealed-sender/types.d.ts +1 -1
- package/dist/internal/protocol/sealed-sender/types.js +1 -1
- package/dist/local/store/received-content.d.ts +16 -2
- package/dist/local/store/received-content.d.ts.map +1 -1
- package/dist/local/store/received-content.js +18 -3
- package/dist/local/store/received-content.js.map +1 -1
- package/dist/local/store/reliability.d.ts +258 -2
- package/dist/local/store/reliability.d.ts.map +1 -1
- package/dist/local/store/reliability.js +390 -12
- package/dist/local/store/reliability.js.map +1 -1
- package/dist/remote/relay/types.d.ts +22 -29
- package/dist/remote/relay/types.d.ts.map +1 -1
- package/dist/types/api.d.ts +43 -22
- package/dist/types/api.d.ts.map +1 -1
- package/docs/ASSURANCE.md +5 -5
- package/docs/DEVIATIONS.md +4 -5
- package/docs/INTERFACES.md +2 -0
- package/package.json +1 -1
package/dist/client/client.js
CHANGED
|
@@ -74,6 +74,8 @@ import * as SessionOps from './sessions.js';
|
|
|
74
74
|
import * as GroupOps from './groups.js';
|
|
75
75
|
import * as PreKeyOps from './prekeys.js';
|
|
76
76
|
import * as RetryOps from './retry.js';
|
|
77
|
+
import { receiveInRetryFamily } from './retry-family.js';
|
|
78
|
+
import { deliveredEnvelopeFingerprint } from '../local/store/received-content.js';
|
|
77
79
|
import * as RelaySubscriptionOps from './relay-subscription.js';
|
|
78
80
|
import { SignalProtocolClientState, SIGNAL_PROTOCOL_CLIENT_CONSTANTS } from './state.js';
|
|
79
81
|
import { clientRelayFanOut, rethrowRelayWorkStopped } from './relay-work.js';
|
|
@@ -190,7 +192,6 @@ export class DefaultSignalProtocolClient {
|
|
|
190
192
|
return this.relay?.subscribeRelayConnectionState(listener) ?? (() => undefined);
|
|
191
193
|
}
|
|
192
194
|
relayUnsubscribe;
|
|
193
|
-
retryUnsubscribe;
|
|
194
195
|
/**
|
|
195
196
|
* Cached sender certificate for sealed sender.
|
|
196
197
|
* Lazily fetched and refreshed one hour before its signed expiration.
|
|
@@ -573,26 +574,25 @@ export class DefaultSignalProtocolClient {
|
|
|
573
574
|
get retryConfig() {
|
|
574
575
|
return {
|
|
575
576
|
keyRotationDebounceMs: SIGNAL_PROTOCOL_CLIENT_CONSTANTS.KEY_ROTATION_DEBOUNCE_MS,
|
|
576
|
-
retryDedupWindowMs: SIGNAL_PROTOCOL_CLIENT_CONSTANTS.RETRY_DEDUP_WINDOW_MS,
|
|
577
|
-
retryCleanupIntervalMs: SIGNAL_PROTOCOL_CLIENT_CONSTANTS.RETRY_CLEANUP_INTERVAL_MS,
|
|
578
577
|
};
|
|
579
578
|
}
|
|
580
579
|
/**
|
|
581
|
-
* Get retry
|
|
580
|
+
* Get retry response state for retry operations
|
|
582
581
|
*/
|
|
583
|
-
get
|
|
584
|
-
return this.state.
|
|
582
|
+
get retryResponseState() {
|
|
583
|
+
return this.state.getRetryResponseState();
|
|
585
584
|
}
|
|
586
585
|
/**
|
|
587
|
-
* Get retry callbacks for retry operations. Session changes
|
|
588
|
-
*
|
|
589
|
-
* waits for the recipient's lock only until
|
|
586
|
+
* Get retry callbacks for retry operations. Session changes use the given
|
|
587
|
+
* context, so their hooks run through its relay work. A resend goes only to
|
|
588
|
+
* the device that asked, and waits for the recipient's lock only until the
|
|
589
|
+
* stop signal aborts.
|
|
590
590
|
*/
|
|
591
|
-
retryCallbacks(
|
|
591
|
+
retryCallbacks(stopSignal, ctx) {
|
|
592
592
|
return {
|
|
593
593
|
archiveSession: (address) => SessionOps.archiveSession(ctx, address),
|
|
594
594
|
establishSession: (address, bundle) => SessionOps.establishSession(ctx, this.sesameManager, address, bundle),
|
|
595
|
-
|
|
595
|
+
resend: (userId, deviceId, content, options) => this.cipher.resendToDevice(userId, deviceId, content, options, stopSignal),
|
|
596
596
|
forcePreKeyRotation: () => this.regeneratePreKeysWithFreshIds(),
|
|
597
597
|
};
|
|
598
598
|
}
|
|
@@ -604,15 +604,18 @@ export class DefaultSignalProtocolClient {
|
|
|
604
604
|
}
|
|
605
605
|
/**
|
|
606
606
|
* Get relay subscription callbacks (delegate back to DefaultSignalProtocolClient methods).
|
|
607
|
-
* Receipts
|
|
608
|
-
* through its relay work. Each receipt send is relay work of
|
|
607
|
+
* Receipts, typing indicators and retry requests use the given context, so
|
|
608
|
+
* their hooks run through its relay work. Each receipt send is relay work of
|
|
609
|
+
* its own. A retry request runs in the current work, so a stop of that work
|
|
610
|
+
* reaches the receive route, and the route keeps the request envelope.
|
|
609
611
|
*/
|
|
610
|
-
relaySubscriptionCallbacks(ctx) {
|
|
612
|
+
relaySubscriptionCallbacks(ctx, stopSignal) {
|
|
611
613
|
return {
|
|
612
614
|
forcePreKeyRotation: () => this.regeneratePreKeysWithFreshIds(),
|
|
613
615
|
handleDeliveryReceipt: (envelope, receipt) => this.handleDeliveryReceipt(envelope, receipt, ctx),
|
|
614
616
|
handleTypingIndicator: (envelope, typing) => this.handleTypingIndicator(envelope, typing, ctx),
|
|
615
617
|
sendDeliveryReceipt: (userId, timestamps) => this.sendDeliveryReceiptAsRelayWork(userId, timestamps),
|
|
618
|
+
handleRetryRequest: (request, retry) => RetryOps.handleRetryRequestAndResend(ctx, request, this.retryResponseState, this.retryCallbacks(stopSignal, ctx), this.retryConfig, retry),
|
|
616
619
|
};
|
|
617
620
|
}
|
|
618
621
|
/**
|
|
@@ -883,10 +886,6 @@ export class DefaultSignalProtocolClient {
|
|
|
883
886
|
if (!syncFailed && finalConfig?.relay && finalConfig?.hooks?.onMessageDecrypted) {
|
|
884
887
|
client.startRelaySubscription();
|
|
885
888
|
}
|
|
886
|
-
// Start retry request subscription if relay supports it (SESAME spec §6.2)
|
|
887
|
-
if (!syncFailed && finalConfig?.relay?.subscribeRetryRequests) {
|
|
888
|
-
client.startRetryRequestSubscription();
|
|
889
|
-
}
|
|
890
889
|
// Validate sender keys config and warn on extreme values
|
|
891
890
|
if (finalConfig?.senderKeys) {
|
|
892
891
|
const sk = finalConfig.senderKeys;
|
|
@@ -1232,12 +1231,78 @@ export class DefaultSignalProtocolClient {
|
|
|
1232
1231
|
* 3. On retryable error: sends retry request via relay or options callback
|
|
1233
1232
|
* 4. Re-throws error for caller to handle
|
|
1234
1233
|
*
|
|
1234
|
+
* A `retry_request` envelope holds no plaintext. This method rejects it
|
|
1235
|
+
* before decryption and sends no retry request. Give it to
|
|
1236
|
+
* `receiveIncomingEnvelopes`, which handles it.
|
|
1237
|
+
*
|
|
1238
|
+
* A failed envelope that this client asked a resend for, and each resend
|
|
1239
|
+
* or null message for it, gives plaintext at most once on both receive
|
|
1240
|
+
* paths. When this client processed the envelope, or the application
|
|
1241
|
+
* already has that content, this method acknowledges the envelope and
|
|
1242
|
+
* throws MESSAGE_DUPLICATE with no decrypt.
|
|
1243
|
+
*
|
|
1244
|
+
* A null message carries no content. The client consumes a null message
|
|
1245
|
+
* when the content adapter reports `nullMessage: true`, and a custom
|
|
1246
|
+
* adapter must report it for its own null encoding. This method never
|
|
1247
|
+
* returns a null message. It marks the null message processed,
|
|
1248
|
+
* acknowledges it and throws MESSAGE_DUPLICATE. A null message for a
|
|
1249
|
+
* failed envelope marks that envelope processed, so a redelivery of it
|
|
1250
|
+
* is dropped, but a content resend for it that comes later still gives
|
|
1251
|
+
* its plaintext.
|
|
1252
|
+
*
|
|
1253
|
+
* A resend or null message that fails to decrypt, or whose seal fails to
|
|
1254
|
+
* open, asks again for its failed envelope under the same retry ID. When
|
|
1255
|
+
* the processed record of the failed envelope fails to store after a
|
|
1256
|
+
* decrypt, the client writes a warning with the behavior
|
|
1257
|
+
* RETRY_FULFILL_WRITE_FAILED, as on every receive path, and this method
|
|
1258
|
+
* returns the plaintext. A redelivery of the failed envelope or of
|
|
1259
|
+
* another family member can then give the content again. When a
|
|
1260
|
+
* processed record of a null message or of its failed envelope fails to
|
|
1261
|
+
* store, the client writes a warning with the behavior
|
|
1262
|
+
* RETRY_NULL_WRITE_FAILED, as on every receive path, and this method
|
|
1263
|
+
* still throws MESSAGE_DUPLICATE. A redelivery of the failed envelope
|
|
1264
|
+
* can then give the content.
|
|
1265
|
+
*
|
|
1235
1266
|
* @param envelope - The encrypted message envelope
|
|
1236
1267
|
* @param options - Transport callbacks for background (no relay) scenarios
|
|
1237
1268
|
* @returns Decrypted plaintext
|
|
1238
1269
|
* @throws EncryptionError after sending retry request if decryption fails
|
|
1270
|
+
* @throws EncryptionError INVALID_CIPHERTEXT for a `retry_request` envelope
|
|
1271
|
+
* @throws EncryptionError MESSAGE_DUPLICATE when this client processed the
|
|
1272
|
+
* envelope, the application already has the content of the failed
|
|
1273
|
+
* envelope of a retry request, or the envelope is a null message
|
|
1239
1274
|
*/
|
|
1240
1275
|
async processIncomingEnvelope(envelope, options) {
|
|
1276
|
+
if (envelope.messageType === 'retry_request') {
|
|
1277
|
+
throw new EncryptionError('A retry_request envelope holds no plaintext; give it to receiveIncomingEnvelopes', EncryptionErrorCode.INVALID_CIPHERTEXT, { messageType: envelope.messageType, senderId: envelope.senderUserId });
|
|
1278
|
+
}
|
|
1279
|
+
// The fingerprint covers the envelope as the Relay delivered it, before
|
|
1280
|
+
// any unseal, so every receive path stores one value for it.
|
|
1281
|
+
const fingerprint = envelope.id
|
|
1282
|
+
? await deliveredEnvelopeFingerprint(this.userId, this.deviceId, envelope)
|
|
1283
|
+
: undefined;
|
|
1284
|
+
return receiveInRetryFamily(this._storage, this.logger, envelope, async (family) => {
|
|
1285
|
+
if (!(await family.admit())) {
|
|
1286
|
+
await this.markMessageDeliveredSilently(envelope.id, options);
|
|
1287
|
+
throw new EncryptionError('The application already has the content of this failed envelope', EncryptionErrorCode.MESSAGE_DUPLICATE, { senderId: envelope.senderUserId });
|
|
1288
|
+
}
|
|
1289
|
+
const plaintext = await this.decryptIncomingEnvelope(envelope, family, fingerprint, options);
|
|
1290
|
+
// A null message carries no content, so the caller never gets it.
|
|
1291
|
+
if (this.contentAdapter.inspectContent(plaintext).nullMessage) {
|
|
1292
|
+
await family.consumeNull(fingerprint);
|
|
1293
|
+
await this.markMessageDeliveredSilently(envelope.id, options);
|
|
1294
|
+
throw new EncryptionError('A null message carries no content', EncryptionErrorCode.MESSAGE_DUPLICATE, { senderId: envelope.senderUserId });
|
|
1295
|
+
}
|
|
1296
|
+
await family.fulfill();
|
|
1297
|
+
return plaintext;
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Decrypt one envelope that is not a `retry_request`, under its retry family
|
|
1302
|
+
* lock. The fingerprint is that of the delivered envelope, also for the
|
|
1303
|
+
* inner envelope of a seal.
|
|
1304
|
+
*/
|
|
1305
|
+
async decryptIncomingEnvelope(envelope, family, fingerprint, options) {
|
|
1241
1306
|
// Handle sealed sender envelopes: unseal to reveal sender before decrypting
|
|
1242
1307
|
if (envelope.messageType === 'unidentified_sender' && this.config.sealedSender) {
|
|
1243
1308
|
const identityKeyPair = await this._storage.getIdentityKey();
|
|
@@ -1245,18 +1310,30 @@ export class DefaultSignalProtocolClient {
|
|
|
1245
1310
|
throw new EncryptionError('No identity key pair for sealed sender decryption', EncryptionErrorCode.DECRYPTION_FAILED);
|
|
1246
1311
|
}
|
|
1247
1312
|
const recipientPrivateKeyBytes = base64ToBytes(identityKeyPair.dhKey.privateKey);
|
|
1248
|
-
|
|
1313
|
+
let unsealed;
|
|
1314
|
+
try {
|
|
1315
|
+
unsealed = await unsealMessage(envelope.ciphertext, recipientPrivateKeyBytes, this.userId, this.deviceId, this.config.sealedSender, envelope.serverTimestamp, this.logger);
|
|
1316
|
+
}
|
|
1317
|
+
catch (error) {
|
|
1318
|
+
// A member whose seal fails to open asks the device of its request
|
|
1319
|
+
// for the next attempt, as a member whose decrypt fails does.
|
|
1320
|
+
if (family.member !== undefined) {
|
|
1321
|
+
await this.sendRetryRequestInternal(family.withRequestedSender(envelope), error, family, fingerprint, options);
|
|
1322
|
+
}
|
|
1323
|
+
throw error;
|
|
1324
|
+
}
|
|
1249
1325
|
// Process the inner envelope with revealed sender identity. The inner
|
|
1250
|
-
// type
|
|
1251
|
-
// group message from a pairwise one. The same mapping
|
|
1252
|
-
// receive path via `reconstructEnvelope`.
|
|
1253
|
-
return this.
|
|
1326
|
+
// type and the content hint travel inside the seal. Nothing outside it
|
|
1327
|
+
// distinguishes a group message from a pairwise one. The same mapping
|
|
1328
|
+
// serves the other receive path via `reconstructEnvelope`.
|
|
1329
|
+
return this.decryptIncomingEnvelope({
|
|
1254
1330
|
...envelope,
|
|
1255
1331
|
senderUserId: unsealed.senderUserId,
|
|
1256
1332
|
senderDeviceId: unsealed.senderDeviceId,
|
|
1257
1333
|
ciphertext: unsealed.innerCiphertextBase64,
|
|
1258
1334
|
messageType: envelopeTypeForContent(unsealed.contentType),
|
|
1259
|
-
|
|
1335
|
+
contentHint: unsealed.contentHint,
|
|
1336
|
+
}, family, fingerprint, options);
|
|
1260
1337
|
}
|
|
1261
1338
|
// Route group messages to sender key decryption. `sender_key` says the
|
|
1262
1339
|
// payload is a framed SenderKeyMessage. It does not say which group, so
|
|
@@ -1284,7 +1361,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1284
1361
|
catch (error) {
|
|
1285
1362
|
// Send retry request if error is retryable (per SESAME spec §4.1)
|
|
1286
1363
|
if (isRetryableDecryptionError(error)) {
|
|
1287
|
-
await this.sendRetryRequestInternal(envelope, error, options);
|
|
1364
|
+
await this.sendRetryRequestInternal(envelope, error, family, fingerprint, options);
|
|
1288
1365
|
}
|
|
1289
1366
|
// Re-throw so caller can handle (log, skip message, etc.)
|
|
1290
1367
|
throw error;
|
|
@@ -1339,7 +1416,10 @@ export class DefaultSignalProtocolClient {
|
|
|
1339
1416
|
/**
|
|
1340
1417
|
* Receive a pulled batch through the subscription content handler.
|
|
1341
1418
|
* The application must register onMessageDecrypted before calling this method.
|
|
1342
|
-
*
|
|
1419
|
+
* Processed IDs are ready for transport acknowledgment. They include
|
|
1420
|
+
* each envelope that the client drops as a duplicate, and each null
|
|
1421
|
+
* message that the content adapter reports, which the client consumes
|
|
1422
|
+
* and never gives to onMessageDecrypted.
|
|
1343
1423
|
*/
|
|
1344
1424
|
async receiveIncomingEnvelopes(envelopes) {
|
|
1345
1425
|
if (!this.hooks?.onMessageDecrypted) {
|
|
@@ -1373,10 +1453,12 @@ export class DefaultSignalProtocolClient {
|
|
|
1373
1453
|
*
|
|
1374
1454
|
* @param envelope - The failed message envelope
|
|
1375
1455
|
* @param error - The decryption error
|
|
1456
|
+
* @param family - The retry family of the delivered envelope
|
|
1457
|
+
* @param fingerprint - The fingerprint of the delivered envelope
|
|
1376
1458
|
* @param options - Optional transport callbacks (for background without relay)
|
|
1377
1459
|
*/
|
|
1378
|
-
async sendRetryRequestInternal(envelope, error, options) {
|
|
1379
|
-
return RetryOps.sendRetryRequestInternal(this.ctx, envelope, error, this.rateLimitState, { forcePreKeyRotation: () => this.regeneratePreKeysWithFreshIds() }, this.retryConfig, options);
|
|
1460
|
+
async sendRetryRequestInternal(envelope, error, family, fingerprint, options) {
|
|
1461
|
+
return RetryOps.sendRetryRequestInternal(this.ctx, envelope, error, this.rateLimitState, { forcePreKeyRotation: () => this.regeneratePreKeysWithFreshIds() }, this.retryConfig, family, fingerprint, options);
|
|
1380
1462
|
}
|
|
1381
1463
|
// ============================================================================
|
|
1382
1464
|
// FILE ENCRYPTION/DECRYPTION (delegated to files.ts)
|
|
@@ -1569,23 +1651,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1569
1651
|
}
|
|
1570
1652
|
/** Accept an authenticated composite-identity rotation and reset bound sessions. */
|
|
1571
1653
|
async acceptIdentityRotation(userId, identity, identityType = 'aci') {
|
|
1572
|
-
|
|
1573
|
-
// A retry that first discovered the changed tuple may already have failed
|
|
1574
|
-
// closed and entered the short-lived dedup window. Explicit acceptance is
|
|
1575
|
-
// the authority to try those still-pending requests again. It is never
|
|
1576
|
-
// inferred from the retry request itself.
|
|
1577
|
-
const retryPrefix = `${userId}:`;
|
|
1578
|
-
for (const key of this.retryDedupState.recentRetryRequests.keys()) {
|
|
1579
|
-
if (key.startsWith(retryPrefix)) {
|
|
1580
|
-
this.retryDedupState.recentRetryRequests.delete(key);
|
|
1581
|
-
}
|
|
1582
|
-
}
|
|
1583
|
-
if (this.retryUnsubscribe) {
|
|
1584
|
-
this.retryUnsubscribe();
|
|
1585
|
-
this.retryUnsubscribe = undefined;
|
|
1586
|
-
this.startRetryRequestSubscription();
|
|
1587
|
-
}
|
|
1588
|
-
return accepted;
|
|
1654
|
+
return SessionOps.acceptIdentityRotation(this.ctx, userId, identity, identityType);
|
|
1589
1655
|
}
|
|
1590
1656
|
/**
|
|
1591
1657
|
* Mark a message as read/delivered
|
|
@@ -1699,7 +1765,7 @@ export class DefaultSignalProtocolClient {
|
|
|
1699
1765
|
this.relayUnsubscribe = this.relay.subscribe(this.userId, this.deviceId, (envelope) => {
|
|
1700
1766
|
const handled = this.state.relayWork.run((work) => {
|
|
1701
1767
|
const ctx = this.relayWorkContext(work);
|
|
1702
|
-
return RelaySubscriptionOps.handleRelayMessage({ ...ctx, cipher: this.cipher, stopSignal: work.stopSignal }, envelope, this.relaySubscriptionState, this.relaySubscriptionCallbacks(ctx), this.relaySubscriptionConfig);
|
|
1768
|
+
return RelaySubscriptionOps.handleRelayMessage({ ...ctx, cipher: this.cipher, stopSignal: work.stopSignal }, envelope, this.relaySubscriptionState, this.relaySubscriptionCallbacks(ctx, work.stopSignal), this.relaySubscriptionConfig);
|
|
1703
1769
|
});
|
|
1704
1770
|
handled.catch((error) => {
|
|
1705
1771
|
// The relay delivers an envelope that it did not acknowledge again.
|
|
@@ -1747,73 +1813,6 @@ export class DefaultSignalProtocolClient {
|
|
|
1747
1813
|
});
|
|
1748
1814
|
}
|
|
1749
1815
|
}
|
|
1750
|
-
/**
|
|
1751
|
-
* Start listening for retry requests from recipients (SESAME spec §6.2)
|
|
1752
|
-
*
|
|
1753
|
-
* When a recipient cannot decrypt a message, they send a retry request.
|
|
1754
|
-
* This method subscribes to incoming retry requests and processes them
|
|
1755
|
-
* by resending the original message with a new session.
|
|
1756
|
-
*
|
|
1757
|
-
* Automatically started if relay.subscribeRetryRequests is available.
|
|
1758
|
-
* Call this manually if you need to restart the subscription.
|
|
1759
|
-
* A call while stop() runs is refused and logged; start again after stop()
|
|
1760
|
-
* resolves.
|
|
1761
|
-
*/
|
|
1762
|
-
startRetryRequestSubscription() {
|
|
1763
|
-
if (!this.relay || !this.relay.subscribeRetryRequests) {
|
|
1764
|
-
this.logger.debug('Relay does not support retry request subscription', {
|
|
1765
|
-
category: 'E2EE',
|
|
1766
|
-
});
|
|
1767
|
-
return;
|
|
1768
|
-
}
|
|
1769
|
-
// A start while stop() runs would outlive stop()
|
|
1770
|
-
if (this.stopping) {
|
|
1771
|
-
this.logger.debug('Retry request subscription not started: stop() in progress', {
|
|
1772
|
-
category: 'E2EE',
|
|
1773
|
-
});
|
|
1774
|
-
return;
|
|
1775
|
-
}
|
|
1776
|
-
// Avoid duplicate subscriptions
|
|
1777
|
-
if (this.retryUnsubscribe) {
|
|
1778
|
-
this.logger.debug('Retry request subscription already active', {
|
|
1779
|
-
category: 'E2EE',
|
|
1780
|
-
});
|
|
1781
|
-
return;
|
|
1782
|
-
}
|
|
1783
|
-
this.logger.debug('Starting retry request subscription', {
|
|
1784
|
-
category: 'E2EE',
|
|
1785
|
-
data: { userId: this.userId, deviceId: this.deviceId },
|
|
1786
|
-
});
|
|
1787
|
-
this.retryUnsubscribe = this.relay.subscribeRetryRequests(this.userId, this.deviceId, async (retryRequest) => {
|
|
1788
|
-
await this.handleRetryRequestAndResend(retryRequest);
|
|
1789
|
-
});
|
|
1790
|
-
}
|
|
1791
|
-
/**
|
|
1792
|
-
* Handle a retry request and resend the original message
|
|
1793
|
-
*
|
|
1794
|
-
* Per SESAME spec §4.1 (Resending Process):
|
|
1795
|
-
* 1. Look up MessageRecord by sequence number
|
|
1796
|
-
* 2. Validate retry limits and message TTL
|
|
1797
|
-
* 3. Check session state:
|
|
1798
|
-
* - If NO active session OR active session IS orphaned (matches MessageRecord.sessionStateId):
|
|
1799
|
-
* → Fetch prekey bundle and create new session
|
|
1800
|
-
* - If DIFFERENT active session exists:
|
|
1801
|
-
* → Use it directly (no prekey fetch, prevents rate limit issues)
|
|
1802
|
-
* 4. Re-encrypt and send the original message
|
|
1803
|
-
*
|
|
1804
|
-
* This prevents infinite retry loops where each retry creates a new session
|
|
1805
|
-
* with different header keys.
|
|
1806
|
-
*
|
|
1807
|
-
* The request runs as relay work, so its hooks run through that work.
|
|
1808
|
-
*
|
|
1809
|
-
* @param retryRequest - The retry request from the recipient
|
|
1810
|
-
*/
|
|
1811
|
-
handleRetryRequestAndResend(retryRequest) {
|
|
1812
|
-
return this.state.relayWork.run((work) => {
|
|
1813
|
-
const ctx = this.relayWorkContext(work);
|
|
1814
|
-
return RetryOps.handleRetryRequestAndResend(ctx, retryRequest, this.retryDedupState, this.retryCallbacks(work, ctx), this.retryConfig);
|
|
1815
|
-
});
|
|
1816
|
-
}
|
|
1817
1816
|
/**
|
|
1818
1817
|
* Send delivery receipt to original sender (all devices)
|
|
1819
1818
|
*
|
|
@@ -2016,10 +2015,9 @@ export class DefaultSignalProtocolClient {
|
|
|
2016
2015
|
* the work after the hook and writes nothing more. The relay delivers the
|
|
2017
2016
|
* envelope again after the next start.
|
|
2018
2017
|
*
|
|
2019
|
-
* A call to startRelaySubscription()
|
|
2020
|
-
*
|
|
2021
|
-
*
|
|
2022
|
-
* settles as it settles.
|
|
2018
|
+
* A call to startRelaySubscription() while stop() runs is refused and
|
|
2019
|
+
* logged; start again after stop() resolves. A stop() call that overlaps a
|
|
2020
|
+
* running stop() waits for it and settles as it settles.
|
|
2023
2021
|
*
|
|
2024
2022
|
* @example
|
|
2025
2023
|
* ```typescript
|
|
@@ -2052,10 +2050,10 @@ export class DefaultSignalProtocolClient {
|
|
|
2052
2050
|
// 1. Start the receipt deadline, 5 s from now. Take the receipt sends in
|
|
2053
2051
|
// progress, except a send that an earlier stop() stopped, and start the
|
|
2054
2052
|
// send of the batched delivery receipts, once each, as relay work. Until
|
|
2055
|
-
// the end of step
|
|
2056
|
-
// timer, and stopRelaySubscription() does not send it. Step
|
|
2053
|
+
// the end of step 6, a delivery that finishes batches its receipt with no
|
|
2054
|
+
// timer, and stopRelaySubscription() does not send it. Step 5 sends the
|
|
2057
2055
|
// batches that wait at the end of each wait, again while the deadline is
|
|
2058
|
-
// not past. A receipt batched after its last flush is dropped in step
|
|
2056
|
+
// not past. A receipt batched after its last flush is dropped in step 6
|
|
2059
2057
|
const receiptDeadline = Date.now() + RelaySubscriptionOps.STOP_RECEIPT_FLUSH_MS;
|
|
2060
2058
|
const receiptSends = [
|
|
2061
2059
|
...[...this.deliveryReceiptSends]
|
|
@@ -2081,23 +2079,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2081
2079
|
}
|
|
2082
2080
|
this.logger.debug('Relay subscription stopped', { category: 'E2EE' });
|
|
2083
2081
|
}
|
|
2084
|
-
// 3. Stop
|
|
2085
|
-
// it back if the call throws, as step 2 does
|
|
2086
|
-
const retryUnsubscribe = this.retryUnsubscribe;
|
|
2087
|
-
this.retryUnsubscribe = undefined;
|
|
2088
|
-
if (retryUnsubscribe) {
|
|
2089
|
-
try {
|
|
2090
|
-
retryUnsubscribe();
|
|
2091
|
-
}
|
|
2092
|
-
catch (error) {
|
|
2093
|
-
this.retryUnsubscribe ??= retryUnsubscribe;
|
|
2094
|
-
throw error;
|
|
2095
|
-
}
|
|
2096
|
-
this.logger.debug('Retry request subscription stopped', {
|
|
2097
|
-
category: 'E2EE',
|
|
2098
|
-
});
|
|
2099
|
-
}
|
|
2100
|
-
// 4. Stop all relay work except the delivery receipt sends, then wait
|
|
2082
|
+
// 3. Stop all relay work except the delivery receipt sends, then wait
|
|
2101
2083
|
// for the receipt sends of step 1, with their app hooks, until the
|
|
2102
2084
|
// receipt deadline. A delivery or retry request whose app hook returns
|
|
2103
2085
|
// during the wait writes nothing more, and the relay delivers it again
|
|
@@ -2108,17 +2090,17 @@ export class DefaultSignalProtocolClient {
|
|
|
2108
2090
|
this.state.relayWork.stopAllExcept(new Set(this.deliveryReceiptSends.keys()));
|
|
2109
2091
|
await this.waitForStopReceiptSends(receiptSends, receiptDeadline - Date.now());
|
|
2110
2092
|
}
|
|
2111
|
-
//
|
|
2093
|
+
// 4. Stop the relay work in progress and wait for its SDK work. Work
|
|
2112
2094
|
// that waits in an app hook is not awaited. It writes nothing after
|
|
2113
2095
|
// the hook, and the relay delivers it again after the next start. So a
|
|
2114
2096
|
// receipt send that waits in an app hook after the deadline stops, and
|
|
2115
2097
|
// that receipt is not sent. A receipt send that is still in its relay
|
|
2116
2098
|
// call is waited for, and that receipt can still be sent
|
|
2117
2099
|
await this.state.relayWork.settle();
|
|
2118
|
-
//
|
|
2100
|
+
// 5. A delivery can finish during steps 3, 4, and 5, and the relay does
|
|
2119
2101
|
// not deliver it again. Send the receipts that such deliveries batched,
|
|
2120
2102
|
// wait for them until the same receipt deadline, and stop what is left
|
|
2121
|
-
// as step
|
|
2103
|
+
// as step 4 does. Do this again while a batch waits and the deadline
|
|
2122
2104
|
// is not past
|
|
2123
2105
|
do {
|
|
2124
2106
|
const lateReceiptSends = this.flushPendingDeliveryReceipts();
|
|
@@ -2127,16 +2109,16 @@ export class DefaultSignalProtocolClient {
|
|
|
2127
2109
|
await this.waitForStopReceiptSends(lateReceiptSends, receiptDeadline - Date.now());
|
|
2128
2110
|
await this.state.relayWork.settle();
|
|
2129
2111
|
} while (Date.now() < receiptDeadline);
|
|
2130
|
-
//
|
|
2131
|
-
// receipts batched after the last flush of step
|
|
2132
|
-
// flush before the deadline or the first flush after step
|
|
2112
|
+
// 6. Clear internal tracking state via state manager. This drops the
|
|
2113
|
+
// receipts batched after the last flush of step 5, which is the last
|
|
2114
|
+
// flush before the deadline or the first flush after step 4. Then the
|
|
2133
2115
|
// hold ends, and new batches set their timers again
|
|
2134
2116
|
this.state.clearForStop();
|
|
2135
2117
|
}
|
|
2136
2118
|
finally {
|
|
2137
2119
|
receiptAccumulator.held = false;
|
|
2138
2120
|
}
|
|
2139
|
-
//
|
|
2121
|
+
// 7. Cleanup expired Sesame sessions
|
|
2140
2122
|
try {
|
|
2141
2123
|
await this.sesameManager.cleanupExpiredSessions();
|
|
2142
2124
|
}
|
|
@@ -2146,7 +2128,7 @@ export class DefaultSignalProtocolClient {
|
|
|
2146
2128
|
data: { error: error.message },
|
|
2147
2129
|
});
|
|
2148
2130
|
}
|
|
2149
|
-
//
|
|
2131
|
+
// 8. Cleanup expired message records (SESAME spec §6.2)
|
|
2150
2132
|
try {
|
|
2151
2133
|
const deleted = await this._storage.deleteExpiredMessageRecords(MESSAGE_RECORD_TTL_MS);
|
|
2152
2134
|
if (deleted > 0) {
|
|
@@ -2205,30 +2187,6 @@ export class DefaultSignalProtocolClient {
|
|
|
2205
2187
|
});
|
|
2206
2188
|
return cleaned;
|
|
2207
2189
|
}
|
|
2208
|
-
/**
|
|
2209
|
-
* Run periodic cleanup of internal tracking state.
|
|
2210
|
-
*
|
|
2211
|
-
* Safe to call frequently - internally throttled to avoid overhead.
|
|
2212
|
-
* Recommended call sites:
|
|
2213
|
-
* - App foreground transition
|
|
2214
|
-
* - After successful message batch processing
|
|
2215
|
-
* - Periodically during long sessions
|
|
2216
|
-
*
|
|
2217
|
-
* Cleans up:
|
|
2218
|
-
* - Expired retry dedup entries (recentRetryRequests)
|
|
2219
|
-
*
|
|
2220
|
-
* @returns Number of entries cleaned up
|
|
2221
|
-
*/
|
|
2222
|
-
runPeriodicCleanup() {
|
|
2223
|
-
const result = this.state.runPeriodicCleanup();
|
|
2224
|
-
if (result.cleaned > 0) {
|
|
2225
|
-
this.logger.debug('Periodic cleanup completed', {
|
|
2226
|
-
category: 'E2EE',
|
|
2227
|
-
data: { cleaned: result.cleaned },
|
|
2228
|
-
});
|
|
2229
|
-
}
|
|
2230
|
-
return result.cleaned;
|
|
2231
|
-
}
|
|
2232
2190
|
// ============================================================================
|
|
2233
2191
|
// KEY ROTATION (delegated to key-rotation.ts)
|
|
2234
2192
|
// ============================================================================
|