2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable no-bitwise */
|
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
/* eslint-disable camelcase */
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
import { isNumber, map } from 'lodash';
|
2020-04-13 17:37:29 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
import { v4 as getGuid } from 'uuid';
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
import {
|
2021-05-28 19:11:19 +00:00
|
|
|
DecryptionErrorMessage,
|
2021-05-14 01:18:43 +00:00
|
|
|
groupDecrypt,
|
2021-05-28 19:11:19 +00:00
|
|
|
PlaintextContent,
|
2021-04-16 23:13:13 +00:00
|
|
|
PreKeySignalMessage,
|
2021-05-14 01:18:43 +00:00
|
|
|
processSenderKeyDistributionMessage,
|
2021-04-16 23:13:13 +00:00
|
|
|
ProtocolAddress,
|
|
|
|
PublicKey,
|
|
|
|
SealedSenderDecryptionResult,
|
|
|
|
sealedSenderDecryptMessage,
|
|
|
|
sealedSenderDecryptToUsmc,
|
2021-07-27 19:55:39 +00:00
|
|
|
SenderCertificate,
|
2021-05-14 01:18:43 +00:00
|
|
|
SenderKeyDistributionMessage,
|
2021-04-16 23:13:13 +00:00
|
|
|
signalDecrypt,
|
|
|
|
signalDecryptPreKey,
|
|
|
|
SignalMessage,
|
2021-05-14 01:18:43 +00:00
|
|
|
UnidentifiedSenderMessageContent,
|
|
|
|
} from '@signalapp/signal-client';
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
IdentityKeys,
|
|
|
|
PreKeys,
|
2021-05-14 01:18:43 +00:00
|
|
|
SenderKeys,
|
2021-04-16 23:13:13 +00:00
|
|
|
Sessions,
|
|
|
|
SignedPreKeys,
|
|
|
|
} from '../LibSignalStores';
|
2021-07-15 19:13:48 +00:00
|
|
|
import { verifySignature } from '../Curve';
|
2021-07-27 19:55:39 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2020-09-24 21:53:21 +00:00
|
|
|
import { BatcherType, createBatcher } from '../util/batcher';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { dropNull } from '../util/dropNull';
|
|
|
|
import { normalizeUuid } from '../util/normalizeUuid';
|
|
|
|
import { normalizeNumber } from '../util/normalizeNumber';
|
2021-04-16 23:13:13 +00:00
|
|
|
import { parseIntOrThrow } from '../util/parseIntOrThrow';
|
2021-05-19 21:25:56 +00:00
|
|
|
import { Zone } from '../util/Zone';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { deriveMasterKeyFromGroupV1 } from '../Crypto';
|
2021-07-14 23:39:52 +00:00
|
|
|
import { DownloadedAttachmentType } from '../types/Attachment';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { Address } from '../types/Address';
|
|
|
|
import { QualifiedAddress } from '../types/QualifiedAddress';
|
|
|
|
import { UUID } from '../types/UUID';
|
2021-07-27 19:55:39 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2021-07-14 23:39:52 +00:00
|
|
|
import { UnprocessedType } from '../textsecure.d';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { deriveGroupFields, MASTER_KEY_LENGTH } from '../groups';
|
|
|
|
|
|
|
|
import createTaskWithTimeout from './TaskWithTimeout';
|
|
|
|
import { processAttachment, processDataMessage } from './processDataMessage';
|
|
|
|
import { processSyncMessage } from './processSyncMessage';
|
|
|
|
import EventTarget, { EventHandler } from './EventTarget';
|
|
|
|
import { downloadAttachment } from './downloadAttachment';
|
|
|
|
import { IncomingWebSocketRequest } from './WebsocketResources';
|
|
|
|
import { ContactBuffer, GroupBuffer } from './ContactsParser';
|
|
|
|
import type { WebAPIType } from './WebAPI';
|
|
|
|
import type { Storage } from './Storage';
|
2021-09-22 00:58:03 +00:00
|
|
|
import { WarnOnlyError } from './Errors';
|
2021-07-28 21:37:09 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2021-07-09 19:36:10 +00:00
|
|
|
import {
|
|
|
|
ProcessedDataMessage,
|
|
|
|
ProcessedSyncMessage,
|
|
|
|
ProcessedSent,
|
|
|
|
ProcessedEnvelope,
|
2021-07-28 21:37:09 +00:00
|
|
|
IRequestHandler,
|
2021-07-09 19:36:10 +00:00
|
|
|
} from './Types.d';
|
2020-04-13 17:37:29 +00:00
|
|
|
import {
|
2021-07-09 19:36:10 +00:00
|
|
|
ReconnectEvent,
|
|
|
|
EmptyEvent,
|
|
|
|
ProgressEvent,
|
|
|
|
TypingEvent,
|
|
|
|
ErrorEvent,
|
|
|
|
DeliveryEvent,
|
|
|
|
DecryptionErrorEvent,
|
|
|
|
SentEvent,
|
|
|
|
ProfileKeyUpdateEvent,
|
|
|
|
MessageEvent,
|
|
|
|
RetryRequestEvent,
|
|
|
|
ReadEvent,
|
2021-07-27 15:42:25 +00:00
|
|
|
ViewEvent,
|
2021-07-09 19:36:10 +00:00
|
|
|
ConfigurationEvent,
|
2021-07-22 17:07:53 +00:00
|
|
|
ViewOnceOpenSyncEvent,
|
2021-07-09 19:36:10 +00:00
|
|
|
MessageRequestResponseEvent,
|
|
|
|
FetchLatestEvent,
|
|
|
|
KeysEvent,
|
|
|
|
StickerPackEvent,
|
|
|
|
VerifiedEvent,
|
|
|
|
ReadSyncEvent,
|
2021-08-12 18:15:55 +00:00
|
|
|
ViewSyncEvent,
|
2021-07-09 19:36:10 +00:00
|
|
|
ContactEvent,
|
|
|
|
ContactSyncEvent,
|
|
|
|
GroupEvent,
|
|
|
|
GroupSyncEvent,
|
2021-08-05 23:34:49 +00:00
|
|
|
EnvelopeEvent,
|
2021-07-09 19:36:10 +00:00
|
|
|
} from './messageReceiverEvents';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
const GROUPV1_ID_LENGTH = 16;
|
|
|
|
const GROUPV2_ID_LENGTH = 32;
|
2019-08-16 16:15:39 +00:00
|
|
|
const RETRY_TIMEOUT = 2 * 60 * 1000;
|
2018-08-16 17:45:09 +00:00
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
type UnsealedEnvelope = Readonly<
|
2021-07-09 19:36:10 +00:00
|
|
|
ProcessedEnvelope & {
|
|
|
|
unidentifiedDeliveryReceived?: boolean;
|
|
|
|
contentHint?: number;
|
2020-05-27 21:37:06 +00:00
|
|
|
groupId?: string;
|
2021-07-09 19:36:10 +00:00
|
|
|
usmc?: UnidentifiedSenderMessageContent;
|
2021-07-27 19:55:39 +00:00
|
|
|
certificate?: SenderCertificate;
|
|
|
|
unsealedContent?: UnidentifiedSenderMessageContent;
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
type DecryptResult = Readonly<{
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope;
|
2021-07-09 19:36:10 +00:00
|
|
|
plaintext?: Uint8Array;
|
|
|
|
}>;
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
type DecryptSealedSenderResult = Readonly<{
|
|
|
|
plaintext?: Uint8Array;
|
|
|
|
unsealedPlaintext?: SealedSenderDecryptionResult;
|
|
|
|
}>;
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
type CacheAddItemType = {
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope;
|
2020-04-13 17:37:29 +00:00
|
|
|
data: UnprocessedType;
|
2021-05-24 21:30:56 +00:00
|
|
|
request: Pick<IncomingWebSocketRequest, 'respond'>;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2021-05-18 00:41:28 +00:00
|
|
|
type LockedStores = {
|
|
|
|
readonly sessionStore: Sessions;
|
|
|
|
readonly identityKeyStore: IdentityKeys;
|
2021-05-20 23:49:08 +00:00
|
|
|
readonly zone?: Zone;
|
2021-05-18 00:41:28 +00:00
|
|
|
};
|
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
enum TaskType {
|
|
|
|
Encrypted = 'Encrypted',
|
|
|
|
Decrypted = 'Decrypted',
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
export type MessageReceiverOptions = {
|
|
|
|
server: WebAPIType;
|
|
|
|
storage: Storage;
|
|
|
|
serverTrustRoot: string;
|
|
|
|
};
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
export default class MessageReceiver
|
|
|
|
extends EventTarget
|
|
|
|
implements IRequestHandler {
|
|
|
|
private server: WebAPIType;
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
private storage: Storage;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private appQueue: PQueue;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private decryptAndCacheBatcher: BatcherType<CacheAddItemType>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private cacheRemoveBatcher: BatcherType<string>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private count: number;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private processedCount: number;
|
2021-03-09 19:11:52 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private incomingQueue: PQueue;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private isEmptied?: boolean;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private encryptedQueue: PQueue;
|
2021-05-21 00:24:53 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private decryptedQueue: PQueue;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private retryCachedTimeout: NodeJS.Timeout | undefined;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private serverTrustRoot: Uint8Array;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private stoppingProcessing?: boolean;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
constructor({ server, storage, serverTrustRoot }: MessageReceiverOptions) {
|
2020-04-13 17:37:29 +00:00
|
|
|
super();
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
this.server = server;
|
|
|
|
this.storage = storage;
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
this.count = 0;
|
2021-03-09 19:11:52 +00:00
|
|
|
this.processedCount = 0;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (!serverTrustRoot) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new Error('Server trust root is required!');
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
this.serverTrustRoot = Bytes.fromBase64(serverTrustRoot);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-09-18 20:40:41 +00:00
|
|
|
this.incomingQueue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
|
|
|
this.appQueue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
// All envelopes start in encryptedQueue and progress to decryptedQueue
|
|
|
|
this.encryptedQueue = new PQueue({
|
|
|
|
concurrency: 1,
|
|
|
|
timeout: 1000 * 60 * 2,
|
|
|
|
});
|
|
|
|
this.decryptedQueue = new PQueue({
|
|
|
|
concurrency: 1,
|
|
|
|
timeout: 1000 * 60 * 2,
|
|
|
|
});
|
|
|
|
|
2021-05-24 21:30:56 +00:00
|
|
|
this.decryptAndCacheBatcher = createBatcher<CacheAddItemType>({
|
|
|
|
name: 'MessageReceiver.decryptAndCacheBatcher',
|
2021-03-30 16:56:03 +00:00
|
|
|
wait: 75,
|
2020-04-13 17:37:29 +00:00
|
|
|
maxSize: 30,
|
2021-03-26 00:00:03 +00:00
|
|
|
processBatch: (items: Array<CacheAddItemType>) => {
|
|
|
|
// Not returning the promise here because we don't want to stall
|
|
|
|
// the batch.
|
2021-05-24 21:30:56 +00:00
|
|
|
this.decryptAndCacheBatch(items);
|
2021-03-26 00:00:03 +00:00
|
|
|
},
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
this.cacheRemoveBatcher = createBatcher<string>({
|
2021-03-26 00:00:03 +00:00
|
|
|
name: 'MessageReceiver.cacheRemoveBatcher',
|
2021-03-30 16:56:03 +00:00
|
|
|
wait: 75,
|
2020-04-13 17:37:29 +00:00
|
|
|
maxSize: 30,
|
|
|
|
processBatch: this.cacheRemoveBatch.bind(this),
|
|
|
|
});
|
2021-07-23 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getProcessedCount(): number {
|
|
|
|
return this.processedCount;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
public handleRequest(request: IncomingWebSocketRequest): void {
|
|
|
|
// We do the message decryption here, instead of in the ordered pending queue,
|
|
|
|
// to avoid exposing the time it took us to process messages through the time-to-ack.
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver: got request', request.verb, request.path);
|
2021-07-28 21:37:09 +00:00
|
|
|
if (request.path !== '/api/v1/message') {
|
|
|
|
request.respond(200, 'OK');
|
2017-10-06 23:28:13 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (request.verb === 'PUT' && request.path === '/api/v1/queue/empty') {
|
|
|
|
this.incomingQueue.add(() => {
|
|
|
|
this.onEmpty();
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
return;
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const job = async () => {
|
|
|
|
const headers = request.headers || [];
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (!request.body) {
|
|
|
|
throw new Error(
|
|
|
|
'MessageReceiver.handleRequest: request.body was falsey!'
|
|
|
|
);
|
2021-06-23 14:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const plaintext = request.body;
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
try {
|
|
|
|
const decoded = Proto.Envelope.decode(plaintext);
|
|
|
|
const serverTimestamp = normalizeNumber(decoded.serverTimestamp);
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const envelope: ProcessedEnvelope = {
|
|
|
|
// Make non-private envelope IDs dashless so they don't get redacted
|
|
|
|
// from logs
|
|
|
|
id: getGuid().replace(/-/g, ''),
|
|
|
|
receivedAtCounter: window.Signal.Util.incrementMessageCounter(),
|
|
|
|
receivedAtDate: Date.now(),
|
|
|
|
// Calculate the message age (time on server).
|
|
|
|
messageAgeSec: this.calculateMessageAge(headers, serverTimestamp),
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
// Proto.Envelope fields
|
|
|
|
type: decoded.type,
|
|
|
|
source: decoded.source,
|
|
|
|
sourceUuid: decoded.sourceUuid
|
|
|
|
? normalizeUuid(
|
|
|
|
decoded.sourceUuid,
|
|
|
|
'MessageReceiver.handleRequest.sourceUuid'
|
|
|
|
)
|
|
|
|
: undefined,
|
|
|
|
sourceDevice: decoded.sourceDevice,
|
|
|
|
timestamp: normalizeNumber(decoded.timestamp),
|
|
|
|
legacyMessage: dropNull(decoded.legacyMessage),
|
|
|
|
content: dropNull(decoded.content),
|
|
|
|
serverGuid: decoded.serverGuid,
|
|
|
|
serverTimestamp,
|
|
|
|
};
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
// After this point, decoding errors are not the server's
|
|
|
|
// fault, and we should handle them gracefully and tell the
|
|
|
|
// user they received an invalid message
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
this.decryptAndCache(envelope, plaintext, request);
|
|
|
|
this.processedCount += 1;
|
|
|
|
} catch (e) {
|
|
|
|
request.respond(500, 'Bad encrypted websocket message');
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error('Error handling incoming message:', Errors.toLogFormat(e));
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.dispatchAndWait(new ErrorEvent(e));
|
|
|
|
}
|
|
|
|
};
|
2021-07-23 20:47:03 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
this.incomingQueue.add(job);
|
|
|
|
}
|
2021-07-23 20:47:03 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
public reset(): void {
|
|
|
|
// We always process our cache before processing a new websocket message
|
|
|
|
this.incomingQueue.add(async () => this.queueAllCached());
|
2021-07-23 20:47:03 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
this.count = 0;
|
|
|
|
this.isEmptied = false;
|
|
|
|
this.stoppingProcessing = false;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
public stopProcessing(): void {
|
|
|
|
this.stoppingProcessing = true;
|
2021-07-23 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
public hasEmptied(): boolean {
|
|
|
|
return Boolean(this.isEmptied);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
public async drain(): Promise<void> {
|
|
|
|
const waitForEncryptedQueue = async () =>
|
|
|
|
this.addToQueue(async () => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('drained');
|
2021-07-28 21:37:09 +00:00
|
|
|
}, TaskType.Decrypted);
|
2021-07-23 20:47:03 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const waitForIncomingQueue = async () =>
|
|
|
|
this.addToQueue(waitForEncryptedQueue, TaskType.Encrypted);
|
2021-07-23 20:47:03 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
return this.incomingQueue.add(waitForIncomingQueue);
|
2021-07-23 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// EventTarget types
|
|
|
|
//
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'reconnect',
|
|
|
|
handler: (ev: ReconnectEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'empty',
|
|
|
|
handler: (ev: EmptyEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'progress',
|
|
|
|
handler: (ev: ProgressEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'typing',
|
|
|
|
handler: (ev: TypingEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'error',
|
|
|
|
handler: (ev: ErrorEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'delivery',
|
|
|
|
handler: (ev: DeliveryEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'decryption-error',
|
|
|
|
handler: (ev: DecryptionErrorEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(name: 'sent', handler: (ev: SentEvent) => void): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'profileKeyUpdate',
|
|
|
|
handler: (ev: ProfileKeyUpdateEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'message',
|
|
|
|
handler: (ev: MessageEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'retry-request',
|
|
|
|
handler: (ev: RetryRequestEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(name: 'read', handler: (ev: ReadEvent) => void): void;
|
|
|
|
|
2021-07-27 15:42:25 +00:00
|
|
|
public addEventListener(name: 'view', handler: (ev: ViewEvent) => void): void;
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
public addEventListener(
|
|
|
|
name: 'configuration',
|
|
|
|
handler: (ev: ConfigurationEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'viewOnceOpenSync',
|
|
|
|
handler: (ev: ViewOnceOpenSyncEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'messageRequestResponse',
|
|
|
|
handler: (ev: MessageRequestResponseEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'fetchLatest',
|
|
|
|
handler: (ev: FetchLatestEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(name: 'keys', handler: (ev: KeysEvent) => void): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'sticker-pack',
|
|
|
|
handler: (ev: StickerPackEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'verified',
|
|
|
|
handler: (ev: VerifiedEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'readSync',
|
|
|
|
handler: (ev: ReadSyncEvent) => void
|
|
|
|
): void;
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
public addEventListener(
|
|
|
|
name: 'viewSync',
|
|
|
|
handler: (ev: ViewSyncEvent) => void
|
|
|
|
): void;
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
public addEventListener(
|
|
|
|
name: 'contact',
|
|
|
|
handler: (ev: ContactEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'contactSync',
|
|
|
|
handler: (ev: ContactSyncEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'group',
|
|
|
|
handler: (ev: GroupEvent) => void
|
|
|
|
): void;
|
|
|
|
|
|
|
|
public addEventListener(
|
|
|
|
name: 'groupSync',
|
|
|
|
handler: (ev: GroupSyncEvent) => void
|
|
|
|
): void;
|
|
|
|
|
2021-08-05 23:34:49 +00:00
|
|
|
public addEventListener(
|
|
|
|
name: 'envelope',
|
|
|
|
handler: (ev: EnvelopeEvent) => void
|
|
|
|
): void;
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
public addEventListener(name: string, handler: EventHandler): void {
|
|
|
|
return super.addEventListener(name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeEventListener(name: string, handler: EventHandler): void {
|
|
|
|
return super.removeEventListener(name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
|
|
|
private async dispatchAndWait(event: Event): Promise<void> {
|
2020-04-13 17:37:29 +00:00
|
|
|
this.appQueue.add(async () => Promise.all(this.dispatchEvent(event)));
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private calculateMessageAge(
|
2021-07-02 19:21:24 +00:00
|
|
|
headers: ReadonlyArray<string>,
|
2020-06-25 21:42:32 +00:00
|
|
|
serverTimestamp?: number
|
|
|
|
): number {
|
|
|
|
let messageAgeSec = 0; // Default to 0 in case of unreliable parameters.
|
|
|
|
|
|
|
|
if (serverTimestamp) {
|
|
|
|
// The 'X-Signal-Timestamp' is usually the last item, so start there.
|
|
|
|
let it = headers.length;
|
2020-09-24 21:53:21 +00:00
|
|
|
// eslint-disable-next-line no-plusplus
|
2020-06-25 21:42:32 +00:00
|
|
|
while (--it >= 0) {
|
|
|
|
const match = headers[it].match(/^X-Signal-Timestamp:\s*(\d+)\s*$/);
|
|
|
|
if (match && match.length === 2) {
|
|
|
|
const timestamp = Number(match[1]);
|
|
|
|
|
|
|
|
// One final sanity check, the timestamp when a message is pulled from
|
|
|
|
// the server should be later than when it was pushed.
|
|
|
|
if (timestamp > serverTimestamp) {
|
|
|
|
messageAgeSec = Math.floor((timestamp - serverTimestamp) / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageAgeSec;
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async addToQueue<T>(
|
|
|
|
task: () => Promise<T>,
|
|
|
|
taskType: TaskType
|
|
|
|
): Promise<T> {
|
2021-05-21 00:24:53 +00:00
|
|
|
if (taskType === TaskType.Encrypted) {
|
|
|
|
this.count += 1;
|
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
const queue =
|
|
|
|
taskType === TaskType.Encrypted
|
|
|
|
? this.encryptedQueue
|
|
|
|
: this.decryptedQueue;
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
try {
|
|
|
|
return await queue.add(task);
|
|
|
|
} finally {
|
|
|
|
this.updateProgress(this.count);
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private onEmpty(): void {
|
2021-03-26 00:00:03 +00:00
|
|
|
const emitEmpty = async () => {
|
|
|
|
await Promise.all([
|
2021-05-24 21:30:56 +00:00
|
|
|
this.decryptAndCacheBatcher.flushAndWait(),
|
2021-03-26 00:00:03 +00:00
|
|
|
this.cacheRemoveBatcher.flushAndWait(),
|
|
|
|
]);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info("MessageReceiver: emitting 'empty' event");
|
2021-07-09 19:36:10 +00:00
|
|
|
this.dispatchEvent(new EmptyEvent());
|
2019-08-16 16:15:39 +00:00
|
|
|
this.isEmptied = true;
|
2019-10-22 01:54:40 +00:00
|
|
|
|
2019-08-16 16:15:39 +00:00
|
|
|
this.maybeScheduleRetryTimeout();
|
2019-02-05 18:04:38 +00:00
|
|
|
};
|
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
const waitForDecryptedQueue = async () => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2019-02-05 18:04:38 +00:00
|
|
|
"MessageReceiver: finished processing messages after 'empty', now waiting for application"
|
|
|
|
);
|
|
|
|
|
2019-07-09 18:46:48 +00:00
|
|
|
// We don't await here because we don't want this to gate future message processing
|
|
|
|
this.appQueue.add(emitEmpty);
|
2018-04-10 17:23:09 +00:00
|
|
|
};
|
|
|
|
|
2021-05-21 00:24:53 +00:00
|
|
|
const waitForEncryptedQueue = async () => {
|
|
|
|
this.addToQueue(waitForDecryptedQueue, TaskType.Decrypted);
|
|
|
|
};
|
|
|
|
|
2019-07-09 18:46:48 +00:00
|
|
|
const waitForIncomingQueue = () => {
|
2021-05-21 00:24:53 +00:00
|
|
|
this.addToQueue(waitForEncryptedQueue, TaskType.Encrypted);
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2019-07-09 18:46:48 +00:00
|
|
|
// Note: this.count is used in addToQueue
|
|
|
|
// Resetting count so everything from the websocket after this starts at zero
|
|
|
|
this.count = 0;
|
2018-04-10 17:23:09 +00:00
|
|
|
};
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
const waitForCacheAddBatcher = async () => {
|
2021-05-24 21:30:56 +00:00
|
|
|
await this.decryptAndCacheBatcher.onIdle();
|
2019-09-26 19:56:31 +00:00
|
|
|
this.incomingQueue.add(waitForIncomingQueue);
|
|
|
|
};
|
|
|
|
|
|
|
|
waitForCacheAddBatcher();
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private updateProgress(count: number): void {
|
2018-04-10 17:23:09 +00:00
|
|
|
// count by 10s
|
|
|
|
if (count % 10 !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
this.dispatchEvent(new ProgressEvent({ count }));
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async queueAllCached(): Promise<void> {
|
2018-08-15 22:14:12 +00:00
|
|
|
const items = await this.getAllFromCache();
|
2020-04-13 17:37:29 +00:00
|
|
|
const max = items.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-08-16 16:50:56 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-03-17 21:20:05 +00:00
|
|
|
await this.queueCached(items[i]);
|
2018-08-15 22:14:12 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async queueCached(item: UnprocessedType): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.queueCached', item.id);
|
2018-04-10 17:23:09 +00:00
|
|
|
try {
|
2021-07-09 19:36:10 +00:00
|
|
|
let envelopePlaintext: Uint8Array;
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
if (item.envelope && item.version === 2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
envelopePlaintext = Bytes.fromBase64(item.envelope);
|
2021-05-24 21:30:56 +00:00
|
|
|
} else if (item.envelope && typeof item.envelope === 'string') {
|
2021-07-09 19:36:10 +00:00
|
|
|
envelopePlaintext = Bytes.fromBinary(item.envelope);
|
2020-04-13 17:37:29 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(
|
2021-03-17 21:20:05 +00:00
|
|
|
'MessageReceiver.queueCached: item.envelope was malformed'
|
2018-08-06 19:11:02 +00:00
|
|
|
);
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const decoded = Proto.Envelope.decode(envelopePlaintext);
|
|
|
|
|
|
|
|
const envelope: ProcessedEnvelope = {
|
|
|
|
id: item.id,
|
|
|
|
receivedAtCounter: item.timestamp,
|
|
|
|
receivedAtDate: Date.now(),
|
|
|
|
messageAgeSec: item.messageAgeSec || 0,
|
|
|
|
|
|
|
|
// Proto.Envelope fields
|
|
|
|
type: decoded.type,
|
|
|
|
source: decoded.source || item.source,
|
|
|
|
sourceUuid: decoded.sourceUuid || item.sourceUuid,
|
|
|
|
sourceDevice: decoded.sourceDevice || item.sourceDevice,
|
|
|
|
timestamp: normalizeNumber(decoded.timestamp),
|
|
|
|
legacyMessage: dropNull(decoded.legacyMessage),
|
|
|
|
content: dropNull(decoded.content),
|
|
|
|
serverGuid: decoded.serverGuid,
|
|
|
|
serverTimestamp: normalizeNumber(
|
|
|
|
item.serverTimestamp || decoded.serverTimestamp
|
|
|
|
),
|
|
|
|
};
|
2018-04-10 17:23:09 +00:00
|
|
|
|
|
|
|
const { decrypted } = item;
|
|
|
|
if (decrypted) {
|
2021-07-09 19:36:10 +00:00
|
|
|
let payloadPlaintext: Uint8Array;
|
2018-08-10 00:28:51 +00:00
|
|
|
|
|
|
|
if (item.version === 2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
payloadPlaintext = Bytes.fromBase64(decrypted);
|
2020-04-13 17:37:29 +00:00
|
|
|
} else if (typeof decrypted === 'string') {
|
2021-07-09 19:36:10 +00:00
|
|
|
payloadPlaintext = Bytes.fromBinary(decrypted);
|
2020-04-13 17:37:29 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Cached decrypted value was not a string!');
|
2017-09-13 18:25:51 +00:00
|
|
|
}
|
2021-05-21 00:24:53 +00:00
|
|
|
|
|
|
|
// Maintain invariant: encrypted queue => decrypted queue
|
|
|
|
this.addToQueue(async () => {
|
|
|
|
this.queueDecryptedEnvelope(envelope, payloadPlaintext);
|
|
|
|
}, TaskType.Encrypted);
|
2018-04-10 17:23:09 +00:00
|
|
|
} else {
|
2021-05-24 21:30:56 +00:00
|
|
|
this.queueCachedEnvelope(item, envelope);
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 21:20:05 +00:00
|
|
|
'queueCached error handling item',
|
2018-08-03 18:55:43 +00:00
|
|
|
item.id,
|
|
|
|
'removing it. Error:',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error)
|
2018-08-03 18:55:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { id } = item;
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.protocol.removeUnprocessed(id);
|
2018-08-03 18:55:43 +00:00
|
|
|
} catch (deleteError) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 21:20:05 +00:00
|
|
|
'queueCached error deleting item',
|
2018-08-03 18:55:43 +00:00
|
|
|
item.id,
|
|
|
|
'Error:',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(deleteError)
|
2018-08-03 18:55:43 +00:00
|
|
|
);
|
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private getEnvelopeId(envelope: ProcessedEnvelope): string {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { timestamp } = envelope;
|
2021-02-12 17:43:24 +00:00
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
if (envelope.sourceUuid || envelope.source) {
|
2021-02-12 17:43:24 +00:00
|
|
|
const sender = envelope.sourceUuid || envelope.source;
|
|
|
|
return `${sender}.${envelope.sourceDevice} ${timestamp} (${envelope.id})`;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 22:37:04 +00:00
|
|
|
return `${timestamp} (${envelope.id})`;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private clearRetryTimeout(): void {
|
2019-08-16 16:15:39 +00:00
|
|
|
if (this.retryCachedTimeout) {
|
|
|
|
clearInterval(this.retryCachedTimeout);
|
2021-07-23 20:47:03 +00:00
|
|
|
this.retryCachedTimeout = undefined;
|
2019-08-16 16:15:39 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private maybeScheduleRetryTimeout(): void {
|
2019-08-16 16:15:39 +00:00
|
|
|
if (this.isEmptied) {
|
2019-10-22 01:54:40 +00:00
|
|
|
this.clearRetryTimeout();
|
2019-08-16 16:15:39 +00:00
|
|
|
this.retryCachedTimeout = setTimeout(() => {
|
2021-05-24 18:00:53 +00:00
|
|
|
this.incomingQueue.add(async () => this.queueAllCached());
|
2019-08-16 16:15:39 +00:00
|
|
|
}, RETRY_TIMEOUT);
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async getAllFromCache(): Promise<Array<UnprocessedType>> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('getAllFromCache');
|
2021-07-28 21:37:09 +00:00
|
|
|
const count = await this.storage.protocol.getUnprocessedCount();
|
2018-09-28 22:51:26 +00:00
|
|
|
|
2018-11-29 22:13:54 +00:00
|
|
|
if (count > 1500) {
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.protocol.removeAllUnprocessed();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2018-09-28 22:51:26 +00:00
|
|
|
`There were ${count} messages in cache. Deleted all instead of reprocessing`
|
2018-07-21 19:00:08 +00:00
|
|
|
);
|
2018-09-28 22:51:26 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const items = await this.storage.protocol.getAllUnprocessed();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('getAllFromCache loaded', items.length, 'saved envelopes');
|
2018-09-28 22:51:26 +00:00
|
|
|
|
2021-09-17 23:11:24 +00:00
|
|
|
return items.map(item => {
|
|
|
|
const { attempts = 0 } = item;
|
2018-09-28 22:51:26 +00:00
|
|
|
|
2021-09-17 23:11:24 +00:00
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
attempts: attempts + 1,
|
|
|
|
};
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async decryptAndCacheBatch(
|
|
|
|
items: Array<CacheAddItemType>
|
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.decryptAndCacheBatch', items.length);
|
2021-05-17 18:03:42 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const decrypted: Array<
|
|
|
|
Readonly<{
|
|
|
|
plaintext: Uint8Array;
|
|
|
|
data: UnprocessedType;
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope;
|
2021-07-09 19:36:10 +00:00
|
|
|
}>
|
|
|
|
> = [];
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const storageProtocol = this.storage.protocol;
|
2021-05-17 18:03:42 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
try {
|
2021-05-24 21:30:56 +00:00
|
|
|
const zone = new Zone('decryptAndCacheBatch', {
|
2021-05-19 21:25:56 +00:00
|
|
|
pendingSessions: true,
|
|
|
|
pendingUnprocessed: true,
|
2021-05-17 18:03:42 +00:00
|
|
|
});
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid();
|
|
|
|
const sessionStore = new Sessions({ zone, ourUuid });
|
|
|
|
const identityKeyStore = new IdentityKeys({ zone, ourUuid });
|
2021-05-17 18:03:42 +00:00
|
|
|
const failed: Array<UnprocessedType> = [];
|
|
|
|
|
|
|
|
// Below we:
|
|
|
|
//
|
2021-05-19 21:25:56 +00:00
|
|
|
// 1. Enter zone
|
2021-05-17 18:03:42 +00:00
|
|
|
// 2. Decrypt all batched envelopes
|
|
|
|
// 3. Persist both decrypted envelopes and envelopes that we failed to
|
|
|
|
// decrypt (for future retries, see `attempts` field)
|
2021-05-19 21:25:56 +00:00
|
|
|
// 4. Leave zone and commit all pending sessions and unprocesseds
|
2021-05-17 18:03:42 +00:00
|
|
|
// 5. Acknowledge envelopes (can't fail)
|
|
|
|
// 6. Finally process decrypted envelopes
|
2021-05-19 21:25:56 +00:00
|
|
|
await storageProtocol.withZone(zone, 'MessageReceiver', async () => {
|
2021-05-17 18:03:42 +00:00
|
|
|
await Promise.all<void>(
|
|
|
|
items.map(async ({ data, envelope }) => {
|
|
|
|
try {
|
2021-07-09 19:36:10 +00:00
|
|
|
const result = await this.queueEncryptedEnvelope(
|
2021-05-20 23:49:08 +00:00
|
|
|
{ sessionStore, identityKeyStore, zone },
|
2021-05-17 18:03:42 +00:00
|
|
|
envelope
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
if (result.plaintext) {
|
|
|
|
decrypted.push({
|
|
|
|
plaintext: result.plaintext,
|
|
|
|
envelope: result.envelope,
|
|
|
|
data,
|
|
|
|
});
|
2021-05-17 18:03:42 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
failed.push(data);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-05-24 21:30:56 +00:00
|
|
|
'decryptAndCache error when processing the envelope',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-05-17 18:03:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-24 21:30:56 +00:00
|
|
|
'MessageReceiver.decryptAndCacheBatch storing ' +
|
2021-08-25 23:10:09 +00:00
|
|
|
`${decrypted.length} decrypted envelopes, keeping ` +
|
|
|
|
`${failed.length} failed envelopes.`
|
2021-05-17 18:03:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Store both decrypted and failed unprocessed envelopes
|
|
|
|
const unprocesseds: Array<UnprocessedType> = decrypted.map(
|
|
|
|
({ envelope, data, plaintext }) => {
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
|
|
|
|
source: envelope.source,
|
|
|
|
sourceUuid: envelope.sourceUuid,
|
|
|
|
sourceDevice: envelope.sourceDevice,
|
2021-05-27 20:17:05 +00:00
|
|
|
serverGuid: envelope.serverGuid,
|
2021-05-17 18:03:42 +00:00
|
|
|
serverTimestamp: envelope.serverTimestamp,
|
2021-07-09 19:36:10 +00:00
|
|
|
decrypted: Bytes.toBase64(plaintext),
|
2021-05-17 18:03:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-05-19 21:25:56 +00:00
|
|
|
await storageProtocol.addMultipleUnprocessed(
|
|
|
|
unprocesseds.concat(failed),
|
|
|
|
{ zone }
|
|
|
|
);
|
2021-05-17 18:03:42 +00:00
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.decryptAndCacheBatch acknowledging receipt');
|
2021-05-17 18:03:42 +00:00
|
|
|
|
|
|
|
// Acknowledge all envelopes
|
|
|
|
for (const { request } of items) {
|
2020-12-03 16:04:50 +00:00
|
|
|
try {
|
2021-05-17 18:03:42 +00:00
|
|
|
request.respond(200, 'OK');
|
2020-12-03 16:04:50 +00:00
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-05-24 21:30:56 +00:00
|
|
|
'decryptAndCacheBatch: Failed to send 200 to server; still queuing envelope'
|
2020-12-03 16:04:50 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-17 18:03:42 +00:00
|
|
|
}
|
2019-09-26 19:56:31 +00:00
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-05-24 21:30:56 +00:00
|
|
|
'decryptAndCache error trying to add messages to cache:',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error)
|
2019-09-26 19:56:31 +00:00
|
|
|
);
|
2020-12-03 16:04:50 +00:00
|
|
|
|
|
|
|
items.forEach(item => {
|
|
|
|
item.request.respond(500, 'Failed to cache message');
|
|
|
|
});
|
2021-05-17 18:03:42 +00:00
|
|
|
return;
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
2021-05-17 18:03:42 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
decrypted.map(async ({ envelope, plaintext }) => {
|
|
|
|
try {
|
|
|
|
await this.queueDecryptedEnvelope(envelope, plaintext);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-05-24 21:30:56 +00:00
|
|
|
'decryptAndCache error when processing decrypted envelope',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-05-17 18:03:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.decryptAndCacheBatch fully processed');
|
2021-05-17 18:03:42 +00:00
|
|
|
|
|
|
|
this.maybeScheduleRetryTimeout();
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private decryptAndCache(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
plaintext: Uint8Array,
|
2020-04-13 17:37:29 +00:00
|
|
|
request: IncomingWebSocketRequest
|
2021-07-23 20:47:03 +00:00
|
|
|
): void {
|
2018-10-18 01:01:21 +00:00
|
|
|
const { id } = envelope;
|
2021-07-09 19:36:10 +00:00
|
|
|
const data: UnprocessedType = {
|
2018-04-10 17:23:09 +00:00
|
|
|
id,
|
2018-08-10 00:28:51 +00:00
|
|
|
version: 2,
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: Bytes.toBase64(plaintext),
|
2021-03-04 21:44:57 +00:00
|
|
|
timestamp: envelope.receivedAtCounter,
|
2018-04-10 17:23:09 +00:00
|
|
|
attempts: 1,
|
2021-07-09 19:36:10 +00:00
|
|
|
messageAgeSec: envelope.messageAgeSec,
|
2018-04-10 17:23:09 +00:00
|
|
|
};
|
2021-05-24 21:30:56 +00:00
|
|
|
this.decryptAndCacheBatcher.add({
|
2019-09-26 19:56:31 +00:00
|
|
|
request,
|
|
|
|
envelope,
|
|
|
|
data,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async cacheRemoveBatch(items: Array<string>): Promise<void> {
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.protocol.removeUnprocessed(items);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private removeFromCache(envelope: ProcessedEnvelope): void {
|
2018-10-18 01:01:21 +00:00
|
|
|
const { id } = envelope;
|
2019-09-26 19:56:31 +00:00
|
|
|
this.cacheRemoveBatcher.add(id);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async queueDecryptedEnvelope(
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope,
|
2021-07-09 19:36:10 +00:00
|
|
|
plaintext: Uint8Array
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2019-02-11 22:53:57 +00:00
|
|
|
const id = this.getEnvelopeId(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('queueing decrypted envelope', id);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-03-17 21:20:05 +00:00
|
|
|
const task = this.handleDecryptedEnvelope.bind(this, envelope, plaintext);
|
2021-07-28 21:37:09 +00:00
|
|
|
const taskWithTimeout = createTaskWithTimeout(
|
2021-03-17 21:20:05 +00:00
|
|
|
task,
|
2021-05-24 21:30:56 +00:00
|
|
|
`queueDecryptedEnvelope ${id}`
|
2021-03-17 21:20:05 +00:00
|
|
|
);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
try {
|
|
|
|
await this.addToQueue(taskWithTimeout, TaskType.Decrypted);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 21:20:05 +00:00
|
|
|
`queueDecryptedEnvelope error handling envelope ${id}:`,
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error)
|
2018-04-10 17:23:09 +00:00
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async queueEncryptedEnvelope(
|
2021-05-18 00:41:28 +00:00
|
|
|
stores: LockedStores,
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope
|
|
|
|
): Promise<DecryptResult> {
|
2021-08-02 21:11:18 +00:00
|
|
|
let logId = this.getEnvelopeId(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('queueing envelope', logId);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-08-02 21:11:18 +00:00
|
|
|
const task = createTaskWithTimeout(async (): Promise<DecryptResult> => {
|
|
|
|
const unsealedEnvelope = await this.unsealEnvelope(stores, envelope);
|
|
|
|
|
|
|
|
logId = this.getEnvelopeId(unsealedEnvelope);
|
|
|
|
|
|
|
|
return this.decryptEnvelope(stores, unsealedEnvelope);
|
|
|
|
}, `MessageReceiver: unseal and decrypt ${logId}`);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-05-17 18:03:42 +00:00
|
|
|
try {
|
2021-08-02 21:11:18 +00:00
|
|
|
return await this.addToQueue(task, TaskType.Encrypted);
|
2021-05-17 18:03:42 +00:00
|
|
|
} catch (error) {
|
2020-03-20 19:01:15 +00:00
|
|
|
const args = [
|
2021-05-21 00:24:53 +00:00
|
|
|
'queueEncryptedEnvelope error handling envelope',
|
2021-08-02 21:11:18 +00:00
|
|
|
logId,
|
2018-04-10 17:23:09 +00:00
|
|
|
':',
|
2021-07-27 19:55:39 +00:00
|
|
|
Errors.toLogFormat(error),
|
2020-03-20 19:01:15 +00:00
|
|
|
];
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof WarnOnlyError) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(...args);
|
2020-03-20 19:01:15 +00:00
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(...args);
|
2020-03-20 19:01:15 +00:00
|
|
|
}
|
2021-08-25 23:10:09 +00:00
|
|
|
throw error;
|
2021-05-17 18:03:42 +00:00
|
|
|
}
|
2021-03-17 21:20:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async queueCachedEnvelope(
|
2021-05-24 21:30:56 +00:00
|
|
|
data: UnprocessedType,
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope
|
2021-05-24 21:30:56 +00:00
|
|
|
): Promise<void> {
|
|
|
|
this.decryptAndCacheBatcher.add({
|
|
|
|
request: {
|
|
|
|
respond(code, status) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-24 21:30:56 +00:00
|
|
|
'queueCachedEnvelope: fake response ' +
|
|
|
|
`with code ${code} and status ${status}`
|
|
|
|
);
|
|
|
|
},
|
2021-05-21 00:24:53 +00:00
|
|
|
},
|
2021-05-24 21:30:56 +00:00
|
|
|
envelope,
|
|
|
|
data,
|
|
|
|
});
|
2021-05-21 00:24:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 18:03:42 +00:00
|
|
|
// Called after `decryptEnvelope` decrypted the message.
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleDecryptedEnvelope(
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope,
|
2021-07-09 19:36:10 +00:00
|
|
|
plaintext: Uint8Array
|
2021-03-17 21:20:05 +00:00
|
|
|
): Promise<void> {
|
|
|
|
if (this.stoppingProcessing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (envelope.content) {
|
|
|
|
await this.innerHandleContentMessage(envelope, plaintext);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (envelope.legacyMessage) {
|
|
|
|
await this.innerHandleLegacyMessage(envelope, plaintext);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
throw new Error('Received message with no content and no legacyMessage');
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:11:18 +00:00
|
|
|
private async unsealEnvelope(
|
|
|
|
stores: LockedStores,
|
|
|
|
envelope: ProcessedEnvelope
|
2021-08-25 23:10:09 +00:00
|
|
|
): Promise<UnsealedEnvelope> {
|
2021-08-02 21:11:18 +00:00
|
|
|
const logId = this.getEnvelopeId(envelope);
|
|
|
|
|
|
|
|
if (this.stoppingProcessing) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`MessageReceiver.unsealEnvelope(${logId}): dropping`);
|
2021-08-25 23:10:09 +00:00
|
|
|
throw new Error('Sealed envelope dropped due to stopping processing');
|
2021-08-02 21:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (envelope.type !== Proto.Envelope.Type.UNIDENTIFIED_SENDER) {
|
|
|
|
return envelope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphertext = envelope.content || envelope.legacyMessage;
|
|
|
|
if (!ciphertext) {
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
throw new Error('Received message with no content and no legacyMessage');
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`MessageReceiver.unsealEnvelope(${logId}): unidentified message`);
|
2021-08-02 21:11:18 +00:00
|
|
|
const messageContent = await sealedSenderDecryptToUsmc(
|
|
|
|
Buffer.from(ciphertext),
|
|
|
|
stores.identityKeyStore
|
|
|
|
);
|
|
|
|
|
|
|
|
// Here we take this sender information and attach it back to the envelope
|
|
|
|
// to make the rest of the app work properly.
|
|
|
|
const certificate = messageContent.senderCertificate();
|
|
|
|
|
|
|
|
const originalSource = envelope.source;
|
|
|
|
const originalSourceUuid = envelope.sourceUuid;
|
|
|
|
|
|
|
|
const newEnvelope: UnsealedEnvelope = {
|
|
|
|
...envelope,
|
|
|
|
|
|
|
|
// Overwrite Envelope fields
|
|
|
|
source: dropNull(certificate.senderE164()),
|
|
|
|
sourceUuid: normalizeUuid(
|
|
|
|
certificate.senderUuid(),
|
|
|
|
'MessageReceiver.unsealEnvelope.UNIDENTIFIED_SENDER.sourceUuid'
|
|
|
|
),
|
|
|
|
sourceDevice: certificate.senderDeviceId(),
|
|
|
|
|
|
|
|
// UnsealedEnvelope-only fields
|
|
|
|
unidentifiedDeliveryReceived: !(originalSource || originalSourceUuid),
|
|
|
|
contentHint: messageContent.contentHint(),
|
|
|
|
groupId: messageContent.groupId()?.toString('base64'),
|
|
|
|
usmc: messageContent,
|
|
|
|
certificate,
|
|
|
|
unsealedContent: messageContent,
|
|
|
|
};
|
|
|
|
|
2021-08-17 18:40:55 +00:00
|
|
|
// This will throw if there's a problem
|
|
|
|
this.validateUnsealedEnvelope(newEnvelope);
|
2021-08-02 21:11:18 +00:00
|
|
|
|
|
|
|
return newEnvelope;
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async decryptEnvelope(
|
2021-05-18 00:41:28 +00:00
|
|
|
stores: LockedStores,
|
2021-08-02 21:11:18 +00:00
|
|
|
envelope: UnsealedEnvelope
|
2021-07-09 19:36:10 +00:00
|
|
|
): Promise<DecryptResult> {
|
2021-08-02 21:11:18 +00:00
|
|
|
const logId = this.getEnvelopeId(envelope);
|
2021-07-27 19:55:39 +00:00
|
|
|
|
2021-03-17 21:20:05 +00:00
|
|
|
if (this.stoppingProcessing) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`MessageReceiver.decryptEnvelope(${logId}): dropping unsealed`);
|
2021-08-25 23:10:09 +00:00
|
|
|
throw new Error('Unsealed envelope dropped due to stopping processing');
|
2019-02-19 20:10:26 +00:00
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (envelope.type === Proto.Envelope.Type.RECEIPT) {
|
2021-05-17 18:03:42 +00:00
|
|
|
await this.onDeliveryReceipt(envelope);
|
2021-07-09 19:36:10 +00:00
|
|
|
return { plaintext: undefined, envelope };
|
2021-03-17 21:20:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
let ciphertext: Uint8Array;
|
|
|
|
let isLegacy = false;
|
2021-03-17 21:20:05 +00:00
|
|
|
if (envelope.content) {
|
2021-07-27 19:55:39 +00:00
|
|
|
ciphertext = envelope.content;
|
|
|
|
} else if (envelope.legacyMessage) {
|
|
|
|
ciphertext = envelope.legacyMessage;
|
|
|
|
isLegacy = true;
|
|
|
|
} else {
|
|
|
|
this.removeFromCache(envelope);
|
2021-08-02 21:11:18 +00:00
|
|
|
strictAssert(
|
|
|
|
false,
|
|
|
|
'Contentless envelope should be handled by unsealEnvelope'
|
2021-07-27 19:55:39 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-27 19:55:39 +00:00
|
|
|
`MessageReceiver.decryptEnvelope(${logId})${isLegacy ? ' (legacy)' : ''}`
|
2018-04-10 17:23:09 +00:00
|
|
|
);
|
2021-07-27 19:55:39 +00:00
|
|
|
const plaintext = await this.decrypt(stores, envelope, ciphertext);
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
if (!plaintext) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('MessageReceiver.decryptEnvelope: plaintext was falsey');
|
2021-07-27 19:55:39 +00:00
|
|
|
return { plaintext, envelope };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy envelopes do not carry senderKeyDistributionMessage
|
|
|
|
if (isLegacy) {
|
|
|
|
return { plaintext, envelope };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: we need to process this as part of decryption, because we might need this
|
|
|
|
// sender key to decrypt the next message in the queue!
|
|
|
|
try {
|
|
|
|
const content = Proto.Content.decode(plaintext);
|
|
|
|
|
|
|
|
if (
|
|
|
|
content.senderKeyDistributionMessage &&
|
|
|
|
Bytes.isNotEmpty(content.senderKeyDistributionMessage)
|
|
|
|
) {
|
|
|
|
await this.handleSenderKeyDistributionMessage(
|
|
|
|
stores,
|
|
|
|
envelope,
|
|
|
|
content.senderKeyDistributionMessage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-08-02 21:11:18 +00:00
|
|
|
'MessageReceiver.decryptEnvelope: Failed to process sender ' +
|
2021-07-27 19:55:39 +00:00
|
|
|
`key distribution message: ${Errors.toLogFormat(error)}`
|
2021-07-09 19:36:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:40:55 +00:00
|
|
|
if (
|
|
|
|
(envelope.source && this.isBlocked(envelope.source)) ||
|
|
|
|
(envelope.sourceUuid && this.isUuidBlocked(envelope.sourceUuid))
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-17 18:40:55 +00:00
|
|
|
'MessageReceiver.decryptEnvelope: Dropping message from blocked sender'
|
|
|
|
);
|
|
|
|
return { plaintext: undefined, envelope };
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
return { plaintext, envelope };
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-08-17 18:40:55 +00:00
|
|
|
private validateUnsealedEnvelope(envelope: UnsealedEnvelope): void {
|
2021-07-27 19:55:39 +00:00
|
|
|
const { unsealedContent: messageContent, certificate } = envelope;
|
|
|
|
strictAssert(
|
|
|
|
messageContent !== undefined,
|
|
|
|
'Missing message content for sealed sender message'
|
|
|
|
);
|
|
|
|
strictAssert(
|
|
|
|
certificate !== undefined,
|
|
|
|
'Missing sender certificate for sealed sender message'
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
if (!envelope.serverTimestamp) {
|
2021-07-09 19:36:10 +00:00
|
|
|
throw new Error(
|
|
|
|
'MessageReceiver.decryptSealedSender: ' +
|
|
|
|
'Sealed sender message was missing serverTimestamp'
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 19:13:48 +00:00
|
|
|
const serverCertificate = certificate.serverCertificate();
|
|
|
|
|
|
|
|
if (
|
|
|
|
!verifySignature(
|
2021-09-24 00:49:05 +00:00
|
|
|
this.serverTrustRoot,
|
|
|
|
serverCertificate.certificateData(),
|
|
|
|
serverCertificate.signature()
|
2021-07-15 19:13:48 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
2021-07-27 19:55:39 +00:00
|
|
|
'MessageReceiver.validateUnsealedEnvelope: ' +
|
|
|
|
'Server certificate trust root validation failed'
|
2021-07-15 19:13:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!verifySignature(
|
2021-09-24 00:49:05 +00:00
|
|
|
serverCertificate.key().serialize(),
|
|
|
|
certificate.certificate(),
|
|
|
|
certificate.signature()
|
2021-07-15 19:13:48 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
2021-07-27 19:55:39 +00:00
|
|
|
'MessageReceiver.validateUnsealedEnvelope: ' +
|
|
|
|
'Server certificate server signature validation failed'
|
2021-07-15 19:13:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
const logId = this.getEnvelopeId(envelope);
|
2021-07-23 22:37:04 +00:00
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
if (envelope.serverTimestamp > certificate.expiration()) {
|
2021-07-15 19:13:48 +00:00
|
|
|
throw new Error(
|
2021-09-20 18:51:30 +00:00
|
|
|
'MessageReceiver.validateUnsealedEnvelope: ' +
|
|
|
|
`Sender certificate is expired for envelope ${logId}`
|
2021-07-15 19:13:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async onDeliveryReceipt(envelope: ProcessedEnvelope): Promise<void> {
|
|
|
|
await this.dispatchAndWait(
|
|
|
|
new DeliveryEvent(
|
|
|
|
{
|
|
|
|
timestamp: envelope.timestamp,
|
|
|
|
source: envelope.source,
|
|
|
|
sourceUuid: envelope.sourceUuid,
|
|
|
|
sourceDevice: envelope.sourceDevice,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private unpad(paddedPlaintext: Uint8Array): Uint8Array {
|
|
|
|
for (let i = paddedPlaintext.length - 1; i >= 0; i -= 1) {
|
|
|
|
if (paddedPlaintext[i] === 0x80) {
|
|
|
|
return new Uint8Array(paddedPlaintext.slice(0, i));
|
|
|
|
}
|
|
|
|
if (paddedPlaintext[i] !== 0x00) {
|
|
|
|
throw new Error('Invalid padding');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paddedPlaintext;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async decryptSealedSender(
|
|
|
|
{ sessionStore, identityKeyStore, zone }: LockedStores,
|
|
|
|
envelope: UnsealedEnvelope,
|
|
|
|
ciphertext: Uint8Array
|
|
|
|
): Promise<DecryptSealedSenderResult> {
|
2021-07-28 21:37:09 +00:00
|
|
|
const localE164 = this.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid();
|
2021-07-27 19:55:39 +00:00
|
|
|
const localDeviceId = parseIntOrThrow(
|
2021-07-28 21:37:09 +00:00
|
|
|
this.storage.user.getDeviceId(),
|
2021-07-27 19:55:39 +00:00
|
|
|
'MessageReceiver.decryptSealedSender: localDeviceId'
|
|
|
|
);
|
|
|
|
|
|
|
|
const logId = this.getEnvelopeId(envelope);
|
|
|
|
|
|
|
|
const { unsealedContent: messageContent, certificate } = envelope;
|
|
|
|
strictAssert(
|
|
|
|
messageContent !== undefined,
|
|
|
|
'Missing message content for sealed sender message'
|
|
|
|
);
|
|
|
|
strictAssert(
|
|
|
|
certificate !== undefined,
|
|
|
|
'Missing sender certificate for sealed sender message'
|
|
|
|
);
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const unidentifiedSenderTypeEnum =
|
|
|
|
Proto.UnidentifiedSenderMessage.Message.Type;
|
|
|
|
|
|
|
|
if (
|
|
|
|
messageContent.msgType() === unidentifiedSenderTypeEnum.PLAINTEXT_CONTENT
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-27 19:55:39 +00:00
|
|
|
`MessageReceiver.decryptSealedSender(${logId}): ` +
|
|
|
|
'unidentified message/plaintext contents'
|
2021-07-09 19:36:10 +00:00
|
|
|
);
|
|
|
|
const plaintextContent = PlaintextContent.deserialize(
|
|
|
|
messageContent.contents()
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2021-07-27 19:55:39 +00:00
|
|
|
plaintext: plaintextContent.body(),
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
messageContent.msgType() === unidentifiedSenderTypeEnum.SENDERKEY_MESSAGE
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-27 19:55:39 +00:00
|
|
|
`MessageReceiver.decryptSealedSender(${logId}): ` +
|
|
|
|
'unidentified message/sender key contents'
|
2021-07-09 19:36:10 +00:00
|
|
|
);
|
|
|
|
const sealedSenderIdentifier = certificate.senderUuid();
|
|
|
|
const sealedSenderSourceDevice = certificate.senderDeviceId();
|
2021-09-10 02:38:11 +00:00
|
|
|
const senderKeyStore = new SenderKeys({ ourUuid });
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
Address.create(sealedSenderIdentifier, sealedSenderSourceDevice)
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const plaintext = await this.storage.protocol.enqueueSenderKeyJob(
|
2021-07-27 19:55:39 +00:00
|
|
|
address,
|
|
|
|
() =>
|
|
|
|
groupDecrypt(
|
|
|
|
ProtocolAddress.new(
|
|
|
|
sealedSenderIdentifier,
|
|
|
|
sealedSenderSourceDevice
|
2021-07-09 19:36:10 +00:00
|
|
|
),
|
2021-07-27 19:55:39 +00:00
|
|
|
senderKeyStore,
|
|
|
|
messageContent.contents()
|
|
|
|
),
|
|
|
|
zone
|
|
|
|
);
|
|
|
|
return { plaintext };
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-27 19:55:39 +00:00
|
|
|
`MessageReceiver.decryptSealedSender(${logId}): ` +
|
|
|
|
'unidentified message/passing to sealedSenderDecryptMessage'
|
2021-07-09 19:36:10 +00:00
|
|
|
);
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const preKeyStore = new PreKeys({ ourUuid });
|
|
|
|
const signedPreKeyStore = new SignedPreKeys({ ourUuid });
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const sealedSenderIdentifier = envelope.sourceUuid;
|
|
|
|
strictAssert(
|
|
|
|
sealedSenderIdentifier !== undefined,
|
|
|
|
'Empty sealed sender identifier'
|
|
|
|
);
|
|
|
|
strictAssert(
|
|
|
|
envelope.sourceDevice !== undefined,
|
|
|
|
'Empty sealed sender device'
|
|
|
|
);
|
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
Address.create(sealedSenderIdentifier, envelope.sourceDevice)
|
|
|
|
);
|
2021-07-28 21:37:09 +00:00
|
|
|
const unsealedPlaintext = await this.storage.protocol.enqueueSessionJob(
|
2021-07-27 19:55:39 +00:00
|
|
|
address,
|
|
|
|
() =>
|
|
|
|
sealedSenderDecryptMessage(
|
|
|
|
Buffer.from(ciphertext),
|
|
|
|
PublicKey.deserialize(Buffer.from(this.serverTrustRoot)),
|
|
|
|
envelope.serverTimestamp,
|
|
|
|
localE164 || null,
|
2021-09-10 02:38:11 +00:00
|
|
|
ourUuid.toString(),
|
2021-07-27 19:55:39 +00:00
|
|
|
localDeviceId,
|
|
|
|
sessionStore,
|
|
|
|
identityKeyStore,
|
|
|
|
preKeyStore,
|
|
|
|
signedPreKeyStore
|
|
|
|
),
|
|
|
|
zone
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
return { unsealedPlaintext };
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async innerDecrypt(
|
|
|
|
stores: LockedStores,
|
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
ciphertext: Uint8Array
|
2021-07-27 19:55:39 +00:00
|
|
|
): Promise<Uint8Array> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { sessionStore, identityKeyStore, zone } = stores;
|
|
|
|
|
|
|
|
const logId = this.getEnvelopeId(envelope);
|
|
|
|
const envelopeTypeEnum = Proto.Envelope.Type;
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const identifier = envelope.sourceUuid;
|
2021-07-09 19:36:10 +00:00
|
|
|
const { sourceDevice } = envelope;
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid();
|
|
|
|
const preKeyStore = new PreKeys({ ourUuid });
|
|
|
|
const signedPreKeyStore = new SignedPreKeys({ ourUuid });
|
|
|
|
|
|
|
|
strictAssert(identifier !== undefined, 'Empty identifier');
|
|
|
|
strictAssert(sourceDevice !== undefined, 'Empty source device');
|
|
|
|
|
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
Address.create(identifier, sourceDevice)
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
if (envelope.type === envelopeTypeEnum.PLAINTEXT_CONTENT) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`decrypt/${logId}: plaintext message`);
|
2021-07-09 19:36:10 +00:00
|
|
|
const buffer = Buffer.from(ciphertext);
|
2021-05-28 19:11:19 +00:00
|
|
|
const plaintextContent = PlaintextContent.deserialize(buffer);
|
|
|
|
|
2021-07-27 19:55:39 +00:00
|
|
|
return this.unpad(plaintextContent.body());
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
if (envelope.type === envelopeTypeEnum.CIPHERTEXT) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`decrypt/${logId}: ciphertext message`);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identifier) {
|
|
|
|
throw new Error(
|
2021-07-09 19:36:10 +00:00
|
|
|
'MessageReceiver.innerDecrypt: No identifier for CIPHERTEXT message'
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!sourceDevice) {
|
|
|
|
throw new Error(
|
2021-07-09 19:36:10 +00:00
|
|
|
'MessageReceiver.innerDecrypt: No sourceDevice for CIPHERTEXT message'
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
const signalMessage = SignalMessage.deserialize(Buffer.from(ciphertext));
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const plaintext = await this.storage.protocol.enqueueSessionJob(
|
2021-07-15 19:13:48 +00:00
|
|
|
address,
|
|
|
|
async () =>
|
|
|
|
this.unpad(
|
|
|
|
await signalDecrypt(
|
|
|
|
signalMessage,
|
|
|
|
ProtocolAddress.new(identifier, sourceDevice),
|
|
|
|
sessionStore,
|
|
|
|
identityKeyStore
|
|
|
|
)
|
|
|
|
),
|
|
|
|
zone
|
|
|
|
);
|
2021-07-27 19:55:39 +00:00
|
|
|
return plaintext;
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
if (envelope.type === envelopeTypeEnum.PREKEY_BUNDLE) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`decrypt/${logId}: prekey message`);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identifier) {
|
|
|
|
throw new Error(
|
2021-07-09 19:36:10 +00:00
|
|
|
'MessageReceiver.innerDecrypt: No identifier for PREKEY_BUNDLE message'
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!sourceDevice) {
|
|
|
|
throw new Error(
|
2021-07-09 19:36:10 +00:00
|
|
|
'MessageReceiver.innerDecrypt: No sourceDevice for PREKEY_BUNDLE message'
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const preKeySignalMessage = PreKeySignalMessage.deserialize(
|
2021-07-09 19:36:10 +00:00
|
|
|
Buffer.from(ciphertext)
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
2015-09-01 23:59:06 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const plaintext = await this.storage.protocol.enqueueSessionJob(
|
2021-07-15 19:13:48 +00:00
|
|
|
address,
|
|
|
|
async () =>
|
|
|
|
this.unpad(
|
|
|
|
await signalDecryptPreKey(
|
|
|
|
preKeySignalMessage,
|
|
|
|
ProtocolAddress.new(identifier, sourceDevice),
|
|
|
|
sessionStore,
|
|
|
|
identityKeyStore,
|
|
|
|
preKeyStore,
|
|
|
|
signedPreKeyStore
|
|
|
|
)
|
|
|
|
),
|
|
|
|
zone
|
|
|
|
);
|
2021-07-27 19:55:39 +00:00
|
|
|
return plaintext;
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
if (envelope.type === envelopeTypeEnum.UNIDENTIFIED_SENDER) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`decrypt/${logId}: unidentified message`);
|
2021-07-27 19:55:39 +00:00
|
|
|
const { plaintext, unsealedPlaintext } = await this.decryptSealedSender(
|
2021-07-15 19:13:48 +00:00
|
|
|
stores,
|
|
|
|
envelope,
|
|
|
|
ciphertext
|
|
|
|
);
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (plaintext) {
|
2021-07-27 19:55:39 +00:00
|
|
|
return this.unpad(plaintext);
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2019-02-13 20:04:45 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (unsealedPlaintext) {
|
|
|
|
const content = unsealedPlaintext.message();
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
throw new Error(
|
2021-07-09 19:36:10 +00:00
|
|
|
'MessageReceiver.innerDecrypt: Content returned was falsey!'
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return just the content because that matches the signature of the other
|
|
|
|
// decrypt methods used above.
|
2021-07-27 19:55:39 +00:00
|
|
|
return this.unpad(content);
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
throw new Error('Unexpected lack of plaintext from unidentified sender');
|
|
|
|
}
|
|
|
|
throw new Error('Unknown message type');
|
|
|
|
}
|
2021-02-18 16:40:26 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async decrypt(
|
2021-07-09 19:36:10 +00:00
|
|
|
stores: LockedStores,
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope,
|
2021-07-09 19:36:10 +00:00
|
|
|
ciphertext: Uint8Array
|
2021-07-27 19:55:39 +00:00
|
|
|
): Promise<Uint8Array | undefined> {
|
2021-07-09 19:36:10 +00:00
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
return await this.innerDecrypt(stores, envelope, ciphertext);
|
2021-07-09 19:36:10 +00:00
|
|
|
} catch (error) {
|
2021-07-27 19:55:39 +00:00
|
|
|
const uuid = envelope.sourceUuid;
|
|
|
|
const deviceId = envelope.sourceDevice;
|
2021-02-08 22:19:35 +00:00
|
|
|
|
2021-08-17 18:40:55 +00:00
|
|
|
// We don't do anything if it's just a duplicated message
|
2021-07-09 19:36:10 +00:00
|
|
|
if (
|
|
|
|
error?.message?.includes &&
|
|
|
|
error.message.includes('message with old counter')
|
|
|
|
) {
|
2021-10-20 21:50:00 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-02-18 16:40:26 +00:00
|
|
|
throw error;
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't do a light session reset if it's an error with the sealed sender
|
|
|
|
// wrapper, since we don't trust the sender information.
|
|
|
|
if (
|
|
|
|
error?.message?.includes &&
|
|
|
|
error.message.includes('trust root validation failed')
|
|
|
|
) {
|
2021-10-20 21:50:00 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-07-09 19:36:10 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:40:55 +00:00
|
|
|
if (
|
|
|
|
(envelope.source && this.isBlocked(envelope.source)) ||
|
|
|
|
(envelope.sourceUuid && this.isUuidBlocked(envelope.sourceUuid))
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-17 18:40:55 +00:00
|
|
|
'MessageReceiver.decrypt: Error from blocked sender; no further processing'
|
|
|
|
);
|
2021-10-20 21:50:00 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-08-17 18:40:55 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (uuid && deviceId) {
|
2021-07-27 19:55:39 +00:00
|
|
|
const { usmc } = envelope;
|
2021-10-20 21:50:00 +00:00
|
|
|
const event = new DecryptionErrorEvent(
|
|
|
|
{
|
|
|
|
cipherTextBytes: usmc ? usmc.contents() : undefined,
|
|
|
|
cipherTextType: usmc ? usmc.msgType() : undefined,
|
|
|
|
contentHint: envelope.contentHint,
|
|
|
|
groupId: envelope.groupId,
|
|
|
|
receivedAtCounter: envelope.receivedAtCounter,
|
|
|
|
receivedAtDate: envelope.receivedAtDate,
|
|
|
|
senderDevice: deviceId,
|
|
|
|
senderUuid: uuid,
|
|
|
|
timestamp: envelope.timestamp,
|
|
|
|
},
|
|
|
|
() => this.removeFromCache(envelope)
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
// Avoid deadlocks by scheduling processing on decrypted queue
|
2021-07-15 23:48:09 +00:00
|
|
|
this.addToQueue(
|
|
|
|
async () => this.dispatchEvent(event),
|
|
|
|
TaskType.Decrypted
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
} else {
|
2021-07-27 19:55:39 +00:00
|
|
|
const envelopeId = this.getEnvelopeId(envelope);
|
2021-10-20 21:50:00 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-07-09 19:36:10 +00:00
|
|
|
`MessageReceiver.decrypt: Envelope ${envelopeId} missing uuid or deviceId`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2021-02-18 16:40:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleSentMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
sentContainer: ProcessedSent
|
2020-04-13 17:37:29 +00:00
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.handleSentMessage', this.getEnvelopeId(envelope));
|
2018-10-18 01:01:21 +00:00
|
|
|
const {
|
|
|
|
destination,
|
2020-07-10 18:28:49 +00:00
|
|
|
destinationUuid,
|
2018-10-18 01:01:21 +00:00
|
|
|
timestamp,
|
2019-05-09 15:38:05 +00:00
|
|
|
message: msg,
|
2018-10-18 01:01:21 +00:00
|
|
|
expirationStartTimestamp,
|
|
|
|
unidentifiedStatus,
|
2019-05-09 15:38:05 +00:00
|
|
|
isRecipientUpdate,
|
2018-10-18 01:01:21 +00:00
|
|
|
} = sentContainer;
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
if (!msg) {
|
|
|
|
throw new Error('MessageReceiver.handleSentMessage: message was falsey!');
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
let p: Promise<void> = Promise.resolve();
|
2018-04-10 17:23:09 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
2021-07-09 19:36:10 +00:00
|
|
|
if (msg.flags && msg.flags & Proto.DataMessage.Flags.END_SESSION) {
|
2021-09-10 17:17:32 +00:00
|
|
|
if (destinationUuid) {
|
|
|
|
p = this.handleEndSession(new UUID(destinationUuid));
|
|
|
|
} else if (destination) {
|
|
|
|
const theirUuid = UUID.lookup(destination);
|
|
|
|
if (theirUuid) {
|
|
|
|
p = this.handleEndSession(theirUuid);
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`handleSentMessage: uuid not found for ${destination}`);
|
2021-09-10 17:17:32 +00:00
|
|
|
p = Promise.resolve();
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new Error(
|
|
|
|
'MessageReceiver.handleSentMessage: Cannot end session with falsey destination'
|
|
|
|
);
|
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
await p;
|
|
|
|
|
|
|
|
const message = await this.processDecrypted(envelope, msg);
|
|
|
|
const groupId = this.getProcessedGroupId(message);
|
|
|
|
const isBlocked = groupId ? this.isGroupBlocked(groupId) : false;
|
|
|
|
const { source, sourceUuid } = envelope;
|
2021-07-28 21:37:09 +00:00
|
|
|
const ourE164 = this.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid().toString();
|
2021-07-09 19:36:10 +00:00
|
|
|
const isMe =
|
|
|
|
(source && ourE164 && source === ourE164) ||
|
|
|
|
(sourceUuid && ourUuid && sourceUuid === ourUuid);
|
|
|
|
const isLeavingGroup = Boolean(
|
|
|
|
!message.groupV2 &&
|
|
|
|
message.group &&
|
|
|
|
message.group.type === Proto.GroupContext.Type.QUIT
|
|
|
|
);
|
2018-09-17 19:18:18 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (groupId && isBlocked && !(isMe && isLeavingGroup)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-09 19:36:10 +00:00
|
|
|
`Message ${this.getEnvelopeId(
|
|
|
|
envelope
|
|
|
|
)} ignored; destined for blocked group`
|
|
|
|
);
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-09-13 19:57:07 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new SentEvent(
|
|
|
|
{
|
|
|
|
destination: dropNull(destination),
|
|
|
|
destinationUuid: dropNull(destinationUuid),
|
|
|
|
timestamp: timestamp ? normalizeNumber(timestamp) : undefined,
|
|
|
|
serverTimestamp: envelope.serverTimestamp,
|
|
|
|
device: envelope.sourceDevice,
|
|
|
|
unidentifiedStatus,
|
|
|
|
message,
|
|
|
|
isRecipientUpdate: Boolean(isRecipientUpdate),
|
|
|
|
receivedAtCounter: envelope.receivedAtCounter,
|
|
|
|
receivedAtDate: envelope.receivedAtDate,
|
|
|
|
expirationStartTimestamp: expirationStartTimestamp
|
|
|
|
? normalizeNumber(expirationStartTimestamp)
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2018-05-02 16:51:22 +00:00
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleDataMessage(
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope,
|
2021-07-09 19:36:10 +00:00
|
|
|
msg: Proto.IDataMessage
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.handleDataMessage', this.getEnvelopeId(envelope));
|
2021-07-23 20:47:03 +00:00
|
|
|
let p: Promise<void> = Promise.resolve();
|
2018-04-10 17:23:09 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
2021-09-10 02:38:11 +00:00
|
|
|
const destination = envelope.sourceUuid;
|
2020-04-13 17:37:29 +00:00
|
|
|
if (!destination) {
|
|
|
|
throw new Error(
|
|
|
|
'MessageReceiver.handleDataMessage: source and sourceUuid were falsey'
|
|
|
|
);
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
if (this.isInvalidGroupData(msg, envelope)) {
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
await this.checkGroupV1Data(msg);
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (msg.flags && msg.flags & Proto.DataMessage.Flags.END_SESSION) {
|
2021-09-10 17:17:32 +00:00
|
|
|
p = this.handleEndSession(new UUID(destination));
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-05-27 21:37:06 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE) {
|
|
|
|
strictAssert(msg.profileKey, 'PROFILE_KEY_UPDATE without profileKey');
|
|
|
|
|
|
|
|
const ev = new ProfileKeyUpdateEvent(
|
|
|
|
{
|
|
|
|
source: envelope.source,
|
|
|
|
sourceUuid: envelope.sourceUuid,
|
|
|
|
profileKey: Bytes.toBase64(msg.profileKey),
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
2020-05-27 21:37:06 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
await p;
|
|
|
|
|
|
|
|
const message = await this.processDecrypted(envelope, msg);
|
|
|
|
const groupId = this.getProcessedGroupId(message);
|
|
|
|
const isBlocked = groupId ? this.isGroupBlocked(groupId) : false;
|
|
|
|
const { source, sourceUuid } = envelope;
|
2021-07-28 21:37:09 +00:00
|
|
|
const ourE164 = this.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid().toString();
|
2021-07-09 19:36:10 +00:00
|
|
|
const isMe =
|
|
|
|
(source && ourE164 && source === ourE164) ||
|
|
|
|
(sourceUuid && ourUuid && sourceUuid === ourUuid);
|
|
|
|
const isLeavingGroup = Boolean(
|
|
|
|
!message.groupV2 &&
|
|
|
|
message.group &&
|
|
|
|
message.group.type === Proto.GroupContext.Type.QUIT
|
|
|
|
);
|
2020-05-27 21:37:06 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (groupId && isBlocked && !(isMe && isLeavingGroup)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-09 19:36:10 +00:00
|
|
|
`Message ${this.getEnvelopeId(
|
|
|
|
envelope
|
|
|
|
)} ignored; destined for blocked group`
|
|
|
|
);
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-09-13 19:57:07 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new MessageEvent(
|
|
|
|
{
|
|
|
|
source: envelope.source,
|
|
|
|
sourceUuid: envelope.sourceUuid,
|
|
|
|
sourceDevice: envelope.sourceDevice,
|
|
|
|
timestamp: envelope.timestamp,
|
|
|
|
serverGuid: envelope.serverGuid,
|
|
|
|
serverTimestamp: envelope.serverTimestamp,
|
|
|
|
unidentifiedDeliveryReceived: Boolean(
|
|
|
|
envelope.unidentifiedDeliveryReceived
|
|
|
|
),
|
|
|
|
message,
|
|
|
|
receivedAtCounter: envelope.receivedAtCounter,
|
|
|
|
receivedAtDate: envelope.receivedAtDate,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2018-05-02 16:51:22 +00:00
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async innerHandleLegacyMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
plaintext: Uint8Array
|
2020-04-13 17:37:29 +00:00
|
|
|
) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const message = Proto.DataMessage.decode(plaintext);
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.handleDataMessage(envelope, message);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async maybeUpdateTimestamp(
|
2021-07-15 23:48:09 +00:00
|
|
|
envelope: ProcessedEnvelope
|
|
|
|
): Promise<ProcessedEnvelope> {
|
|
|
|
const { retryPlaceholders } = window.Signal.Services;
|
|
|
|
if (!retryPlaceholders) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('maybeUpdateTimestamp: retry placeholders not available!');
|
2021-07-15 23:48:09 +00:00
|
|
|
return envelope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { timestamp } = envelope;
|
2021-09-10 02:38:11 +00:00
|
|
|
const identifier = envelope.groupId || envelope.sourceUuid;
|
2021-07-15 23:48:09 +00:00
|
|
|
const conversation = window.ConversationController.get(identifier);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!conversation) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-15 23:48:09 +00:00
|
|
|
`maybeUpdateTimestamp/${timestamp}: No conversation found for identifier ${identifier}`
|
|
|
|
);
|
|
|
|
return envelope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logId = `${conversation.idForLogging()}/${timestamp}`;
|
|
|
|
const item = await retryPlaceholders.findByMessageAndRemove(
|
|
|
|
conversation.id,
|
|
|
|
timestamp
|
|
|
|
);
|
|
|
|
if (item && item.wasOpened) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-15 23:48:09 +00:00
|
|
|
`maybeUpdateTimestamp/${logId}: found retry placeholder, but conversation was opened. No updates made.`
|
|
|
|
);
|
|
|
|
} else if (item) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-15 23:48:09 +00:00
|
|
|
`maybeUpdateTimestamp/${logId}: found retry placeholder. Updating receivedAtCounter/receivedAtDate`
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...envelope,
|
|
|
|
receivedAtCounter: item.receivedAtCounter,
|
|
|
|
receivedAtDate: item.receivedAt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-08-11 00:41:27 +00:00
|
|
|
`maybeUpdateTimestamp/${timestamp}: Failed to process message: ${Errors.toLogFormat(
|
|
|
|
error
|
|
|
|
)}`
|
2021-05-14 01:18:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return envelope;
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async innerHandleContentMessage(
|
2021-07-15 23:48:09 +00:00
|
|
|
incomingEnvelope: ProcessedEnvelope,
|
|
|
|
plaintext: Uint8Array
|
|
|
|
): Promise<void> {
|
|
|
|
const content = Proto.Content.decode(plaintext);
|
|
|
|
const envelope = await this.maybeUpdateTimestamp(incomingEnvelope);
|
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
if (
|
|
|
|
content.decryptionErrorMessage &&
|
2021-07-09 19:36:10 +00:00
|
|
|
Bytes.isNotEmpty(content.decryptionErrorMessage)
|
2021-05-28 19:11:19 +00:00
|
|
|
) {
|
|
|
|
await this.handleDecryptionError(
|
|
|
|
envelope,
|
|
|
|
content.decryptionErrorMessage
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
if (content.syncMessage) {
|
2021-07-09 19:36:10 +00:00
|
|
|
await this.handleSyncMessage(
|
|
|
|
envelope,
|
|
|
|
processSyncMessage(content.syncMessage)
|
|
|
|
);
|
2021-05-14 01:18:43 +00:00
|
|
|
return;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (content.dataMessage) {
|
2021-05-14 01:18:43 +00:00
|
|
|
await this.handleDataMessage(envelope, content.dataMessage);
|
|
|
|
return;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (content.nullMessage) {
|
2021-05-14 01:18:43 +00:00
|
|
|
await this.handleNullMessage(envelope);
|
|
|
|
return;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (content.callingMessage) {
|
2021-05-14 01:18:43 +00:00
|
|
|
await this.handleCallingMessage(envelope, content.callingMessage);
|
|
|
|
return;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (content.receiptMessage) {
|
2021-05-14 01:18:43 +00:00
|
|
|
await this.handleReceiptMessage(envelope, content.receiptMessage);
|
|
|
|
return;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (content.typingMessage) {
|
2021-05-14 01:18:43 +00:00
|
|
|
await this.handleTypingMessage(envelope, content.typingMessage);
|
|
|
|
return;
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2021-05-14 01:18:43 +00:00
|
|
|
|
2018-04-10 17:23:09 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-05-14 01:18:43 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (Bytes.isEmpty(content.senderKeyDistributionMessage)) {
|
2021-05-14 01:18:43 +00:00
|
|
|
throw new Error('Unsupported content message');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleDecryptionError(
|
2021-07-27 19:55:39 +00:00
|
|
|
envelope: UnsealedEnvelope,
|
2021-07-09 19:36:10 +00:00
|
|
|
decryptionError: Uint8Array
|
2021-05-28 19:11:19 +00:00
|
|
|
) {
|
2021-06-01 19:40:09 +00:00
|
|
|
const logId = this.getEnvelopeId(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`handleDecryptionError: ${logId}`);
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const buffer = Buffer.from(decryptionError);
|
2021-05-28 19:11:19 +00:00
|
|
|
const request = DecryptionErrorMessage.deserialize(buffer);
|
|
|
|
|
|
|
|
const { sourceUuid, sourceDevice } = envelope;
|
|
|
|
if (!sourceUuid || !sourceDevice) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(`handleDecryptionError/${logId}: Missing uuid or device!`);
|
2021-10-20 21:50:00 +00:00
|
|
|
this.removeFromCache(envelope);
|
2021-05-28 19:11:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-20 21:50:00 +00:00
|
|
|
const event = new RetryRequestEvent(
|
|
|
|
{
|
|
|
|
groupId: envelope.groupId,
|
|
|
|
requesterDevice: sourceDevice,
|
|
|
|
requesterUuid: sourceUuid,
|
|
|
|
ratchetKey: request.ratchetKey(),
|
|
|
|
senderDevice: request.deviceId(),
|
|
|
|
sentAt: request.timestamp(),
|
|
|
|
},
|
|
|
|
() => this.removeFromCache(envelope)
|
|
|
|
);
|
2021-07-15 23:48:09 +00:00
|
|
|
await this.dispatchEvent(event);
|
2021-05-28 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleSenderKeyDistributionMessage(
|
2021-07-15 23:48:09 +00:00
|
|
|
stores: LockedStores,
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
distributionMessage: Uint8Array
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const envelopeId = this.getEnvelopeId(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`handleSenderKeyDistributionMessage/${envelopeId}`);
|
2021-05-14 01:18:43 +00:00
|
|
|
|
|
|
|
// Note: we don't call removeFromCache here because this message can be combined
|
|
|
|
// with a dataMessage, for example. That processing will dictate cache removal.
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const identifier = envelope.sourceUuid;
|
2021-05-14 01:18:43 +00:00
|
|
|
const { sourceDevice } = envelope;
|
|
|
|
if (!identifier) {
|
|
|
|
throw new Error(
|
|
|
|
`handleSenderKeyDistributionMessage: No identifier for envelope ${envelopeId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!isNumber(sourceDevice)) {
|
|
|
|
throw new Error(
|
|
|
|
`handleSenderKeyDistributionMessage: Missing sourceDevice for envelope ${envelopeId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = ProtocolAddress.new(identifier, sourceDevice);
|
|
|
|
const senderKeyDistributionMessage = SenderKeyDistributionMessage.deserialize(
|
2021-07-09 19:36:10 +00:00
|
|
|
Buffer.from(distributionMessage)
|
2021-05-14 01:18:43 +00:00
|
|
|
);
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid();
|
|
|
|
const senderKeyStore = new SenderKeys({ ourUuid });
|
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
Address.create(identifier, sourceDevice)
|
|
|
|
);
|
2021-05-14 01:18:43 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.protocol.enqueueSenderKeyJob(
|
2021-07-15 23:48:09 +00:00
|
|
|
address,
|
|
|
|
() =>
|
|
|
|
processSenderKeyDistributionMessage(
|
|
|
|
sender,
|
|
|
|
senderKeyDistributionMessage,
|
|
|
|
senderKeyStore
|
|
|
|
),
|
|
|
|
stores.zone
|
2021-05-14 01:18:43 +00:00
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleCallingMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
callingMessage: Proto.ICallingMessage
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2018-04-10 17:23:09 +00:00
|
|
|
this.removeFromCache(envelope);
|
2020-06-04 18:16:19 +00:00
|
|
|
await window.Signal.Services.calling.handleCallingMessage(
|
|
|
|
envelope,
|
|
|
|
callingMessage
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleReceiptMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
receiptMessage: Proto.IReceiptMessage
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2021-07-09 19:36:10 +00:00
|
|
|
strictAssert(receiptMessage.timestamp, 'Receipt message without timestamp');
|
2021-07-27 15:42:25 +00:00
|
|
|
|
|
|
|
let EventClass: typeof DeliveryEvent | typeof ReadEvent | typeof ViewEvent;
|
|
|
|
switch (receiptMessage.type) {
|
|
|
|
case Proto.ReceiptMessage.Type.DELIVERY:
|
|
|
|
EventClass = DeliveryEvent;
|
|
|
|
break;
|
|
|
|
case Proto.ReceiptMessage.Type.READ:
|
|
|
|
EventClass = ReadEvent;
|
|
|
|
break;
|
|
|
|
case Proto.ReceiptMessage.Type.VIEWED:
|
|
|
|
EventClass = ViewEvent;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This can happen if we get a receipt type we don't know about yet, which
|
|
|
|
// is totally fine.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
receiptMessage.timestamp.map(async rawTimestamp => {
|
|
|
|
const ev = new EventClass(
|
2021-07-09 19:36:10 +00:00
|
|
|
{
|
2021-07-27 15:42:25 +00:00
|
|
|
timestamp: normalizeNumber(rawTimestamp),
|
2021-07-09 19:36:10 +00:00
|
|
|
envelopeTimestamp: envelope.timestamp,
|
|
|
|
source: envelope.source,
|
|
|
|
sourceUuid: envelope.sourceUuid,
|
2021-07-15 23:48:09 +00:00
|
|
|
sourceDevice: envelope.sourceDevice,
|
2021-07-09 19:36:10 +00:00
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
2021-07-27 15:42:25 +00:00
|
|
|
await this.dispatchAndWait(ev);
|
|
|
|
})
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleTypingMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
typingMessage: Proto.ITypingMessage
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2018-11-14 19:10:32 +00:00
|
|
|
this.removeFromCache(envelope);
|
|
|
|
|
|
|
|
if (envelope.timestamp && typingMessage.timestamp) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const envelopeTimestamp = envelope.timestamp;
|
|
|
|
const typingTimestamp = normalizeNumber(typingMessage.timestamp);
|
2018-11-14 19:10:32 +00:00
|
|
|
|
|
|
|
if (typingTimestamp !== envelopeTimestamp) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2018-11-14 19:10:32 +00:00
|
|
|
`Typing message envelope timestamp (${envelopeTimestamp}) did not match typing timestamp (${typingTimestamp})`
|
|
|
|
);
|
2021-05-14 01:18:43 +00:00
|
|
|
return;
|
2018-11-14 19:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
strictAssert(
|
|
|
|
envelope.sourceDevice !== undefined,
|
|
|
|
'TypingMessage requires sourceDevice in the envelope'
|
|
|
|
);
|
2018-11-14 19:10:32 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { groupId, timestamp, action } = typingMessage;
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
let groupIdString: string | undefined;
|
|
|
|
let groupV2IdString: string | undefined;
|
|
|
|
if (groupId && groupId.byteLength > 0) {
|
|
|
|
if (groupId.byteLength === GROUPV1_ID_LENGTH) {
|
|
|
|
groupIdString = Bytes.toBinary(groupId);
|
2021-09-24 00:49:05 +00:00
|
|
|
groupV2IdString = this.deriveGroupV2FromV1(groupId);
|
2021-07-09 19:36:10 +00:00
|
|
|
} else if (groupId.byteLength === GROUPV2_ID_LENGTH) {
|
|
|
|
groupV2IdString = Bytes.toBase64(groupId);
|
2020-11-20 17:30:45 +00:00
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error('handleTypingMessage: Received invalid groupId value');
|
2020-11-20 17:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
await this.dispatchEvent(
|
|
|
|
new TypingEvent({
|
|
|
|
sender: envelope.source,
|
|
|
|
senderUuid: envelope.sourceUuid,
|
|
|
|
senderDevice: envelope.sourceDevice,
|
|
|
|
typing: {
|
|
|
|
typingMessage,
|
|
|
|
timestamp: timestamp ? normalizeNumber(timestamp) : Date.now(),
|
|
|
|
started: action === Proto.TypingMessage.Action.STARTED,
|
|
|
|
stopped: action === Proto.TypingMessage.Action.STOPPED,
|
|
|
|
|
|
|
|
groupId: groupIdString,
|
|
|
|
groupV2Id: groupV2IdString,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private handleNullMessage(envelope: ProcessedEnvelope): void {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.handleNullMessage', this.getEnvelopeId(envelope));
|
2018-04-10 17:23:09 +00:00
|
|
|
this.removeFromCache(envelope);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private isInvalidGroupData(
|
2021-07-09 19:36:10 +00:00
|
|
|
message: Proto.IDataMessage,
|
|
|
|
envelope: ProcessedEnvelope
|
2020-11-20 17:30:45 +00:00
|
|
|
): boolean {
|
|
|
|
const { group, groupV2 } = message;
|
|
|
|
|
|
|
|
if (group) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { id } = group;
|
|
|
|
strictAssert(id, 'Group data has no id');
|
2020-11-20 17:30:45 +00:00
|
|
|
const isInvalid = id.byteLength !== GROUPV1_ID_LENGTH;
|
|
|
|
|
|
|
|
if (isInvalid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-11-20 17:30:45 +00:00
|
|
|
'isInvalidGroupData: invalid GroupV1 message from',
|
|
|
|
this.getEnvelopeId(envelope)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupV2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { masterKey } = groupV2;
|
|
|
|
strictAssert(masterKey, 'Group v2 data has no masterKey');
|
2020-11-20 17:30:45 +00:00
|
|
|
const isInvalid = masterKey.byteLength !== MASTER_KEY_LENGTH;
|
|
|
|
|
|
|
|
if (isInvalid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-11-20 17:30:45 +00:00
|
|
|
'isInvalidGroupData: invalid GroupV2 message from',
|
|
|
|
this.getEnvelopeId(envelope)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return isInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
private deriveGroupV2FromV1(groupId: Uint8Array): string {
|
2020-11-20 17:30:45 +00:00
|
|
|
if (groupId.byteLength !== GROUPV1_ID_LENGTH) {
|
|
|
|
throw new Error(
|
|
|
|
`deriveGroupV2FromV1: had id with wrong byteLength: ${groupId.byteLength}`
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const masterKey = deriveMasterKeyFromGroupV1(groupId);
|
|
|
|
const data = deriveGroupFields(masterKey);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-06-22 14:46:42 +00:00
|
|
|
return Bytes.toBase64(data.id);
|
2020-11-20 17:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async checkGroupV1Data(
|
|
|
|
message: Readonly<Proto.IDataMessage>
|
|
|
|
): Promise<void> {
|
2020-11-20 17:30:45 +00:00
|
|
|
const { group } = message;
|
|
|
|
|
|
|
|
if (!group) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!group.id) {
|
|
|
|
throw new Error('deriveGroupV1Data: had falsey id');
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { id } = group;
|
2020-11-20 17:30:45 +00:00
|
|
|
if (id.byteLength !== GROUPV1_ID_LENGTH) {
|
|
|
|
throw new Error(
|
|
|
|
`deriveGroupV1Data: had id with wrong byteLength: ${id.byteLength}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private getProcessedGroupId(
|
|
|
|
message: ProcessedDataMessage
|
|
|
|
): string | undefined {
|
2021-07-09 19:36:10 +00:00
|
|
|
if (message.groupV2) {
|
|
|
|
return message.groupV2.id;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
if (message.group && message.group.id) {
|
|
|
|
return message.group.id;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
return undefined;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private getGroupId(message: Proto.IDataMessage): string | undefined {
|
2020-09-09 02:25:05 +00:00
|
|
|
if (message.groupV2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
strictAssert(message.groupV2.masterKey, 'Missing groupV2.masterKey');
|
|
|
|
const { id } = deriveGroupFields(message.groupV2.masterKey);
|
|
|
|
return Bytes.toBase64(id);
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
if (message.group && message.group.id) {
|
|
|
|
return Bytes.toBinary(message.group.id);
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
return undefined;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private getDestination(sentMessage: Proto.SyncMessage.ISent) {
|
2020-09-09 02:25:05 +00:00
|
|
|
if (sentMessage.message && sentMessage.message.groupV2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
return `groupv2(${this.getGroupId(sentMessage.message)})`;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (sentMessage.message && sentMessage.message.group) {
|
2021-07-09 19:36:10 +00:00
|
|
|
strictAssert(sentMessage.message.group.id, 'group without id');
|
|
|
|
return `group(${this.getGroupId(sentMessage.message)})`;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
return sentMessage.destination || sentMessage.destinationUuid;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleSyncMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
syncMessage: ProcessedSyncMessage
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2021-07-28 21:37:09 +00:00
|
|
|
const ourNumber = this.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = this.storage.user.getCheckedUuid();
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const fromSelfSource = envelope.source && envelope.source === ourNumber;
|
2020-03-05 21:14:58 +00:00
|
|
|
const fromSelfSourceUuid =
|
2021-09-10 02:38:11 +00:00
|
|
|
envelope.sourceUuid && envelope.sourceUuid === ourUuid.toString();
|
2020-03-05 21:14:58 +00:00
|
|
|
if (!fromSelfSource && !fromSelfSourceUuid) {
|
2018-04-10 17:23:09 +00:00
|
|
|
throw new Error('Received sync message from another number');
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const ourDeviceId = this.storage.user.getDeviceId();
|
2020-09-24 21:53:21 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2021-07-28 21:37:09 +00:00
|
|
|
if (envelope.sourceDevice == ourDeviceId) {
|
2018-04-10 17:23:09 +00:00
|
|
|
throw new Error('Received sync message from our own device');
|
|
|
|
}
|
|
|
|
if (syncMessage.sent) {
|
|
|
|
const sentMessage = syncMessage.sent;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
if (!sentMessage || !sentMessage.message) {
|
|
|
|
throw new Error(
|
|
|
|
'MessageReceiver.handleSyncMessage: sync sent message was missing message'
|
|
|
|
);
|
|
|
|
}
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
if (this.isInvalidGroupData(sentMessage.message, envelope)) {
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
await this.checkGroupV1Data(sentMessage.message);
|
|
|
|
|
|
|
|
strictAssert(sentMessage.timestamp, 'sent message without timestamp');
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2018-04-10 17:23:09 +00:00
|
|
|
'sent message to',
|
2020-09-09 02:25:05 +00:00
|
|
|
this.getDestination(sentMessage),
|
2021-07-09 19:36:10 +00:00
|
|
|
normalizeNumber(sentMessage.timestamp),
|
2018-04-10 17:23:09 +00:00
|
|
|
'from',
|
|
|
|
this.getEnvelopeId(envelope)
|
|
|
|
);
|
2019-05-09 15:38:05 +00:00
|
|
|
return this.handleSentMessage(envelope, sentMessage);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.contacts) {
|
2020-04-13 17:37:29 +00:00
|
|
|
this.handleContacts(envelope, syncMessage.contacts);
|
2020-09-24 21:53:21 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (syncMessage.groups) {
|
2020-04-13 17:37:29 +00:00
|
|
|
this.handleGroups(envelope, syncMessage.groups);
|
2020-09-24 21:53:21 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (syncMessage.blocked) {
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.handleBlocked(envelope, syncMessage.blocked);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.request) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Got SyncMessage Request');
|
2020-04-13 17:37:29 +00:00
|
|
|
this.removeFromCache(envelope);
|
2020-09-24 21:53:21 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (syncMessage.read && syncMessage.read.length) {
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.handleRead(envelope, syncMessage.read);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.verified) {
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.handleVerified(envelope, syncMessage.verified);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.configuration) {
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.handleConfiguration(envelope, syncMessage.configuration);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (
|
2019-06-26 19:33:13 +00:00
|
|
|
syncMessage.stickerPackOperation &&
|
|
|
|
syncMessage.stickerPackOperation.length > 0
|
|
|
|
) {
|
2019-05-16 22:32:11 +00:00
|
|
|
return this.handleStickerPackOperation(
|
|
|
|
envelope,
|
|
|
|
syncMessage.stickerPackOperation
|
|
|
|
);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.viewOnceOpen) {
|
2019-08-05 20:53:15 +00:00
|
|
|
return this.handleViewOnceOpen(envelope, syncMessage.viewOnceOpen);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.messageRequestResponse) {
|
2020-05-27 21:37:06 +00:00
|
|
|
return this.handleMessageRequestResponse(
|
|
|
|
envelope,
|
|
|
|
syncMessage.messageRequestResponse
|
|
|
|
);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.fetchLatest) {
|
2020-07-07 00:56:56 +00:00
|
|
|
return this.handleFetchLatest(envelope, syncMessage.fetchLatest);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (syncMessage.keys) {
|
2020-07-07 00:56:56 +00:00
|
|
|
return this.handleKeys(envelope, syncMessage.keys);
|
2018-04-10 17:23:09 +00:00
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
if (syncMessage.viewed && syncMessage.viewed.length) {
|
|
|
|
return this.handleViewed(envelope, syncMessage.viewed);
|
|
|
|
}
|
2019-10-04 17:30:43 +00:00
|
|
|
|
|
|
|
this.removeFromCache(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-04-16 23:13:13 +00:00
|
|
|
`handleSyncMessage/${this.getEnvelopeId(envelope)}: Got empty SyncMessage`
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleConfiguration(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
configuration: Proto.SyncMessage.IConfiguration
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got configuration sync message');
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new ConfigurationEvent(
|
|
|
|
configuration,
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleViewOnceOpen(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
sync: Proto.SyncMessage.IViewOnceOpen
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got view once open sync message');
|
2019-06-26 19:33:13 +00:00
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
const ev = new ViewOnceOpenSyncEvent(
|
2021-07-09 19:36:10 +00:00
|
|
|
{
|
|
|
|
source: dropNull(sync.sender),
|
|
|
|
sourceUuid: sync.senderUuid
|
|
|
|
? normalizeUuid(sync.senderUuid, 'handleViewOnceOpen.senderUuid')
|
|
|
|
: undefined,
|
|
|
|
timestamp: sync.timestamp ? normalizeNumber(sync.timestamp) : undefined,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2020-03-05 21:14:58 +00:00
|
|
|
);
|
|
|
|
|
2019-06-26 19:33:13 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleMessageRequestResponse(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
sync: Proto.SyncMessage.IMessageRequestResponse
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got message request response sync message');
|
2020-05-27 21:37:06 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { groupId } = sync;
|
|
|
|
|
|
|
|
let groupIdString: string | undefined;
|
|
|
|
let groupV2IdString: string | undefined;
|
|
|
|
if (groupId && groupId.byteLength > 0) {
|
|
|
|
if (groupId.byteLength === GROUPV1_ID_LENGTH) {
|
|
|
|
groupIdString = Bytes.toBinary(groupId);
|
2021-09-24 00:49:05 +00:00
|
|
|
groupV2IdString = this.deriveGroupV2FromV1(groupId);
|
2021-07-09 19:36:10 +00:00
|
|
|
} else if (groupId.byteLength === GROUPV2_ID_LENGTH) {
|
|
|
|
groupV2IdString = Bytes.toBase64(groupId);
|
2020-11-20 17:30:45 +00:00
|
|
|
} else {
|
|
|
|
this.removeFromCache(envelope);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error('Received message request with invalid groupId');
|
2020-11-20 17:30:45 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new MessageRequestResponseEvent(
|
|
|
|
{
|
|
|
|
threadE164: dropNull(sync.threadE164),
|
|
|
|
threadUuid: sync.threadUuid
|
|
|
|
? normalizeUuid(
|
|
|
|
sync.threadUuid,
|
|
|
|
'handleMessageRequestResponse.threadUuid'
|
|
|
|
)
|
|
|
|
: undefined,
|
|
|
|
messageRequestResponseType: sync.type,
|
|
|
|
groupId: groupIdString,
|
|
|
|
groupV2Id: groupV2IdString,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2020-05-27 21:37:06 +00:00
|
|
|
);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
|
|
|
return this.dispatchAndWait(ev);
|
2020-07-07 00:56:56 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleFetchLatest(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
sync: Proto.SyncMessage.IFetchLatest
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got fetch latest sync message');
|
2020-07-07 00:56:56 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new FetchLatestEvent(
|
|
|
|
sync.type,
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
2020-07-07 00:56:56 +00:00
|
|
|
|
|
|
|
return this.dispatchAndWait(ev);
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleKeys(
|
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
sync: Proto.SyncMessage.IKeys
|
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got keys sync message');
|
2020-07-07 00:56:56 +00:00
|
|
|
|
|
|
|
if (!sync.storageService) {
|
2020-09-24 21:53:21 +00:00
|
|
|
return undefined;
|
2020-07-07 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new KeysEvent(
|
2021-09-24 00:49:05 +00:00
|
|
|
sync.storageService,
|
2021-07-09 19:36:10 +00:00
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
2020-05-27 21:37:06 +00:00
|
|
|
|
|
|
|
return this.dispatchAndWait(ev);
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleStickerPackOperation(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
operations: Array<Proto.SyncMessage.IStickerPackOperation>
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const ENUM = Proto.SyncMessage.StickerPackOperation.Type;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('got sticker pack operation sync message');
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
const stickerPacks = operations.map(operation => ({
|
|
|
|
id: operation.packId ? Bytes.toHex(operation.packId) : undefined,
|
|
|
|
key: operation.packKey ? Bytes.toBase64(operation.packKey) : undefined,
|
2019-05-16 22:32:11 +00:00
|
|
|
isInstall: operation.type === ENUM.INSTALL,
|
|
|
|
isRemove: operation.type === ENUM.REMOVE,
|
|
|
|
}));
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
const ev = new StickerPackEvent(
|
|
|
|
stickerPacks,
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleVerified(
|
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
verified: Proto.IVerified
|
|
|
|
): Promise<void> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const ev = new VerifiedEvent(
|
|
|
|
{
|
|
|
|
state: verified.state,
|
|
|
|
destination: dropNull(verified.destination),
|
|
|
|
destinationUuid: verified.destinationUuid
|
|
|
|
? normalizeUuid(
|
|
|
|
verified.destinationUuid,
|
|
|
|
'handleVerified.destinationUuid'
|
|
|
|
)
|
|
|
|
: undefined,
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: verified.identityKey ? verified.identityKey : undefined,
|
2021-07-09 19:36:10 +00:00
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2020-03-05 21:14:58 +00:00
|
|
|
);
|
2018-04-10 17:23:09 +00:00
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleRead(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
read: Array<Proto.SyncMessage.IRead>
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.handleRead', this.getEnvelopeId(envelope));
|
2018-04-10 17:23:09 +00:00
|
|
|
const results = [];
|
2021-07-09 19:36:10 +00:00
|
|
|
for (const { timestamp, sender, senderUuid } of read) {
|
|
|
|
const ev = new ReadSyncEvent(
|
|
|
|
{
|
|
|
|
envelopeTimestamp: envelope.timestamp,
|
|
|
|
timestamp: normalizeNumber(dropNull(timestamp)),
|
|
|
|
sender: dropNull(sender),
|
|
|
|
senderUuid: senderUuid
|
|
|
|
? normalizeUuid(senderUuid, 'handleRead.senderUuid')
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
2020-03-05 21:14:58 +00:00
|
|
|
);
|
2018-04-10 17:23:09 +00:00
|
|
|
results.push(this.dispatchAndWait(ev));
|
|
|
|
}
|
2021-05-14 01:18:43 +00:00
|
|
|
await Promise.all(results);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
private async handleViewed(
|
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
viewed: ReadonlyArray<Proto.SyncMessage.IViewed>
|
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('MessageReceiver.handleViewed', this.getEnvelopeId(envelope));
|
2021-08-12 18:15:55 +00:00
|
|
|
await Promise.all(
|
|
|
|
viewed.map(async ({ timestamp, senderE164, senderUuid }) => {
|
|
|
|
const ev = new ViewSyncEvent(
|
|
|
|
{
|
|
|
|
envelopeTimestamp: envelope.timestamp,
|
|
|
|
timestamp: normalizeNumber(dropNull(timestamp)),
|
|
|
|
senderE164: dropNull(senderE164),
|
|
|
|
senderUuid: senderUuid
|
|
|
|
? normalizeUuid(senderUuid, 'handleViewed.senderUuid')
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
this.removeFromCache.bind(this, envelope)
|
|
|
|
);
|
|
|
|
await this.dispatchAndWait(ev);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleContacts(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
contacts: Proto.SyncMessage.IContacts
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('contact sync');
|
2019-01-30 20:15:07 +00:00
|
|
|
const { blob } = contacts;
|
2020-04-13 17:37:29 +00:00
|
|
|
if (!blob) {
|
|
|
|
throw new Error('MessageReceiver.handleContacts: blob field was missing');
|
|
|
|
}
|
2019-01-30 20:15:07 +00:00
|
|
|
|
2019-10-04 17:30:43 +00:00
|
|
|
this.removeFromCache(envelope);
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
// Note: we do not return here because we don't want to block the next message on
|
|
|
|
// this attachment download and a lot of processing of that attachment.
|
2021-07-09 19:36:10 +00:00
|
|
|
const attachmentPointer = await this.handleAttachment(blob);
|
|
|
|
const results = [];
|
|
|
|
const contactBuffer = new ContactBuffer(attachmentPointer.data);
|
|
|
|
let contactDetails = contactBuffer.next();
|
|
|
|
while (contactDetails !== undefined) {
|
|
|
|
const contactEvent = new ContactEvent(contactDetails);
|
|
|
|
results.push(this.dispatchAndWait(contactEvent));
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
contactDetails = contactBuffer.next();
|
|
|
|
}
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const finalEvent = new ContactSyncEvent();
|
|
|
|
results.push(this.dispatchAndWait(finalEvent));
|
2018-04-10 17:23:09 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
await Promise.all(results);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('handleContacts: finished');
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleGroups(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
groups: Proto.SyncMessage.IGroups
|
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('group sync');
|
2019-01-30 20:15:07 +00:00
|
|
|
const { blob } = groups;
|
|
|
|
|
2019-10-04 17:30:43 +00:00
|
|
|
this.removeFromCache(envelope);
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
if (!blob) {
|
|
|
|
throw new Error('MessageReceiver.handleGroups: blob field was missing');
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
// Note: we do not return here because we don't want to block the next message on
|
|
|
|
// this attachment download and a lot of processing of that attachment.
|
2021-07-09 19:36:10 +00:00
|
|
|
const attachmentPointer = await this.handleAttachment(blob);
|
|
|
|
const groupBuffer = new GroupBuffer(attachmentPointer.data);
|
2021-07-23 20:47:03 +00:00
|
|
|
let groupDetails = groupBuffer.next();
|
2021-07-09 19:36:10 +00:00
|
|
|
const promises = [];
|
|
|
|
while (groupDetails) {
|
2021-07-23 20:47:03 +00:00
|
|
|
const { id } = groupDetails;
|
|
|
|
strictAssert(id, 'Group details without id');
|
|
|
|
|
|
|
|
if (id.byteLength !== 16) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-07-23 20:47:03 +00:00
|
|
|
`onGroupReceived: Id was ${id} bytes, expected 16 bytes. Dropping group.`
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ev = new GroupEvent({
|
|
|
|
...groupDetails,
|
|
|
|
id: Bytes.toBinary(id),
|
|
|
|
});
|
2021-07-09 19:36:10 +00:00
|
|
|
const promise = this.dispatchAndWait(ev).catch(e => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error('error processing group', e);
|
2018-04-10 17:23:09 +00:00
|
|
|
});
|
2021-07-09 19:36:10 +00:00
|
|
|
groupDetails = groupBuffer.next();
|
|
|
|
promises.push(promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
const ev = new GroupSyncEvent();
|
|
|
|
return this.dispatchAndWait(ev);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleBlocked(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
blocked: Proto.SyncMessage.IBlocked
|
2021-07-23 20:47:03 +00:00
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Setting these numbers as blocked:', blocked.numbers);
|
2021-06-15 00:09:37 +00:00
|
|
|
if (blocked.numbers) {
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.put('blocked', blocked.numbers);
|
2021-06-15 00:09:37 +00:00
|
|
|
}
|
2020-03-05 21:14:58 +00:00
|
|
|
if (blocked.uuids) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const uuids = blocked.uuids.map((uuid, index) => {
|
|
|
|
return normalizeUuid(uuid, `handleBlocked.uuids.${index}`);
|
|
|
|
});
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Setting these uuids as blocked:', uuids);
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.put('blocked-uuids', uuids);
|
2020-03-05 21:14:58 +00:00
|
|
|
}
|
2018-09-13 19:57:07 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const groupIds = map(blocked.groupIds, groupId => Bytes.toBinary(groupId));
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2018-09-13 19:57:07 +00:00
|
|
|
'Setting these groups as blocked:',
|
|
|
|
groupIds.map(groupId => `group(${groupId})`)
|
|
|
|
);
|
2021-07-28 21:37:09 +00:00
|
|
|
await this.storage.put('blocked-groups', groupIds);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
this.removeFromCache(envelope);
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private isBlocked(number: string): boolean {
|
2021-07-28 21:37:09 +00:00
|
|
|
return this.storage.blocked.isBlocked(number);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private isUuidBlocked(uuid: string): boolean {
|
2021-07-28 21:37:09 +00:00
|
|
|
return this.storage.blocked.isUuidBlocked(uuid);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private isGroupBlocked(groupId: string): boolean {
|
2021-07-28 21:37:09 +00:00
|
|
|
return this.storage.blocked.isGroupBlocked(groupId);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async handleAttachment(
|
2021-07-09 19:36:10 +00:00
|
|
|
attachment: Proto.IAttachmentPointer
|
2021-07-14 23:39:52 +00:00
|
|
|
): Promise<DownloadedAttachmentType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const cleaned = processAttachment(attachment);
|
2021-07-28 21:37:09 +00:00
|
|
|
return downloadAttachment(this.server, cleaned);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-09-10 17:17:32 +00:00
|
|
|
private async handleEndSession(theirUuid: UUID): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`handleEndSession: closing sessions for ${theirUuid.toString()}`);
|
2021-09-10 02:38:11 +00:00
|
|
|
await this.storage.protocol.archiveAllSessions(theirUuid);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 20:47:03 +00:00
|
|
|
private async processDecrypted(
|
2021-07-09 19:36:10 +00:00
|
|
|
envelope: ProcessedEnvelope,
|
|
|
|
decrypted: Proto.IDataMessage
|
|
|
|
): Promise<ProcessedDataMessage> {
|
|
|
|
return processDataMessage(decrypted, envelope.timestamp);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
}
|