2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-07-28 21:37:09 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-02-05 23:17:28 +00:00
|
|
|
import { createWriteStream } from 'fs';
|
2024-03-18 18:38:52 +00:00
|
|
|
import { isNumber } from 'lodash';
|
2023-10-30 16:24:28 +00:00
|
|
|
import type { Readable } from 'stream';
|
|
|
|
import { Transform } from 'stream';
|
|
|
|
import { pipeline } from 'stream/promises';
|
|
|
|
import { ensureFile } from 'fs-extra';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import * as Errors from '../types/errors';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2024-06-14 17:17:04 +00:00
|
|
|
import {
|
|
|
|
AttachmentSizeError,
|
|
|
|
mightBeOnBackupTier,
|
|
|
|
type AttachmentType,
|
|
|
|
} from '../types/Attachment';
|
2021-07-28 21:37:09 +00:00
|
|
|
import * as MIME from '../types/MIME';
|
|
|
|
import * as Bytes from '../Bytes';
|
2024-05-02 17:11:34 +00:00
|
|
|
import {
|
|
|
|
deriveBackupMediaKeyMaterial,
|
|
|
|
type BackupMediaKeyMaterialType,
|
|
|
|
} from '../Crypto';
|
2023-10-30 16:24:28 +00:00
|
|
|
import {
|
|
|
|
decryptAttachmentV2,
|
2024-05-02 17:11:34 +00:00
|
|
|
getAttachmentCiphertextLength,
|
2024-02-05 23:17:28 +00:00
|
|
|
safeUnlinkSync,
|
2024-05-02 17:11:34 +00:00
|
|
|
splitKeys,
|
2023-10-30 16:24:28 +00:00
|
|
|
} from '../AttachmentCrypto';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ProcessedAttachment } from './Types.d';
|
2021-07-28 21:37:09 +00:00
|
|
|
import type { WebAPIType } from './WebAPI';
|
2023-10-30 16:24:28 +00:00
|
|
|
import { createName, getRelativePath } from '../windows/attachments';
|
2024-05-02 17:11:34 +00:00
|
|
|
import { MediaTier } from '../types/AttachmentDownload';
|
|
|
|
import { getBackupKey } from '../services/backups/crypto';
|
|
|
|
import { backupsService } from '../services/backups';
|
2024-05-29 23:46:43 +00:00
|
|
|
import { getMediaIdForAttachment } from '../services/backups/util/mediaId';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
const DEFAULT_BACKUP_CDN_NUMBER = 3;
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
export function getCdnKey(attachment: ProcessedAttachment): string {
|
|
|
|
const cdnKey = attachment.cdnId || attachment.cdnKey;
|
|
|
|
strictAssert(cdnKey, 'Attachment was missing cdnId or cdnKey');
|
|
|
|
return cdnKey;
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
function getBackupMediaKeyMaterial(
|
2024-05-29 23:46:43 +00:00
|
|
|
attachment: AttachmentType
|
2024-05-02 17:11:34 +00:00
|
|
|
): BackupMediaKeyMaterialType {
|
2024-05-29 23:46:43 +00:00
|
|
|
const mediaId = getMediaIdForAttachment(attachment);
|
2024-05-02 17:11:34 +00:00
|
|
|
const backupKey = getBackupKey();
|
2024-05-29 23:46:43 +00:00
|
|
|
return deriveBackupMediaKeyMaterial(backupKey, mediaId.bytes);
|
2024-05-02 17:11:34 +00:00
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-06-14 17:17:04 +00:00
|
|
|
export async function getCdnNumberForBackupTier(
|
2024-05-02 17:11:34 +00:00
|
|
|
attachment: ProcessedAttachment
|
|
|
|
): Promise<number> {
|
|
|
|
strictAssert(
|
|
|
|
attachment.backupLocator,
|
|
|
|
'Attachment was missing backupLocator'
|
|
|
|
);
|
2024-06-14 17:17:04 +00:00
|
|
|
let backupCdnNumber = attachment.backupLocator.cdnNumber;
|
|
|
|
|
|
|
|
if (backupCdnNumber == null) {
|
|
|
|
const mediaId = getMediaIdForAttachment(attachment);
|
|
|
|
const backupCdnInfo = await backupsService.getBackupCdnInfo(mediaId.string);
|
|
|
|
if (backupCdnInfo.isInBackupTier) {
|
|
|
|
backupCdnNumber = backupCdnInfo.cdnNumber;
|
|
|
|
} else {
|
|
|
|
backupCdnNumber = DEFAULT_BACKUP_CDN_NUMBER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return backupCdnNumber;
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
2023-10-30 16:24:28 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
export async function downloadAttachment(
|
2023-10-30 16:24:28 +00:00
|
|
|
server: WebAPIType,
|
|
|
|
attachment: ProcessedAttachment,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
2024-06-14 17:17:04 +00:00
|
|
|
mediaTier?: MediaTier;
|
2024-05-02 17:11:34 +00:00
|
|
|
logPrefix?: string;
|
2023-10-30 16:24:28 +00:00
|
|
|
}
|
|
|
|
): Promise<AttachmentType> {
|
2024-06-14 17:17:04 +00:00
|
|
|
const logId = `${options?.logPrefix}/downloadAttachment`;
|
2024-05-02 17:11:34 +00:00
|
|
|
|
|
|
|
const { digest, key, size, contentType } = attachment;
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
strictAssert(digest, `${logId}: missing digest`);
|
|
|
|
strictAssert(key, `${logId}: missing key`);
|
|
|
|
strictAssert(isNumber(size), `${logId}: missing size`);
|
|
|
|
|
2024-06-14 17:17:04 +00:00
|
|
|
const mediaTier =
|
|
|
|
options?.mediaTier ??
|
|
|
|
(mightBeOnBackupTier(attachment) ? MediaTier.BACKUP : MediaTier.STANDARD);
|
2024-05-02 17:11:34 +00:00
|
|
|
|
|
|
|
let downloadedPath: string;
|
|
|
|
if (mediaTier === MediaTier.STANDARD) {
|
|
|
|
const cdnKey = getCdnKey(attachment);
|
|
|
|
const { cdnNumber } = attachment;
|
|
|
|
|
|
|
|
const downloadStream = await server.getAttachment({
|
|
|
|
cdnKey,
|
|
|
|
cdnNumber,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
downloadedPath = await downloadToDisk({ downloadStream, size });
|
|
|
|
} else {
|
2024-05-29 23:46:43 +00:00
|
|
|
const mediaId = getMediaIdForAttachment(attachment);
|
2024-05-02 17:11:34 +00:00
|
|
|
const cdnNumber = await getCdnNumberForBackupTier(attachment);
|
|
|
|
const cdnCredentials =
|
|
|
|
await backupsService.credentials.getCDNReadCredentials(cdnNumber);
|
|
|
|
|
|
|
|
const backupDir = await backupsService.api.getBackupDir();
|
|
|
|
const mediaDir = await backupsService.api.getMediaDir();
|
|
|
|
|
|
|
|
const downloadStream = await server.getAttachmentFromBackupTier({
|
2024-05-29 23:46:43 +00:00
|
|
|
mediaId: mediaId.string,
|
2024-05-02 17:11:34 +00:00
|
|
|
backupDir,
|
|
|
|
mediaDir,
|
|
|
|
headers: cdnCredentials.headers,
|
|
|
|
cdnNumber,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
downloadedPath = await downloadToDisk({
|
|
|
|
downloadStream,
|
|
|
|
size: getAttachmentCiphertextLength(size),
|
|
|
|
});
|
|
|
|
}
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
const cipherTextAbsolutePath =
|
2024-05-02 17:11:34 +00:00
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath(downloadedPath);
|
2023-10-30 16:24:28 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
const { aesKey, macKey } = splitKeys(Bytes.fromBase64(key));
|
2024-05-29 01:12:14 +00:00
|
|
|
const { path, plaintextHash, iv } = await decryptAttachmentV2({
|
2023-10-30 16:24:28 +00:00
|
|
|
ciphertextPath: cipherTextAbsolutePath,
|
2024-05-02 17:11:34 +00:00
|
|
|
idForLogging: logId,
|
|
|
|
aesKey,
|
|
|
|
macKey,
|
2023-10-30 16:24:28 +00:00
|
|
|
size,
|
|
|
|
theirDigest: Bytes.fromBase64(digest),
|
2024-05-02 17:11:34 +00:00
|
|
|
outerEncryption:
|
|
|
|
mediaTier === 'backup'
|
|
|
|
? getBackupMediaKeyMaterial(attachment)
|
|
|
|
: undefined,
|
2023-10-30 16:24:28 +00:00
|
|
|
});
|
|
|
|
|
2024-02-05 23:17:28 +00:00
|
|
|
safeUnlinkSync(cipherTextAbsolutePath);
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
return {
|
2024-03-18 18:38:52 +00:00
|
|
|
...attachment,
|
2023-11-17 20:02:02 +00:00
|
|
|
path,
|
2023-10-30 16:24:28 +00:00
|
|
|
size,
|
|
|
|
contentType: contentType
|
|
|
|
? MIME.stringToMIMEType(contentType)
|
|
|
|
: MIME.APPLICATION_OCTET_STREAM,
|
2023-11-17 20:02:02 +00:00
|
|
|
plaintextHash,
|
2024-05-29 01:12:14 +00:00
|
|
|
iv: Bytes.toBase64(iv),
|
2023-10-30 16:24:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadToDisk({
|
|
|
|
downloadStream,
|
|
|
|
size,
|
|
|
|
}: {
|
|
|
|
downloadStream: Readable;
|
|
|
|
size: number;
|
|
|
|
}): Promise<string> {
|
|
|
|
const relativeTargetPath = getRelativePath(createName());
|
|
|
|
const absoluteTargetPath =
|
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath(relativeTargetPath);
|
|
|
|
await ensureFile(absoluteTargetPath);
|
|
|
|
const writeStream = createWriteStream(absoluteTargetPath);
|
2024-05-02 17:11:34 +00:00
|
|
|
const targetSize = getAttachmentCiphertextLength(size);
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
try {
|
2024-02-05 23:17:28 +00:00
|
|
|
await pipeline(downloadStream, checkSize(targetSize), writeStream);
|
2023-10-30 16:24:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
try {
|
2024-02-05 23:17:28 +00:00
|
|
|
safeUnlinkSync(absoluteTargetPath);
|
2023-10-30 16:24:28 +00:00
|
|
|
} catch (cleanupError) {
|
|
|
|
log.error(
|
|
|
|
'downloadToDisk: Error while cleaning up',
|
|
|
|
Errors.toLogFormat(cleanupError)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return relativeTargetPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple transform that throws if it sees more than maxBytes on the stream.
|
2024-02-05 23:17:28 +00:00
|
|
|
function checkSize(expectedBytes: number) {
|
|
|
|
let totalBytes = 0;
|
2024-05-02 17:11:34 +00:00
|
|
|
|
|
|
|
// TODO (DESKTOP-7046): remove size buffer
|
|
|
|
const maximumSizeBeforeError = expectedBytes * 1.05;
|
2024-02-05 23:17:28 +00:00
|
|
|
return new Transform({
|
|
|
|
transform(chunk, encoding, callback) {
|
|
|
|
totalBytes += chunk.byteLength;
|
2024-05-02 17:11:34 +00:00
|
|
|
if (totalBytes > maximumSizeBeforeError) {
|
2024-02-05 23:17:28 +00:00
|
|
|
callback(
|
2023-10-30 16:24:28 +00:00
|
|
|
new AttachmentSizeError(
|
2024-05-02 17:11:34 +00:00
|
|
|
`checkSize: Received ${totalBytes} bytes, max is ${maximumSizeBeforeError}`
|
2023-10-30 16:24:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-05-02 17:11:34 +00:00
|
|
|
|
|
|
|
if (totalBytes > expectedBytes) {
|
|
|
|
log.warn(
|
|
|
|
`checkSize: Received ${totalBytes} bytes, expected ${expectedBytes}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:17:28 +00:00
|
|
|
this.push(chunk, encoding);
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
});
|
2023-10-30 16:24:28 +00:00
|
|
|
}
|