Normalize UUID in ConversationModel.initialize

Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
This commit is contained in:
automated-signal 2022-02-02 14:17:55 -08:00 committed by GitHub
parent f03cf1ba0e
commit a62075e5c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View File

@ -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<ConversationAttributesType> = {}
): 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 });
}

View File

@ -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"'
);
});
});

View File

@ -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;
}