From fdbb2bfb36017bc1b8b3fd3413a0e53fe92303b7 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Wed, 2 Feb 2022 13:41:29 -0800 Subject: [PATCH] Normalize UUID in ConversationModel.initialize --- ts/models/conversations.ts | 12 ++++++++++++ ts/test-both/util/normalizeUuid_test.ts | 5 +++-- ts/util/normalizeUuid.ts | 11 +++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 5635090dfe..549d33a81a 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -17,6 +17,7 @@ import type { WhatIsThis, } from '../model-types.d'; import { getInitials } from '../util/getInitials'; +import { normalizeUuid } from '../util/normalizeUuid'; import type { AttachmentType } from '../types/Attachment'; import { isGIF } from '../types/Attachment'; import type { CallHistoryDetailsType } from '../types/Calling'; @@ -259,6 +260,17 @@ export class ConversationModel extends window.Backbone override initialize( attributes: Partial = {} ): void { + const uuid = this.get('uuid'); + const normalizedUuid = + uuid && normalizeUuid(uuid, 'ConversationModel.initialize'); + if (uuid && normalizedUuid !== uuid) { + log.warn( + 'ConversationModel.initialize: normalizing uuid from ' + + `${uuid} to ${normalizedUuid}` + ); + this.set('uuid', normalizedUuid); + } + if (isValidE164(attributes.id, false)) { this.set({ id: UUID.generate().toString(), e164: attributes.id }); } diff --git a/ts/test-both/util/normalizeUuid_test.ts b/ts/test-both/util/normalizeUuid_test.ts index 0f559de03b..af840ccac1 100644 --- a/ts/test-both/util/normalizeUuid_test.ts +++ b/ts/test-both/util/normalizeUuid_test.ts @@ -15,8 +15,9 @@ describe('normalizeUuid', () => { it("throws if passed a string that's not a UUID", () => { assert.throws( - () => normalizeUuid('not-uuid-at-all', 'context 3'), - 'Normalizing invalid uuid: not-uuid-at-all in context "context 3"' + () => normalizeUuid('not-UUID-at-all', 'context 3'), + 'Normalizing invalid uuid: not-UUID-at-all to not-uuid-at-all in ' + + 'context "context 3"' ); }); }); diff --git a/ts/util/normalizeUuid.ts b/ts/util/normalizeUuid.ts index 5cef63511f..ca24ced58e 100644 --- a/ts/util/normalizeUuid.ts +++ b/ts/util/normalizeUuid.ts @@ -1,14 +1,17 @@ // Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only +import type { UUIDStringType } from '../types/UUID'; import { isValidUuid } from '../types/UUID'; import { assert } from './assert'; -export function normalizeUuid(uuid: string, context: string): string { +export function normalizeUuid(uuid: string, context: string): UUIDStringType { + const result = uuid.toLowerCase(); + assert( - isValidUuid(uuid), - `Normalizing invalid uuid: ${uuid} in context "${context}"` + isValidUuid(uuid) && isValidUuid(result), + `Normalizing invalid uuid: ${uuid} to ${result} in context "${context}"` ); - return uuid.toLowerCase(); + return result; }