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
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
import { createWriteStream, existsSync, unlinkSync } from 'fs';
|
|
|
|
import { isNumber, omit } from 'lodash';
|
|
|
|
import type { Readable } from 'stream';
|
|
|
|
import { Transform } from 'stream';
|
|
|
|
import { pipeline } from 'stream/promises';
|
|
|
|
import { ensureFile } from 'fs-extra';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
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';
|
2023-10-30 16:24:28 +00:00
|
|
|
import {
|
|
|
|
getFirstBytes,
|
|
|
|
decryptAttachmentV1,
|
|
|
|
getAttachmentSizeBucket,
|
|
|
|
} from '../Crypto';
|
|
|
|
import {
|
|
|
|
decryptAttachmentV2,
|
|
|
|
IV_LENGTH,
|
|
|
|
ATTACHMENT_MAC_LENGTH,
|
|
|
|
} from '../AttachmentCrypto';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
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';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
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> {
|
|
|
|
const cdnId = attachment.cdnId || attachment.cdnKey;
|
|
|
|
const { cdnNumber } = attachment;
|
|
|
|
|
|
|
|
if (!cdnId) {
|
|
|
|
throw new Error('downloadAttachment: Attachment was missing cdnId!');
|
|
|
|
}
|
|
|
|
|
2023-06-14 20:51:49 +00:00
|
|
|
const encrypted = await server.getAttachment(
|
|
|
|
cdnId,
|
|
|
|
dropNull(cdnNumber),
|
|
|
|
options
|
|
|
|
);
|
2021-07-28 21:37:09 +00:00
|
|
|
const { key, digest, size, contentType } = attachment;
|
|
|
|
|
|
|
|
if (!digest) {
|
|
|
|
throw new Error('Failure: Ask sender to update Signal and resend.');
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-28 21:37:09 +00:00
|
|
|
|
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> {
|
|
|
|
const { cdnId, cdnKey, cdnNumber, contentType, digest, key, size } =
|
|
|
|
attachment;
|
|
|
|
|
|
|
|
const cdn = cdnId || cdnKey;
|
|
|
|
const logId = `downloadAttachmentV2(${cdn}):`;
|
|
|
|
|
|
|
|
strictAssert(cdn, `${logId}: missing cdnId or cdnKey`);
|
|
|
|
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),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existsSync(cipherTextAbsolutePath)) {
|
|
|
|
unlinkSync(cipherTextAbsolutePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...omit(attachment, 'key'),
|
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);
|
|
|
|
|
|
|
|
const targetSize =
|
|
|
|
getAttachmentSizeBucket(size) * 1.05 + IV_LENGTH + ATTACHMENT_MAC_LENGTH;
|
|
|
|
const checkSizeTransform = new CheckSizeTransform(targetSize);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await pipeline(downloadStream, checkSizeTransform, writeStream);
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
writeStream.close();
|
|
|
|
if (absoluteTargetPath && existsSync(absoluteTargetPath)) {
|
|
|
|
unlinkSync(absoluteTargetPath);
|
|
|
|
}
|
|
|
|
} 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.
|
|
|
|
class CheckSizeTransform extends Transform {
|
|
|
|
private bytesSeen = 0;
|
|
|
|
|
|
|
|
constructor(private maxBytes: number) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
override _transform(
|
|
|
|
chunk: Buffer | undefined,
|
|
|
|
_encoding: string,
|
|
|
|
done: (error?: Error) => void
|
|
|
|
) {
|
|
|
|
if (!chunk || chunk.byteLength === 0) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.bytesSeen += chunk.byteLength;
|
|
|
|
|
|
|
|
if (this.bytesSeen > this.maxBytes) {
|
|
|
|
done(
|
|
|
|
new AttachmentSizeError(
|
|
|
|
`CheckSizeTransform: Saw ${this.bytesSeen} bytes, max is ${this.maxBytes} bytes`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.push(chunk);
|
|
|
|
} catch (error) {
|
|
|
|
done(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|