Add plaintext hash to existing message attachments

This commit is contained in:
trevor-signal 2024-01-02 15:14:11 -05:00 committed by GitHub
parent e28a07588e
commit dcf52aa619
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 122 additions and 42 deletions

View file

@ -5,7 +5,6 @@ import moment from 'moment';
import {
isNumber,
padStart,
isTypedArray,
isFunction,
isUndefined,
isString,
@ -185,42 +184,6 @@ export type ThumbnailType = Pick<
objectUrl?: string;
};
export async function migrateDataToFileSystem(
attachment: AttachmentType,
{
writeNewAttachmentData,
logger,
}: {
writeNewAttachmentData: (data: Uint8Array) => Promise<string>;
logger: LoggerType;
}
): Promise<AttachmentType> {
if (!isFunction(writeNewAttachmentData)) {
throw new TypeError("'writeNewAttachmentData' must be a function");
}
const { data } = attachment;
const attachmentHasData = !isUndefined(data);
const shouldSkipSchemaUpgrade = !attachmentHasData;
if (shouldSkipSchemaUpgrade) {
return attachment;
}
// This attachment was already broken by a roundtrip to the database - repair it now
if (!isTypedArray(data)) {
logger.warn(
'migrateDataToFileSystem: Attachment had non-array `data` field; deleting.'
);
return omit({ ...attachment }, ['data']);
}
const path = await writeNewAttachmentData(data);
const attachmentWithoutData = omit({ ...attachment, path }, ['data']);
return attachmentWithoutData;
}
// // Incoming message attachment fields
// {
// id: string

View file

@ -15,11 +15,11 @@ import type {
AttachmentType,
AttachmentWithHydratedData,
UploadedAttachmentType,
migrateDataToFileSystem,
} from './Attachment';
import { toLogFormat } from './errors';
import type { LoggerType } from './Logging';
import type { ServiceIdString } from './ServiceId';
import type { migrateDataToFileSystem } from '../util/attachments/migrateDataToFilesystem';
type GenericEmbeddedContactType<AvatarType> = {
name?: Name;

View file

@ -9,7 +9,6 @@ import { autoOrientJPEG } from '../util/attachments';
import {
captureDimensionsAndScreenshot,
hasData,
migrateDataToFileSystem,
removeSchemaVersion,
replaceUnicodeOrderOverrides,
replaceUnicodeV2,
@ -34,6 +33,8 @@ import type {
LinkPreviewWithHydratedData,
} from './message/LinkPreviews';
import type { StickerType, StickerWithHydratedData } from './Stickers';
import { addPlaintextHashToAttachment } from '../AttachmentCrypto';
import { migrateDataToFileSystem } from '../util/attachments/migrateDataToFilesystem';
export { hasExpiration } from './Message';
@ -118,6 +119,8 @@ export type ContextWithMessageType = ContextType & {
// attachment filenames
// Version 10
// - Preview: A new type of attachment can be included in a message.
// Version 11
// - Attachments: add sha256 plaintextHash
const INITIAL_SCHEMA_VERSION = 0;
@ -438,6 +441,11 @@ const toVersion10 = _withSchemaVersion({
},
});
const toVersion11 = _withSchemaVersion({
schemaVersion: 11,
upgrade: _mapAttachments(addPlaintextHashToAttachment),
});
const VERSIONS = [
toVersion0,
toVersion1,
@ -450,6 +458,7 @@ const VERSIONS = [
toVersion8,
toVersion9,
toVersion10,
toVersion11,
];
export const CURRENT_SCHEMA_VERSION = VERSIONS.length - 1;