2016-11-04 12:46:45 +00:00
|
|
|
/*
|
2021-03-04 01:22:57 +00:00
|
|
|
Copyright 2016, 2018, 2021 The Matrix.org Foundation C.I.C.
|
2016-11-04 12:46:45 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Pull in the encryption lib so that we can decrypt attachments.
|
2022-04-05 17:29:27 +00:00
|
|
|
import encrypt from 'matrix-encrypt-attachment';
|
2022-11-10 09:27:20 +00:00
|
|
|
import { parseErrorResponse } from 'matrix-js-sdk/src/http-api';
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { mediaFromContent } from "../customisations/Media";
|
2021-08-10 23:25:56 +00:00
|
|
|
import { IEncryptedFile, IMediaEventInfo } from "../customisations/models/IMediaEventContent";
|
2021-05-06 12:53:27 +00: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 {
|
|
|
|
constructor(e) {
|
|
|
|
super(e.message);
|
|
|
|
this.name = "DownloadError";
|
|
|
|
this.stack = e.stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DecryptError extends Error {
|
|
|
|
constructor(e) {
|
|
|
|
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.
|
2021-08-10 23:25:56 +00:00
|
|
|
* @param {IEncryptedFile} file The encrypted file information taken from the matrix event.
|
2022-04-05 17:29:27 +00: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.
|
2021-08-10 23:25:56 +00:00
|
|
|
* @param {IMediaEventInfo} info The info parameter taken from the matrix event.
|
2021-03-04 01:22:57 +00:00
|
|
|
* @returns {Promise<Blob>} Resolves to a Blob of the file.
|
2016-11-08 11:42:20 +00:00
|
|
|
*/
|
2022-11-10 09:27:20 +00:00
|
|
|
export async function decryptFile(
|
2021-08-10 23:25:56 +00:00
|
|
|
file: IEncryptedFile,
|
2021-08-10 23:37:18 +00:00
|
|
|
info?: IMediaEventInfo,
|
2021-08-10 23:25:56 +00:00
|
|
|
): Promise<Blob> {
|
2021-06-29 12:11:58 +00: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) {
|
|
|
|
throw new DownloadError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Decrypt the array buffer using the information taken from the event content.
|
|
|
|
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 02:07:31 +00: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 23:25:56 +00:00
|
|
|
let mimetype = info?.mimetype ? info.mimetype.split(";")[0].trim() : '';
|
2021-05-06 12:53:27 +00:00
|
|
|
mimetype = getBlobSafeMimeType(mimetype);
|
2018-04-29 02:07:31 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
return new Blob([dataArray], { type: mimetype });
|
2022-11-10 09:27:20 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new DecryptError(e);
|
|
|
|
}
|
2016-11-04 12:46:45 +00:00
|
|
|
}
|