@open-e2ee/signal-protocol-sdk 9.4.0 → 10.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/ADAPTERS.md +1 -0
- package/CHANGELOG.md +229 -0
- package/README.md +1 -1
- package/SECURITY.md +4 -4
- package/client/README.md +29 -12
- package/dist/client/client.d.ts +75 -63
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +207 -179
- package/dist/client/client.js.map +1 -1
- package/dist/client/config.d.ts +18 -3
- package/dist/client/config.d.ts.map +1 -1
- package/dist/client/config.js.map +1 -1
- package/dist/client/constants.d.ts +2 -10
- package/dist/client/constants.d.ts.map +1 -1
- package/dist/client/constants.js +3 -12
- package/dist/client/constants.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/delivery-status.d.ts +50 -0
- package/dist/client/delivery-status.d.ts.map +1 -0
- package/dist/client/delivery-status.js +121 -0
- package/dist/client/delivery-status.js.map +1 -0
- package/dist/client/event-hooks.d.ts +44 -10
- package/dist/client/event-hooks.d.ts.map +1 -1
- package/dist/client/event-hooks.js +26 -0
- package/dist/client/event-hooks.js.map +1 -1
- package/dist/client/hosted-anonymous-delivery.d.ts +2 -2
- package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
- package/dist/client/hosted-anonymous-delivery.js +7 -4
- package/dist/client/hosted-anonymous-delivery.js.map +1 -1
- package/dist/client/hosted-mailbox-subscription.d.ts +39 -6
- package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
- package/dist/client/hosted-mailbox-subscription.js +133 -26
- package/dist/client/hosted-mailbox-subscription.js.map +1 -1
- package/dist/client/hosted-push.d.ts +4 -3
- package/dist/client/hosted-push.d.ts.map +1 -1
- package/dist/client/hosted-push.js +18 -6
- package/dist/client/hosted-push.js.map +1 -1
- package/dist/client/hosted-transport.d.ts +26 -6
- package/dist/client/hosted-transport.d.ts.map +1 -1
- package/dist/client/hosted-transport.js +101 -14
- package/dist/client/hosted-transport.js.map +1 -1
- 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/messages.d.ts +4 -2
- package/dist/client/messages.d.ts.map +1 -1
- package/dist/client/messages.js +32 -7
- package/dist/client/messages.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-receipt-intake.d.ts +70 -0
- package/dist/client/relay-receipt-intake.d.ts.map +1 -0
- package/dist/client/relay-receipt-intake.js +111 -0
- package/dist/client/relay-receipt-intake.js.map +1 -0
- package/dist/client/relay-receipt-join.d.ts +50 -0
- package/dist/client/relay-receipt-join.d.ts.map +1 -0
- package/dist/client/relay-receipt-join.js +106 -0
- package/dist/client/relay-receipt-join.js.map +1 -0
- 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 +246 -83
- 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 +64 -1
- package/dist/client/signal-service-cipher.d.ts.map +1 -1
- package/dist/client/signal-service-cipher.js +212 -17
- 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 +11 -3
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.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 +291 -2
- package/dist/local/store/reliability.d.ts.map +1 -1
- package/dist/local/store/reliability.js +451 -13
- package/dist/local/store/reliability.js.map +1 -1
- package/dist/remote/relay/index.d.ts +1 -1
- package/dist/remote/relay/index.d.ts.map +1 -1
- package/dist/remote/relay/index.js.map +1 -1
- package/dist/remote/relay/types.d.ts +34 -30
- 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
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The retry family of a received envelope.
|
|
3
|
+
*
|
|
4
|
+
* A failed envelope, the request record that the requester keeps for it, and
|
|
5
|
+
* each resend or null message that answers that record form one retry
|
|
6
|
+
* family. The family index gives the failed envelope of a retry ID. Both
|
|
7
|
+
* receive paths, the relay subscription route and `processIncomingEnvelope`,
|
|
8
|
+
* receive each envelope through `receiveInRetryFamily`, so the application
|
|
9
|
+
* gets the content of one failed envelope at most once, from the envelope
|
|
10
|
+
* itself or from one family member.
|
|
11
|
+
*/
|
|
12
|
+
import { getProcessedEnvelope, getRetryRequest, markRetryRequestAnswered, resolveRetryFamilyMember, RetryRequestedEnvelopeChangedError, storeProcessedEnvelope, withProcessedEnvelopeLock, } from '../local/store/reliability.js';
|
|
13
|
+
/**
|
|
14
|
+
* Receive one envelope in the retry family of its failed envelope.
|
|
15
|
+
*
|
|
16
|
+
* A resend or a null message resolves to its failed envelope through the
|
|
17
|
+
* family index, by its SDK ID: the client message ID, or the envelope ID
|
|
18
|
+
* when the relay delivers no client message ID. A relay that assigns its
|
|
19
|
+
* own envelope IDs delivers the SDK ID as the client message ID. The
|
|
20
|
+
* receive then holds the processed-envelope lock of that failed envelope,
|
|
21
|
+
* so the fulfilled check, the decrypt and the fulfilled store of one
|
|
22
|
+
* family run one at a time on all paths. Any other envelope holds the lock
|
|
23
|
+
* of its own ID. An envelope with no ID holds no lock and has no family.
|
|
24
|
+
* The processed record and the acknowledgment use the delivered envelope
|
|
25
|
+
* ID.
|
|
26
|
+
*
|
|
27
|
+
* @param storage - The local store that keeps the retry records.
|
|
28
|
+
* @param logger - The client logger.
|
|
29
|
+
* @param envelope - The IDs of the received envelope: the delivered
|
|
30
|
+
* envelope ID, and the client message ID when the relay delivers one.
|
|
31
|
+
* @param receive - The receive path. It runs under the lock.
|
|
32
|
+
*/
|
|
33
|
+
export async function receiveInRetryFamily(storage, logger, envelope, receive) {
|
|
34
|
+
const envelopeId = envelope.id;
|
|
35
|
+
if (!envelopeId)
|
|
36
|
+
return receive(retryFamilyReceive(storage, logger, undefined, undefined));
|
|
37
|
+
const member = await resolveRetryFamilyMember(storage, envelope.clientMessageId ?? envelopeId);
|
|
38
|
+
return withProcessedEnvelopeLock(storage, member?.failedEnvelopeId ?? envelopeId, () => receive(retryFamilyReceive(storage, logger, envelopeId, member)));
|
|
39
|
+
}
|
|
40
|
+
function retryFamilyReceive(storage, logger, envelopeId, member) {
|
|
41
|
+
const failedEnvelopeId = member?.failedEnvelopeId ?? envelopeId;
|
|
42
|
+
// The read comes before the decrypt, so only writes follow the
|
|
43
|
+
// application hook.
|
|
44
|
+
let request = null;
|
|
45
|
+
return {
|
|
46
|
+
member,
|
|
47
|
+
async admit() {
|
|
48
|
+
if (failedEnvelopeId === undefined)
|
|
49
|
+
return true;
|
|
50
|
+
// A member that posted a request is processed, so its redelivery asks
|
|
51
|
+
// for nothing again.
|
|
52
|
+
if (envelopeId && (await getProcessedEnvelope(storage, envelopeId))) {
|
|
53
|
+
logger.debug('Acknowledging previously processed relay envelope', {
|
|
54
|
+
category: 'E2EE',
|
|
55
|
+
data: { envelopeId, behavior: 'PERSISTENT_DUPLICATE_DISCARD' },
|
|
56
|
+
});
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
// The Relay frees a message ID after the acknowledgment, so a sender
|
|
60
|
+
// can post a second resend attempt for one failed envelope, and the
|
|
61
|
+
// failed envelope can arrive again after its resend. Process one.
|
|
62
|
+
if (member && (await getProcessedEnvelope(storage, failedEnvelopeId))?.fulfilled) {
|
|
63
|
+
logger.debug('Acknowledging a second envelope for one failed envelope', {
|
|
64
|
+
category: 'E2EE',
|
|
65
|
+
data: { envelopeId, behavior: 'RETRY_RESEND_DUPLICATE_DISCARD' },
|
|
66
|
+
});
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
// A member of attempt n answers attempt n, so a member failure then
|
|
70
|
+
// posts the next attempt. A member of an earlier attempt answers
|
|
71
|
+
// nothing, so its failure posts the stored attempt again.
|
|
72
|
+
if (member) {
|
|
73
|
+
await markRetryRequestAnswered(storage, failedEnvelopeId, member.retryId, member.attempt);
|
|
74
|
+
}
|
|
75
|
+
request = await getRetryRequest(storage, failedEnvelopeId);
|
|
76
|
+
return true;
|
|
77
|
+
},
|
|
78
|
+
async fulfill() {
|
|
79
|
+
try {
|
|
80
|
+
// One write marks the failed envelope processed and fulfilled. A
|
|
81
|
+
// redelivery of it is then acknowledged with no decrypt, and each
|
|
82
|
+
// later member of its request is dropped.
|
|
83
|
+
if (failedEnvelopeId !== undefined && (member || request)) {
|
|
84
|
+
await storeProcessedEnvelope(storage, failedEnvelopeId, Date.now(), undefined, true);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// The application gets the plaintext. Only a redelivery can
|
|
89
|
+
// decrypt the content again.
|
|
90
|
+
logger.warn('The retry family record of a decrypted envelope failed to store', {
|
|
91
|
+
category: 'E2EE',
|
|
92
|
+
data: {
|
|
93
|
+
envelopeId,
|
|
94
|
+
behavior: 'RETRY_FULFILL_WRITE_FAILED',
|
|
95
|
+
error: error.message,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
async consumeNull(fingerprint) {
|
|
101
|
+
try {
|
|
102
|
+
if (member)
|
|
103
|
+
await storeProcessedEnvelope(storage, member.failedEnvelopeId);
|
|
104
|
+
if (envelopeId) {
|
|
105
|
+
await storeProcessedEnvelope(storage, envelopeId, Date.now(), fingerprint);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
// The null message carries no content, so the caller still
|
|
110
|
+
// consumes it. A redelivery of the failed envelope can then
|
|
111
|
+
// decrypt the content.
|
|
112
|
+
logger.warn('The retry family record of a null message failed to store', {
|
|
113
|
+
category: 'E2EE',
|
|
114
|
+
data: {
|
|
115
|
+
envelopeId,
|
|
116
|
+
behavior: 'RETRY_NULL_WRITE_FAILED',
|
|
117
|
+
error: error.message,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
withRequestedSender(envelope) {
|
|
123
|
+
if (!member || !request)
|
|
124
|
+
return envelope;
|
|
125
|
+
if (envelope.senderUserId && envelope.senderDeviceId >= 1)
|
|
126
|
+
return envelope;
|
|
127
|
+
const { targetUserId, targetDeviceId } = request.envelope;
|
|
128
|
+
return { ...envelope, senderUserId: targetUserId, senderDeviceId: targetDeviceId };
|
|
129
|
+
},
|
|
130
|
+
async requestRetry(fingerprint, post) {
|
|
131
|
+
if (failedEnvelopeId === undefined)
|
|
132
|
+
return post({ stored: null });
|
|
133
|
+
if (!member) {
|
|
134
|
+
let stored;
|
|
135
|
+
try {
|
|
136
|
+
stored = await getRetryRequest(storage, failedEnvelopeId, Date.now(), fingerprint);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
if (!(error instanceof RetryRequestedEnvelopeChangedError))
|
|
140
|
+
throw error;
|
|
141
|
+
// The envelope stays unacknowledged, as for a failed retry request.
|
|
142
|
+
logger.warn('No retry request: the relay changed an envelope after its retry request', {
|
|
143
|
+
category: 'E2EE',
|
|
144
|
+
data: { envelopeId, behavior: 'RETRY_REQUEST_ENVELOPE_CHANGED' },
|
|
145
|
+
});
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return post({
|
|
149
|
+
id: failedEnvelopeId,
|
|
150
|
+
...(fingerprint !== undefined && { fingerprint }),
|
|
151
|
+
stored,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// A member has its own content, so its fingerprint is not the
|
|
155
|
+
// fingerprint of the failed envelope.
|
|
156
|
+
const stored = await getRetryRequest(storage, failedEnvelopeId);
|
|
157
|
+
const posted = await post({
|
|
158
|
+
id: failedEnvelopeId,
|
|
159
|
+
...(stored?.fingerprint !== undefined && { fingerprint: stored.fingerprint }),
|
|
160
|
+
stored,
|
|
161
|
+
...(envelopeId !== undefined && { member: envelopeId }),
|
|
162
|
+
});
|
|
163
|
+
if (posted && envelopeId) {
|
|
164
|
+
await storeProcessedEnvelope(storage, envelopeId, Date.now(), fingerprint);
|
|
165
|
+
}
|
|
166
|
+
return posted;
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=retry-family.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
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AAO,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,WAAW,CAAC,CAAC;YAC7E,CAAC;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire form of a retry request.
|
|
3
|
+
*
|
|
4
|
+
* A retry request travels as an identified mailbox envelope with
|
|
5
|
+
* `messageType: 'retry_request'`. The envelope sender is the requester: the
|
|
6
|
+
* relay stamps it, so the payload never carries it. The `ciphertext` field
|
|
7
|
+
* holds canonical base64 of a small UTF-8 JSON payload. The payload is not
|
|
8
|
+
* encrypted. It holds only what the requester knows about the failed message.
|
|
9
|
+
*
|
|
10
|
+
* The requester keeps one request record for each failed envelope. The record
|
|
11
|
+
* holds a random retry ID, and every wire ID of the request family derives from
|
|
12
|
+
* it: `<retryId>:retry-request`, `<retryId>:resend` and `<retryId>:null`, with
|
|
13
|
+
* `:<n>` from attempt 2. The resend and the null message carry the attempt
|
|
14
|
+
* number of the request that they answer. The failed envelope ID travels
|
|
15
|
+
* only in the payload.
|
|
16
|
+
* The requester stores each request envelope before the post, and posts the
|
|
17
|
+
* stored envelope again on a later decrypt failure of the same envelope.
|
|
18
|
+
*/
|
|
19
|
+
import type { Envelope } from '../remote/relay/types.js';
|
|
20
|
+
import { type RetryRequest } from '../internal/sesame/types.js';
|
|
21
|
+
import { storeRetryRequest, type RetryRequestIds, type StoredRetryRequest } from '../local/store/reliability.js';
|
|
22
|
+
/** The decoder refuses a longer `ciphertext` value before it decodes it. */
|
|
23
|
+
export declare const MAX_RETRY_REQUEST_CIPHERTEXT_LENGTH = 2048;
|
|
24
|
+
/** A user and one of its devices. */
|
|
25
|
+
export interface RetryRequestDevice {
|
|
26
|
+
userId: string;
|
|
27
|
+
deviceId: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The result of `readRetryRequestEnvelope`. `retry` holds the ID of the
|
|
31
|
+
* envelope that the requester could not decrypt and the retry ID of its
|
|
32
|
+
* request record, when the requester knew the failed envelope. The resend
|
|
33
|
+
* IDs of the request derive from the retry ID.
|
|
34
|
+
*/
|
|
35
|
+
export type RetryRequestEnvelopeRead = {
|
|
36
|
+
ok: true;
|
|
37
|
+
request: RetryRequest;
|
|
38
|
+
retry?: RetryRequestIds;
|
|
39
|
+
} | {
|
|
40
|
+
ok: false;
|
|
41
|
+
cause: string;
|
|
42
|
+
};
|
|
43
|
+
/** The failed envelope that a retry request names, and its stored request. */
|
|
44
|
+
export interface RetryRequestFailure {
|
|
45
|
+
/** The Relay message ID of the failed envelope. Without it, nothing is stored. */
|
|
46
|
+
id?: string;
|
|
47
|
+
/** The fingerprint of the failed envelope. */
|
|
48
|
+
fingerprint?: string;
|
|
49
|
+
/** The stored attempt of the request for this envelope, from `getRetryRequest`. */
|
|
50
|
+
stored: StoredRetryRequest | null;
|
|
51
|
+
/**
|
|
52
|
+
* The envelope ID of the resend or the null message that failed to
|
|
53
|
+
* decrypt. Undefined when the failed envelope itself failed.
|
|
54
|
+
*/
|
|
55
|
+
member?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Make the envelope that asks the original sender device for a resend.
|
|
59
|
+
*
|
|
60
|
+
* @param request - The retry request. Its original sender is the target.
|
|
61
|
+
* @param sender - The local identity, which sends the envelope.
|
|
62
|
+
* @param failed - The ID of the failed envelope, the retry ID of its request
|
|
63
|
+
* record and the attempt number. The client message ID derives from the
|
|
64
|
+
* retry ID and the attempt. Without them, it is a new UUID.
|
|
65
|
+
*/
|
|
66
|
+
export declare function retryRequestEnvelope(request: RetryRequest, sender: RetryRequestDevice, failed?: RetryRequestIds & {
|
|
67
|
+
attempt: number;
|
|
68
|
+
}): Promise<Envelope>;
|
|
69
|
+
/**
|
|
70
|
+
* Post the retry request for one failed envelope.
|
|
71
|
+
*
|
|
72
|
+
* Without a failed envelope ID, it creates a request and posts it under a
|
|
73
|
+
* new UUID, and stores nothing. With a stored attempt, it posts the stored
|
|
74
|
+
* envelope again. Otherwise it creates attempt 1 with a new retry ID and
|
|
75
|
+
* stores it before the post. Each later attempt keeps the retry ID. When
|
|
76
|
+
* the Relay refuses a stored attempt with OPERATION_EXPIRED, or when a
|
|
77
|
+
* member failed and the stored attempt is answered, it creates the next
|
|
78
|
+
* attempt, replaces the record in one write, and posts it. Each new
|
|
79
|
+
* attempt is not answered. A RETRY_CONFLICT answer to any post completes
|
|
80
|
+
* the attempt, because the Relay holds an earlier encryption of this
|
|
81
|
+
* attempt.
|
|
82
|
+
*
|
|
83
|
+
* @param store - The local store that keeps the request records.
|
|
84
|
+
* @param failed - The failed envelope and its stored request.
|
|
85
|
+
* @param createRequest - Creates the request of a new attempt. Only a new
|
|
86
|
+
* attempt calls it.
|
|
87
|
+
* @param sender - The local identity, which sends the envelope.
|
|
88
|
+
* @param send - Posts one request envelope.
|
|
89
|
+
*/
|
|
90
|
+
export declare function postRetryRequest(store: Parameters<typeof storeRetryRequest>[0], failed: RetryRequestFailure, createRequest: () => Promise<RetryRequest>, sender: RetryRequestDevice, send: (envelope: Envelope) => Promise<unknown>): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Read a `retry_request` envelope that the local device received.
|
|
93
|
+
*
|
|
94
|
+
* The requester is the envelope sender. The original sender is the local
|
|
95
|
+
* identity. The other fields come from the payload. A `Uint8Array`
|
|
96
|
+
* ciphertext is read as its base64 form. The function ignores
|
|
97
|
+
* every payload key that it does not read, a requester key included. It does
|
|
98
|
+
* not check the envelope sender: the caller does that first.
|
|
99
|
+
*
|
|
100
|
+
* @param envelope - The received envelope.
|
|
101
|
+
* @param local - The local identity.
|
|
102
|
+
* @returns The retry request, or the cause of the refusal. The cause never
|
|
103
|
+
* holds payload bytes.
|
|
104
|
+
*/
|
|
105
|
+
export declare function readRetryRequestEnvelope(envelope: Envelope, local: RetryRequestDevice): RetryRequestEnvelopeRead;
|
|
106
|
+
//# sourceMappingURL=retry-request-envelope.d.ts.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;;;;;;;;;;GAoBG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC9C,MAAM,EAAE,mBAAmB,EAC3B,aAAa,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,EAC1C,MAAM,EAAE,kBAAkB,EAC1B,IAAI,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,OAAO,CAAC,OAAO,CAAC,GAC7C,OAAO,CAAC,IAAI,CAAC,CAgDf;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,kBAAkB,GACxB,wBAAwB,CAyF1B"}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire form of a retry request.
|
|
3
|
+
*
|
|
4
|
+
* A retry request travels as an identified mailbox envelope with
|
|
5
|
+
* `messageType: 'retry_request'`. The envelope sender is the requester: the
|
|
6
|
+
* relay stamps it, so the payload never carries it. The `ciphertext` field
|
|
7
|
+
* holds canonical base64 of a small UTF-8 JSON payload. The payload is not
|
|
8
|
+
* encrypted. It holds only what the requester knows about the failed message.
|
|
9
|
+
*
|
|
10
|
+
* The requester keeps one request record for each failed envelope. The record
|
|
11
|
+
* holds a random retry ID, and every wire ID of the request family derives from
|
|
12
|
+
* it: `<retryId>:retry-request`, `<retryId>:resend` and `<retryId>:null`, with
|
|
13
|
+
* `:<n>` from attempt 2. The resend and the null message carry the attempt
|
|
14
|
+
* number of the request that they answer. The failed envelope ID travels
|
|
15
|
+
* only in the payload.
|
|
16
|
+
* The requester stores each request envelope before the post, and posts the
|
|
17
|
+
* stored envelope again on a later decrypt failure of the same envelope.
|
|
18
|
+
*/
|
|
19
|
+
import { RetryReason } from '../internal/sesame/types.js';
|
|
20
|
+
import { base64ToBytes, bytesToBase64, generateUuidV4, stringToBytes } from '../internal/crypto/index.js';
|
|
21
|
+
import { utf8Decode } from '../internal/platform/index.js';
|
|
22
|
+
import { isRetryId, retryAttemptClientMessageId, retryRequestClientMessageId, storeRetryRequest, } from '../local/store/reliability.js';
|
|
23
|
+
import { relayOperationRefusal } from './relay-acceptance.js';
|
|
24
|
+
/** The payload version that this client writes and reads. */
|
|
25
|
+
const RETRY_REQUEST_PAYLOAD_VERSION = 1;
|
|
26
|
+
/** The decoder refuses a longer `ciphertext` value before it decodes it. */
|
|
27
|
+
export const MAX_RETRY_REQUEST_CIPHERTEXT_LENGTH = 2_048;
|
|
28
|
+
/** The Relay refuses a longer message ID, so a failed envelope ID is 1 to this many characters. */
|
|
29
|
+
const MAX_FAILED_ENVELOPE_ID_LENGTH = 128;
|
|
30
|
+
/** The decoded ratchet key is 1 to this many bytes. */
|
|
31
|
+
const MAX_RATCHET_KEY_BYTES = 64;
|
|
32
|
+
const RETRY_REASONS = new Set(Object.values(RetryReason));
|
|
33
|
+
/**
|
|
34
|
+
* Make the envelope that asks the original sender device for a resend.
|
|
35
|
+
*
|
|
36
|
+
* @param request - The retry request. Its original sender is the target.
|
|
37
|
+
* @param sender - The local identity, which sends the envelope.
|
|
38
|
+
* @param failed - The ID of the failed envelope, the retry ID of its request
|
|
39
|
+
* record and the attempt number. The client message ID derives from the
|
|
40
|
+
* retry ID and the attempt. Without them, it is a new UUID.
|
|
41
|
+
*/
|
|
42
|
+
export async function retryRequestEnvelope(request, sender, failed) {
|
|
43
|
+
const payload = {
|
|
44
|
+
v: RETRY_REQUEST_PAYLOAD_VERSION,
|
|
45
|
+
failedTimestamp: request.failedTimestamp,
|
|
46
|
+
deviceId: request.originalSenderDeviceId,
|
|
47
|
+
reason: request.reason,
|
|
48
|
+
timestamp: request.timestamp,
|
|
49
|
+
...(request.ratchetKey === undefined ? {} : { ratchetKey: request.ratchetKey }),
|
|
50
|
+
...(failed && { failedEnvelopeId: failed.failedEnvelopeId, retryId: failed.retryId }),
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
targetUserId: request.originalSenderUserId,
|
|
54
|
+
targetDeviceId: request.originalSenderDeviceId,
|
|
55
|
+
senderUserId: sender.userId,
|
|
56
|
+
senderDeviceId: sender.deviceId,
|
|
57
|
+
ciphertext: bytesToBase64(stringToBytes(JSON.stringify(payload))),
|
|
58
|
+
messageType: 'retry_request',
|
|
59
|
+
deliveryClass: 'background-sync',
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
clientMessageId: failed
|
|
62
|
+
? retryAttemptClientMessageId(retryRequestClientMessageId(failed.retryId), failed.attempt)
|
|
63
|
+
: await generateUuidV4(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Post the retry request for one failed envelope.
|
|
68
|
+
*
|
|
69
|
+
* Without a failed envelope ID, it creates a request and posts it under a
|
|
70
|
+
* new UUID, and stores nothing. With a stored attempt, it posts the stored
|
|
71
|
+
* envelope again. Otherwise it creates attempt 1 with a new retry ID and
|
|
72
|
+
* stores it before the post. Each later attempt keeps the retry ID. When
|
|
73
|
+
* the Relay refuses a stored attempt with OPERATION_EXPIRED, or when a
|
|
74
|
+
* member failed and the stored attempt is answered, it creates the next
|
|
75
|
+
* attempt, replaces the record in one write, and posts it. Each new
|
|
76
|
+
* attempt is not answered. A RETRY_CONFLICT answer to any post completes
|
|
77
|
+
* the attempt, because the Relay holds an earlier encryption of this
|
|
78
|
+
* attempt.
|
|
79
|
+
*
|
|
80
|
+
* @param store - The local store that keeps the request records.
|
|
81
|
+
* @param failed - The failed envelope and its stored request.
|
|
82
|
+
* @param createRequest - Creates the request of a new attempt. Only a new
|
|
83
|
+
* attempt calls it.
|
|
84
|
+
* @param sender - The local identity, which sends the envelope.
|
|
85
|
+
* @param send - Posts one request envelope.
|
|
86
|
+
*/
|
|
87
|
+
export async function postRetryRequest(store, failed, createRequest, sender, send) {
|
|
88
|
+
const failedEnvelopeId = failed.id;
|
|
89
|
+
if (failedEnvelopeId === undefined) {
|
|
90
|
+
await send(await retryRequestEnvelope(await createRequest(), sender));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const post = async (envelope) => {
|
|
94
|
+
try {
|
|
95
|
+
await send(envelope);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (relayOperationRefusal(error) !== 'RETRY_CONFLICT')
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const create = async (attempt, retryId) => {
|
|
103
|
+
const envelope = await retryRequestEnvelope(await createRequest(), sender, {
|
|
104
|
+
failedEnvelopeId,
|
|
105
|
+
retryId,
|
|
106
|
+
attempt,
|
|
107
|
+
});
|
|
108
|
+
await storeRetryRequest(store, {
|
|
109
|
+
failedEnvelopeId,
|
|
110
|
+
retryId,
|
|
111
|
+
attempt,
|
|
112
|
+
requestedAt: envelope.timestamp,
|
|
113
|
+
...(failed.fingerprint !== undefined && { fingerprint: failed.fingerprint }),
|
|
114
|
+
answered: false,
|
|
115
|
+
envelope,
|
|
116
|
+
});
|
|
117
|
+
return envelope;
|
|
118
|
+
};
|
|
119
|
+
if (!failed.stored) {
|
|
120
|
+
await post(await create(1, await generateUuidV4()));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const { attempt, retryId } = failed.stored;
|
|
124
|
+
if (failed.member !== undefined && failed.stored.answered) {
|
|
125
|
+
await post(await create(attempt + 1, retryId));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
await post(failed.stored.envelope);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (relayOperationRefusal(error) !== 'OPERATION_EXPIRED')
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
// The Relay no longer admits or replays the stored operation. The new
|
|
137
|
+
// attempt replaces the record before its post.
|
|
138
|
+
await post(await create(attempt + 1, retryId));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Read a `retry_request` envelope that the local device received.
|
|
142
|
+
*
|
|
143
|
+
* The requester is the envelope sender. The original sender is the local
|
|
144
|
+
* identity. The other fields come from the payload. A `Uint8Array`
|
|
145
|
+
* ciphertext is read as its base64 form. The function ignores
|
|
146
|
+
* every payload key that it does not read, a requester key included. It does
|
|
147
|
+
* not check the envelope sender: the caller does that first.
|
|
148
|
+
*
|
|
149
|
+
* @param envelope - The received envelope.
|
|
150
|
+
* @param local - The local identity.
|
|
151
|
+
* @returns The retry request, or the cause of the refusal. The cause never
|
|
152
|
+
* holds payload bytes.
|
|
153
|
+
*/
|
|
154
|
+
export function readRetryRequestEnvelope(envelope, local) {
|
|
155
|
+
// A relay can deliver raw bytes. The checks apply to their base64 form.
|
|
156
|
+
const ciphertext = typeof envelope.ciphertext === 'string'
|
|
157
|
+
? envelope.ciphertext
|
|
158
|
+
: bytesToBase64(envelope.ciphertext);
|
|
159
|
+
if (ciphertext.length > MAX_RETRY_REQUEST_CIPHERTEXT_LENGTH) {
|
|
160
|
+
return { ok: false, cause: 'payload is too long' };
|
|
161
|
+
}
|
|
162
|
+
const bytes = canonicalBase64Bytes(ciphertext);
|
|
163
|
+
if (bytes === undefined) {
|
|
164
|
+
return { ok: false, cause: 'payload is not canonical base64' };
|
|
165
|
+
}
|
|
166
|
+
let value;
|
|
167
|
+
try {
|
|
168
|
+
value = JSON.parse(utf8Decode(bytes, { fatal: true }));
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return { ok: false, cause: 'payload is not JSON' };
|
|
172
|
+
}
|
|
173
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
174
|
+
return { ok: false, cause: 'payload is not a JSON object' };
|
|
175
|
+
}
|
|
176
|
+
const payload = value;
|
|
177
|
+
if (payload.v !== RETRY_REQUEST_PAYLOAD_VERSION) {
|
|
178
|
+
return { ok: false, cause: 'payload version is not supported' };
|
|
179
|
+
}
|
|
180
|
+
const { failedTimestamp, deviceId, reason, timestamp, ratchetKey, failedEnvelopeId, retryId } = payload;
|
|
181
|
+
if (!Number.isSafeInteger(failedTimestamp) || failedTimestamp <= 0) {
|
|
182
|
+
return { ok: false, cause: 'failedTimestamp is not valid' };
|
|
183
|
+
}
|
|
184
|
+
if (!Number.isSafeInteger(deviceId) || deviceId < 1) {
|
|
185
|
+
return { ok: false, cause: 'deviceId is not valid' };
|
|
186
|
+
}
|
|
187
|
+
if (typeof reason !== 'string' || !RETRY_REASONS.has(reason)) {
|
|
188
|
+
return { ok: false, cause: 'reason is not known' };
|
|
189
|
+
}
|
|
190
|
+
if (!Number.isSafeInteger(timestamp)) {
|
|
191
|
+
return { ok: false, cause: 'timestamp is not valid' };
|
|
192
|
+
}
|
|
193
|
+
if (ratchetKey !== undefined) {
|
|
194
|
+
const ratchetKeyBytes = typeof ratchetKey === 'string' ? canonicalBase64Bytes(ratchetKey) : undefined;
|
|
195
|
+
if (ratchetKeyBytes === undefined ||
|
|
196
|
+
ratchetKeyBytes.length < 1 ||
|
|
197
|
+
ratchetKeyBytes.length > MAX_RATCHET_KEY_BYTES) {
|
|
198
|
+
return { ok: false, cause: 'ratchetKey is not valid' };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (failedEnvelopeId !== undefined &&
|
|
202
|
+
(typeof failedEnvelopeId !== 'string' ||
|
|
203
|
+
failedEnvelopeId.length < 1 ||
|
|
204
|
+
failedEnvelopeId.length > MAX_FAILED_ENVELOPE_ID_LENGTH)) {
|
|
205
|
+
return { ok: false, cause: 'failedEnvelopeId is not valid' };
|
|
206
|
+
}
|
|
207
|
+
if (retryId !== undefined && !isRetryId(retryId)) {
|
|
208
|
+
return { ok: false, cause: 'retryId is not valid' };
|
|
209
|
+
}
|
|
210
|
+
// The sender derives the resend IDs from the retry ID and keys its record by
|
|
211
|
+
// the failed envelope ID, so a payload carries both or neither.
|
|
212
|
+
if ((failedEnvelopeId === undefined) !== (retryId === undefined)) {
|
|
213
|
+
return { ok: false, cause: 'failedEnvelopeId and retryId are not both present' };
|
|
214
|
+
}
|
|
215
|
+
if (deviceId !== local.deviceId) {
|
|
216
|
+
return { ok: false, cause: 'deviceId is not the local device' };
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
ok: true,
|
|
220
|
+
request: {
|
|
221
|
+
requesterUserId: envelope.senderUserId,
|
|
222
|
+
requesterDeviceId: envelope.senderDeviceId,
|
|
223
|
+
originalSenderUserId: local.userId,
|
|
224
|
+
originalSenderDeviceId: local.deviceId,
|
|
225
|
+
failedTimestamp: failedTimestamp,
|
|
226
|
+
timestamp: timestamp,
|
|
227
|
+
reason: reason,
|
|
228
|
+
...(ratchetKey === undefined ? {} : { ratchetKey: ratchetKey }),
|
|
229
|
+
},
|
|
230
|
+
...(retryId === undefined
|
|
231
|
+
? {}
|
|
232
|
+
: { retry: { failedEnvelopeId: failedEnvelopeId, retryId: retryId } }),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/** Decode canonical base64, or return undefined. */
|
|
236
|
+
function canonicalBase64Bytes(value) {
|
|
237
|
+
try {
|
|
238
|
+
const bytes = base64ToBytes(value);
|
|
239
|
+
return bytesToBase64(bytes) === value ? bytes : undefined;
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=retry-request-envelope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-request-envelope.js","sourceRoot":"","sources":["../../client/retry-request-envelope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAE,WAAW,EAAqB,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACjG,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EACL,SAAS,EACT,2BAA2B,EAC3B,2BAA2B,EAC3B,iBAAiB,GAGlB,MAAM,4BA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