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-05-02 17:11:34 +00:00
|
|
|
import { AttachmentSizeError, 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 {
|
|
|
|
deriveMediaIdFromMediaName,
|
|
|
|
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';
|
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 getMediaIdBytes(attachment: ProcessedAttachment): Uint8Array {
|
|
|
|
const mediaName = attachment.backupLocator?.mediaName;
|
|
|
|
strictAssert(mediaName, 'Attachment was missing mediaName');
|
|
|
|
const backupKey = getBackupKey();
|
|
|
|
return deriveMediaIdFromMediaName(backupKey, mediaName);
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
function getMediaIdForBackupTier(attachment: ProcessedAttachment): string {
|
|
|
|
return Bytes.toBase64url(getMediaIdBytes(attachment));
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
function getBackupMediaKeyMaterial(
|
|
|
|
attachment: ProcessedAttachment
|
|
|
|
): BackupMediaKeyMaterialType {
|
|
|
|
const mediaId = getMediaIdBytes(attachment);
|
|
|
|
const backupKey = getBackupKey();
|
|
|
|
return deriveBackupMediaKeyMaterial(backupKey, mediaId);
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
async function getCdnNumberForBackupTier(
|
|
|
|
attachment: ProcessedAttachment
|
|
|
|
): Promise<number> {
|
|
|
|
strictAssert(
|
|
|
|
attachment.backupLocator,
|
|
|
|
'Attachment was missing backupLocator'
|
|
|
|
);
|
|
|
|
const backupCdnNumber = attachment.backupLocator.cdnNumber;
|
|
|
|
// TODO (DESKTOP-6983): get backupNumber by querying for all media
|
|
|
|
return backupCdnNumber || DEFAULT_BACKUP_CDN_NUMBER;
|
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-05-02 17:11:34 +00:00
|
|
|
onlyFromTransitTier?: boolean;
|
|
|
|
logPrefix?: string;
|
2023-10-30 16:24:28 +00:00
|
|
|
}
|
|
|
|
): Promise<AttachmentType> {
|
2024-05-02 17:11:34 +00:00
|
|
|
const logId = `${options?.logPrefix}/downloadAttachmentV2`;
|
|
|
|
|
|
|
|
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-05-02 17:11:34 +00:00
|
|
|
// TODO (DESKTOP-7043): allow downloading from transit tier even if there is a backup
|
|
|
|
// locator (as fallback)
|
|
|
|
const mediaTier = attachment.backupLocator
|
|
|
|
? MediaTier.BACKUP
|
|
|
|
: MediaTier.STANDARD;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
const mediaId = getMediaIdForBackupTier(attachment);
|
|
|
|
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({
|
|
|
|
mediaId,
|
|
|
|
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));
|
2023-11-17 20:02:02 +00:00
|
|
|
const { path, plaintextHash } = 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,
|
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
|
|
|
}
|