From 98ea022822825f863f71cf0d4a169ae6e06e1b1d Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:36:02 -0500 Subject: [PATCH] Use profileKey from any incoming DataMessage Co-authored-by: Scott Nonnenberg --- ts/background.ts | 17 +++++++++++----- ts/textsecure/MessageReceiver.ts | 27 +++++++++++++++++--------- ts/textsecure/messageReceiverEvents.ts | 1 + 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ts/background.ts b/ts/background.ts index f1a8c9dbd8..b8b775cd2b 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -205,6 +205,7 @@ import { CallMode } from './types/Calling'; import { queueSyncTasks } from './util/syncTasks'; import { isEnabled } from './RemoteConfig'; import { AttachmentBackupManager } from './jobs/AttachmentBackupManager'; +import { getConversationIdForLogging } from './util/idForLogging'; export function isOverHourIntoPast(timestamp: number): boolean { return isNumber(timestamp) && isOlderThan(timestamp, HOUR); @@ -690,7 +691,7 @@ export async function startApp(): Promise { ); messageReceiver.addEventListener( 'profileKeyUpdate', - queuedEventListener(onProfileKeyUpdate) + queuedEventListener(onProfileKey) ); messageReceiver.addEventListener( 'fetchLatest', @@ -2523,24 +2524,30 @@ export async function startApp(): Promise { drop(message.handleDataMessage(data.message, event.confirm)); } - async function onProfileKeyUpdate({ + async function onProfileKey({ data, + reason, confirm, }: ProfileKeyUpdateEvent): Promise { + const logId = `onProfileKey/${reason}`; const { conversation } = window.ConversationController.maybeMergeContacts({ aci: data.sourceAci, e164: data.source, - reason: 'onProfileKeyUpdate', + reason: logId, }); + const idForLogging = getConversationIdForLogging(conversation.attributes); if (!data.profileKey) { - log.error('onProfileKeyUpdate: missing profileKey', data.profileKey); + log.error( + `${logId}: missing profileKey for ${idForLogging}`, + data.profileKey + ); confirm(); return; } log.info( - 'onProfileKeyUpdate: updating profileKey for', + `${logId}: updating profileKey for ${idForLogging}`, data.sourceAci, data.source ); diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index d5c780ef55..3fd97b6f6b 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -3,7 +3,7 @@ /* eslint-disable no-bitwise */ -import { isBoolean, isNumber, isString, omit } from 'lodash'; +import { isBoolean, isNumber, isString, noop, omit } from 'lodash'; import PQueue from 'p-queue'; import { v4 as getGuid } from 'uuid'; import { existsSync } from 'fs'; @@ -2523,23 +2523,32 @@ export default class MessageReceiver p = this.handleEndSession(envelope, sourceAci); } - if (msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE) { - strictAssert( - msg.profileKey != null && msg.profileKey.length > 0, - 'PROFILE_KEY_UPDATE without profileKey' - ); + const { profileKey } = msg; + const hasProfileKey = profileKey && profileKey.length > 0; + const isProfileKeyUpdate = + msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE; + if (isProfileKeyUpdate) { + strictAssert(hasProfileKey, 'PROFILE_KEY_UPDATE without profileKey'); logUnexpectedUrgentValue(envelope, 'profileKeyUpdate'); + } + if (hasProfileKey) { const ev = new ProfileKeyUpdateEvent( { source: envelope.source, sourceAci, - profileKey: Bytes.toBase64(msg.profileKey), + profileKey: Bytes.toBase64(profileKey), }, - this.removeFromCache.bind(this, envelope) + isProfileKeyUpdate ? 'profileKeyUpdate' : 'profileKeyHarvest', + isProfileKeyUpdate ? this.removeFromCache.bind(this, envelope) : noop ); - return this.dispatchAndWait(logId, ev); + + if (isProfileKeyUpdate) { + return this.dispatchAndWait(logId, ev); + } + + drop(this.dispatchAndWait(logId, ev)); } await p; diff --git a/ts/textsecure/messageReceiverEvents.ts b/ts/textsecure/messageReceiverEvents.ts index c00068508c..b7b6ff792a 100644 --- a/ts/textsecure/messageReceiverEvents.ts +++ b/ts/textsecure/messageReceiverEvents.ts @@ -213,6 +213,7 @@ export type ProfileKeyUpdateData = Readonly<{ export class ProfileKeyUpdateEvent extends ConfirmableEvent { constructor( public readonly data: ProfileKeyUpdateData, + public readonly reason: string, confirm: ConfirmCallback ) { super('profileKeyUpdate', confirm);