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';
|
|
|
|
import { dropNull } from '../util/dropNull';
|
2023-10-30 16:24:28 +00:00
|
|
|
import {
|
|
|
|
AttachmentSizeError,
|
|
|
|
type AttachmentType,
|
|
|
|
type DownloadedAttachmentType,
|
|
|
|
} from '../types/Attachment';
|
2021-07-28 21:37:09 +00:00
|
|
|
import * as MIME from '../types/MIME';
|
|
|
|
import * as Bytes from '../Bytes';
|
2024-02-05 23:17:28 +00:00
|
|
|
import { getFirstBytes, decryptAttachmentV1 } from '../Crypto';
|
2023-10-30 16:24:28 +00:00
|
|
|
import {
|
|
|
|
decryptAttachmentV2,
|
2024-02-05 23:17:28 +00:00
|
|
|
getAttachmentDownloadSize,
|
|
|
|
safeUnlinkSync,
|
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-03-21 20:02:12 +00:00
|
|
|
import { redactCdnKey } from '../util/privacy';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-03-06 18:15:10 +00:00
|
|
|
export function getCdn(attachment: ProcessedAttachment): string {
|
2024-02-05 23:17:28 +00:00
|
|
|
const { cdnId, cdnKey } = attachment;
|
|
|
|
const cdn = cdnId || cdnKey;
|
|
|
|
strictAssert(cdn, 'Attachment was missing cdnId or cdnKey');
|
|
|
|
return cdn;
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
export async function downloadAttachmentV1(
|
2021-07-28 21:37:09 +00:00
|
|
|
server: WebAPIType,
|
2023-06-14 20:51:49 +00:00
|
|
|
attachment: ProcessedAttachment,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
): Promise<DownloadedAttachmentType> {
|
2024-02-05 23:17:28 +00:00
|
|
|
const { cdnNumber, key, digest, size, contentType } = attachment;
|
|
|
|
const cdn = getCdn(attachment);
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2023-06-14 20:51:49 +00:00
|
|
|
const encrypted = await server.getAttachment(
|
2024-02-05 23:17:28 +00:00
|
|
|
cdn,
|
2023-06-14 20:51:49 +00:00
|
|
|
dropNull(cdnNumber),
|
|
|
|
options
|
|
|
|
);
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2024-02-05 23:17:28 +00:00
|
|
|
strictAssert(digest, 'Failure: Ask sender to update Signal and resend.');
|
2021-07-28 21:37:09 +00:00
|
|
|
strictAssert(key, 'attachment has no key');
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
const paddedData = decryptAttachmentV1(
|
2021-07-28 21:37:09 +00:00
|
|
|
encrypted,
|
2021-09-24 00:49:05 +00:00
|
|
|
Bytes.fromBase64(key),
|
|
|
|
Bytes.fromBase64(digest)
|
2021-07-28 21:37:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!isNumber(size)) {
|
|
|
|
throw new Error(
|
|
|
|
`downloadAttachment: Size was not provided, actual size was ${paddedData.byteLength}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const data = getFirstBytes(paddedData, size);
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
return {
|
2023-10-04 00:09:31 +00:00
|
|
|
...attachment,
|
2021-10-05 22:10:08 +00:00
|
|
|
size,
|
2021-07-28 21:37:09 +00:00
|
|
|
contentType: contentType
|
2021-08-09 20:06:21 +00:00
|
|
|
? MIME.stringToMIMEType(contentType)
|
2021-07-28 21:37:09 +00:00
|
|
|
: MIME.APPLICATION_OCTET_STREAM,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
export async function downloadAttachmentV2(
|
|
|
|
server: WebAPIType,
|
|
|
|
attachment: ProcessedAttachment,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
): Promise<AttachmentType> {
|
2024-02-05 23:17:28 +00:00
|
|
|
const { cdnNumber, contentType, digest, key, size } = attachment;
|
|
|
|
const cdn = getCdn(attachment);
|
2024-03-21 20:02:12 +00:00
|
|
|
const logId = `downloadAttachmentV2(${redactCdnKey(cdn)}:`;
|
2023-10-30 16:24:28 +00:00
|
|
|
|
|
|
|
strictAssert(digest, `${logId}: missing digest`);
|
|
|
|
strictAssert(key, `${logId}: missing key`);
|
|
|
|
strictAssert(isNumber(size), `${logId}: missing size`);
|
|
|
|
|
|
|
|
const downloadStream = await server.getAttachmentV2(
|
|
|
|
cdn,
|
|
|
|
dropNull(cdnNumber),
|
|
|
|
options
|
|
|
|
);
|
|
|
|
|
|
|
|
const cipherTextRelativePath = await downloadToDisk({ downloadStream, size });
|
|
|
|
const cipherTextAbsolutePath =
|
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath(cipherTextRelativePath);
|
|
|
|
|
2023-11-17 20:02:02 +00:00
|
|
|
const { path, plaintextHash } = await decryptAttachmentV2({
|
2023-10-30 16:24:28 +00:00
|
|
|
ciphertextPath: cipherTextAbsolutePath,
|
|
|
|
id: cdn,
|
|
|
|
keys: Bytes.fromBase64(key),
|
|
|
|
size,
|
|
|
|
theirDigest: Bytes.fromBase64(digest),
|
|
|
|
});
|
|
|
|
|
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-02-05 23:17:28 +00:00
|
|
|
const targetSize = getAttachmentDownloadSize(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;
|
|
|
|
return new Transform({
|
|
|
|
transform(chunk, encoding, callback) {
|
|
|
|
totalBytes += chunk.byteLength;
|
|
|
|
if (totalBytes > expectedBytes) {
|
|
|
|
callback(
|
2023-10-30 16:24:28 +00:00
|
|
|
new AttachmentSizeError(
|
2024-02-05 23:17:28 +00:00
|
|
|
`checkSize: Received ${totalBytes} bytes, max is ${expectedBytes}, `
|
2023-10-30 16:24:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-02-05 23:17:28 +00:00
|
|
|
this.push(chunk, encoding);
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
});
|
2023-10-30 16:24:28 +00:00
|
|
|
}
|