Repair broken attachments with non-array 'data' fields
This commit is contained in:
parent
a0424be5bb
commit
2f252b8e26
|
@ -9,7 +9,22 @@ import type {
|
||||||
QuotedMessageType,
|
QuotedMessageType,
|
||||||
} from '../model-types.d';
|
} from '../model-types.d';
|
||||||
import type { UUIDStringType } from '../types/UUID';
|
import type { UUIDStringType } from '../types/UUID';
|
||||||
import { isIncoming, isOutgoing, isStory } from '../state/selectors/message';
|
|
||||||
|
export function isIncoming(
|
||||||
|
message: Pick<MessageAttributesType, 'type'>
|
||||||
|
): boolean {
|
||||||
|
return message.type === 'incoming';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isOutgoing(
|
||||||
|
message: Pick<MessageAttributesType, 'type'>
|
||||||
|
): boolean {
|
||||||
|
return message.type === 'outgoing';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isStory(message: Pick<MessageAttributesType, 'type'>): boolean {
|
||||||
|
return message.type === 'story';
|
||||||
|
}
|
||||||
|
|
||||||
export function isQuoteAMatch(
|
export function isQuoteAMatch(
|
||||||
message: MessageAttributesType | null | undefined,
|
message: MessageAttributesType | null | undefined,
|
||||||
|
|
|
@ -2511,6 +2511,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
await Attachment.migrateDataToFileSystem(downloadedAvatar, {
|
await Attachment.migrateDataToFileSystem(downloadedAvatar, {
|
||||||
writeNewAttachmentData:
|
writeNewAttachmentData:
|
||||||
window.Signal.Migrations.writeNewAttachmentData,
|
window.Signal.Migrations.writeNewAttachmentData,
|
||||||
|
logger: log,
|
||||||
});
|
});
|
||||||
avatar = {
|
avatar = {
|
||||||
...onDiskAttachment,
|
...onDiskAttachment,
|
||||||
|
|
|
@ -18,6 +18,7 @@ import {
|
||||||
get,
|
get,
|
||||||
groupBy,
|
groupBy,
|
||||||
isFunction,
|
isFunction,
|
||||||
|
isTypedArray,
|
||||||
last,
|
last,
|
||||||
map,
|
map,
|
||||||
omit,
|
omit,
|
||||||
|
@ -90,6 +91,7 @@ import type {
|
||||||
import Server from './Server';
|
import Server from './Server';
|
||||||
import { isCorruptionError } from './errors';
|
import { isCorruptionError } from './errors';
|
||||||
import { MINUTE } from '../util/durations';
|
import { MINUTE } from '../util/durations';
|
||||||
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
||||||
|
|
||||||
// We listen to a lot of events on ipc, often on the same channel. This prevents
|
// We listen to a lot of events on ipc, often on the same channel. This prevents
|
||||||
// any warnings that might be sent to the console in that case.
|
// any warnings that might be sent to the console in that case.
|
||||||
|
@ -433,12 +435,25 @@ function _cleanData(
|
||||||
return cleaned;
|
return cleaned;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _cleanMessageData(data: MessageType): MessageType {
|
export function _cleanMessageData(data: MessageType): MessageType {
|
||||||
// Ensure that all messages have the received_at set properly
|
// Ensure that all messages have the received_at set properly
|
||||||
if (!data.received_at) {
|
if (!data.received_at) {
|
||||||
assert(false, 'received_at was not set on the message');
|
assert(false, 'received_at was not set on the message');
|
||||||
data.received_at = window.Signal.Util.incrementMessageCounter();
|
data.received_at = window.Signal.Util.incrementMessageCounter();
|
||||||
}
|
}
|
||||||
|
if (data.attachments) {
|
||||||
|
const logId = getMessageIdForLogging(data);
|
||||||
|
data.attachments = data.attachments.map((attachment, index) => {
|
||||||
|
if (attachment.data && !isTypedArray(attachment.data)) {
|
||||||
|
log.warn(
|
||||||
|
`_cleanMessageData/${logId}: Attachment ${index} had non-array \`data\` field; deleting.`
|
||||||
|
);
|
||||||
|
return omit(attachment, ['data']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return attachment;
|
||||||
|
});
|
||||||
|
}
|
||||||
return _cleanData(omit(data, ['dataMessage']));
|
return _cleanData(omit(data, ['dataMessage']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,9 @@ import * as log from '../../logging/log';
|
||||||
import { getConversationColorAttributes } from '../../util/getConversationColorAttributes';
|
import { getConversationColorAttributes } from '../../util/getConversationColorAttributes';
|
||||||
import { DAY, HOUR } from '../../util/durations';
|
import { DAY, HOUR } from '../../util/durations';
|
||||||
import { getStoryReplyText } from '../../util/getStoryReplyText';
|
import { getStoryReplyText } from '../../util/getStoryReplyText';
|
||||||
|
import { isIncoming, isOutgoing, isStory } from '../../messages/helpers';
|
||||||
|
|
||||||
|
export { isIncoming, isOutgoing, isStory };
|
||||||
|
|
||||||
const THREE_HOURS = 3 * HOUR;
|
const THREE_HOURS = 3 * HOUR;
|
||||||
const linkify = LinkifyIt();
|
const linkify = LinkifyIt();
|
||||||
|
@ -129,24 +132,6 @@ export type GetPropsForBubbleOptions = Readonly<{
|
||||||
contactNameColorSelector: ContactNameColorSelectorType;
|
contactNameColorSelector: ContactNameColorSelectorType;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export function isIncoming(
|
|
||||||
message: Pick<MessageWithUIFieldsType, 'type'>
|
|
||||||
): boolean {
|
|
||||||
return message.type === 'incoming';
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isOutgoing(
|
|
||||||
message: Pick<MessageWithUIFieldsType, 'type'>
|
|
||||||
): boolean {
|
|
||||||
return message.type === 'outgoing';
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isStory(
|
|
||||||
message: Pick<MessageWithUIFieldsType, 'type'>
|
|
||||||
): boolean {
|
|
||||||
return message.type === 'story';
|
|
||||||
}
|
|
||||||
|
|
||||||
export function hasErrors(
|
export function hasErrors(
|
||||||
message: Pick<MessageWithUIFieldsType, 'errors'>
|
message: Pick<MessageWithUIFieldsType, 'errors'>
|
||||||
): boolean {
|
): boolean {
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
// Copyright 2021 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import { assert } from 'chai';
|
||||||
|
|
||||||
|
import { _cleanMessageData } from '../../sql/Client';
|
||||||
|
import { IMAGE_GIF } from '../../types/MIME';
|
||||||
|
|
||||||
|
describe('_cleanMessageData', () => {
|
||||||
|
it('throws if message is missing received_at', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
_cleanMessageData({} as any);
|
||||||
|
}, 'received_at');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes `data` field in attachment if it is not a typed array', () => {
|
||||||
|
const data = new Uint8Array([1, 2, 3]);
|
||||||
|
const message = {
|
||||||
|
id: 'something',
|
||||||
|
type: 'incoming' as const,
|
||||||
|
sent_at: Date.now(),
|
||||||
|
conversationId: 'conversation-id',
|
||||||
|
received_at: Date.now(),
|
||||||
|
timestamp: Date.now(),
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
contentType: IMAGE_GIF,
|
||||||
|
size: 4,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
data: 1 as any,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: IMAGE_GIF,
|
||||||
|
size: 4,
|
||||||
|
data: {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
size: 4,
|
||||||
|
contentType: IMAGE_GIF,
|
||||||
|
data,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const actual = _cleanMessageData(message);
|
||||||
|
|
||||||
|
assert.isUndefined(actual.attachments?.[0].data);
|
||||||
|
assert.isUndefined(actual.attachments?.[1].data);
|
||||||
|
assert.strictEqual(actual.attachments?.[2].data, data);
|
||||||
|
});
|
||||||
|
});
|
|
@ -396,6 +396,7 @@ describe('Attachment', () => {
|
||||||
|
|
||||||
const actual = await Attachment.migrateDataToFileSystem(input, {
|
const actual = await Attachment.migrateDataToFileSystem(input, {
|
||||||
writeNewAttachmentData,
|
writeNewAttachmentData,
|
||||||
|
logger,
|
||||||
});
|
});
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
@ -417,11 +418,12 @@ describe('Attachment', () => {
|
||||||
|
|
||||||
const actual = await Attachment.migrateDataToFileSystem(input, {
|
const actual = await Attachment.migrateDataToFileSystem(input, {
|
||||||
writeNewAttachmentData,
|
writeNewAttachmentData,
|
||||||
|
logger,
|
||||||
});
|
});
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw error if data is not valid', async () => {
|
it('should clear `data` field if it is not a typed array', async () => {
|
||||||
const input = {
|
const input = {
|
||||||
contentType: MIME.IMAGE_JPEG,
|
contentType: MIME.IMAGE_JPEG,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
@ -432,12 +434,12 @@ describe('Attachment', () => {
|
||||||
|
|
||||||
const writeNewAttachmentData = async () => 'abc/abcdefgh123456789';
|
const writeNewAttachmentData = async () => 'abc/abcdefgh123456789';
|
||||||
|
|
||||||
await assert.isRejected(
|
const actual = await Attachment.migrateDataToFileSystem(input, {
|
||||||
Attachment.migrateDataToFileSystem(input, {
|
writeNewAttachmentData,
|
||||||
writeNewAttachmentData,
|
logger,
|
||||||
}),
|
});
|
||||||
'Expected `attachment.data` to be a typed array; got: number'
|
|
||||||
);
|
assert.isUndefined(actual.data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import { assert } from 'chai';
|
import { assert } from 'chai';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
|
|
||||||
|
import * as logger from '../../logging/log';
|
||||||
import { IMAGE_GIF, IMAGE_PNG } from '../../types/MIME';
|
import { IMAGE_GIF, IMAGE_PNG } from '../../types/MIME';
|
||||||
import type { MessageAttributesType } from '../../model-types.d';
|
import type { MessageAttributesType } from '../../model-types.d';
|
||||||
import type { Avatar, Email, Phone } from '../../types/EmbeddedContact';
|
import type { Avatar, Email, Phone } from '../../types/EmbeddedContact';
|
||||||
|
@ -18,9 +19,6 @@ import { UUID } from '../../types/UUID';
|
||||||
|
|
||||||
describe('Contact', () => {
|
describe('Contact', () => {
|
||||||
const NUMBER = '+12025550099';
|
const NUMBER = '+12025550099';
|
||||||
const logger = {
|
|
||||||
error: () => undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const writeNewAttachmentData = sinon
|
const writeNewAttachmentData = sinon
|
||||||
.stub()
|
.stub()
|
||||||
|
|
|
@ -180,8 +180,10 @@ export async function migrateDataToFileSystem(
|
||||||
attachment: AttachmentType,
|
attachment: AttachmentType,
|
||||||
{
|
{
|
||||||
writeNewAttachmentData,
|
writeNewAttachmentData,
|
||||||
|
logger,
|
||||||
}: {
|
}: {
|
||||||
writeNewAttachmentData: (data: Uint8Array) => Promise<string>;
|
writeNewAttachmentData: (data: Uint8Array) => Promise<string>;
|
||||||
|
logger: LoggerType;
|
||||||
}
|
}
|
||||||
): Promise<AttachmentType> {
|
): Promise<AttachmentType> {
|
||||||
if (!isFunction(writeNewAttachmentData)) {
|
if (!isFunction(writeNewAttachmentData)) {
|
||||||
|
@ -195,11 +197,12 @@ export async function migrateDataToFileSystem(
|
||||||
return attachment;
|
return attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This attachment was already broken by a roundtrip to the database - repair it now
|
||||||
if (!isTypedArray(data)) {
|
if (!isTypedArray(data)) {
|
||||||
throw new TypeError(
|
logger.warn(
|
||||||
'Expected `attachment.data` to be a typed array;' +
|
'migrateDataToFileSystem: Attachment had non-array `data` field; deleting.'
|
||||||
` got: ${typeof attachment.data}`
|
|
||||||
);
|
);
|
||||||
|
return omit({ ...attachment }, ['data']);
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = await writeNewAttachmentData(data);
|
const path = await writeNewAttachmentData(data);
|
||||||
|
|
|
@ -192,7 +192,7 @@ export function parseAndWriteAvatar(
|
||||||
context: {
|
context: {
|
||||||
message: MessageAttributesType;
|
message: MessageAttributesType;
|
||||||
getRegionCode: () => string | undefined;
|
getRegionCode: () => string | undefined;
|
||||||
logger: Pick<LoggerType, 'error'>;
|
logger: LoggerType;
|
||||||
writeNewAttachmentData: (data: Uint8Array) => Promise<string>;
|
writeNewAttachmentData: (data: Uint8Array) => Promise<string>;
|
||||||
}
|
}
|
||||||
): Promise<EmbeddedContactType> => {
|
): Promise<EmbeddedContactType> => {
|
||||||
|
|
|
@ -589,6 +589,7 @@ export const processNewAttachment = async (
|
||||||
);
|
);
|
||||||
const onDiskAttachment = await migrateDataToFileSystem(rotatedAttachment, {
|
const onDiskAttachment = await migrateDataToFileSystem(rotatedAttachment, {
|
||||||
writeNewAttachmentData,
|
writeNewAttachmentData,
|
||||||
|
logger,
|
||||||
});
|
});
|
||||||
const finalAttachment = await captureDimensionsAndScreenshot(
|
const finalAttachment = await captureDimensionsAndScreenshot(
|
||||||
onDiskAttachment,
|
onDiskAttachment,
|
||||||
|
|
Loading…
Reference in New Issue