2016-11-04 12:46:45 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2016-2018 , 2021 The Matrix.org Foundation C.I.C.
|
2016-11-04 12:46:45 +00:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2016-11-04 12:46:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Pull in the encryption lib so that we can decrypt attachments.
|
2022-04-05 18:29:27 +01:00
|
|
|
import encrypt from "matrix-encrypt-attachment";
|
2023-08-09 16:10:54 +01:00
|
|
|
import { parseErrorResponse } from "matrix-js-sdk/src/matrix";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type EncryptedFile, type MediaEventInfo } from "matrix-js-sdk/src/types";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { mediaFromContent } from "../customisations/Media";
|
2021-05-06 13:53:27 +01:00
|
|
|
import { getBlobSafeMimeType } from "./blobs";
|
2016-11-04 12:46:45 +00:00
|
|
|
|
2022-11-10 09:27:20 +00:00
|
|
|
export class DownloadError extends Error {
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(e: Error) {
|
2022-11-10 09:27:20 +00:00
|
|
|
super(e.message);
|
|
|
|
|
this.name = "DownloadError";
|
|
|
|
|
this.stack = e.stack;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DecryptError extends Error {
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(e: Error) {
|
2022-11-10 09:27:20 +00:00
|
|
|
super(e.message);
|
|
|
|
|
this.name = "DecryptError";
|
|
|
|
|
this.stack = e.stack;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 11:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Decrypt a file attached to a matrix event.
|
2023-07-17 13:07:58 +01:00
|
|
|
* @param {EncryptedFile} file The encrypted file information taken from the matrix event.
|
2022-04-05 18:29:27 +01:00
|
|
|
* This passed to [link]{@link https://github.com/matrix-org/matrix-encrypt-attachment}
|
2016-11-08 11:42:20 +00:00
|
|
|
* as the encryption info object, so will also have the those keys in addition to
|
|
|
|
|
* the keys below.
|
2024-03-11 09:30:00 +00:00
|
|
|
* @param {MediaEventInfo} info The info parameter taken from the matrix event.
|
2021-03-03 18:22:57 -07:00
|
|
|
* @returns {Promise<Blob>} Resolves to a Blob of the file.
|
2016-11-08 11:42:20 +00:00
|
|
|
*/
|
2024-03-11 09:30:00 +00:00
|
|
|
export async function decryptFile(file?: EncryptedFile, info?: MediaEventInfo): Promise<Blob> {
|
2023-03-30 10:47:07 +13:00
|
|
|
// throws if file is falsy
|
2021-06-29 13:11:58 +01:00
|
|
|
const media = mediaFromContent({ file });
|
2022-11-10 09:27:20 +00:00
|
|
|
|
|
|
|
|
let responseData: ArrayBuffer;
|
|
|
|
|
try {
|
|
|
|
|
// Download the encrypted file as an array buffer.
|
|
|
|
|
const response = await media.downloadSource();
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw parseErrorResponse(response, await response.text());
|
|
|
|
|
}
|
|
|
|
|
responseData = await response.arrayBuffer();
|
|
|
|
|
} catch (e) {
|
2023-06-27 17:39:56 +01:00
|
|
|
throw new DownloadError(e as Error);
|
2022-11-10 09:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Decrypt the array buffer using the information taken from the event content.
|
2023-03-30 10:47:07 +13:00
|
|
|
const dataArray = await encrypt.decryptAttachment(responseData, file!);
|
2016-11-04 12:46:45 +00:00
|
|
|
// Turn the array into a Blob and give it the correct MIME-type.
|
2018-04-29 03:07:31 +01:00
|
|
|
|
|
|
|
|
// IMPORTANT: we must not allow scriptable mime-types into Blobs otherwise
|
|
|
|
|
// they introduce XSS attacks if the Blob URI is viewed directly in the
|
|
|
|
|
// browser (e.g. by copying the URI into a new tab or window.)
|
|
|
|
|
// See warning at top of file.
|
2021-08-10 19:25:56 -04:00
|
|
|
let mimetype = info?.mimetype ? info.mimetype.split(";")[0].trim() : "";
|
2021-05-06 13:53:27 +01:00
|
|
|
mimetype = getBlobSafeMimeType(mimetype);
|
2018-04-29 03:07:31 +01:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
return new Blob([dataArray], { type: mimetype });
|
2022-11-10 09:27:20 +00:00
|
|
|
} catch (e) {
|
2023-06-27 17:39:56 +01:00
|
|
|
throw new DecryptError(e as Error);
|
2022-11-10 09:27:20 +00:00
|
|
|
}
|
2016-11-04 12:46:45 +00:00
|
|
|
}
|